@putout/test 6.1.1 β 6.3.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.
- package/README.md +19 -0
- package/lib/test.js +127 -98
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,6 +21,14 @@ Set environment variable `UPDATE=1` to update `transform` and `format` fixtures
|
|
|
21
21
|
UPDATE=1 tape test/*.js
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
+
## Printer
|
|
25
|
+
|
|
26
|
+
You can override default printer (`recast`) with [`putout`](https://github.com/putoutjs/printer) or `babel` using:
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
PUTOUT_PRINTER=putout tape test/*.js
|
|
30
|
+
```
|
|
31
|
+
|
|
24
32
|
## Plugins API
|
|
25
33
|
|
|
26
34
|
All π**Putout** plugins written in `CommonJS`, since `ESLint` written on `CommonJS` and we have a huge `ESLint`-based ecosystem which is good to reuse.
|
|
@@ -39,6 +47,17 @@ const test = createTest(import.meta.url, {
|
|
|
39
47
|
});
|
|
40
48
|
```
|
|
41
49
|
|
|
50
|
+
You can also pass all π**Putout** options:
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
const test = createTest(import.meta.url, {
|
|
54
|
+
printer: 'putout',
|
|
55
|
+
plugins: [
|
|
56
|
+
['remove-unused-variables', rmVars],
|
|
57
|
+
],
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
42
61
|
### `report(filename, message | []messages)`
|
|
43
62
|
|
|
44
63
|
Check error message (or messages) of a plugin:
|
package/lib/test.js
CHANGED
|
@@ -16,6 +16,7 @@ const currify = require('currify');
|
|
|
16
16
|
const isCorrectPlugin = require('./is-correct-plugin');
|
|
17
17
|
|
|
18
18
|
const isString = (a) => typeof a === 'string';
|
|
19
|
+
const isObject = (a) => typeof a === 'object';
|
|
19
20
|
const {isArray} = Array;
|
|
20
21
|
const {keys, entries} = Object;
|
|
21
22
|
|
|
@@ -27,6 +28,7 @@ global.__putout_test_fs = {
|
|
|
27
28
|
};
|
|
28
29
|
|
|
29
30
|
const isUpdate = () => Boolean(Number(process.env.UPDATE));
|
|
31
|
+
const getPrinter = () => process.env.PUTOUT_PRINTER;
|
|
30
32
|
|
|
31
33
|
const TS = {
|
|
32
34
|
ENABLED: true,
|
|
@@ -72,48 +74,58 @@ const rmFixture = (name) => {
|
|
|
72
74
|
module.exports = createTest;
|
|
73
75
|
module.exports.createTest = createTest;
|
|
74
76
|
|
|
75
|
-
|
|
77
|
+
const parsePlugin = (plugins) => {
|
|
78
|
+
if (isArray(plugins))
|
|
79
|
+
return plugins[0];
|
|
80
|
+
|
|
81
|
+
return plugins;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
function createTest(dir, maybeOptions) {
|
|
76
85
|
const update = isUpdate();
|
|
77
86
|
|
|
78
87
|
dir = join(dir, 'fixture');
|
|
79
|
-
|
|
88
|
+
|
|
89
|
+
const options = parseOptions(maybeOptions);
|
|
90
|
+
const plugin = parsePlugin(options.plugins);
|
|
80
91
|
|
|
81
92
|
preTest(test, plugin);
|
|
82
93
|
|
|
83
94
|
return test.extend({
|
|
84
|
-
transform: transform(
|
|
85
|
-
noTransform: noTransform(
|
|
86
|
-
transformCode: transformCode(
|
|
87
|
-
noTransformCode: noTransformCode(
|
|
95
|
+
transform: transform(dir, options),
|
|
96
|
+
noTransform: noTransform(dir, options),
|
|
97
|
+
transformCode: transformCode(options),
|
|
98
|
+
noTransformCode: noTransformCode(options),
|
|
88
99
|
|
|
89
|
-
transformWithOptions: transformWithOptions(
|
|
90
|
-
noTransformWithOptions: noTransformWithOptions(
|
|
100
|
+
transformWithOptions: transformWithOptions(dir, options),
|
|
101
|
+
noTransformWithOptions: noTransformWithOptions(dir, options),
|
|
91
102
|
|
|
92
|
-
report: report(
|
|
93
|
-
noReport: noReport(
|
|
94
|
-
noReportAfterTransform: noReportAfterTransform(
|
|
95
|
-
reportWithOptions: reportWithOptions(
|
|
96
|
-
noReportWithOptions: noReportWithOptions(
|
|
97
|
-
reportCode: reportCode(
|
|
98
|
-
plugins,
|
|
99
|
-
rules,
|
|
100
|
-
}),
|
|
103
|
+
report: report(dir, options),
|
|
104
|
+
noReport: noReport(dir, options),
|
|
105
|
+
noReportAfterTransform: noReportAfterTransform(dir, options),
|
|
106
|
+
reportWithOptions: reportWithOptions(dir, options),
|
|
107
|
+
noReportWithOptions: noReportWithOptions(dir, options),
|
|
108
|
+
reportCode: reportCode(options),
|
|
101
109
|
|
|
102
|
-
formatSave: formatSave(
|
|
103
|
-
format: (update ? formatSave : format)(
|
|
104
|
-
formatManySave: formatManySave(
|
|
105
|
-
formatMany: (update ? formatManySave : formatMany)(
|
|
106
|
-
noFormat: noFormat(
|
|
110
|
+
formatSave: formatSave(dir, options),
|
|
111
|
+
format: (update ? formatSave : format)(dir, options),
|
|
112
|
+
formatManySave: formatManySave(dir, options),
|
|
113
|
+
formatMany: (update ? formatManySave : formatMany)(dir, options),
|
|
114
|
+
noFormat: noFormat(dir, options),
|
|
107
115
|
});
|
|
108
116
|
}
|
|
109
117
|
|
|
110
|
-
const format = currify((
|
|
118
|
+
const format = currify((dir, options, t) => async (formatter, name, formatterOptions = {}) => {
|
|
111
119
|
const full = join(dir, name);
|
|
112
120
|
const outputName = `${full}-format`;
|
|
113
121
|
const [input, isTS] = readFixture(full);
|
|
114
122
|
const [expected] = readFixture(outputName);
|
|
115
123
|
|
|
116
|
-
const {places} = putout(input, {
|
|
124
|
+
const {places} = putout(input, {
|
|
125
|
+
fixCount: 1,
|
|
126
|
+
isTS,
|
|
127
|
+
...options,
|
|
128
|
+
});
|
|
117
129
|
|
|
118
130
|
const report = putout.initReport();
|
|
119
131
|
const result = await report(formatter, {
|
|
@@ -128,11 +140,10 @@ const format = currify(({dir, plugins, rules}, t) => async (formatter, name, for
|
|
|
128
140
|
return {is, output, result};
|
|
129
141
|
});
|
|
130
142
|
|
|
131
|
-
const noFormat = currify((
|
|
143
|
+
const noFormat = currify((dir, options, t) => async (formatter, name, formatterOptions = {}) => {
|
|
132
144
|
const full = join(dir, name);
|
|
133
145
|
const [input] = readFixture(full);
|
|
134
|
-
|
|
135
|
-
const {places} = putout(input, {plugins, rules});
|
|
146
|
+
const {places} = putout(input, options);
|
|
136
147
|
|
|
137
148
|
const report = putout.initReport();
|
|
138
149
|
const result = await report(formatter, {
|
|
@@ -146,7 +157,7 @@ const noFormat = currify(({dir, plugins, rules}, t) => async (formatter, name, f
|
|
|
146
157
|
return {is, output, result};
|
|
147
158
|
});
|
|
148
159
|
|
|
149
|
-
const formatMany = currify((
|
|
160
|
+
const formatMany = currify((dir, options, t) => async (formatter, names, formatterOptions = {}) => {
|
|
150
161
|
const joinTwo = (a) => (b) => join(a, b);
|
|
151
162
|
|
|
152
163
|
if (!isArray(names))
|
|
@@ -166,8 +177,7 @@ const formatMany = currify(({dir, plugins, rules}, t) => async (formatter, names
|
|
|
166
177
|
|
|
167
178
|
const {places} = putout(input, {
|
|
168
179
|
fixCount: 1,
|
|
169
|
-
|
|
170
|
-
rules,
|
|
180
|
+
...options,
|
|
171
181
|
});
|
|
172
182
|
|
|
173
183
|
result += await report(formatter, {
|
|
@@ -188,7 +198,7 @@ const formatMany = currify(({dir, plugins, rules}, t) => async (formatter, names
|
|
|
188
198
|
return {is, output, result};
|
|
189
199
|
});
|
|
190
200
|
|
|
191
|
-
const formatManySave = currify((
|
|
201
|
+
const formatManySave = currify((dir, options, t) => async (formatter, names, options = {}) => {
|
|
192
202
|
const {
|
|
193
203
|
existsSync,
|
|
194
204
|
writeFileSync,
|
|
@@ -203,7 +213,7 @@ const formatManySave = currify(({dir, plugins, rules}, t) => async (formatter, n
|
|
|
203
213
|
if (!existsSync(outputName))
|
|
204
214
|
writeFileSync(outputName, '');
|
|
205
215
|
|
|
206
|
-
const runFormat = await formatMany(
|
|
216
|
+
const runFormat = await formatMany(dir, options, t);
|
|
207
217
|
const {result} = await runFormat(formatter, names, options);
|
|
208
218
|
|
|
209
219
|
writeFileSync(outputName, result);
|
|
@@ -211,7 +221,7 @@ const formatManySave = currify(({dir, plugins, rules}, t) => async (formatter, n
|
|
|
211
221
|
return t.pass('fixed fixture updated');
|
|
212
222
|
});
|
|
213
223
|
|
|
214
|
-
const formatSave = currify((
|
|
224
|
+
const formatSave = currify((dir, options, t) => async (formatter, name, options = {}) => {
|
|
215
225
|
const {
|
|
216
226
|
existsSync,
|
|
217
227
|
writeFileSync,
|
|
@@ -223,12 +233,7 @@ const formatSave = currify(({dir, plugins, rules}, t) => async (formatter, name,
|
|
|
223
233
|
if (!existsSync(outputName))
|
|
224
234
|
writeFileSync(outputName, '');
|
|
225
235
|
|
|
226
|
-
const runFormat = format(
|
|
227
|
-
dir,
|
|
228
|
-
plugins,
|
|
229
|
-
rules,
|
|
230
|
-
}, t);
|
|
231
|
-
|
|
236
|
+
const runFormat = format(dir, options, t);
|
|
232
237
|
const {result} = await runFormat(formatter, name, options);
|
|
233
238
|
|
|
234
239
|
writeFileSync(outputName, result);
|
|
@@ -236,7 +241,23 @@ const formatSave = currify(({dir, plugins, rules}, t) => async (formatter, name,
|
|
|
236
241
|
return t.pass('fixed fixture updated');
|
|
237
242
|
});
|
|
238
243
|
|
|
239
|
-
const
|
|
244
|
+
const toObject = (array) => {
|
|
245
|
+
const result = {};
|
|
246
|
+
const first = parsePlugin(array);
|
|
247
|
+
|
|
248
|
+
if (isObject(first) && !isArray(first)) {
|
|
249
|
+
return first;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
for (const [name, value] of array) {
|
|
253
|
+
result[name] = value;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return result;
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
const transform = currify((dir, options, t, name, transformed = null, addons = {}) => {
|
|
260
|
+
const {plugins} = options;
|
|
240
261
|
const full = join(dir, name);
|
|
241
262
|
const [input, isTS] = readFixture(full);
|
|
242
263
|
const isStr = isString(transformed);
|
|
@@ -249,10 +270,11 @@ const transform = currify(({dir, plugins, rules}, t, name, transformed = null, a
|
|
|
249
270
|
addons = addons || {};
|
|
250
271
|
|
|
251
272
|
const {code} = putout(input, {
|
|
273
|
+
printer: getPrinter(),
|
|
252
274
|
isTS,
|
|
253
|
-
|
|
275
|
+
...options,
|
|
254
276
|
plugins: [{
|
|
255
|
-
...plugins
|
|
277
|
+
...toObject(plugins),
|
|
256
278
|
...addons,
|
|
257
279
|
}],
|
|
258
280
|
});
|
|
@@ -280,20 +302,24 @@ const transform = currify(({dir, plugins, rules}, t, name, transformed = null, a
|
|
|
280
302
|
return t.equal(code, output);
|
|
281
303
|
});
|
|
282
304
|
|
|
283
|
-
const transformWithOptions = currify((
|
|
305
|
+
const transformWithOptions = currify((dir, options, t, name, additionalOptions) => {
|
|
284
306
|
const {writeFileSync} = global.__putout_test_fs;
|
|
285
307
|
const full = join(dir, name);
|
|
286
308
|
const [input, isTS] = readFixture(full);
|
|
287
309
|
|
|
288
310
|
const [output] = readFixture(`${full}-fix`);
|
|
289
|
-
const
|
|
290
|
-
const [rule] = keys(plugin);
|
|
311
|
+
const rule = parseRule(options);
|
|
291
312
|
|
|
292
313
|
const rules = {
|
|
293
|
-
[rule]: ['on',
|
|
314
|
+
[rule]: ['on', additionalOptions],
|
|
294
315
|
};
|
|
295
316
|
|
|
296
|
-
const {code} = putout(input, {
|
|
317
|
+
const {code} = putout(input, {
|
|
318
|
+
printer: getPrinter(),
|
|
319
|
+
isTS,
|
|
320
|
+
rules,
|
|
321
|
+
...options,
|
|
322
|
+
});
|
|
297
323
|
|
|
298
324
|
if (isUpdate()) {
|
|
299
325
|
writeFileSync(`${full}-fix.js`, code);
|
|
@@ -303,107 +329,113 @@ const transformWithOptions = currify(({dir, plugins}, t, name, options) => {
|
|
|
303
329
|
return t.equal(code, output);
|
|
304
330
|
});
|
|
305
331
|
|
|
306
|
-
const
|
|
332
|
+
const parseRule = ({plugins}) => {
|
|
333
|
+
const [plugin] = plugins;
|
|
334
|
+
|
|
335
|
+
return plugin[0] || keys(plugin)[0];
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const noTransformWithOptions = currify((dir, options, t, name, ruleOptions) => {
|
|
307
339
|
const full = join(dir, name);
|
|
308
340
|
const [input, isTS] = readFixture(full);
|
|
309
341
|
|
|
310
342
|
rmFixture(`${full}-fix`);
|
|
311
343
|
|
|
312
|
-
const
|
|
313
|
-
const [rule] = keys(plugin);
|
|
344
|
+
const rule = parseRule(options);
|
|
314
345
|
|
|
315
346
|
const rules = {
|
|
316
|
-
[rule]: ['on',
|
|
347
|
+
[rule]: ['on', ruleOptions],
|
|
317
348
|
};
|
|
318
349
|
|
|
319
|
-
const {code} = putout(input, {
|
|
350
|
+
const {code} = putout(input, {
|
|
351
|
+
isTS,
|
|
352
|
+
rules,
|
|
353
|
+
...options,
|
|
354
|
+
});
|
|
320
355
|
|
|
321
356
|
return t.equal(code, input);
|
|
322
357
|
});
|
|
323
358
|
|
|
324
|
-
const noTransform = currify((
|
|
359
|
+
const noTransform = currify((dir, options, t, name, addons = {}) => {
|
|
325
360
|
const full = join(dir, name);
|
|
326
361
|
const [fixture] = readFixture(full);
|
|
327
362
|
|
|
328
363
|
rmFixture(`${full}-fix`);
|
|
329
364
|
|
|
330
|
-
return transform(
|
|
365
|
+
return transform(dir, options, t, name, fixture, addons);
|
|
331
366
|
});
|
|
332
367
|
|
|
333
|
-
const transformCode = currify((
|
|
334
|
-
const {code} = putout(input, {
|
|
368
|
+
const transformCode = currify((options, t, input, output, isTS = false) => {
|
|
369
|
+
const {code} = putout(input, {
|
|
370
|
+
isTS,
|
|
371
|
+
...options,
|
|
372
|
+
});
|
|
373
|
+
|
|
335
374
|
return t.equal(code, output);
|
|
336
375
|
});
|
|
337
376
|
|
|
338
|
-
const noTransformCode = currify((
|
|
339
|
-
const {code} = putout(input,
|
|
377
|
+
const noTransformCode = currify((options, t, input) => {
|
|
378
|
+
const {code} = putout(input, options);
|
|
340
379
|
return t.equal(code, input);
|
|
341
380
|
});
|
|
342
381
|
|
|
343
382
|
const getMessage = ({message}) => message;
|
|
344
383
|
|
|
345
|
-
const report = currify((
|
|
384
|
+
const report = currify((dir, options, t, name, message) => {
|
|
346
385
|
const full = join(dir, name);
|
|
347
386
|
const [source, isTS] = readFixture(full);
|
|
348
387
|
|
|
349
|
-
return reportCode({
|
|
388
|
+
return reportCode({isTS, ...options}, t, source, message);
|
|
350
389
|
});
|
|
351
390
|
|
|
352
|
-
const noReport = currify((
|
|
391
|
+
const noReport = currify((dir, options, t, name) => {
|
|
353
392
|
const full = join(dir, name);
|
|
354
393
|
const [source, isTS] = readFixture(full);
|
|
355
394
|
|
|
356
395
|
rmFixture(`${full}-fix`);
|
|
357
396
|
|
|
358
|
-
return noReportCode({
|
|
397
|
+
return noReportCode({isTS, ...options}, t, source);
|
|
359
398
|
});
|
|
360
399
|
module.exports._createNoReport = noReport;
|
|
361
400
|
|
|
362
|
-
const noReportAfterTransform = currify((
|
|
401
|
+
const noReportAfterTransform = currify((dir, options, t, name) => {
|
|
363
402
|
const full = join(dir, name);
|
|
364
403
|
const [source, isTS] = readFixture(full);
|
|
365
404
|
|
|
366
|
-
return noReportCodeAfterTransform({
|
|
405
|
+
return noReportCodeAfterTransform({isTS, ...options}, t, source);
|
|
367
406
|
});
|
|
368
407
|
module.exports._createNoReportAfterTransform = noReportAfterTransform;
|
|
369
408
|
|
|
370
|
-
const reportWithOptions = currify((
|
|
409
|
+
const reportWithOptions = currify((dir, options, t, name, message, ruleOptions) => {
|
|
371
410
|
const full = join(dir, name);
|
|
372
411
|
const [source, isTS] = readFixture(full);
|
|
373
412
|
|
|
374
|
-
const
|
|
375
|
-
const [rule] = keys(plugin);
|
|
376
|
-
|
|
413
|
+
const rule = parseRule(options);
|
|
377
414
|
const rules = {
|
|
378
|
-
[rule]: ['on',
|
|
415
|
+
[rule]: ['on', ruleOptions],
|
|
379
416
|
};
|
|
380
417
|
|
|
381
|
-
return reportCode({
|
|
418
|
+
return reportCode({...options, rules, isTS}, t, source, message);
|
|
382
419
|
});
|
|
383
420
|
|
|
384
|
-
const noReportWithOptions = currify((
|
|
421
|
+
const noReportWithOptions = currify((dir, options, t, name, ruleOptions) => {
|
|
385
422
|
const full = join(dir, name);
|
|
386
423
|
const [source, isTS] = readFixture(full);
|
|
387
424
|
|
|
388
425
|
rmFixture(`${full}-fix`);
|
|
389
426
|
|
|
390
|
-
const
|
|
391
|
-
const [rule] = keys(plugin);
|
|
392
|
-
|
|
427
|
+
const rule = parseRule(options);
|
|
393
428
|
const rules = {
|
|
394
|
-
[rule]: ['on',
|
|
429
|
+
[rule]: ['on', ruleOptions],
|
|
395
430
|
};
|
|
396
431
|
|
|
397
|
-
return noReportCode({
|
|
432
|
+
return noReportCode({isTS, ...options, rules}, t, source);
|
|
398
433
|
});
|
|
399
434
|
|
|
400
|
-
const reportCode = currify((
|
|
401
|
-
const fix = false;
|
|
435
|
+
const reportCode = currify((options, t, source, message) => {
|
|
402
436
|
const {places} = putout(source, {
|
|
403
|
-
fix,
|
|
404
|
-
|
|
405
|
-
rules,
|
|
406
|
-
plugins,
|
|
437
|
+
fix: false,
|
|
438
|
+
...options,
|
|
407
439
|
});
|
|
408
440
|
|
|
409
441
|
const resultMessages = places.map(getMessage);
|
|
@@ -414,34 +446,31 @@ const reportCode = currify(({plugins, rules, isTS}, t, source, message) => {
|
|
|
414
446
|
return t.equal(resultMessages[0], message);
|
|
415
447
|
});
|
|
416
448
|
|
|
417
|
-
const noReportCode = currify((
|
|
418
|
-
const fix = false;
|
|
449
|
+
const noReportCode = currify((options, t, source) => {
|
|
419
450
|
const {places} = putout(source, {
|
|
420
|
-
fix,
|
|
421
|
-
|
|
422
|
-
rules,
|
|
423
|
-
plugins,
|
|
451
|
+
fix: false,
|
|
452
|
+
...options,
|
|
424
453
|
});
|
|
425
454
|
|
|
426
455
|
return t.deepEqual(places, [], 'should not report');
|
|
427
456
|
});
|
|
428
457
|
|
|
429
|
-
const noReportCodeAfterTransform = currify((
|
|
430
|
-
const fix = true;
|
|
458
|
+
const noReportCodeAfterTransform = currify((options, t, source) => {
|
|
431
459
|
const {places} = putout(source, {
|
|
432
|
-
fix,
|
|
433
|
-
|
|
434
|
-
rules,
|
|
435
|
-
plugins,
|
|
460
|
+
fix: true,
|
|
461
|
+
...options,
|
|
436
462
|
});
|
|
437
463
|
|
|
438
464
|
return t.deepEqual(places, [], 'should not report after transform');
|
|
439
465
|
});
|
|
440
466
|
|
|
441
|
-
function
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
467
|
+
function parseOptions(plugin) {
|
|
468
|
+
if (!plugin.plugins)
|
|
469
|
+
return {
|
|
470
|
+
plugins: [plugin],
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
return plugin;
|
|
445
474
|
}
|
|
446
475
|
|
|
447
476
|
function preTest(test, plugin) {
|