@@ -17,152 +17,214 @@ function fixture(name) {
17
17
return path . resolve ( __dirname , "../../fixtures/shebang" , name )
18
18
}
19
19
20
+ /** @type {import('eslint').RuleTester } */
20
21
const ruleTester = new RuleTester ( )
21
22
ruleTester . run ( "shebang" , rule , {
22
23
valid : [
23
24
{
25
+ name : "string-bin/bin/test.js" ,
24
26
filename : fixture ( "string-bin/bin/test.js" ) ,
25
27
code : "#!/usr/bin/env node\nhello();" ,
26
28
} ,
27
29
{
30
+ name : "string-bin/lib/test.js" ,
28
31
filename : fixture ( "string-bin/lib/test.js" ) ,
29
32
code : "hello();" ,
30
33
} ,
31
34
{
35
+ name : "object-bin/bin/a.js" ,
32
36
filename : fixture ( "object-bin/bin/a.js" ) ,
33
37
code : "#!/usr/bin/env node\nhello();" ,
34
38
} ,
35
39
{
40
+ name : "object-bin/bin/b.js" ,
36
41
filename : fixture ( "object-bin/bin/b.js" ) ,
37
42
code : "#!/usr/bin/env node\nhello();" ,
38
43
} ,
39
44
{
45
+ name : "object-bin/bin/c.js" ,
40
46
filename : fixture ( "object-bin/bin/c.js" ) ,
41
47
code : "hello();" ,
42
48
} ,
43
49
{
50
+ name : "no-bin-field/lib/test.js" ,
44
51
filename : fixture ( "no-bin-field/lib/test.js" ) ,
45
52
code : "hello();" ,
46
53
} ,
47
- "#!/usr/bin/env node\nhello();" ,
48
- "hello();" ,
54
+ {
55
+ name : "<input> with shebang" ,
56
+ code : "#!/usr/bin/env node\nhello();" ,
57
+ } ,
58
+ {
59
+ name : "<input> without shebang" ,
60
+ code : "hello();" ,
61
+ } ,
49
62
50
63
// convertPath
51
64
{
65
+ name : "convertPath - string-bin/src/bin/test.js" ,
52
66
filename : fixture ( "string-bin/src/bin/test.js" ) ,
53
67
code : "#!/usr/bin/env node\nhello();" ,
54
68
options : [ { convertPath : { "src/**" : [ "^src/(.+)$" , "$1" ] } } ] ,
55
69
} ,
56
70
{
71
+ name : "convertPath - string-bin/src/lib/test.js" ,
57
72
filename : fixture ( "string-bin/src/lib/test.js" ) ,
58
73
code : "hello();" ,
59
74
options : [ { convertPath : { "src/**" : [ "^src/(.+)$" , "$1" ] } } ] ,
60
75
} ,
61
76
{
77
+ name : "convertPath - object-bin/src/bin/a.js" ,
62
78
filename : fixture ( "object-bin/src/bin/a.js" ) ,
63
79
code : "#!/usr/bin/env node\nhello();" ,
64
80
options : [ { convertPath : { "src/**" : [ "^src/(.+)$" , "$1" ] } } ] ,
65
81
} ,
66
82
{
83
+ name : "convertPath - object-bin/src/bin/b.js" ,
67
84
filename : fixture ( "object-bin/src/bin/b.js" ) ,
68
85
code : "#!/usr/bin/env node\nhello();" ,
69
86
options : [ { convertPath : { "src/**" : [ "^src/(.+)$" , "$1" ] } } ] ,
70
87
} ,
71
88
{
89
+ name : "convertPath - object-bin/src/bin/c.js" ,
72
90
filename : fixture ( "object-bin/src/bin/c.js" ) ,
73
91
code : "hello();" ,
74
92
options : [ { convertPath : { "src/**" : [ "^src/(.+)$" , "$1" ] } } ] ,
75
93
} ,
76
94
{
95
+ name : "convertPath - no-bin-field/src/lib/test.js" ,
77
96
filename : fixture ( "no-bin-field/src/lib/test.js" ) ,
78
97
code : "hello();" ,
79
98
options : [ { convertPath : { "src/**" : [ "^src/(.+)$" , "$1" ] } } ] ,
80
99
} ,
81
100
82
101
// Should work fine if the filename is relative.
83
102
{
103
+ name : "relative path - string-bin/bin/test.js" ,
84
104
filename : "tests/fixtures/shebang/string-bin/bin/test.js" ,
85
105
code : "#!/usr/bin/env node\nhello();" ,
86
106
} ,
87
107
{
108
+ name : "relative path - string-bin/lib/test.js" ,
88
109
filename : "tests/fixtures/shebang/string-bin/lib/test.js" ,
89
110
code : "hello();" ,
90
111
} ,
91
112
92
113
// BOM and \r\n
93
114
{
115
+ name : "BOM without newline" ,
94
116
filename : fixture ( "string-bin/lib/test.js" ) ,
95
117
code : "\uFEFFhello();" ,
96
118
} ,
97
119
{
120
+ name : "BOM with newline" ,
98
121
filename : fixture ( "string-bin/lib/test.js" ) ,
99
122
code : "\uFEFFhello();\n" ,
100
123
} ,
101
124
{
125
+ name : "with windows newline" ,
102
126
filename : fixture ( "string-bin/lib/test.js" ) ,
103
127
code : "hello();\r\n" ,
104
128
} ,
105
129
{
130
+ name : "BOM with windows newline" ,
106
131
filename : fixture ( "string-bin/lib/test.js" ) ,
107
132
code : "\uFEFFhello();\r\n" ,
108
133
} ,
109
134
110
135
// blank lines on the top of files.
111
136
{
137
+ name : "blank lines on the top of files." ,
112
138
filename : fixture ( "string-bin/lib/test.js" ) ,
113
139
code : "\n\n\nhello();" ,
114
140
} ,
115
141
116
142
// https://github.com/mysticatea/eslint-plugin-node/issues/51
117
143
{
144
+ name : "Shebang with CLI flags" ,
118
145
filename : fixture ( "string-bin/bin/test.js" ) ,
119
146
code : "#!/usr/bin/env node --harmony\nhello();" ,
120
147
} ,
121
148
122
149
// use node resolution
123
150
{
151
+ name : "use node resolution" ,
124
152
filename : fixture ( "object-bin/bin/index.js" ) ,
125
153
code : "#!/usr/bin/env node\nhello();" ,
126
154
} ,
155
+
156
+ // npm unpublished files are ignored
157
+ {
158
+ name : "published file cant have shebang" ,
159
+ filename : fixture ( "unpublished/published.js" ) ,
160
+ code : "hello();" ,
161
+ options : [ { ignoreUnpublished : true } ] ,
162
+ } ,
163
+ {
164
+ name : "unpublished file can have shebang" ,
165
+ filename : fixture ( "unpublished/unpublished.js" ) ,
166
+ code : "#!/usr/bin/env node\nhello();" ,
167
+ options : [ { ignoreUnpublished : true } ] ,
168
+ } ,
169
+ {
170
+ name : "unpublished file can have noshebang" ,
171
+ filename : fixture ( "unpublished/unpublished.js" ) ,
172
+ code : "hello();" ,
173
+ options : [ { ignoreUnpublished : true } ] ,
174
+ } ,
175
+
176
+ {
177
+ name : "file matching additionalExecutables" ,
178
+ filename : fixture ( "unpublished/something.test.js" ) ,
179
+ code : "#!/usr/bin/env node\nhello();" ,
180
+ options : [ { additionalExecutables : [ "*.test.js" ] } ] ,
181
+ } ,
127
182
] ,
128
183
invalid : [
129
184
{
185
+ name : "bin: string - match - no shebang" ,
130
186
filename : fixture ( "string-bin/bin/test.js" ) ,
131
187
code : "hello();" ,
132
188
output : "#!/usr/bin/env node\nhello();" ,
133
189
errors : [ 'This file needs shebang "#!/usr/bin/env node".' ] ,
134
190
} ,
135
191
{
192
+ name : "bin: string - match - incorrect shebang" ,
136
193
filename : fixture ( "string-bin/bin/test.js" ) ,
137
194
code : "#!/usr/bin/node\nhello();" ,
138
195
output : "#!/usr/bin/env node\nhello();" ,
139
196
errors : [ 'This file needs shebang "#!/usr/bin/env node".' ] ,
140
197
} ,
141
198
{
199
+ name : "bin: string - no match - with shebang" ,
142
200
filename : fixture ( "string-bin/lib/test.js" ) ,
143
201
code : "#!/usr/bin/env node\nhello();" ,
144
202
output : "hello();" ,
145
203
errors : [ "This file needs no shebang." ] ,
146
204
} ,
147
205
{
206
+ name : 'bin: {a: "./bin/a.js"} - match - no shebang' ,
148
207
filename : fixture ( "object-bin/bin/a.js" ) ,
149
208
code : "hello();" ,
150
209
output : "#!/usr/bin/env node\nhello();" ,
151
210
errors : [ 'This file needs shebang "#!/usr/bin/env node".' ] ,
152
211
} ,
153
212
{
213
+ name : 'bin: {b: "./bin/b.js"} - match - no shebang' ,
154
214
filename : fixture ( "object-bin/bin/b.js" ) ,
155
215
code : "#!/usr/bin/node\nhello();" ,
156
216
output : "#!/usr/bin/env node\nhello();" ,
157
217
errors : [ 'This file needs shebang "#!/usr/bin/env node".' ] ,
158
218
} ,
159
219
{
220
+ name : 'bin: {c: "./bin"} - no match - with shebang' ,
160
221
filename : fixture ( "object-bin/bin/c.js" ) ,
161
222
code : "#!/usr/bin/env node\nhello();" ,
162
223
output : "hello();" ,
163
224
errors : [ "This file needs no shebang." ] ,
164
225
} ,
165
226
{
227
+ name : "bin: undefined - no match - with shebang" ,
166
228
filename : fixture ( "no-bin-field/lib/test.js" ) ,
167
229
code : "#!/usr/bin/env node\nhello();" ,
168
230
output : "hello();" ,
@@ -171,13 +233,15 @@ ruleTester.run("shebang", rule, {
171
233
172
234
// convertPath
173
235
{
236
+ name : "convertPath in options" ,
174
237
filename : fixture ( "string-bin/src/bin/test.js" ) ,
175
238
code : "hello();" ,
176
239
output : "#!/usr/bin/env node\nhello();" ,
177
240
options : [ { convertPath : { "src/**" : [ "^src/(.+)$" , "$1" ] } } ] ,
178
241
errors : [ 'This file needs shebang "#!/usr/bin/env node".' ] ,
179
242
} ,
180
243
{
244
+ name : "convertPath in settings" ,
181
245
filename : fixture ( "string-bin/src/bin/test.js" ) ,
182
246
code : "hello();" ,
183
247
output : "#!/usr/bin/env node\nhello();" ,
@@ -187,41 +251,47 @@ ruleTester.run("shebang", rule, {
187
251
} ,
188
252
} ,
189
253
{
254
+ name : "converted path - string-bin/src/bin/test.js" ,
190
255
filename : fixture ( "string-bin/src/bin/test.js" ) ,
191
256
code : "#!/usr/bin/node\nhello();" ,
192
257
output : "#!/usr/bin/env node\nhello();" ,
193
258
options : [ { convertPath : { "src/**" : [ "^src/(.+)$" , "$1" ] } } ] ,
194
259
errors : [ 'This file needs shebang "#!/usr/bin/env node".' ] ,
195
260
} ,
196
261
{
262
+ name : "converted path - string-bin/src/lib/test.js" ,
197
263
filename : fixture ( "string-bin/src/lib/test.js" ) ,
198
264
code : "#!/usr/bin/env node\nhello();" ,
199
265
output : "hello();" ,
200
266
options : [ { convertPath : { "src/**" : [ "^src/(.+)$" , "$1" ] } } ] ,
201
267
errors : [ "This file needs no shebang." ] ,
202
268
} ,
203
269
{
270
+ name : "converted path - object-bin/src/bin/a.js" ,
204
271
filename : fixture ( "object-bin/src/bin/a.js" ) ,
205
272
code : "hello();" ,
206
273
output : "#!/usr/bin/env node\nhello();" ,
207
274
options : [ { convertPath : { "src/**" : [ "^src/(.+)$" , "$1" ] } } ] ,
208
275
errors : [ 'This file needs shebang "#!/usr/bin/env node".' ] ,
209
276
} ,
210
277
{
278
+ name : "converted path - object-bin/src/bin/b.js" ,
211
279
filename : fixture ( "object-bin/src/bin/b.js" ) ,
212
280
code : "#!/usr/bin/node\nhello();" ,
213
281
output : "#!/usr/bin/env node\nhello();" ,
214
282
options : [ { convertPath : { "src/**" : [ "^src/(.+)$" , "$1" ] } } ] ,
215
283
errors : [ 'This file needs shebang "#!/usr/bin/env node".' ] ,
216
284
} ,
217
285
{
286
+ name : "converted path - object-bin/src/bin/c.js" ,
218
287
filename : fixture ( "object-bin/src/bin/c.js" ) ,
219
288
code : "#!/usr/bin/env node\nhello();" ,
220
289
output : "hello();" ,
221
290
options : [ { convertPath : { "src/**" : [ "^src/(.+)$" , "$1" ] } } ] ,
222
291
errors : [ "This file needs no shebang." ] ,
223
292
} ,
224
293
{
294
+ name : "converted path - no-bin-field/src/lib/test.js" ,
225
295
filename : fixture ( "no-bin-field/src/lib/test.js" ) ,
226
296
code : "#!/usr/bin/env node\nhello();" ,
227
297
output : "hello();" ,
@@ -231,12 +301,14 @@ ruleTester.run("shebang", rule, {
231
301
232
302
// Should work fine if the filename is relative.
233
303
{
304
+ name : "relative path - string-bin/bin/test.js" ,
234
305
filename : "tests/fixtures/shebang/string-bin/bin/test.js" ,
235
306
code : "hello();" ,
236
307
output : "#!/usr/bin/env node\nhello();" ,
237
308
errors : [ 'This file needs shebang "#!/usr/bin/env node".' ] ,
238
309
} ,
239
310
{
311
+ name : "relative path - string-bin/lib/test.js" ,
240
312
filename : "tests/fixtures/shebang/string-bin/lib/test.js" ,
241
313
code : "#!/usr/bin/env node\nhello();" ,
242
314
output : "hello();" ,
@@ -245,6 +317,7 @@ ruleTester.run("shebang", rule, {
245
317
246
318
// header comments
247
319
{
320
+ name : "header comments" ,
248
321
filename : fixture ( "string-bin/bin/test.js" ) ,
249
322
code : "/* header */\nhello();" ,
250
323
output : "#!/usr/bin/env node\n/* header */\nhello();" ,
@@ -306,6 +379,7 @@ ruleTester.run("shebang", rule, {
306
379
307
380
// https://github.com/mysticatea/eslint-plugin-node/issues/51
308
381
{
382
+ name : "Shebang with CLI flags" ,
309
383
filename : fixture ( "string-bin/lib/test.js" ) ,
310
384
code : "#!/usr/bin/env node --harmony\nhello();" ,
311
385
output : "hello();" ,
@@ -314,10 +388,53 @@ ruleTester.run("shebang", rule, {
314
388
315
389
// use node resolution
316
390
{
391
+ name : "use node resolution" ,
317
392
filename : fixture ( "object-bin/bin/index.js" ) ,
318
393
code : "hello();" ,
319
394
output : "#!/usr/bin/env node\nhello();" ,
320
395
errors : [ 'This file needs shebang "#!/usr/bin/env node".' ] ,
321
396
} ,
397
+
398
+ // npm unpublished files are ignored
399
+ {
400
+ name : "unpublished file should not have shebang" ,
401
+ filename : fixture ( "unpublished/unpublished.js" ) ,
402
+ code : "#!/usr/bin/env node\nhello();" ,
403
+ output : "hello();" ,
404
+ errors : [ "This file needs no shebang." ] ,
405
+ } ,
406
+ {
407
+ name : "published file should have shebang" ,
408
+ filename : fixture ( "unpublished/published.js" ) ,
409
+ code : "#!/usr/bin/env node\nhello();" ,
410
+ output : "hello();" ,
411
+ errors : [ "This file needs no shebang." ] ,
412
+ } ,
413
+
414
+ {
415
+ name : "unpublished file shebang ignored" ,
416
+ filename : fixture ( "unpublished/published.js" ) ,
417
+ code : "#!/usr/bin/env node\nhello();" ,
418
+ options : [ { ignoreUnpublished : true } ] ,
419
+ output : "hello();" ,
420
+ errors : [ "This file needs no shebang." ] ,
421
+ } ,
422
+
423
+ {
424
+ name : "executable in additionalExecutables without shebang" ,
425
+ filename : fixture ( "unpublished/something.test.js" ) ,
426
+ code : "hello();" ,
427
+ options : [ { additionalExecutables : [ "*.test.js" ] } ] ,
428
+ output : "#!/usr/bin/env node\nhello();" ,
429
+ errors : [ 'This file needs shebang "#!/usr/bin/env node".' ] ,
430
+ } ,
431
+ {
432
+ name : "file not in additionalExecutables with shebang" ,
433
+ filename : fixture ( "unpublished/not-a-test.js" ) ,
434
+ code : "#!/usr/bin/env node\nhello();" ,
435
+ options : [ { additionalExecutables : [ "*.test.js" ] } ] ,
436
+ output : "hello();" ,
437
+ errors : [ "This file needs no shebang." ] ,
438
+ } ,
322
439
] ,
323
440
} )
0 commit comments