forked from vercel/pkg
-
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathconfig-parse.test.ts
More file actions
861 lines (750 loc) · 24.5 KB
/
Copy pathconfig-parse.test.ts
File metadata and controls
861 lines (750 loc) · 24.5 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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { after, afterEach, before, beforeEach, describe, it } from 'node:test';
import { system } from '@yao-pkg/pkg-fetch';
import { CompressType } from '../../lib/compress_type';
import { log } from '../../lib/log';
import {
PKGRC_FILENAMES,
differentParts,
findPkgrc,
isConfiguration,
parseInput,
parseTargets,
resolveFlags,
stringifyTarget,
stringifyTargetForOutput,
validatePkgConfig,
} from '../../lib/config';
import type { NodeTarget } from '../../lib/types';
type LogFn = (..._a: unknown[]) => void;
// validatePkgConfig calls log.warn; wasReported() (used elsewhere) calls
// log.error before returning an Error. Capture the former for assertions,
// silence the latter so assertion-triggered throws don't pollute output.
// log.info is silenced too — some tests fire resolveTargetList/parseTargets
// fallback paths that would otherwise print to stderr.
let warned: string[];
let originals: { warn: LogFn; info: LogFn; error: LogFn };
beforeEach(() => {
warned = [];
originals = {
warn: log.warn as LogFn,
info: log.info as LogFn,
error: log.error as LogFn,
};
log.warn = ((...a: unknown[]) => {
warned.push(a.join(' '));
}) as typeof log.warn;
log.info = (() => {}) as typeof log.info;
log.error = (() => {}) as typeof log.error;
});
afterEach(() => {
log.warn = originals.warn as typeof log.warn;
log.info = originals.info as typeof log.info;
log.error = originals.error as typeof log.error;
});
describe('parseInput — CLI argv', () => {
describe('positional handling', () => {
it('single positional becomes entry', () => {
assert.equal(parseInput(['app.js']).entry, 'app.js');
});
it('zero positionals → entry undefined (help/version path)', () => {
const p = parseInput(['--help']);
assert.equal(p.entry, undefined);
assert.equal(p.help, true);
});
it('multiple positionals throws', () => {
assert.throws(
() => parseInput(['a.js', 'b.js']),
/Not more than one entry/,
);
});
});
describe('short-circuits', () => {
it('-h is short for --help', () => {
assert.equal(parseInput(['-h']).help, true);
});
it('-v is short for --version', () => {
assert.equal(parseInput(['-v']).version, true);
});
});
describe('non-flag strings', () => {
it('--config / -c', () => {
assert.equal(parseInput(['-c', 'cfg.json', 'a.js']).config, 'cfg.json');
});
it('--output / -o', () => {
assert.equal(parseInput(['-o', 'bin', 'a.js']).output, 'bin');
});
it('--target aliased to targets', () => {
assert.equal(parseInput(['--target', 'host', 'a.js']).targets, 'host');
});
it('--targets canonical name', () => {
assert.equal(
parseInput(['--targets', 'node22-linux-x64', 'a.js']).targets,
'node22-linux-x64',
);
});
it('-t is short for --target', () => {
assert.equal(parseInput(['-t', 'host', 'a.js']).targets, 'host');
});
it('--out-path / --outdir / --out-dir all collapse to outputPath', () => {
assert.equal(parseInput(['--out-path', '/o', 'a.js']).outputPath, '/o');
assert.equal(parseInput(['--outdir', '/o', 'a.js']).outputPath, '/o');
assert.equal(parseInput(['--out-dir', '/o', 'a.js']).outputPath, '/o');
});
it('--build / -b', () => {
assert.equal(parseInput(['-b', 'a.js']).build, true);
});
it('--no-build (build is CLI-only negatable)', () => {
assert.equal(parseInput(['--no-build', 'a.js']).build, false);
});
});
describe('flag kinds', () => {
it('bool flag --debug', () => {
assert.equal(parseInput(['--debug', 'a.js']).flags.debug, true);
});
it('-d is short for --debug', () => {
assert.equal(parseInput(['-d', 'a.js']).flags.debug, true);
});
it('string flag --compress Brotli', () => {
assert.equal(
parseInput(['--compress', 'Brotli', 'a.js']).flags.compress,
'Brotli',
);
});
it('-C is short for --compress', () => {
assert.equal(parseInput(['-C', 'GZip', 'a.js']).flags.compress, 'GZip');
});
it('list flag --options (csv stored raw)', () => {
assert.equal(
parseInput(['--options', 'expose-gc,use-strict', 'a.js']).flags.options,
'expose-gc,use-strict',
);
});
it('list flag --public-packages (kebab key preserved)', () => {
assert.equal(
parseInput(['--public-packages', '*', 'a.js']).flags['public-packages'],
'*',
);
});
it('list flag --no-dict (kebab key preserved)', () => {
assert.equal(
parseInput(['--no-dict', 'lodash', 'a.js']).flags['no-dict'],
'lodash',
);
});
it('--options "" preserves the explicit empty signal', () => {
assert.equal(parseInput(['--options', '', 'a.js']).flags.options, '');
});
it('string flag --sea-node-path (kebab key preserved)', () => {
assert.equal(
parseInput(['--sea-node-path', '/opt/node/bin/node', 'a.js']).flags[
'sea-node-path'
],
'/opt/node/bin/node',
);
});
it('bool flag --sea-use-local-node (kebab key preserved)', () => {
assert.equal(
parseInput(['--sea-use-local-node', 'a.js']).flags[
'sea-use-local-node'
],
true,
);
});
});
describe('negation', () => {
it('--no-bytecode → bytecode=false', () => {
assert.equal(parseInput(['--no-bytecode', 'a.js']).flags.bytecode, false);
});
it('--bytecode --no-bytecode → last wins (false)', () => {
assert.equal(
parseInput(['--bytecode', '--no-bytecode', 'a.js']).flags.bytecode,
false,
);
});
it('--no-bytecode --bytecode → last wins (true)', () => {
assert.equal(
parseInput(['--no-bytecode', '--bytecode', 'a.js']).flags.bytecode,
true,
);
});
for (const flag of [
'debug',
'bytecode',
'native-build',
'signature',
'fallback-to-source',
'public',
'sea',
]) {
it(`--no-${flag} sets flag to false`, () => {
assert.equal(parseInput([`--no-${flag}`, 'a.js']).flags[flag], false);
});
}
});
describe('error paths', () => {
it('unknown option throws', () => {
assert.throws(
() => parseInput(['--not-a-thing', 'a.js']),
/Unknown option/i,
);
});
it('--compress followed by a flag-looking token is ambiguous', () => {
assert.throws(
() => parseInput(['--compress', '--', 'a.js']),
/ambiguous|missing/i,
);
});
});
});
describe('parseInput — PkgExecOptions', () => {
it('null throws', () => {
assert.throws(
() => parseInput(null as unknown as Parameters<typeof parseInput>[0]),
/options must be an object/,
);
});
it('non-object throws', () => {
assert.throws(
() => parseInput('oops' as unknown as Parameters<typeof parseInput>[0]),
/options must be an object/,
);
});
it('missing input throws', () => {
assert.throws(
() => parseInput({} as Parameters<typeof parseInput>[0]),
/options\.input is required/,
);
});
it('non-string input throws', () => {
assert.throws(
() =>
parseInput({ input: 42 } as unknown as Parameters<
typeof parseInput
>[0]),
/options\.input is required/,
);
});
it('minimum viable options', () => {
const p = parseInput({ input: 'a.js' });
assert.equal(p.entry, 'a.js');
assert.deepEqual(p.flags, {});
});
it('bool flags round-trip', () => {
const p = parseInput({
input: 'a.js',
debug: true,
bytecode: false,
public: true,
sea: true,
signature: false,
nativeBuild: false,
fallbackToSource: true,
});
assert.equal(p.flags.debug, true);
assert.equal(p.flags.bytecode, false);
assert.equal(p.flags.public, true);
assert.equal(p.flags.sea, true);
assert.equal(p.flags.signature, false);
assert.equal(p.flags['native-build'], false);
assert.equal(p.flags['fallback-to-source'], true);
});
it('compress string passes through', () => {
assert.equal(
parseInput({ input: 'a.js', compress: 'Brotli' }).flags.compress,
'Brotli',
);
});
it('bakeOptions array joins to csv under "options"', () => {
assert.equal(
parseInput({
input: 'a.js',
bakeOptions: ['expose-gc', 'use-strict'],
}).flags.options,
'expose-gc,use-strict',
);
});
it('bakeOptions scalar passes through', () => {
assert.equal(
parseInput({ input: 'a.js', bakeOptions: 'expose-gc' }).flags.options,
'expose-gc',
);
});
it('empty bakeOptions array → not set', () => {
assert.equal(
parseInput({ input: 'a.js', bakeOptions: [] }).flags.options,
undefined,
);
});
it('publicPackages array joins under "public-packages"', () => {
assert.equal(
parseInput({ input: 'a.js', publicPackages: ['*'] }).flags[
'public-packages'
],
'*',
);
});
it('noDictionary array joins under "no-dict"', () => {
assert.equal(
parseInput({ input: 'a.js', noDictionary: ['lodash', 'chalk'] }).flags[
'no-dict'
],
'lodash,chalk',
);
});
it('targets array joins', () => {
assert.equal(
parseInput({
input: 'a.js',
targets: ['node22-linux', 'node22-macos'],
}).targets,
'node22-linux,node22-macos',
);
});
it('output + outputPath + config + build round-trip', () => {
const p = parseInput({
input: 'a.js',
output: 'bin',
outputPath: '/dist',
config: 'cfg.json',
build: true,
});
assert.equal(p.output, 'bin');
assert.equal(p.outputPath, '/dist');
assert.equal(p.config, 'cfg.json');
assert.equal(p.build, true);
});
it('unset fields do not appear in flags', () => {
const p = parseInput({ input: 'a.js' });
assert.equal(p.flags.debug, undefined);
assert.equal(p.flags.bytecode, undefined);
assert.equal(p.flags.compress, undefined);
});
});
describe('resolveFlags — CLI > config > default', () => {
it('all defaults when raw + pkg are empty', () => {
const f = resolveFlags({}, {});
assert.equal(f.debug, false);
assert.equal(f.bytecode, true);
assert.equal(f.nativeBuild, true);
assert.equal(f.signature, true);
assert.equal(f.public, false);
assert.equal(f.sea, false);
assert.equal(f.fallbackToSource, false);
assert.equal(f.compress, CompressType.None);
assert.equal(f.bakeOptions, undefined);
assert.equal(f.publicPackages, undefined);
assert.equal(f.noDictionary, undefined);
});
it('config wins when CLI is absent', () => {
const f = resolveFlags(
{},
{
bytecode: false,
public: true,
debug: true,
compress: 'GZip',
fallbackToSource: true,
signature: false,
},
);
assert.equal(f.bytecode, false);
assert.equal(f.public, true);
assert.equal(f.debug, true);
assert.equal(f.compress, CompressType.GZip);
assert.equal(f.fallbackToSource, true);
assert.equal(f.signature, false);
});
it('CLI overrides config', () => {
const f = resolveFlags(
{ bytecode: true, compress: 'None', public: false },
{ bytecode: false, compress: 'Brotli', public: true },
);
assert.equal(f.bytecode, true);
assert.equal(f.compress, CompressType.None);
assert.equal(f.public, false);
});
it('CLI false overrides config true (three-state)', () => {
// Three-state is critical for negation: --no-bytecode must beat
// cfg.bytecode=true.
const f = resolveFlags({ bytecode: false }, { bytecode: true });
assert.equal(f.bytecode, false);
});
it('SEA base-node override — defaults, config, CLI precedence', () => {
const def = resolveFlags({}, {});
assert.equal(def.seaNodePath, undefined);
assert.equal(def.seaUseLocalNode, false);
// config keys (cfg name) resolve when CLI is absent
const cfg = resolveFlags({}, { seaNodePath: '/n', seaUseLocalNode: true });
assert.equal(cfg.seaNodePath, '/n');
assert.equal(cfg.seaUseLocalNode, true);
// CLI (kebab cli name) overrides config
const cli = resolveFlags(
{ 'sea-node-path': '/cli', 'sea-use-local-node': true },
{ seaNodePath: '/cfg' },
);
assert.equal(cli.seaNodePath, '/cli');
assert.equal(cli.seaUseLocalNode, true);
});
describe('list handling', () => {
it('CLI "" clears the configured list (empty wins)', () => {
const f = resolveFlags({ options: '' }, { options: ['expose-gc'] });
assert.equal(f.bakeOptions, undefined);
});
it('CLI csv parses into a cleaned array', () => {
const f = resolveFlags({ 'public-packages': 'a, b , c' }, {});
assert.deepEqual(f.publicPackages, ['a', 'b', 'c']);
});
it('config array cleaned', () => {
const f = resolveFlags({}, { options: ['expose-gc', ' use-strict '] });
assert.deepEqual(f.bakeOptions, ['expose-gc', 'use-strict']);
});
it('config string (comma) normalized', () => {
const f = resolveFlags({}, { publicPackages: 'x,y,z' });
assert.deepEqual(f.publicPackages, ['x', 'y', 'z']);
});
it('list with only whitespace/empty → undefined', () => {
const f = resolveFlags({ 'no-dict': ' , , ' }, {});
assert.equal(f.noDictionary, undefined);
});
it('"*" is preserved', () => {
const f = resolveFlags({}, { publicPackages: ['*'] });
assert.deepEqual(f.publicPackages, ['*']);
});
});
describe('compress decoding', () => {
for (const [input, expected] of [
['None', CompressType.None],
['none', CompressType.None],
['Brotli', CompressType.Brotli],
['br', CompressType.Brotli],
['brotli', CompressType.Brotli],
['BROTLI', CompressType.Brotli],
['GZip', CompressType.GZip],
['gz', CompressType.GZip],
['gzip', CompressType.GZip],
['Zstd', CompressType.Zstd],
['zs', CompressType.Zstd],
['zstd', CompressType.Zstd],
] as const) {
it(`"${input}" → CompressType.${CompressType[expected]}`, () => {
const f = resolveFlags({ compress: input }, {});
assert.equal(f.compress, expected);
});
}
it('invalid value throws', () => {
assert.throws(
() => resolveFlags({ compress: 'lz4' }, {}),
/Invalid compression/,
);
});
it('empty string throws', () => {
assert.throws(
() => resolveFlags({ compress: '' }, {}),
/Invalid compression/,
);
});
});
});
describe('validatePkgConfig', () => {
it('undefined is a no-op', () => {
validatePkgConfig(undefined);
assert.equal(warned.length, 0);
});
it('empty object is a no-op', () => {
validatePkgConfig({});
assert.equal(warned.length, 0);
});
it('unknown key warns (does not throw)', () => {
validatePkgConfig({ totallyMadeUp: 1 });
assert.ok(
warned.some((w) => /totallyMadeUp/.test(w)),
`got: ${warned.join('|')}`,
);
});
it('all known non-flag keys accepted silently', () => {
validatePkgConfig({
scripts: [],
assets: [],
ignore: [],
patches: {},
deployFiles: [],
dictionary: {},
log: null,
targets: [],
outputPath: '',
seaConfig: {},
});
assert.equal(warned.length, 0, `unexpected warns: ${warned.join('|')}`);
});
it('all FLAG_SPECS keys accepted silently with correct types', () => {
validatePkgConfig({
debug: true,
bytecode: false,
nativeBuild: true,
signature: false,
fallbackToSource: true,
public: true,
sea: false,
compress: 'GZip',
options: ['a'],
publicPackages: 'x,y',
noDictionary: ['*'],
});
assert.equal(warned.length, 0, `unexpected warns: ${warned.join('|')}`);
});
it('bool with string throws', () => {
assert.throws(
() => validatePkgConfig({ bytecode: 'yes' }),
/"bytecode" must be a boolean/,
);
});
it('bool with number throws', () => {
assert.throws(
() => validatePkgConfig({ debug: 1 }),
/"debug" must be a boolean/,
);
});
it('string with bool throws', () => {
assert.throws(
() => validatePkgConfig({ compress: true }),
/"compress" must be a string/,
);
});
it('list with number throws', () => {
assert.throws(
() => validatePkgConfig({ publicPackages: 42 }),
/"publicPackages" must be a string or string/,
);
});
it('list with mixed array throws', () => {
assert.throws(
() => validatePkgConfig({ options: ['ok', 42] }),
/"options" must be a string or string/,
);
});
it('list with string OK', () => {
validatePkgConfig({ publicPackages: 'a,b' });
});
it('list with string[] OK', () => {
validatePkgConfig({ publicPackages: ['a', 'b'] });
});
});
describe('isConfiguration', () => {
it('true for package.json regardless of directory', () => {
assert.equal(isConfiguration('package.json'), true);
assert.equal(isConfiguration(path.join('a', 'b', 'package.json')), true);
});
it('true for *.config.json (suffix match, no basename split)', () => {
assert.equal(isConfiguration('pkg.config.json'), true);
assert.equal(isConfiguration('webpack.config.json'), true);
assert.equal(isConfiguration(path.join('deep', 'pkg.config.json')), true);
});
it('false for other json / non-json files', () => {
assert.equal(isConfiguration('tsconfig.json'), false);
assert.equal(isConfiguration('pkg.json'), false);
assert.equal(isConfiguration('pkg.config.js'), false);
assert.equal(isConfiguration('package.js'), false);
});
});
describe('stringifyTarget', () => {
it('joins nodeRange-platform-arch', () => {
assert.equal(
stringifyTarget({ nodeRange: 'node22', platform: 'linux', arch: 'x64' }),
'node22-linux-x64',
);
assert.equal(
stringifyTarget({
nodeRange: 'node24',
platform: 'macos',
arch: 'arm64',
}),
'node24-macos-arm64',
);
});
});
describe('PKGRC_FILENAMES + findPkgrc', () => {
let tmp: string;
before(() => {
tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'pkg-pkgrc-'));
});
after(() => {
fs.rmSync(tmp, { recursive: true, force: true });
});
it('lists filenames in precedence order (no duplicates)', () => {
assert.deepEqual(PKGRC_FILENAMES, [
'.pkgrc',
'.pkgrc.json',
'pkg.config.js',
'pkg.config.cjs',
'pkg.config.mjs',
]);
});
it('finds the first candidate that exists', () => {
const dir = fs.mkdtempSync(path.join(tmp, 'only-'));
fs.writeFileSync(path.join(dir, 'pkg.config.cjs'), 'module.exports = {};');
assert.equal(findPkgrc(dir), path.join(dir, 'pkg.config.cjs'));
});
it('honours precedence when multiple exist', () => {
const dir = fs.mkdtempSync(path.join(tmp, 'many-'));
fs.writeFileSync(path.join(dir, '.pkgrc.json'), '{}');
fs.writeFileSync(path.join(dir, 'pkg.config.cjs'), 'module.exports = {};');
// .pkgrc.json has higher precedence than pkg.config.cjs.
assert.equal(findPkgrc(dir), path.join(dir, '.pkgrc.json'));
});
it('returns undefined when none exist', () => {
const dir = fs.mkdtempSync(path.join(tmp, 'empty-'));
assert.equal(findPkgrc(dir), undefined);
});
});
// Target parsing is the hot path on every pkg invocation — every build runs
// through it. The logic is token-classified (nodeRange / platform / arch)
// with host fallbacks, so these tests pin the classification table and the
// host defaults instead of any specific arch/platform.
describe('parseTargets', () => {
const hostNodeRange = `node${process.version.match(/^v(\d+)/)![1]}`;
it("'host' short-circuits to the full host triple", () => {
const [t] = parseTargets(['host']);
assert.equal(t.nodeRange, hostNodeRange);
assert.equal(t.platform, system.hostPlatform);
assert.equal(t.arch, system.hostArch);
});
it('single token — platform — fills the rest from host', () => {
const [t] = parseTargets(['linux']);
assert.equal(t.platform, 'linux');
assert.equal(t.nodeRange, hostNodeRange);
assert.equal(t.arch, system.hostArch);
});
it('single token — arch — fills the rest from host', () => {
const [t] = parseTargets(['x64']);
assert.equal(t.arch, 'x64');
assert.equal(t.nodeRange, hostNodeRange);
assert.equal(t.platform, system.hostPlatform);
});
it('single token — nodeRange — fills the rest from host', () => {
const [t] = parseTargets(['node22']);
assert.equal(t.nodeRange, 'node22');
assert.equal(t.platform, system.hostPlatform);
assert.equal(t.arch, system.hostArch);
});
it('full triple parses in any token order', () => {
assert.deepEqual(parseTargets(['node22-linux-x64'])[0], {
nodeRange: 'node22',
platform: 'linux',
arch: 'x64',
});
// Order is classified by token, not position.
assert.deepEqual(parseTargets(['x64-linux-node22'])[0], {
nodeRange: 'node22',
platform: 'linux',
arch: 'x64',
});
});
it('empty tokens from repeated "-" are skipped', () => {
assert.deepEqual(parseTargets(['node22--linux'])[0].platform, 'linux');
});
it('aliasing via toFancyPlatform/toFancyArch (win ↔ windows etc.)', () => {
// pkg-fetch normalizes common aliases (e.g. 'win' → 'win',
// 'windows' → 'win', 'x86_64' → 'x64'). Exact alias set is pkg-fetch's
// business — we only pin that an obvious variant reaches a known value.
const t = parseTargets(['windows'])[0];
assert.equal(t.platform, 'win');
});
it('throws on unknown tokens with the offending spec in the message', () => {
assert.throws(
() => parseTargets(['node22-bogus']),
/Unknown token 'bogus' in 'node22-bogus'/,
);
});
it('preserves list order for multiple items', () => {
const out = parseTargets(['node22-linux', 'node24-macos']);
assert.equal(out.length, 2);
assert.equal(out[0].nodeRange, 'node22');
assert.equal(out[0].platform, 'linux');
assert.equal(out[1].nodeRange, 'node24');
assert.equal(out[1].platform, 'macos');
});
it('empty input list yields empty output (resolveTargetList supplies defaults)', () => {
assert.deepEqual(parseTargets([]), []);
});
});
describe('stringifyTarget ↔ parseTargets round-trip', () => {
it('round-trips a fully-specified triple', () => {
const spec = 'node24-linux-arm64';
const [t] = parseTargets([spec]);
assert.equal(stringifyTarget(t), spec);
});
});
describe('differentParts', () => {
const mk = (nodeRange: string, platform: string, arch: string): NodeTarget =>
({ nodeRange, platform, arch }) as unknown as NodeTarget;
it('empty list → no dimensions vary', () => {
assert.deepEqual(differentParts([]), {});
});
it('singleton → no dimensions vary', () => {
assert.deepEqual(differentParts([mk('node22', 'linux', 'x64')]), {});
});
it('all three axes constant → empty object', () => {
assert.deepEqual(
differentParts([
mk('node22', 'linux', 'x64'),
mk('node22', 'linux', 'x64'),
]),
{},
);
});
it('platform differs only', () => {
assert.deepEqual(
differentParts([
mk('node22', 'linux', 'x64'),
mk('node22', 'macos', 'x64'),
]),
{ platform: true },
);
});
it('all three axes differ', () => {
assert.deepEqual(
differentParts([
mk('node22', 'linux', 'x64'),
mk('node24', 'macos', 'arm64'),
]),
{ nodeRange: true, platform: true, arch: true },
);
});
});
describe('stringifyTargetForOutput', () => {
const t: NodeTarget = {
nodeRange: 'node22',
platform: 'linux',
arch: 'x64',
} as unknown as NodeTarget;
it('no axes vary → returns baseOutput unchanged', () => {
assert.equal(stringifyTargetForOutput('app', t, {}), 'app');
});
it('appends only the axes flagged as varying, in order', () => {
assert.equal(
stringifyTargetForOutput('app', t, { platform: true }),
'app-linux',
);
assert.equal(
stringifyTargetForOutput('app', t, { platform: true, arch: true }),
'app-linux-x64',
);
assert.equal(
stringifyTargetForOutput('app', t, {
nodeRange: true,
platform: true,
arch: true,
}),
'app-node22-linux-x64',
);
});
it('arch only (skipping middle axes) does not emit placeholders', () => {
// Regression guard: the order of appending matters so a
// `{arch:true}`-only diff doesn't produce 'app--x64'.
assert.equal(stringifyTargetForOutput('app', t, { arch: true }), 'app-x64');
});
});