forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-config-file.js
More file actions
545 lines (504 loc) Β· 18.8 KB
/
test-config-file.js
File metadata and controls
545 lines (504 loc) Β· 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
'use strict';
const {
isWindows,
spawnPromisified,
skipIfSQLiteMissing,
} = require('../common');
skipIfSQLiteMissing();
const fixtures = require('../common/fixtures');
const tmpdir = require('../common/tmpdir');
const { match, strictEqual, deepStrictEqual } = require('node:assert');
const { test, it, describe } = require('node:test');
const { chmodSync, writeFileSync, constants } = require('node:fs');
const { join } = require('node:path');
test('should handle non existing json', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-config-file',
'i-do-not-exist.json',
'-p', '"Hello, World!"',
]);
match(result.stderr, /Cannot read configuration from i-do-not-exist\.json: no such file or directory/);
match(result.stderr, /i-do-not-exist\.json: not found/);
strictEqual(result.stdout, '');
strictEqual(result.code, 9);
});
test('should handle empty json', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-config-file',
fixtures.path('rc/empty.json'),
'-p', '"Hello, World!"',
]);
match(result.stderr, /Can't parse/);
match(result.stderr, /empty\.json: invalid content/);
strictEqual(result.stdout, '');
strictEqual(result.code, 9);
});
test('should handle empty object json', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/empty-object.json'),
'-p', '"Hello, World!"',
]);
strictEqual(result.stderr, '');
match(result.stdout, /Hello, World!/);
strictEqual(result.code, 0);
});
test('should parse boolean flag', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-config-file',
fixtures.path('rc/transform-types.json'),
fixtures.path('typescript/ts/transformation/test-enum.ts'),
]);
match(result.stderr, /--experimental-config-file is an experimental feature and might change at any time/);
match(result.stdout, /Hello, TypeScript!/);
strictEqual(result.code, 0);
});
test('should parse boolean flag defaulted to true', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-config-file',
fixtures.path('rc/warnings-false.json'),
'-p', 'process.emitWarning("A warning")',
]);
strictEqual(result.stderr, '');
strictEqual(result.stdout, 'undefined\n');
strictEqual(result.code, 0);
});
test('should throw an error when a flag is declared twice', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/override-property.json'),
fixtures.path('typescript/ts/transformation/test-enum.ts'),
]);
match(result.stderr, /Option --experimental-transform-types is already defined/);
strictEqual(result.stdout, '');
strictEqual(result.code, 9);
});
test('should override env-file', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/transform-types.json'),
'--env-file', fixtures.path('dotenv/node-options-no-tranform.env'),
fixtures.path('typescript/ts/transformation/test-enum.ts'),
]);
strictEqual(result.stderr, '');
match(result.stdout, /Hello, TypeScript!/);
strictEqual(result.code, 0);
});
test('should not override NODE_OPTIONS', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/transform-types.json'),
fixtures.path('typescript/ts/transformation/test-enum.ts'),
], {
env: {
...process.env,
NODE_OPTIONS: '--no-experimental-transform-types',
},
});
match(result.stderr, /ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX/);
strictEqual(result.stdout, '');
strictEqual(result.code, 1);
});
test('should not override CLI flags', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--no-experimental-transform-types',
'--experimental-config-file',
fixtures.path('rc/transform-types.json'),
fixtures.path('typescript/ts/transformation/test-enum.ts'),
]);
match(result.stderr, /ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX/);
strictEqual(result.stdout, '');
strictEqual(result.code, 1);
});
test('should parse array flag correctly', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/import.json'),
'--eval', 'setTimeout(() => console.log("D"),99)',
]);
strictEqual(result.stderr, '');
strictEqual(result.stdout, 'A\nB\nC\nD\n');
strictEqual(result.code, 0);
});
test('should validate invalid array flag', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/invalid-import.json'),
'--eval', 'setTimeout(() => console.log("D"),99)',
]);
match(result.stderr, /invalid-import\.json: invalid content/);
strictEqual(result.stdout, '');
strictEqual(result.code, 9);
});
test('should validate array flag as string', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/import-as-string.json'),
'--eval', 'setTimeout(() => console.log("B"),99)',
]);
strictEqual(result.stderr, '');
strictEqual(result.stdout, 'A\nB\n');
strictEqual(result.code, 0);
});
test('should throw at unknown flag', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/unknown-flag.json'),
'-p', '"Hello, World!"',
]);
match(result.stderr, /Unknown or not allowed option some-unknown-flag for namespace nodeOptions/);
strictEqual(result.stdout, '');
strictEqual(result.code, 9);
});
test('should throw at flag not available in NODE_OPTIONS', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/not-node-options-flag.json'),
'-p', '"Hello, World!"',
]);
match(result.stderr, /Unknown or not allowed option test for namespace nodeOptions/);
strictEqual(result.stdout, '');
strictEqual(result.code, 9);
});
test('unsigned flag should be parsed correctly', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/numeric.json'),
'-p', 'http.maxHeaderSize',
]);
strictEqual(result.stderr, '');
strictEqual(result.stdout, '4294967295\n');
strictEqual(result.code, 0);
});
test('numeric flag should not allow negative values', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/negative-numeric.json'),
'-p', 'http.maxHeaderSize',
]);
match(result.stderr, /Invalid value for --max-http-header-size/);
match(result.stderr, /negative-numeric\.json: invalid content/);
strictEqual(result.stdout, '');
strictEqual(result.code, 9);
});
test('v8 flag should not be allowed in config file', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/v8-flag.json'),
'-p', '"Hello, World!"',
]);
match(result.stderr, /V8 flag --abort-on-uncaught-exception is currently not supported/);
strictEqual(result.stdout, '');
strictEqual(result.code, 9);
});
test('string flag should be parsed correctly', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--test',
'--experimental-config-file',
fixtures.path('rc/string.json'),
fixtures.path('rc/test.js'),
]);
strictEqual(result.stderr, '');
strictEqual(result.stdout, '.\n');
strictEqual(result.code, 0);
});
test('host port flag should be parsed correctly', { skip: !process.features.inspector }, async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--expose-internals',
'--experimental-config-file',
fixtures.path('rc/host-port.json'),
'-p', 'require("internal/options").getOptionValue("--inspect-port").port',
]);
strictEqual(result.stderr, '');
strictEqual(result.stdout, '65535\n');
strictEqual(result.code, 0);
});
test('--inspect=true should be parsed correctly', { skip: !process.features.inspector }, async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/inspect-true.json'),
'--inspect-port', '0',
'-p', 'require("node:inspector").url()',
]);
match(result.stderr, /^Debugger listening on (ws:\/\/[^\s]+)/);
match(result.stdout, /ws:\/\/[^\s]+/);
strictEqual(result.code, 0);
});
test('--inspect=false should be parsed correctly', { skip: !process.features.inspector }, async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/inspect-false.json'),
'-p', 'require("node:inspector").url()',
]);
strictEqual(result.stderr, '');
strictEqual(result.stdout, 'undefined\n');
strictEqual(result.code, 0);
});
test('no op flag should throw', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/no-op.json'),
'-p', '"Hello, World!"',
]);
match(result.stderr, /No-op flag --http-parser is currently not supported/);
match(result.stderr, /no-op\.json: invalid content/);
strictEqual(result.stdout, '');
strictEqual(result.code, 9);
});
test('should not allow users to sneak in a flag', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/sneaky-flag.json'),
'-p', '"Hello, World!"',
]);
match(result.stderr, /The number of NODE_OPTIONS doesn't match the number of flags in the config file/);
strictEqual(result.stdout, '');
strictEqual(result.code, 9);
});
test('non object root', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/non-object-root.json'),
'-p', '"Hello, World!"',
]);
match(result.stderr, /Root value unexpected not an object for/);
strictEqual(result.stdout, '');
strictEqual(result.code, 9);
});
test('non object node options', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/non-object-node-options.json'),
'-p', '"Hello, World!"',
]);
match(result.stderr, /"nodeOptions" value unexpected for/);
strictEqual(result.stdout, '');
strictEqual(result.code, 9);
});
test('should throw correct error when a json is broken', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/broken.json'),
'-p', '"Hello, World!"',
]);
match(result.stderr, /Can't parse/);
match(result.stderr, /broken\.json: invalid content/);
strictEqual(result.stdout, '');
strictEqual(result.code, 9);
});
test('broken value in node_options', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/broken-node-options.json'),
'-p', '"Hello, World!"',
]);
match(result.stderr, /Can't parse/);
match(result.stderr, /broken-node-options\.json: invalid content/);
strictEqual(result.stdout, '');
strictEqual(result.code, 9);
});
test('should use node.config.json as default', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-default-config-file',
'-p', 'http.maxHeaderSize',
], {
cwd: fixtures.path('rc/default'),
});
strictEqual(result.stderr, '');
strictEqual(result.stdout, '10\n');
strictEqual(result.code, 0);
});
test('should override node.config.json when specificied', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-default-config-file',
'--experimental-config-file',
fixtures.path('rc/default/override.json'),
'-p', 'http.maxHeaderSize',
], {
cwd: fixtures.path('rc/default'),
});
strictEqual(result.stderr, '');
strictEqual(result.stdout, '20\n');
strictEqual(result.code, 0);
});
// Skip on windows because it doesn't support chmod changing read permissions
// Also skip if user is root because it would have read permissions anyway
test('should throw an error when the file is non readable', {
skip: isWindows || process.getuid() === 0,
}, async () => {
tmpdir.refresh();
const dest = join(tmpdir.path, 'node.config.json');
writeFileSync(dest, JSON.stringify({
nodeOptions: { 'max-http-header-size': 10 }
}));
chmodSync(dest, constants.O_RDONLY);
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-default-config-file',
'-p', 'http.maxHeaderSize',
], {
cwd: tmpdir.path,
});
match(result.stderr, /Cannot read configuration from node\.config\.json: permission denied/);
strictEqual(result.stdout, '');
strictEqual(result.code, 9);
});
describe('namespace-scoped options', () => {
it('should parse a namespace-scoped option correctly', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--expose-internals',
'--experimental-config-file',
fixtures.path('rc/namespaced/node.config.json'),
'-p', 'require("internal/options").getOptionValue("--test-isolation")',
]);
strictEqual(result.stderr, '');
strictEqual(result.stdout, 'none\n');
strictEqual(result.code, 0);
});
it('should throw an error when a namespace-scoped option is not recognised', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/unknown-flag-namespace.json'),
'-p', '"Hello, World!"',
]);
match(result.stderr, /Unknown or not allowed option unknown-flag for namespace testRunner/);
strictEqual(result.stdout, '');
strictEqual(result.code, 9);
});
it('should not throw an error when a namespace is not recognised', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/unknown-namespace.json'),
'-p', '"Hello, World!"',
]);
strictEqual(result.stderr, '');
strictEqual(result.stdout, 'Hello, World!\n');
strictEqual(result.code, 0);
});
it('should handle an empty namespace valid namespace', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-config-file',
fixtures.path('rc/empty-valid-namespace.json'),
'-p', '"Hello, World!"',
]);
strictEqual(result.stderr, '');
strictEqual(result.stdout, 'Hello, World!\n');
strictEqual(result.code, 0);
});
it('should throw an error if a namespace-scoped option has already been set in node options', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--expose-internals',
'--experimental-config-file',
fixtures.path('rc/override-node-option-with-namespace.json'),
'-p', 'require("internal/options").getOptionValue("--test-isolation")',
]);
match(result.stderr, /Option --test-isolation is already defined/);
strictEqual(result.stdout, '');
strictEqual(result.code, 9);
});
it('should throw an error if a node option has already been set in a namespace-scoped option', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--expose-internals',
'--experimental-config-file',
fixtures.path('rc/override-namespace.json'),
'-p', 'require("internal/options").getOptionValue("--test-isolation")',
]);
match(result.stderr, /Option --test-isolation is already defined/);
strictEqual(result.stdout, '');
strictEqual(result.code, 9);
});
it('should prioritise CLI namespace-scoped options over config file options', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--expose-internals',
'--test-isolation', 'process',
'--experimental-config-file',
fixtures.path('rc/namespaced/node.config.json'),
'-p', 'require("internal/options").getOptionValue("--test-isolation")',
]);
strictEqual(result.stderr, '');
strictEqual(result.stdout, 'process\n');
strictEqual(result.code, 0);
});
it('should append namespace-scoped config file options with CLI options in case of array', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--expose-internals',
'--test-coverage-exclude', 'cli-pattern1',
'--test-coverage-exclude', 'cli-pattern2',
'--experimental-config-file',
fixtures.path('rc/namespace-with-array.json'),
'-p', 'JSON.stringify(require("internal/options").getOptionValue("--test-coverage-exclude"))',
]);
strictEqual(result.stderr, '');
const excludePatterns = JSON.parse(result.stdout);
const expected = [
'config-pattern1',
'config-pattern2',
'cli-pattern1',
'cli-pattern2',
];
deepStrictEqual(excludePatterns, expected);
strictEqual(result.code, 0);
});
it('should allow setting kDisallowedInEnvvar in the config file if part of a namespace', async () => {
// This test assumes that the --test-concurrency flag is configured as kDisallowedInEnvVar
// and that it is part of at least one namespace.
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--expose-internals',
'--experimental-config-file',
fixtures.path('rc/namespace-with-disallowed-envvar.json'),
'-p', 'require("internal/options").getOptionValue("--test-concurrency")',
]);
strictEqual(result.stderr, '');
strictEqual(result.stdout, '1\n');
strictEqual(result.code, 0);
});
it('should override namespace-scoped config file options with CLI options', async () => {
// This test assumes that the --test-concurrency flag is configured as kDisallowedInEnvVar
// and that it is part of at least one namespace.
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--expose-internals',
'--test-concurrency', '2',
'--experimental-config-file',
fixtures.path('rc/namespace-with-disallowed-envvar.json'),
'-p', 'require("internal/options").getOptionValue("--test-concurrency")',
]);
strictEqual(result.stderr, '');
strictEqual(result.stdout, '2\n');
strictEqual(result.code, 0);
});
});