@putout/test 13.1.0 → 13.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +12 -0
  2. package/lib/test.js +102 -61
  3. package/package.json +2 -1
package/README.md CHANGED
@@ -58,6 +58,18 @@ const test = createTest(import.meta.url, {
58
58
  });
59
59
  ```
60
60
 
61
+ You can also pass extensions to read from `fixture` directory:
62
+
63
+ ```js
64
+ const test = createTest(import.meta.url, {
65
+ ext: '.wast',
66
+ lint: putout,
67
+ plugins: [
68
+ ['remove-unused-variables', rmVars],
69
+ ],
70
+ });
71
+ ```
72
+
61
73
  ### `report(filename, message: string | string[], plugins?: PutoutPlugin[])`
62
74
 
63
75
  Check error message (or messages) of a plugin:
package/lib/test.js CHANGED
@@ -19,7 +19,7 @@ const {createProgress} = require('@putout/engine-runner/progress');
19
19
 
20
20
  const {createError} = require('./create-error');
21
21
  const {preTest} = require('./pre-test');
22
-
22
+ const maybeArray = (a) => isArray(a) ? a : [a];
23
23
  const {isArray} = Array;
24
24
  const isString = (a) => typeof a === 'string';
25
25
  const isObject = (a) => typeof a === 'object';
@@ -48,37 +48,51 @@ const TS = {
48
48
  DISABLED: false,
49
49
  };
50
50
 
51
- const readFixture = (name) => {
51
+ const readFixture = (name, ext) => {
52
52
  const {readFileSync} = global.__putout_test_fs;
53
- const [e, data] = tryCatch(readFileSync, `${name}.ts`, 'utf8');
53
+ const [eTS, dataTS] = tryCatch(readFileSync, `${name}.ts`, 'utf8');
54
54
 
55
- if (!e)
55
+ if (!eTS)
56
56
  return [
57
- data,
57
+ dataTS,
58
58
  TS.ENABLED,
59
+ '.ts',
60
+ ];
61
+
62
+ const [eJS, dataJS] = tryCatch(readFileSync, `${name}.js`, 'utf8');
63
+
64
+ if (!eJS)
65
+ return [
66
+ dataJS,
67
+ TS.DISABLED,
68
+ '.js',
59
69
  ];
60
70
 
61
- return [
62
- readFileSync(`${name}.js`, 'utf8'),
63
- TS.DISABLED,
64
- ];
71
+ for (const currentExt of maybeArray(ext)) {
72
+ const [e, data] = tryCatch(readFileSync, `${name}${currentExt}`, 'utf8');
73
+
74
+ if (!e)
75
+ return [
76
+ data,
77
+ TS.DISABLED,
78
+ currentExt,
79
+ ];
80
+ }
81
+
82
+ throw eJS;
65
83
  };
66
84
 
67
- const writeFixture = ({full, code, isTS}) => {
85
+ const writeFixture = ({full, code, currentExt}) => {
68
86
  writeSourceFixture({
69
87
  full: `${full}-fix`,
70
88
  code,
71
- isTS,
89
+ currentExt,
72
90
  });
73
91
  };
74
92
 
75
- const writeSourceFixture = ({full, code, isTS}) => {
93
+ const writeSourceFixture = ({full, code, currentExt}) => {
76
94
  const {writeFileSync} = global.__putout_test_fs;
77
-
78
- if (!isTS)
79
- return writeFileSync(`${full}.js`, code);
80
-
81
- writeFileSync(`${full}.ts`, code);
95
+ writeFileSync(`${full}${currentExt}`, code);
82
96
  };
83
97
 
84
98
  const rmFixture = (name) => {
@@ -104,30 +118,39 @@ const parsePlugin = (plugins) => {
104
118
  function createTest(dir, maybeOptions) {
105
119
  dir = join(dir, 'fixture');
106
120
 
107
- const {lint = putout, ...options} = parseOptions(maybeOptions);
121
+ const {
122
+ ext = [],
123
+ lint = putout,
124
+ ...options
125
+ } = parseOptions(maybeOptions);
108
126
 
109
127
  const plugin = parsePlugin(options.plugins);
110
128
 
129
+ const linterOptions = {
130
+ lint,
131
+ ext,
132
+ };
133
+
111
134
  preTest(test, plugin);
112
135
 
113
136
  return test.extend({
114
- transform: transform(dir, lint, options),
115
- noTransform: noTransform(dir, lint, options),
116
- transformCode: transformCode(lint, options),
117
- noTransformCode: noTransformCode(lint, options),
137
+ transform: transform(dir, linterOptions, options),
138
+ noTransform: noTransform(dir, linterOptions, options),
139
+ transformCode: transformCode(linterOptions, options),
140
+ noTransformCode: noTransformCode(linterOptions, options),
118
141
 
119
142
  progress: progress(dir, options),
120
143
  progressWithOptions: progressWithOptions(dir, options),
121
144
 
122
- transformWithOptions: transformWithOptions(dir, lint, options),
123
- noTransformWithOptions: noTransformWithOptions(dir, lint, options),
145
+ transformWithOptions: transformWithOptions(dir, linterOptions, options),
146
+ noTransformWithOptions: noTransformWithOptions(dir, linterOptions, options),
124
147
 
125
- report: report(dir, lint, options),
126
- noReport: noReport(dir, lint, options),
127
- noReportAfterTransform: noReportAfterTransform(dir, lint, options),
128
- noReportAfterTransformWithOptions: noReportAfterTransformWithOptions(dir, lint, options),
129
- reportWithOptions: reportWithOptions(dir, lint, options),
130
- noReportWithOptions: noReportWithOptions(dir, lint, options),
148
+ report: report(dir, linterOptions, options),
149
+ noReport: noReport(dir, linterOptions, options),
150
+ noReportAfterTransform: noReportAfterTransform(dir, linterOptions, options),
151
+ noReportAfterTransformWithOptions: noReportAfterTransformWithOptions(dir, linterOptions, options),
152
+ reportWithOptions: reportWithOptions(dir, linterOptions, options),
153
+ noReportWithOptions: noReportWithOptions(dir, linterOptions, options),
131
154
  reportCode: reportCode(lint, options),
132
155
  noReportCode: noReportCode(lint, options),
133
156
  format: formatSave(dir, options),
@@ -329,12 +352,12 @@ const progressWithOptions = (dir, options) => (t) => async (name, pluginOptions,
329
352
  return t.deepEqual(result, expected);
330
353
  };
331
354
 
332
- const transform = currify((dir, lint, options, t, name, transformed = null, addons = {}) => {
355
+ const transform = currify((dir, linterOptions, options, t, name, transformed = null, addons = {}) => {
356
+ const {lint, ext} = linterOptions;
333
357
  const {plugins} = options;
334
358
  const full = join(dir, name);
335
359
  const isStr = isString(transformed);
336
-
337
- const [input, isTS] = readFixture(full);
360
+ const [input, isTS, currentExt] = readFixture(full, ext);
338
361
 
339
362
  if (!isStr)
340
363
  addons = transformed;
@@ -357,20 +380,22 @@ const transform = currify((dir, lint, options, t, name, transformed = null, addo
357
380
  writeFixture({
358
381
  full,
359
382
  code,
360
- isTS,
383
+
384
+ currentExt,
361
385
  });
362
386
  return t.pass('fixed fixture updated');
363
387
  }
364
388
 
365
- const [output] = isStr ? [transformed] : readFixture(`${full}-fix`);
389
+ const [output] = isStr ? [transformed] : readFixture(`${full}-fix`, ext);
366
390
 
367
391
  return t.equal(code, output);
368
392
  });
369
393
 
370
- const transformWithOptions = currify((dir, lint, options, t, name, pluginOptions) => {
371
- const {writeFileSync} = global.__putout_test_fs;
394
+ const transformWithOptions = currify((dir, linterOptions, options, t, name, pluginOptions) => {
395
+ const {lint, ext} = linterOptions;
396
+
372
397
  const full = join(dir, name);
373
- const [input, isTS] = readFixture(full);
398
+ const [input, isTS, currentExt] = readFixture(full, ext);
374
399
 
375
400
  const rule = parseRule(options);
376
401
 
@@ -385,11 +410,15 @@ const transformWithOptions = currify((dir, lint, options, t, name, pluginOptions
385
410
  });
386
411
 
387
412
  if (isUpdate()) {
388
- writeFileSync(`${full}-fix.js`, code);
413
+ writeFixture({
414
+ full,
415
+ code,
416
+ currentExt,
417
+ });
389
418
  return t.pass('fixed fixture updated');
390
419
  }
391
420
 
392
- const [output] = readFixture(`${full}-fix`);
421
+ const [output] = readFixture(`${full}-fix`, ext);
393
422
 
394
423
  return t.equal(code, output);
395
424
  });
@@ -400,9 +429,10 @@ const parseRule = ({plugins}) => {
400
429
  return plugin[0] || keys(plugin)[0];
401
430
  };
402
431
 
403
- const noTransformWithOptions = currify((dir, lint, options, t, name, ruleOptions) => {
432
+ const noTransformWithOptions = currify((dir, linterOptions, options, t, name, ruleOptions) => {
433
+ const {lint, ext} = linterOptions;
404
434
  const full = join(dir, name);
405
- const [input, isTS] = readFixture(full);
435
+ const [input, isTS, currentExt] = readFixture(full, ext);
406
436
 
407
437
  rmFixture(`${full}-fix`);
408
438
 
@@ -421,7 +451,8 @@ const noTransformWithOptions = currify((dir, lint, options, t, name, ruleOptions
421
451
  writeSourceFixture({
422
452
  full,
423
453
  code,
424
- isTS,
454
+
455
+ currentExt,
425
456
  });
426
457
 
427
458
  return t.pass('source fixture updated');
@@ -430,14 +461,15 @@ const noTransformWithOptions = currify((dir, lint, options, t, name, ruleOptions
430
461
  return t.equal(code, input);
431
462
  });
432
463
 
433
- const noTransform = currify((dir, lint, options, t, name, addons = {}) => {
464
+ const noTransform = currify((dir, linterOptions, options, t, name, addons = {}) => {
465
+ const {lint, ext} = linterOptions;
434
466
  const full = join(dir, name);
435
- const [fixture] = readFixture(full);
467
+ const [fixture] = readFixture(full, ext);
436
468
 
437
469
  rmFixture(`${full}-fix`);
438
470
 
439
471
  const {plugins} = options;
440
- const [input, isTS] = readFixture(full);
472
+ const [input, isTS, currentExt] = readFixture(full, ext);
441
473
 
442
474
  const {code} = lint(input, {
443
475
  isTS,
@@ -452,7 +484,7 @@ const noTransform = currify((dir, lint, options, t, name, addons = {}) => {
452
484
  writeSourceFixture({
453
485
  full,
454
486
  code,
455
- isTS,
487
+ currentExt,
456
488
  });
457
489
 
458
490
  return t.pass('source fixture updated');
@@ -461,7 +493,8 @@ const noTransform = currify((dir, lint, options, t, name, addons = {}) => {
461
493
  return t.equal(code, fixture);
462
494
  });
463
495
 
464
- const transformCode = currify((lint, options, t, input, output, isTS = false) => {
496
+ const transformCode = currify((linterOptions, options, t, input, output, isTS = false) => {
497
+ const {lint} = linterOptions;
465
498
  const {code} = lint(input, {
466
499
  isTS,
467
500
  ...options,
@@ -470,18 +503,21 @@ const transformCode = currify((lint, options, t, input, output, isTS = false) =>
470
503
  return t.equal(code, output);
471
504
  });
472
505
 
473
- const noTransformCode = currify((lint, options, t, input) => {
506
+ const noTransformCode = currify((linterOptions, options, t, input) => {
507
+ const {lint} = linterOptions;
474
508
  const {code} = lint(input, options);
509
+
475
510
  return t.equal(code, input);
476
511
  });
477
512
 
478
513
  const getMessage = ({message}) => message;
479
514
 
480
- const report = (dir, lint, options) => (t) => (name, message, plugins) => {
515
+ const report = (dir, linterOptions, options) => (t) => (name, message, plugins) => {
516
+ const {lint, ext} = linterOptions;
481
517
  checkReport(name, message);
482
518
 
483
519
  const full = join(dir, name);
484
- const [source, isTS] = readFixture(full);
520
+ const [source, isTS] = readFixture(full, ext);
485
521
 
486
522
  const addT = reportCode(lint, {
487
523
  isTS,
@@ -493,9 +529,10 @@ const report = (dir, lint, options) => (t) => (name, message, plugins) => {
493
529
  return run(source, message, plugins);
494
530
  };
495
531
 
496
- const noReport = currify((dir, lint, options, t, name) => {
532
+ const noReport = currify((dir, linterOptions, options, t, name) => {
533
+ const {lint, ext} = linterOptions;
497
534
  const full = join(dir, name);
498
- const [source, isTS] = readFixture(full);
535
+ const [source, isTS] = readFixture(full, ext);
499
536
 
500
537
  rmFixture(`${full}-fix`);
501
538
 
@@ -507,9 +544,10 @@ const noReport = currify((dir, lint, options, t, name) => {
507
544
 
508
545
  module.exports._createNoReport = noReport;
509
546
 
510
- const noReportAfterTransform = currify((dir, lint, options, t, name, addons = {}) => {
547
+ const noReportAfterTransform = currify((dir, linterOptions, options, t, name, addons = {}) => {
548
+ const {lint, ext} = linterOptions;
511
549
  const full = join(dir, name);
512
- const [source, isTS] = readFixture(full);
550
+ const [source, isTS] = readFixture(full, ext);
513
551
 
514
552
  return noReportCodeAfterTransform(lint, {
515
553
  isTS,
@@ -519,9 +557,10 @@ const noReportAfterTransform = currify((dir, lint, options, t, name, addons = {}
519
557
 
520
558
  module.exports._createNoReportAfterTransform = noReportAfterTransform;
521
559
 
522
- const noReportAfterTransformWithOptions = currify((dir, lint, options, t, name, ruleOptions) => {
560
+ const noReportAfterTransformWithOptions = currify((dir, linterOptions, options, t, name, ruleOptions) => {
561
+ const {lint, ext} = linterOptions;
523
562
  const full = join(dir, name);
524
- const [source, isTS] = readFixture(full);
563
+ const [source, isTS] = readFixture(full, ext);
525
564
  const rule = parseRule(options);
526
565
 
527
566
  const rules = {
@@ -540,9 +579,10 @@ const noReportAfterTransformWithOptions = currify((dir, lint, options, t, name,
540
579
 
541
580
  module.exports._createNoReportAfterTransformWithOptions = noReportAfterTransformWithOptions;
542
581
 
543
- const reportWithOptions = currify((dir, lint, options, t, name, message, ruleOptions) => {
582
+ const reportWithOptions = currify((dir, linterOptions, options, t, name, message, ruleOptions) => {
583
+ const {lint, ext} = linterOptions;
544
584
  const full = join(dir, name);
545
- const [source, isTS] = readFixture(full);
585
+ const [source, isTS] = readFixture(full, ext);
546
586
 
547
587
  const rule = parseRule(options);
548
588
 
@@ -561,9 +601,10 @@ const reportWithOptions = currify((dir, lint, options, t, name, message, ruleOpt
561
601
  return run(source, message);
562
602
  });
563
603
 
564
- const noReportWithOptions = currify((dir, lint, options, t, name, ruleOptions) => {
604
+ const noReportWithOptions = currify((dir, linterOptions, options, t, name, ruleOptions) => {
605
+ const {lint, ext} = linterOptions;
565
606
  const full = join(dir, name);
566
- const [source, isTS] = readFixture(full);
607
+ const [source, isTS] = readFixture(full, ext);
567
608
 
568
609
  rmFixture(`${full}-fix`);
569
610
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/test",
3
- "version": "13.1.0",
3
+ "version": "13.2.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Test runner for 🐊Putout plugins ",
@@ -66,6 +66,7 @@
66
66
  "@putout/plugin-putout": "*",
67
67
  "@putout/plugin-remove-console": "*",
68
68
  "@putout/plugin-remove-unused-variables": "*",
69
+ "@putout/processor-wasm": "^3.0.0",
69
70
  "c8": "^10.0.0",
70
71
  "eslint": "^9.0.0",
71
72
  "eslint-plugin-n": "^17.0.0",