@@ -10,7 +10,7 @@ import { setupTempDirectory } from "../test-utils.js";
1010type TestTransformationOptions = {
1111 files : Record < string , string > ;
1212 inputFilePath : string ;
13- assertion : ( code : string ) => void ;
13+ assertion ( code : string ) : void ;
1414 options ?: PluginOptions ;
1515} ;
1616
@@ -27,10 +27,26 @@ function itTransforms(
2727 assert ( result , "Expected transformation to produce a result" ) ;
2828 const { code } = result ;
2929 assert ( code , "Expected transformation to produce code" ) ;
30- assert ( assertion ( code ) , `Unexpected code: ${ code } ` ) ;
30+ assertion ( code ) ;
3131 } ) ;
3232}
3333
34+ function assertIncludes ( needle : string , { reverse = false } = { } ) {
35+ return ( code : string ) => {
36+ if ( reverse ) {
37+ assert (
38+ ! code . includes ( needle ) ,
39+ `Expected code to not include: ${ needle } , got ${ code } `
40+ ) ;
41+ } else {
42+ assert (
43+ code . includes ( needle ) ,
44+ `Expected code to include: ${ needle } , got ${ code } `
45+ ) ;
46+ }
47+ } ;
48+ }
49+
3450describe ( "plugin" , ( ) => {
3551 describe ( "transforming require(...) calls" , ( ) => {
3652 itTransforms ( "a simple call" , {
@@ -44,8 +60,7 @@ describe("plugin", () => {
4460 ` ,
4561 } ,
4662 inputFilePath : "index.js" ,
47- assertion : ( code ) =>
48- code . includes ( `requireNodeAddon("my-package--my-addon")` ) ,
63+ assertion : assertIncludes ( `requireNodeAddon("my-package--my-addon")` ) ,
4964 } ) ;
5065
5166 itTransforms ( "from sub-directory" , {
@@ -59,42 +74,57 @@ describe("plugin", () => {
5974 ` ,
6075 } ,
6176 inputFilePath : "sub-dir/index.js" ,
62- assertion : ( code ) =>
63- code . includes ( `requireNodeAddon("my-package--my-addon")` ) ,
77+ assertion : assertIncludes ( `requireNodeAddon("my-package--my-addon")` ) ,
6478 } ) ;
6579
66- itTransforms ( "addon in sub-directory" , {
67- files : {
68- "package.json" : `{ "name": "my-package" }` ,
69- "sub-dir/my-addon.apple.node/my-addon.node" :
70- "// This is supposed to be a binary file" ,
71- "index.js" : `
72- const addon = require('./sub-dir/my-addon.node');
73- console.log(addon);
74- ` ,
75- } ,
76- inputFilePath : "index.js" ,
77- assertion : ( code ) =>
78- code . includes ( `requireNodeAddon("my-package--sub-dir-my-addon")` ) ,
79- } ) ;
80+ describe ( "in 'sub-dir'" , ( ) => {
81+ itTransforms ( "a nested addon (keeping suffix)" , {
82+ files : {
83+ "package.json" : `{ "name": "my-package" }` ,
84+ "sub-dir/my-addon.apple.node/my-addon.node" :
85+ "// This is supposed to be a binary file" ,
86+ "index.js" : `
87+ const addon = require('./sub-dir/my-addon.node');
88+ console.log(addon);
89+ ` ,
90+ } ,
91+ inputFilePath : "index.js" ,
92+ options : { pathSuffix : "keep" } ,
93+ assertion : assertIncludes (
94+ `requireNodeAddon("my-package--sub-dir-my-addon")`
95+ ) ,
96+ } ) ;
8097
81- itTransforms (
82- "and returns package name when passed stripPathSuffix option" ,
83- {
98+ itTransforms ( "a nested addon (stripping suffix)" , {
8499 files : {
85100 "package.json" : `{ "name": "my-package" }` ,
86101 "sub-dir/my-addon.apple.node/my-addon.node" :
87102 "// This is supposed to be a binary file" ,
88103 "index.js" : `
89- const addon = require('./sub-dir/my-addon.node');
90- console.log(addon);
91- ` ,
104+ const addon = require('./sub-dir/my-addon.node');
105+ console.log(addon);
106+ ` ,
92107 } ,
93108 inputFilePath : "index.js" ,
94- options : { stripPathSuffix : true } ,
95- assertion : ( code ) => code . includes ( `requireNodeAddon("my-package")` ) ,
96- }
97- ) ;
109+ options : { pathSuffix : "strip" } ,
110+ assertion : assertIncludes ( `requireNodeAddon("my-package--my-addon")` ) ,
111+ } ) ;
112+
113+ itTransforms ( "a nested addon (omitting suffix)" , {
114+ files : {
115+ "package.json" : `{ "name": "my-package" }` ,
116+ "sub-dir/my-addon.apple.node/my-addon.node" :
117+ "// This is supposed to be a binary file" ,
118+ "index.js" : `
119+ const addon = require('./sub-dir/my-addon.node');
120+ console.log(addon);
121+ ` ,
122+ } ,
123+ inputFilePath : "index.js" ,
124+ options : { pathSuffix : "omit" } ,
125+ assertion : assertIncludes ( `requireNodeAddon("my-package")` ) ,
126+ } ) ;
127+ } ) ;
98128
99129 itTransforms ( "and does not touch required JS files" , {
100130 files : {
@@ -107,8 +137,7 @@ describe("plugin", () => {
107137 ` ,
108138 } ,
109139 inputFilePath : "index.js" ,
110- options : { stripPathSuffix : true } ,
111- assertion : ( code ) => ! code . includes ( "requireNodeAddon" ) ,
140+ assertion : assertIncludes ( "requireNodeAddon" , { reverse : true } ) ,
112141 } ) ;
113142 } ) ;
114143
@@ -124,12 +153,28 @@ describe("plugin", () => {
124153 ` ,
125154 } ,
126155 inputFilePath : "index.js" ,
127- assertion : ( code ) =>
128- code . includes ( `requireNodeAddon("my-package--my-addon")` ) ,
156+ assertion : assertIncludes ( `requireNodeAddon("my-package--my-addon")` ) ,
129157 } ) ;
130158
131159 describe ( "in 'build/Release'" , ( ) => {
132- itTransforms ( "a nested addon" , {
160+ itTransforms ( "a nested addon (keeping suffix)" , {
161+ files : {
162+ "package.json" : `{ "name": "my-package" }` ,
163+ "build/Release/my-addon.apple.node/my-addon.node" :
164+ "// This is supposed to be a binary file" ,
165+ "index.js" : `
166+ const addon = require('bindings')('my-addon');
167+ console.log(addon);
168+ ` ,
169+ } ,
170+ inputFilePath : "index.js" ,
171+ options : { pathSuffix : "keep" } ,
172+ assertion : assertIncludes (
173+ `requireNodeAddon("my-package--build-Release-my-addon")`
174+ ) ,
175+ } ) ;
176+
177+ itTransforms ( "a nested addon (stripping suffix)" , {
133178 files : {
134179 "package.json" : `{ "name": "my-package" }` ,
135180 "build/Release/my-addon.apple.node/my-addon.node" :
@@ -140,13 +185,11 @@ describe("plugin", () => {
140185 ` ,
141186 } ,
142187 inputFilePath : "index.js" ,
143- assertion : ( code ) =>
144- code . includes (
145- `requireNodeAddon("my-package--build-Release-my-addon")`
146- ) ,
188+ options : { pathSuffix : "strip" } ,
189+ assertion : assertIncludes ( `requireNodeAddon("my-package--my-addon")` ) ,
147190 } ) ;
148191
149- itTransforms ( "strips path suffix when passing stripPathSuffix option " , {
192+ itTransforms ( "a nested addon (omitting suffix) " , {
150193 files : {
151194 "package.json" : `{ "name": "my-package" }` ,
152195 "build/Release/my-addon.apple.node/my-addon.node" :
@@ -157,8 +200,8 @@ describe("plugin", () => {
157200 ` ,
158201 } ,
159202 inputFilePath : "index.js" ,
160- options : { stripPathSuffix : true } ,
161- assertion : ( code ) => code . includes ( `requireNodeAddon("my-package")` ) ,
203+ options : { pathSuffix : "omit" } ,
204+ assertion : assertIncludes ( `requireNodeAddon("my-package")` ) ,
162205 } ) ;
163206 } ) ;
164207 } ) ;
0 commit comments