Skip to content

Commit 5547e4c

Browse files
committed
fix: @putout/test: ext -> extension
1 parent 94dc749 commit 5547e4c

File tree

5 files changed

+98
-101
lines changed

5 files changed

+98
-101
lines changed

packages/processor-wasm/test/create-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {lint} from '../lib/lint.js';
33

44
export const createTest = (url, options) => {
55
return createPutoutTest(url, {
6-
ext: '.wast',
6+
extension: 'wast',
77
lint,
88
...options,
99
});

packages/test/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ You can also pass extensions to read from `fixture` directory:
6262
6363
```js
6464
const test = createTest(import.meta.url, {
65-
ext: '.wast',
65+
extension: 'wast',
6666
lint: putout,
6767
plugins: [
6868
['remove-unused-variables', rmVars],

packages/test/lib/fixture.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
3+
const process = require('node:process');
4+
const tryCatch = require('try-catch');
5+
6+
const isUpdate = () => Boolean(Number(process.env.UPDATE));
7+
const TS = {
8+
ENABLED: true,
9+
DISABLED: false,
10+
};
11+
12+
module.exports.readFixture = (name, extension) => {
13+
const {readFileSync} = global.__putout_test_fs;
14+
const [eTS, dataTS] = tryCatch(readFileSync, `${name}.ts`, 'utf8');
15+
16+
if (!eTS)
17+
return [
18+
dataTS,
19+
TS.ENABLED,
20+
];
21+
22+
const [eJS, dataJS] = tryCatch(readFileSync, `${name}.js`, 'utf8');
23+
24+
if (!eJS)
25+
return [
26+
dataJS,
27+
TS.DISABLED,
28+
];
29+
30+
if (extension) {
31+
const [e, data] = tryCatch(readFileSync, `${name}.${extension}`, 'utf8');
32+
33+
if (!e)
34+
return [
35+
data,
36+
TS.DISABLED,
37+
];
38+
}
39+
40+
throw eJS;
41+
};
42+
43+
module.exports.writeFixture = ({full, code, extension}) => {
44+
const {writeFileSync} = global.__putout_test_fs;
45+
writeFileSync(`${full}-fix.${extension}`, code);
46+
};
47+
48+
module.exports.rmFixture = (name) => {
49+
const {unlinkSync} = global.__putout_test_fs;
50+
51+
if (!isUpdate())
52+
return;
53+
54+
tryCatch(unlinkSync, `${name}.js`);
55+
tryCatch(unlinkSync, `${name}.ts`);
56+
};

packages/test/lib/test.js

Lines changed: 38 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@ const {
1111
unlinkSync,
1212
} = require('node:fs');
1313

14-
const tryCatch = require('try-catch');
1514
const test = require('supertape');
1615
const putout = require('putout');
1716
const currify = require('currify');
1817
const {createProgress} = require('@putout/engine-runner/progress');
1918

2019
const {createError} = require('./create-error');
2120
const {preTest} = require('./pre-test');
22-
const maybeArray = (a) => isArray(a) ? a : [a];
21+
22+
const {
23+
readFixture,
24+
writeFixture,
25+
rmFixture,
26+
} = require('./fixture');
27+
2328
const {isArray} = Array;
2429
const isString = (a) => typeof a === 'string';
2530
const isObject = (a) => typeof a === 'object';
@@ -43,68 +48,6 @@ const fail = (t, message) => {
4348
return __putout_test_fail(message);
4449
};
4550

46-
const TS = {
47-
ENABLED: true,
48-
DISABLED: false,
49-
};
50-
51-
const readFixture = (name, ext) => {
52-
const {readFileSync} = global.__putout_test_fs;
53-
const [eTS, dataTS] = tryCatch(readFileSync, `${name}.ts`, 'utf8');
54-
55-
if (!eTS)
56-
return [
57-
dataTS,
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',
69-
];
70-
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;
83-
};
84-
85-
const writeFixture = ({full, code, currentExt}) => {
86-
writeSourceFixture({
87-
full: `${full}-fix`,
88-
code,
89-
currentExt,
90-
});
91-
};
92-
93-
const writeSourceFixture = ({full, code, currentExt}) => {
94-
const {writeFileSync} = global.__putout_test_fs;
95-
writeFileSync(`${full}${currentExt}`, code);
96-
};
97-
98-
const rmFixture = (name) => {
99-
const {unlinkSync} = global.__putout_test_fs;
100-
101-
if (!isUpdate())
102-
return;
103-
104-
tryCatch(unlinkSync, `${name}.js`);
105-
tryCatch(unlinkSync, `${name}.ts`);
106-
};
107-
10851
module.exports = createTest;
10952
module.exports.createTest = createTest;
11053

@@ -119,7 +62,7 @@ function createTest(dir, maybeOptions) {
11962
dir = join(dir, 'fixture');
12063

12164
const {
122-
ext = [],
65+
extension = [],
12366
lint = putout,
12467
...options
12568
} = parseOptions(maybeOptions);
@@ -128,7 +71,7 @@ function createTest(dir, maybeOptions) {
12871

12972
const linterOptions = {
13073
lint,
131-
ext,
74+
extension,
13275
};
13376

13477
preTest(test, plugin);
@@ -353,11 +296,11 @@ const progressWithOptions = (dir, options) => (t) => async (name, pluginOptions,
353296
};
354297

355298
const transform = currify((dir, linterOptions, options, t, name, transformed = null, addons = {}) => {
356-
const {lint, ext} = linterOptions;
299+
const {lint, extension} = linterOptions;
357300
const {plugins} = options;
358301
const full = join(dir, name);
359302
const isStr = isString(transformed);
360-
const [input, isTS, currentExt] = readFixture(full, ext);
303+
const [input, isTS] = readFixture(full, extension);
361304

362305
if (!isStr)
363306
addons = transformed;
@@ -380,22 +323,21 @@ const transform = currify((dir, linterOptions, options, t, name, transformed = n
380323
writeFixture({
381324
full,
382325
code,
383-
384-
currentExt,
326+
extension,
385327
});
386328
return t.pass('fixed fixture updated');
387329
}
388330

389-
const [output] = isStr ? [transformed] : readFixture(`${full}-fix`, ext);
331+
const [output] = isStr ? [transformed] : readFixture(`${full}-fix`, extension);
390332

391333
return t.equal(code, output);
392334
});
393335

394336
const transformWithOptions = currify((dir, linterOptions, options, t, name, pluginOptions) => {
395-
const {lint, ext} = linterOptions;
337+
const {lint, extension} = linterOptions;
396338

397339
const full = join(dir, name);
398-
const [input, isTS, currentExt] = readFixture(full, ext);
340+
const [input, isTS] = readFixture(full, extension);
399341

400342
const rule = parseRule(options);
401343

@@ -413,12 +355,12 @@ const transformWithOptions = currify((dir, linterOptions, options, t, name, plug
413355
writeFixture({
414356
full,
415357
code,
416-
currentExt,
358+
extension,
417359
});
418360
return t.pass('fixed fixture updated');
419361
}
420362

421-
const [output] = readFixture(`${full}-fix`, ext);
363+
const [output] = readFixture(`${full}-fix`, extension);
422364

423365
return t.equal(code, output);
424366
});
@@ -430,9 +372,9 @@ const parseRule = ({plugins}) => {
430372
};
431373

432374
const noTransformWithOptions = currify((dir, linterOptions, options, t, name, ruleOptions) => {
433-
const {lint, ext} = linterOptions;
375+
const {lint, extension} = linterOptions;
434376
const full = join(dir, name);
435-
const [input, isTS, currentExt] = readFixture(full, ext);
377+
const [input, isTS] = readFixture(full, extension);
436378

437379
rmFixture(`${full}-fix`);
438380

@@ -448,11 +390,10 @@ const noTransformWithOptions = currify((dir, linterOptions, options, t, name, ru
448390
});
449391

450392
if (isUpdate()) {
451-
writeSourceFixture({
393+
writeFixture({
452394
full,
453395
code,
454-
455-
currentExt,
396+
extension,
456397
});
457398

458399
return t.pass('source fixture updated');
@@ -462,14 +403,14 @@ const noTransformWithOptions = currify((dir, linterOptions, options, t, name, ru
462403
});
463404

464405
const noTransform = currify((dir, linterOptions, options, t, name, addons = {}) => {
465-
const {lint, ext} = linterOptions;
406+
const {lint, extension} = linterOptions;
466407
const full = join(dir, name);
467-
const [fixture] = readFixture(full, ext);
408+
const [fixture] = readFixture(full, extension);
468409

469410
rmFixture(`${full}-fix`);
470411

471412
const {plugins} = options;
472-
const [input, isTS, currentExt] = readFixture(full, ext);
413+
const [input, isTS] = readFixture(full, extension);
473414

474415
const {code} = lint(input, {
475416
isTS,
@@ -481,10 +422,10 @@ const noTransform = currify((dir, linterOptions, options, t, name, addons = {})
481422
});
482423

483424
if (isUpdate()) {
484-
writeSourceFixture({
425+
writeFixture({
485426
full,
486427
code,
487-
currentExt,
428+
extension,
488429
});
489430

490431
return t.pass('source fixture updated');
@@ -513,11 +454,11 @@ const noTransformCode = currify((linterOptions, options, t, input) => {
513454
const getMessage = ({message}) => message;
514455

515456
const report = (dir, linterOptions, options) => (t) => (name, message, plugins) => {
516-
const {lint, ext} = linterOptions;
457+
const {lint, extension} = linterOptions;
517458
checkReport(name, message);
518459

519460
const full = join(dir, name);
520-
const [source, isTS] = readFixture(full, ext);
461+
const [source, isTS] = readFixture(full, extension);
521462

522463
const addT = reportCode(lint, {
523464
isTS,
@@ -530,9 +471,9 @@ const report = (dir, linterOptions, options) => (t) => (name, message, plugins)
530471
};
531472

532473
const noReport = currify((dir, linterOptions, options, t, name) => {
533-
const {lint, ext} = linterOptions;
474+
const {lint, extension} = linterOptions;
534475
const full = join(dir, name);
535-
const [source, isTS] = readFixture(full, ext);
476+
const [source, isTS] = readFixture(full, extension);
536477

537478
rmFixture(`${full}-fix`);
538479

@@ -545,9 +486,9 @@ const noReport = currify((dir, linterOptions, options, t, name) => {
545486
module.exports._createNoReport = noReport;
546487

547488
const noReportAfterTransform = currify((dir, linterOptions, options, t, name, addons = {}) => {
548-
const {lint, ext} = linterOptions;
489+
const {lint, extension} = linterOptions;
549490
const full = join(dir, name);
550-
const [source, isTS] = readFixture(full, ext);
491+
const [source, isTS] = readFixture(full, extension);
551492

552493
return noReportCodeAfterTransform(lint, {
553494
isTS,
@@ -558,9 +499,9 @@ const noReportAfterTransform = currify((dir, linterOptions, options, t, name, ad
558499
module.exports._createNoReportAfterTransform = noReportAfterTransform;
559500

560501
const noReportAfterTransformWithOptions = currify((dir, linterOptions, options, t, name, ruleOptions) => {
561-
const {lint, ext} = linterOptions;
502+
const {lint, extension} = linterOptions;
562503
const full = join(dir, name);
563-
const [source, isTS] = readFixture(full, ext);
504+
const [source, isTS] = readFixture(full, extension);
564505
const rule = parseRule(options);
565506

566507
const rules = {
@@ -580,9 +521,9 @@ const noReportAfterTransformWithOptions = currify((dir, linterOptions, options,
580521
module.exports._createNoReportAfterTransformWithOptions = noReportAfterTransformWithOptions;
581522

582523
const reportWithOptions = currify((dir, linterOptions, options, t, name, message, ruleOptions) => {
583-
const {lint, ext} = linterOptions;
524+
const {lint, extension} = linterOptions;
584525
const full = join(dir, name);
585-
const [source, isTS] = readFixture(full, ext);
526+
const [source, isTS] = readFixture(full, extension);
586527

587528
const rule = parseRule(options);
588529

@@ -602,9 +543,9 @@ const reportWithOptions = currify((dir, linterOptions, options, t, name, message
602543
});
603544

604545
const noReportWithOptions = currify((dir, linterOptions, options, t, name, ruleOptions) => {
605-
const {lint, ext} = linterOptions;
546+
const {lint, extension} = linterOptions;
606547
const full = join(dir, name);
607-
const [source, isTS] = readFixture(full, ext);
548+
const [source, isTS] = readFixture(full, extension);
608549

609550
rmFixture(`${full}-fix`);
610551

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ const update = createUpdate();
1717
const {writeFileSync} = fs;
1818

1919
const test = createTest(__dirname, {
20-
ext: '.wast',
2120
lint,
21+
extension: 'wast',
2222
plugins: [
2323
['wasm', require('@putout/processor-wasm/plugin')],
2424
],
2525
});
2626

2727
const test2 = createTest(__dirname, {
28-
ext: ['.wast'],
28+
extension: 'wast',
2929
lint,
3030
plugins: [
3131
['wasm', require('@putout/processor-wasm/plugin')],

0 commit comments

Comments
 (0)