@putout/test 6.2.0 → 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 +11 -0
- package/lib/test.js +120 -98
- 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,9 +272,9 @@ 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
|
});
|
|
@@ -282,24 +302,23 @@ const transform = currify(({dir, plugins, rules}, t, name, transformed = null, a
|
|
|
282
302
|
return t.equal(code, output);
|
|
283
303
|
});
|
|
284
304
|
|
|
285
|
-
const transformWithOptions = currify((
|
|
305
|
+
const transformWithOptions = currify((dir, options, t, name, additionalOptions) => {
|
|
286
306
|
const {writeFileSync} = global.__putout_test_fs;
|
|
287
307
|
const full = join(dir, name);
|
|
288
308
|
const [input, isTS] = readFixture(full);
|
|
289
309
|
|
|
290
310
|
const [output] = readFixture(`${full}-fix`);
|
|
291
|
-
const
|
|
292
|
-
const [rule] = keys(plugin);
|
|
311
|
+
const rule = parseRule(options);
|
|
293
312
|
|
|
294
313
|
const rules = {
|
|
295
|
-
[rule]: ['on',
|
|
314
|
+
[rule]: ['on', additionalOptions],
|
|
296
315
|
};
|
|
297
316
|
|
|
298
317
|
const {code} = putout(input, {
|
|
299
318
|
printer: getPrinter(),
|
|
300
319
|
isTS,
|
|
301
|
-
plugins,
|
|
302
320
|
rules,
|
|
321
|
+
...options,
|
|
303
322
|
});
|
|
304
323
|
|
|
305
324
|
if (isUpdate()) {
|
|
@@ -310,107 +329,113 @@ const transformWithOptions = currify(({dir, plugins}, t, name, options) => {
|
|
|
310
329
|
return t.equal(code, output);
|
|
311
330
|
});
|
|
312
331
|
|
|
313
|
-
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) => {
|
|
314
339
|
const full = join(dir, name);
|
|
315
340
|
const [input, isTS] = readFixture(full);
|
|
316
341
|
|
|
317
342
|
rmFixture(`${full}-fix`);
|
|
318
343
|
|
|
319
|
-
const
|
|
320
|
-
const [rule] = keys(plugin);
|
|
344
|
+
const rule = parseRule(options);
|
|
321
345
|
|
|
322
346
|
const rules = {
|
|
323
|
-
[rule]: ['on',
|
|
347
|
+
[rule]: ['on', ruleOptions],
|
|
324
348
|
};
|
|
325
349
|
|
|
326
|
-
const {code} = putout(input, {
|
|
350
|
+
const {code} = putout(input, {
|
|
351
|
+
isTS,
|
|
352
|
+
rules,
|
|
353
|
+
...options,
|
|
354
|
+
});
|
|
327
355
|
|
|
328
356
|
return t.equal(code, input);
|
|
329
357
|
});
|
|
330
358
|
|
|
331
|
-
const noTransform = currify((
|
|
359
|
+
const noTransform = currify((dir, options, t, name, addons = {}) => {
|
|
332
360
|
const full = join(dir, name);
|
|
333
361
|
const [fixture] = readFixture(full);
|
|
334
362
|
|
|
335
363
|
rmFixture(`${full}-fix`);
|
|
336
364
|
|
|
337
|
-
return transform(
|
|
365
|
+
return transform(dir, options, t, name, fixture, addons);
|
|
338
366
|
});
|
|
339
367
|
|
|
340
|
-
const transformCode = currify((
|
|
341
|
-
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
|
+
|
|
342
374
|
return t.equal(code, output);
|
|
343
375
|
});
|
|
344
376
|
|
|
345
|
-
const noTransformCode = currify((
|
|
346
|
-
const {code} = putout(input,
|
|
377
|
+
const noTransformCode = currify((options, t, input) => {
|
|
378
|
+
const {code} = putout(input, options);
|
|
347
379
|
return t.equal(code, input);
|
|
348
380
|
});
|
|
349
381
|
|
|
350
382
|
const getMessage = ({message}) => message;
|
|
351
383
|
|
|
352
|
-
const report = currify((
|
|
384
|
+
const report = currify((dir, options, t, name, message) => {
|
|
353
385
|
const full = join(dir, name);
|
|
354
386
|
const [source, isTS] = readFixture(full);
|
|
355
387
|
|
|
356
|
-
return reportCode({
|
|
388
|
+
return reportCode({isTS, ...options}, t, source, message);
|
|
357
389
|
});
|
|
358
390
|
|
|
359
|
-
const noReport = currify((
|
|
391
|
+
const noReport = currify((dir, options, t, name) => {
|
|
360
392
|
const full = join(dir, name);
|
|
361
393
|
const [source, isTS] = readFixture(full);
|
|
362
394
|
|
|
363
395
|
rmFixture(`${full}-fix`);
|
|
364
396
|
|
|
365
|
-
return noReportCode({
|
|
397
|
+
return noReportCode({isTS, ...options}, t, source);
|
|
366
398
|
});
|
|
367
399
|
module.exports._createNoReport = noReport;
|
|
368
400
|
|
|
369
|
-
const noReportAfterTransform = currify((
|
|
401
|
+
const noReportAfterTransform = currify((dir, options, t, name) => {
|
|
370
402
|
const full = join(dir, name);
|
|
371
403
|
const [source, isTS] = readFixture(full);
|
|
372
404
|
|
|
373
|
-
return noReportCodeAfterTransform({
|
|
405
|
+
return noReportCodeAfterTransform({isTS, ...options}, t, source);
|
|
374
406
|
});
|
|
375
407
|
module.exports._createNoReportAfterTransform = noReportAfterTransform;
|
|
376
408
|
|
|
377
|
-
const reportWithOptions = currify((
|
|
409
|
+
const reportWithOptions = currify((dir, options, t, name, message, ruleOptions) => {
|
|
378
410
|
const full = join(dir, name);
|
|
379
411
|
const [source, isTS] = readFixture(full);
|
|
380
412
|
|
|
381
|
-
const
|
|
382
|
-
const [rule] = keys(plugin);
|
|
383
|
-
|
|
413
|
+
const rule = parseRule(options);
|
|
384
414
|
const rules = {
|
|
385
|
-
[rule]: ['on',
|
|
415
|
+
[rule]: ['on', ruleOptions],
|
|
386
416
|
};
|
|
387
417
|
|
|
388
|
-
return reportCode({
|
|
418
|
+
return reportCode({...options, rules, isTS}, t, source, message);
|
|
389
419
|
});
|
|
390
420
|
|
|
391
|
-
const noReportWithOptions = currify((
|
|
421
|
+
const noReportWithOptions = currify((dir, options, t, name, ruleOptions) => {
|
|
392
422
|
const full = join(dir, name);
|
|
393
423
|
const [source, isTS] = readFixture(full);
|
|
394
424
|
|
|
395
425
|
rmFixture(`${full}-fix`);
|
|
396
426
|
|
|
397
|
-
const
|
|
398
|
-
const [rule] = keys(plugin);
|
|
399
|
-
|
|
427
|
+
const rule = parseRule(options);
|
|
400
428
|
const rules = {
|
|
401
|
-
[rule]: ['on',
|
|
429
|
+
[rule]: ['on', ruleOptions],
|
|
402
430
|
};
|
|
403
431
|
|
|
404
|
-
return noReportCode({
|
|
432
|
+
return noReportCode({isTS, ...options, rules}, t, source);
|
|
405
433
|
});
|
|
406
434
|
|
|
407
|
-
const reportCode = currify((
|
|
408
|
-
const fix = false;
|
|
435
|
+
const reportCode = currify((options, t, source, message) => {
|
|
409
436
|
const {places} = putout(source, {
|
|
410
|
-
fix,
|
|
411
|
-
|
|
412
|
-
rules,
|
|
413
|
-
plugins,
|
|
437
|
+
fix: false,
|
|
438
|
+
...options,
|
|
414
439
|
});
|
|
415
440
|
|
|
416
441
|
const resultMessages = places.map(getMessage);
|
|
@@ -421,34 +446,31 @@ const reportCode = currify(({plugins, rules, isTS}, t, source, message) => {
|
|
|
421
446
|
return t.equal(resultMessages[0], message);
|
|
422
447
|
});
|
|
423
448
|
|
|
424
|
-
const noReportCode = currify((
|
|
425
|
-
const fix = false;
|
|
449
|
+
const noReportCode = currify((options, t, source) => {
|
|
426
450
|
const {places} = putout(source, {
|
|
427
|
-
fix,
|
|
428
|
-
|
|
429
|
-
rules,
|
|
430
|
-
plugins,
|
|
451
|
+
fix: false,
|
|
452
|
+
...options,
|
|
431
453
|
});
|
|
432
454
|
|
|
433
455
|
return t.deepEqual(places, [], 'should not report');
|
|
434
456
|
});
|
|
435
457
|
|
|
436
|
-
const noReportCodeAfterTransform = currify((
|
|
437
|
-
const fix = true;
|
|
458
|
+
const noReportCodeAfterTransform = currify((options, t, source) => {
|
|
438
459
|
const {places} = putout(source, {
|
|
439
|
-
fix,
|
|
440
|
-
|
|
441
|
-
rules,
|
|
442
|
-
plugins,
|
|
460
|
+
fix: true,
|
|
461
|
+
...options,
|
|
443
462
|
});
|
|
444
463
|
|
|
445
464
|
return t.deepEqual(places, [], 'should not report after transform');
|
|
446
465
|
});
|
|
447
466
|
|
|
448
|
-
function
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
467
|
+
function parseOptions(plugin) {
|
|
468
|
+
if (!plugin.plugins)
|
|
469
|
+
return {
|
|
470
|
+
plugins: [plugin],
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
return plugin;
|
|
452
474
|
}
|
|
453
475
|
|
|
454
476
|
function preTest(test, plugin) {
|