@putout/test 4.1.0 → 4.5.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 CHANGED
@@ -21,8 +21,8 @@ UPDATE=1 tape test/*.js
21
21
 
22
22
  ## Plugins API
23
23
 
24
- All plugins 🐊`Putout` plugins written in `CommonJS`, since `ESLint` written on `CommonJS` and we have a huge `ESLint`-based ecosystem wich is good to reuse.
25
- `🐊`Putout`can be used in all IDE's supported by`ESLint` as [`eslint-plugin-putout`](https://github.com/coderaiser/putout/tree/packages/eslint-plugin-putout).
24
+ All plugins 🐊`Putout` plugins written in `CommonJS`, since `ESLint` written on `CommonJS` and we have a huge `ESLint`-based ecosystem which is good to reuse.
25
+ 🐊`Putout`can be used in all IDE's supported by`ESLint` as [`eslint-plugin-putout`](https://github.com/coderaiser/putout/tree/packages/eslint-plugin-putout).
26
26
  When [async rules will be supported](https://github.com/eslint/eslint/issues/15394) we can switch to `ESM`.
27
27
 
28
28
  To write test for your plugins you need initialize `test` using `createTest`:
@@ -231,8 +231,11 @@ test('formatter: dump: many', async ({formatMany}) => {
231
231
  Here is example of tests for [remove-console](https://github.com/coderaiser/putout/tree/master/packages/plugin-remove-console):
232
232
 
233
233
  ```js
234
- const test = require('@putout/test')(__dirname, {
235
- 'remove-console': require('..'),
234
+ const {createTest} = require('@putout/test');
235
+ const removeConsole = require('@putout/plugin-remove-console');
236
+
237
+ const test = createTest(__dirname, {
238
+ 'remove-console': removeConsole,
236
239
  });
237
240
 
238
241
  test('remove-console: report', (t) => {
@@ -252,6 +255,70 @@ test('test: declared', (t) => {
252
255
  });
253
256
  ```
254
257
 
258
+ ## ESLint API
259
+
260
+ First you need to create test with:
261
+
262
+ ```js
263
+ import {createTest} from '@putout/test/eslint';
264
+ const test = createTest(import.meta.url);
265
+ ```
266
+
267
+ ### `process(filename [, config])`
268
+
269
+ Works in similar to [transform](#transformfilename--output-plugins) way:
270
+
271
+ - ✅ reads `operator-linebreak.js`;
272
+ - ✅ transforms it;
273
+ - ✅ checks that transformed is equal to `operator-linebreak-fix.js`;
274
+
275
+ Example:
276
+
277
+ ```js
278
+ test('test: eslint: transform', async ({process}) => {
279
+ await process('operator-linebreak');
280
+ });
281
+
282
+ test('test: eslint: transform', async ({process}) => {
283
+ await process('operator-linebreak', {
284
+ rules: {
285
+ 'putout/putout': {
286
+ rules: {
287
+ 'convert-esm-to-commonjs': 'on',
288
+ },
289
+ },
290
+ },
291
+ });
292
+ });
293
+ ```
294
+
295
+ ### `noProcess(filename)`
296
+
297
+ Check that filename would not be processed.
298
+
299
+ Example:
300
+
301
+ ```js
302
+ test('test: eslint: noProcess', async ({noProcess}) => {
303
+ await noProcess('operator-linebreak-fix');
304
+ });
305
+ ```
306
+
307
+ ### `comparePlaces(filename, places)`
308
+
309
+ ```js
310
+ test('eslint-config: operator-line-break', async ({comparePlaces}) => {
311
+ await comparePlaces('operator-linebreak', [{
312
+ "message": "There should be no line break before or after '='.",
313
+ "position": {
314
+ "column": 1,
315
+ "line": 2,
316
+ },
317
+ "rule": "operator-linebreak (eslint)",
318
+ }]);
319
+ });
320
+ ```
321
+
255
322
  ## Processors API
256
323
 
257
324
  With `processors api` you can test `processors` in a simplest possible way.
@@ -0,0 +1,76 @@
1
+ import {readFile} from 'fs/promises';
2
+ import {join} from 'path';
3
+
4
+ import eslint from 'putout/eslint';
5
+ import tryToCatch from 'try-to-catch';
6
+ import {extend} from 'supertape';
7
+
8
+ const config = {
9
+ extends: [
10
+ 'plugin:node/recommended',
11
+ 'plugin:eslint-plugin/recommended',
12
+ 'plugin:putout/recommended',
13
+ ],
14
+ };
15
+
16
+ const read = async (name) => {
17
+ const [, data] = await tryToCatch(readFile, `${name}.js`, 'utf8');
18
+
19
+ if (data)
20
+ return [`${name}.js`, data];
21
+
22
+ return [`${name}.ts`, await readFile(`${name}.ts`, 'utf8')];
23
+ };
24
+
25
+ export const createTest = (url) => {
26
+ const fixtureDir = new URL('fixture', url).pathname;
27
+
28
+ return extend({
29
+ process: (operator) => async (name, override) => {
30
+ const full = join(fixtureDir, name);
31
+ const [resolvedName, code] = await read(full);
32
+ const [, fixture] = await read(`${full}-fix`);
33
+ const fix = true;
34
+
35
+ const [source] = await eslint({
36
+ name: resolvedName,
37
+ code,
38
+ fix,
39
+ putout: true,
40
+ config: {
41
+ ...config,
42
+ ...override,
43
+ },
44
+ });
45
+
46
+ return operator.equal(source, fixture);
47
+ },
48
+ noProcess: (operator) => async (name) => {
49
+ const full = join(fixtureDir, name);
50
+ const [resolvedName, code] = await read(full);
51
+ const fix = true;
52
+
53
+ const [source] = await eslint({
54
+ name: resolvedName,
55
+ config,
56
+ code,
57
+ fix,
58
+ });
59
+
60
+ return operator.equal(source, code);
61
+ },
62
+ comparePlaces: (operator) => async (name, expected) => {
63
+ const full = join(fixtureDir, name);
64
+ const [resolvedName, code] = await read(full);
65
+
66
+ const [, places] = await eslint({
67
+ config,
68
+ name: resolvedName,
69
+ code,
70
+ });
71
+
72
+ return operator.deepEqual(places, expected);
73
+ },
74
+ });
75
+ };
76
+
package/lib/test.js CHANGED
@@ -18,15 +18,13 @@ const isString = (a) => typeof a === 'string';
18
18
  const {isArray} = Array;
19
19
  const {keys, entries} = Object;
20
20
 
21
- const {UPDATE} = process.env;
22
- global.__putout_test_update = UPDATE;
23
21
  global.__putout_test_fs = {
24
22
  readFileSync,
25
23
  writeFileSync,
26
24
  existsSync,
27
25
  };
28
26
 
29
- const isUpdate = () => UPDATE || global.__putout_test_update;
27
+ const isUpdate = () => Boolean(Number(process.env.UPDATE));
30
28
 
31
29
  const TS = {
32
30
  ENABLED: true,
@@ -214,6 +212,8 @@ const formatSave = currify(({dir, plugins, rules}, t) => async (formatter, name,
214
212
  });
215
213
 
216
214
  const transform = currify(({dir, plugins, rules}, t, name, transformed = null, addons = {}) => {
215
+ const {writeFileSync} = global.__putout_test_fs;
216
+
217
217
  const full = join(dir, name);
218
218
  const [input, isTS] = readFixture(full);
219
219
  const isStr = isString(transformed);
@@ -234,13 +234,15 @@ const transform = currify(({dir, plugins, rules}, t, name, transformed = null, a
234
234
  }],
235
235
  });
236
236
 
237
- if (isUpdate())
237
+ if (isUpdate() && !isStr) {
238
238
  writeFileSync(`${full}-fix.js`, code);
239
+ }
239
240
 
240
241
  return t.equal(code, output);
241
242
  });
242
243
 
243
244
  const transformWithOptions = currify(({dir, plugins}, t, name, options) => {
245
+ const {writeFileSync} = global.__putout_test_fs;
244
246
  const full = join(dir, name);
245
247
  const [input, isTS] = readFixture(full);
246
248
 
@@ -407,11 +409,15 @@ function preTest(test, plugin) {
407
409
  match,
408
410
  }] = entries(plugin).pop();
409
411
 
412
+ const options = {
413
+ checkDuplicates: false,
414
+ };
415
+
410
416
  if (rules) {
411
417
  test(`${name}: rules is an object`, (t) => {
412
418
  t.equal(typeof rules, 'object', 'should export "rules" object');
413
419
  t.end();
414
- }, {checkDuplicates: false});
420
+ }, options);
415
421
 
416
422
  const entries = Object.entries(rules);
417
423
  for (const [entryName, plugin] of entries) {
@@ -423,10 +429,6 @@ function preTest(test, plugin) {
423
429
  return;
424
430
  }
425
431
 
426
- const options = {
427
- checkDuplicates: false,
428
- };
429
-
430
432
  test(`${name}: report: is function`, (t) => {
431
433
  t.equal(typeof report, 'function', 'should export "report" function');
432
434
  t.end();
package/lib/test.mjs CHANGED
@@ -1,9 +1,12 @@
1
- import {createCommons} from 'simport';
1
+ import {dirname} from 'path';
2
+ import {fileURLToPath} from 'url';
2
3
  import create from './test.js';
3
4
 
4
5
  export default create;
5
6
 
6
7
  export const createTest = (url, plugins) => {
7
- const {__dirname} = createCommons(url);
8
+ const __filename = fileURLToPath(url);
9
+ const __dirname = dirname(__filename);
10
+
8
11
  return create(__dirname, plugins);
9
12
  };
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@putout/test",
3
- "version": "4.1.0",
3
+ "version": "4.5.0",
4
+ "type": "commonjs",
4
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
5
6
  "description": "test runner for putout plugins ",
6
7
  "homepage": "https://github.com/coderaiser/putout/tree/master/packages/test#readme",
@@ -14,7 +15,8 @@
14
15
  "import": "./lib/test.mjs"
15
16
  },
16
17
  "./processor": "./lib/processor/index.js",
17
- "./formatter": "./lib/formatter/index.mjs"
18
+ "./formatter": "./lib/formatter/index.mjs",
19
+ "./eslint": "./lib/eslint/eslint.mjs"
18
20
  },
19
21
  "release": false,
20
22
  "tag": false,
@@ -37,9 +39,9 @@
37
39
  "@putout/engine-processor": "*",
38
40
  "currify": "^4.0.0",
39
41
  "putout": "*",
40
- "simport": "^1.2.0",
41
- "supertape": "^6.0.0",
42
- "try-catch": "^3.0.0"
42
+ "supertape": "^7.0.0",
43
+ "try-catch": "^3.0.0",
44
+ "try-to-catch": "^3.0.0"
43
45
  },
44
46
  "keywords": [
45
47
  "putout",
@@ -59,7 +61,7 @@
59
61
  "c8": "^7.5.0",
60
62
  "eslint": "^8.0.1",
61
63
  "eslint-plugin-node": "^11.0.0",
62
- "eslint-plugin-putout": "^12.0.0",
64
+ "eslint-plugin-putout": "^13.0.0",
63
65
  "lerna": "^4.0.0",
64
66
  "madrun": "^8.0.1",
65
67
  "mock-require": "^3.0.3",