@putout/test 6.2.0 → 6.3.1
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 +11 -0
- package/lib/test.js +143 -108
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -47,6 +47,17 @@ const test = createTest(import.meta.url, {
|
|
|
47
47
|
});
|
|
48
48
|
```
|
|
49
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
|
+
|
|
50
61
|
### `report(filename, message | []messages)`
|
|
51
62
|
|
|
52
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
|
|
|
@@ -73,48 +74,58 @@ const rmFixture = (name) => {
|
|
|
73
74
|
module.exports = createTest;
|
|
74
75
|
module.exports.createTest = createTest;
|
|
75
76
|
|
|
76
|
-
|
|
77
|
+
const parsePlugin = (plugins) => {
|
|
78
|
+
if (isArray(plugins))
|
|
79
|
+
return plugins[0];
|
|
80
|
+
|
|
81
|
+
return plugins;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
function createTest(dir, maybeOptions) {
|
|
77
85
|
const update = isUpdate();
|
|
78
86
|
|
|
79
87
|
dir = join(dir, 'fixture');
|
|
80
|
-
|
|
88
|
+
|
|
89
|
+
const options = parseOptions(maybeOptions);
|
|
90
|
+
const plugin = parsePlugin(options.plugins);
|
|
81
91
|
|
|
82
92
|
preTest(test, plugin);
|
|
83
93
|
|
|
84
94
|
return test.extend({
|
|
85
|
-
transform: transform(
|
|
86
|
-
noTransform: noTransform(
|
|
87
|
-
transformCode: transformCode(
|
|
88
|
-
noTransformCode: noTransformCode(
|
|
95
|
+
transform: transform(dir, options),
|
|
96
|
+
noTransform: noTransform(dir, options),
|
|
97
|
+
transformCode: transformCode(options),
|
|
98
|
+
noTransformCode: noTransformCode(options),
|
|
89
99
|
|
|
90
|
-
transformWithOptions: transformWithOptions(
|
|
91
|
-
noTransformWithOptions: noTransformWithOptions(
|
|
100
|
+
transformWithOptions: transformWithOptions(dir, options),
|
|
101
|
+
noTransformWithOptions: noTransformWithOptions(dir, options),
|
|
92
102
|
|
|
93
|
-
report: report(
|
|
94
|
-
noReport: noReport(
|
|
95
|
-
noReportAfterTransform: noReportAfterTransform(
|
|
96
|
-
reportWithOptions: reportWithOptions(
|
|
97
|
-
noReportWithOptions: noReportWithOptions(
|
|
98
|
-
reportCode: reportCode(
|
|
99
|
-
plugins,
|
|
100
|
-
rules,
|
|
101
|
-
}),
|
|
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),
|
|
102
109
|
|
|
103
|
-
formatSave: formatSave(
|
|
104
|
-
format: (update ? formatSave : format)(
|
|
105
|
-
formatManySave: formatManySave(
|
|
106
|
-
formatMany: (update ? formatManySave : formatMany)(
|
|
107
|
-
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),
|
|
108
115
|
});
|
|
109
116
|
}
|
|
110
117
|
|
|
111
|
-
const format = currify((
|
|
118
|
+
const format = currify((dir, options, t) => async (formatter, name, formatterOptions = {}) => {
|
|
112
119
|
const full = join(dir, name);
|
|
113
120
|
const outputName = `${full}-format`;
|
|
114
121
|
const [input, isTS] = readFixture(full);
|
|
115
122
|
const [expected] = readFixture(outputName);
|
|
116
123
|
|
|
117
|
-
const {places} = putout(input, {
|
|
124
|
+
const {places} = putout(input, {
|
|
125
|
+
fixCount: 1,
|
|
126
|
+
isTS,
|
|
127
|
+
...options,
|
|
128
|
+
});
|
|
118
129
|
|
|
119
130
|
const report = putout.initReport();
|
|
120
131
|
const result = await report(formatter, {
|
|
@@ -129,11 +140,10 @@ const format = currify(({dir, plugins, rules}, t) => async (formatter, name, for
|
|
|
129
140
|
return {is, output, result};
|
|
130
141
|
});
|
|
131
142
|
|
|
132
|
-
const noFormat = currify((
|
|
143
|
+
const noFormat = currify((dir, options, t) => async (formatter, name, formatterOptions = {}) => {
|
|
133
144
|
const full = join(dir, name);
|
|
134
145
|
const [input] = readFixture(full);
|
|
135
|
-
|
|
136
|
-
const {places} = putout(input, {plugins, rules});
|
|
146
|
+
const {places} = putout(input, options);
|
|
137
147
|
|
|
138
148
|
const report = putout.initReport();
|
|
139
149
|
const result = await report(formatter, {
|
|
@@ -147,7 +157,7 @@ const noFormat = currify(({dir, plugins, rules}, t) => async (formatter, name, f
|
|
|
147
157
|
return {is, output, result};
|
|
148
158
|
});
|
|
149
159
|
|
|
150
|
-
const formatMany = currify((
|
|
160
|
+
const formatMany = currify((dir, options, t) => async (formatter, names, formatterOptions = {}) => {
|
|
151
161
|
const joinTwo = (a) => (b) => join(a, b);
|
|
152
162
|
|
|
153
163
|
if (!isArray(names))
|
|
@@ -167,8 +177,7 @@ const formatMany = currify(({dir, plugins, rules}, t) => async (formatter, names
|
|
|
167
177
|
|
|
168
178
|
const {places} = putout(input, {
|
|
169
179
|
fixCount: 1,
|
|
170
|
-
|
|
171
|
-
rules,
|
|
180
|
+
...options,
|
|
172
181
|
});
|
|
173
182
|
|
|
174
183
|
result += await report(formatter, {
|
|
@@ -189,7 +198,7 @@ const formatMany = currify(({dir, plugins, rules}, t) => async (formatter, names
|
|
|
189
198
|
return {is, output, result};
|
|
190
199
|
});
|
|
191
200
|
|
|
192
|
-
const formatManySave = currify((
|
|
201
|
+
const formatManySave = currify((dir, options, t) => async (formatter, names, options = {}) => {
|
|
193
202
|
const {
|
|
194
203
|
existsSync,
|
|
195
204
|
writeFileSync,
|
|
@@ -204,7 +213,7 @@ const formatManySave = currify(({dir, plugins, rules}, t) => async (formatter, n
|
|
|
204
213
|
if (!existsSync(outputName))
|
|
205
214
|
writeFileSync(outputName, '');
|
|
206
215
|
|
|
207
|
-
const runFormat = await formatMany(
|
|
216
|
+
const runFormat = await formatMany(dir, options, t);
|
|
208
217
|
const {result} = await runFormat(formatter, names, options);
|
|
209
218
|
|
|
210
219
|
writeFileSync(outputName, result);
|
|
@@ -212,7 +221,7 @@ const formatManySave = currify(({dir, plugins, rules}, t) => async (formatter, n
|
|
|
212
221
|
return t.pass('fixed fixture updated');
|
|
213
222
|
});
|
|
214
223
|
|
|
215
|
-
const formatSave = currify((
|
|
224
|
+
const formatSave = currify((dir, options, t) => async (formatter, name, options = {}) => {
|
|
216
225
|
const {
|
|
217
226
|
existsSync,
|
|
218
227
|
writeFileSync,
|
|
@@ -224,12 +233,7 @@ const formatSave = currify(({dir, plugins, rules}, t) => async (formatter, name,
|
|
|
224
233
|
if (!existsSync(outputName))
|
|
225
234
|
writeFileSync(outputName, '');
|
|
226
235
|
|
|
227
|
-
const runFormat = format(
|
|
228
|
-
dir,
|
|
229
|
-
plugins,
|
|
230
|
-
rules,
|
|
231
|
-
}, t);
|
|
232
|
-
|
|
236
|
+
const runFormat = format(dir, options, t);
|
|
233
237
|
const {result} = await runFormat(formatter, name, options);
|
|
234
238
|
|
|
235
239
|
writeFileSync(outputName, result);
|
|
@@ -237,7 +241,23 @@ const formatSave = currify(({dir, plugins, rules}, t) => async (formatter, name,
|
|
|
237
241
|
return t.pass('fixed fixture updated');
|
|
238
242
|
});
|
|
239
243
|
|
|
240
|
-
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;
|
|
241
261
|
const full = join(dir, name);
|
|
242
262
|
const [input, isTS] = readFixture(full);
|
|
243
263
|
const isStr = isString(transformed);
|
|
@@ -252,23 +272,13 @@ const transform = currify(({dir, plugins, rules}, t, name, transformed = null, a
|
|
|
252
272
|
const {code} = putout(input, {
|
|
253
273
|
printer: getPrinter(),
|
|
254
274
|
isTS,
|
|
255
|
-
|
|
275
|
+
...options,
|
|
256
276
|
plugins: [{
|
|
257
|
-
...plugins
|
|
277
|
+
...toObject(plugins),
|
|
258
278
|
...addons,
|
|
259
279
|
}],
|
|
260
280
|
});
|
|
261
281
|
|
|
262
|
-
if (isUpdate() && isStr) {
|
|
263
|
-
writeSourceFixture({
|
|
264
|
-
full,
|
|
265
|
-
code,
|
|
266
|
-
isTS,
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
return t.pass('source fixture updated');
|
|
270
|
-
}
|
|
271
|
-
|
|
272
282
|
if (isUpdate() && !isStr) {
|
|
273
283
|
writeFixture({
|
|
274
284
|
full,
|
|
@@ -282,24 +292,23 @@ const transform = currify(({dir, plugins, rules}, t, name, transformed = null, a
|
|
|
282
292
|
return t.equal(code, output);
|
|
283
293
|
});
|
|
284
294
|
|
|
285
|
-
const transformWithOptions = currify((
|
|
295
|
+
const transformWithOptions = currify((dir, options, t, name, additionalOptions) => {
|
|
286
296
|
const {writeFileSync} = global.__putout_test_fs;
|
|
287
297
|
const full = join(dir, name);
|
|
288
298
|
const [input, isTS] = readFixture(full);
|
|
289
299
|
|
|
290
300
|
const [output] = readFixture(`${full}-fix`);
|
|
291
|
-
const
|
|
292
|
-
const [rule] = keys(plugin);
|
|
301
|
+
const rule = parseRule(options);
|
|
293
302
|
|
|
294
303
|
const rules = {
|
|
295
|
-
[rule]: ['on',
|
|
304
|
+
[rule]: ['on', additionalOptions],
|
|
296
305
|
};
|
|
297
306
|
|
|
298
307
|
const {code} = putout(input, {
|
|
299
308
|
printer: getPrinter(),
|
|
300
309
|
isTS,
|
|
301
|
-
plugins,
|
|
302
310
|
rules,
|
|
311
|
+
...options,
|
|
303
312
|
});
|
|
304
313
|
|
|
305
314
|
if (isUpdate()) {
|
|
@@ -310,107 +319,136 @@ const transformWithOptions = currify(({dir, plugins}, t, name, options) => {
|
|
|
310
319
|
return t.equal(code, output);
|
|
311
320
|
});
|
|
312
321
|
|
|
313
|
-
const
|
|
322
|
+
const parseRule = ({plugins}) => {
|
|
323
|
+
const [plugin] = plugins;
|
|
324
|
+
|
|
325
|
+
return plugin[0] || keys(plugin)[0];
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
const noTransformWithOptions = currify((dir, options, t, name, ruleOptions) => {
|
|
314
329
|
const full = join(dir, name);
|
|
315
330
|
const [input, isTS] = readFixture(full);
|
|
316
331
|
|
|
317
332
|
rmFixture(`${full}-fix`);
|
|
318
333
|
|
|
319
|
-
const
|
|
320
|
-
const [rule] = keys(plugin);
|
|
334
|
+
const rule = parseRule(options);
|
|
321
335
|
|
|
322
336
|
const rules = {
|
|
323
|
-
[rule]: ['on',
|
|
337
|
+
[rule]: ['on', ruleOptions],
|
|
324
338
|
};
|
|
325
339
|
|
|
326
|
-
const {code} = putout(input, {
|
|
340
|
+
const {code} = putout(input, {
|
|
341
|
+
isTS,
|
|
342
|
+
rules,
|
|
343
|
+
...options,
|
|
344
|
+
});
|
|
327
345
|
|
|
328
346
|
return t.equal(code, input);
|
|
329
347
|
});
|
|
330
348
|
|
|
331
|
-
const noTransform = currify((
|
|
349
|
+
const noTransform = currify((dir, options, t, name, addons = {}) => {
|
|
332
350
|
const full = join(dir, name);
|
|
333
351
|
const [fixture] = readFixture(full);
|
|
334
352
|
|
|
335
353
|
rmFixture(`${full}-fix`);
|
|
336
354
|
|
|
337
|
-
|
|
355
|
+
const {plugins} = options;
|
|
356
|
+
const [input, isTS] = readFixture(full);
|
|
357
|
+
|
|
358
|
+
const {code} = putout(input, {
|
|
359
|
+
printer: getPrinter(),
|
|
360
|
+
isTS,
|
|
361
|
+
...options,
|
|
362
|
+
plugins: [{
|
|
363
|
+
...toObject(plugins),
|
|
364
|
+
...addons,
|
|
365
|
+
}],
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
if (isUpdate()) {
|
|
369
|
+
writeSourceFixture({
|
|
370
|
+
full,
|
|
371
|
+
code,
|
|
372
|
+
isTS,
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
return t.pass('source fixture updated');
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return t.equal(code, fixture);
|
|
338
379
|
});
|
|
339
380
|
|
|
340
|
-
const transformCode = currify((
|
|
341
|
-
const {code} = putout(input, {
|
|
381
|
+
const transformCode = currify((options, t, input, output, isTS = false) => {
|
|
382
|
+
const {code} = putout(input, {
|
|
383
|
+
isTS,
|
|
384
|
+
...options,
|
|
385
|
+
});
|
|
386
|
+
|
|
342
387
|
return t.equal(code, output);
|
|
343
388
|
});
|
|
344
389
|
|
|
345
|
-
const noTransformCode = currify((
|
|
346
|
-
const {code} = putout(input,
|
|
390
|
+
const noTransformCode = currify((options, t, input) => {
|
|
391
|
+
const {code} = putout(input, options);
|
|
347
392
|
return t.equal(code, input);
|
|
348
393
|
});
|
|
349
394
|
|
|
350
395
|
const getMessage = ({message}) => message;
|
|
351
396
|
|
|
352
|
-
const report = currify((
|
|
397
|
+
const report = currify((dir, options, t, name, message) => {
|
|
353
398
|
const full = join(dir, name);
|
|
354
399
|
const [source, isTS] = readFixture(full);
|
|
355
400
|
|
|
356
|
-
return reportCode({
|
|
401
|
+
return reportCode({isTS, ...options}, t, source, message);
|
|
357
402
|
});
|
|
358
403
|
|
|
359
|
-
const noReport = currify((
|
|
404
|
+
const noReport = currify((dir, options, t, name) => {
|
|
360
405
|
const full = join(dir, name);
|
|
361
406
|
const [source, isTS] = readFixture(full);
|
|
362
407
|
|
|
363
408
|
rmFixture(`${full}-fix`);
|
|
364
409
|
|
|
365
|
-
return noReportCode({
|
|
410
|
+
return noReportCode({isTS, ...options}, t, source);
|
|
366
411
|
});
|
|
367
412
|
module.exports._createNoReport = noReport;
|
|
368
413
|
|
|
369
|
-
const noReportAfterTransform = currify((
|
|
414
|
+
const noReportAfterTransform = currify((dir, options, t, name) => {
|
|
370
415
|
const full = join(dir, name);
|
|
371
416
|
const [source, isTS] = readFixture(full);
|
|
372
417
|
|
|
373
|
-
return noReportCodeAfterTransform({
|
|
418
|
+
return noReportCodeAfterTransform({isTS, ...options}, t, source);
|
|
374
419
|
});
|
|
375
420
|
module.exports._createNoReportAfterTransform = noReportAfterTransform;
|
|
376
421
|
|
|
377
|
-
const reportWithOptions = currify((
|
|
422
|
+
const reportWithOptions = currify((dir, options, t, name, message, ruleOptions) => {
|
|
378
423
|
const full = join(dir, name);
|
|
379
424
|
const [source, isTS] = readFixture(full);
|
|
380
425
|
|
|
381
|
-
const
|
|
382
|
-
const [rule] = keys(plugin);
|
|
383
|
-
|
|
426
|
+
const rule = parseRule(options);
|
|
384
427
|
const rules = {
|
|
385
|
-
[rule]: ['on',
|
|
428
|
+
[rule]: ['on', ruleOptions],
|
|
386
429
|
};
|
|
387
430
|
|
|
388
|
-
return reportCode({
|
|
431
|
+
return reportCode({...options, rules, isTS}, t, source, message);
|
|
389
432
|
});
|
|
390
433
|
|
|
391
|
-
const noReportWithOptions = currify((
|
|
434
|
+
const noReportWithOptions = currify((dir, options, t, name, ruleOptions) => {
|
|
392
435
|
const full = join(dir, name);
|
|
393
436
|
const [source, isTS] = readFixture(full);
|
|
394
437
|
|
|
395
438
|
rmFixture(`${full}-fix`);
|
|
396
439
|
|
|
397
|
-
const
|
|
398
|
-
const [rule] = keys(plugin);
|
|
399
|
-
|
|
440
|
+
const rule = parseRule(options);
|
|
400
441
|
const rules = {
|
|
401
|
-
[rule]: ['on',
|
|
442
|
+
[rule]: ['on', ruleOptions],
|
|
402
443
|
};
|
|
403
444
|
|
|
404
|
-
return noReportCode({
|
|
445
|
+
return noReportCode({isTS, ...options, rules}, t, source);
|
|
405
446
|
});
|
|
406
447
|
|
|
407
|
-
const reportCode = currify((
|
|
408
|
-
const fix = false;
|
|
448
|
+
const reportCode = currify((options, t, source, message) => {
|
|
409
449
|
const {places} = putout(source, {
|
|
410
|
-
fix,
|
|
411
|
-
|
|
412
|
-
rules,
|
|
413
|
-
plugins,
|
|
450
|
+
fix: false,
|
|
451
|
+
...options,
|
|
414
452
|
});
|
|
415
453
|
|
|
416
454
|
const resultMessages = places.map(getMessage);
|
|
@@ -421,34 +459,31 @@ const reportCode = currify(({plugins, rules, isTS}, t, source, message) => {
|
|
|
421
459
|
return t.equal(resultMessages[0], message);
|
|
422
460
|
});
|
|
423
461
|
|
|
424
|
-
const noReportCode = currify((
|
|
425
|
-
const fix = false;
|
|
462
|
+
const noReportCode = currify((options, t, source) => {
|
|
426
463
|
const {places} = putout(source, {
|
|
427
|
-
fix,
|
|
428
|
-
|
|
429
|
-
rules,
|
|
430
|
-
plugins,
|
|
464
|
+
fix: false,
|
|
465
|
+
...options,
|
|
431
466
|
});
|
|
432
467
|
|
|
433
468
|
return t.deepEqual(places, [], 'should not report');
|
|
434
469
|
});
|
|
435
470
|
|
|
436
|
-
const noReportCodeAfterTransform = currify((
|
|
437
|
-
const fix = true;
|
|
471
|
+
const noReportCodeAfterTransform = currify((options, t, source) => {
|
|
438
472
|
const {places} = putout(source, {
|
|
439
|
-
fix,
|
|
440
|
-
|
|
441
|
-
rules,
|
|
442
|
-
plugins,
|
|
473
|
+
fix: true,
|
|
474
|
+
...options,
|
|
443
475
|
});
|
|
444
476
|
|
|
445
477
|
return t.deepEqual(places, [], 'should not report after transform');
|
|
446
478
|
});
|
|
447
479
|
|
|
448
|
-
function
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
480
|
+
function parseOptions(plugin) {
|
|
481
|
+
if (!plugin.plugins)
|
|
482
|
+
return {
|
|
483
|
+
plugins: [plugin],
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
return plugin;
|
|
452
487
|
}
|
|
453
488
|
|
|
454
489
|
function preTest(test, plugin) {
|