@putout/test 9.0.1 → 10.0.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
@@ -100,6 +100,8 @@ test('remove usless variables: for-of', (t) => {
100
100
  });
101
101
  ```
102
102
 
103
+ *☝️When input and output the same test fails. Use [`noTransform()`](#notransformfilename) for such cases.*
104
+
103
105
  ### `transformCode(input, output)`
104
106
 
105
107
  Check transform of `input` -> `output` code:
@@ -372,6 +374,8 @@ test('test: eslint: transform', async ({process}) => {
372
374
  });
373
375
  ```
374
376
 
377
+ *☝️When input and output the same test fails. Use [`noProcess()`](#noprocessfilename--overrides) for such cases.*
378
+
375
379
  ### `noProcess(filename [, overrides])`
376
380
 
377
381
  Check that filename would not be processed.
@@ -1,12 +1,17 @@
1
1
  'use strict';
2
2
 
3
- const {readFile, writeFile} = require('fs/promises');
3
+ const tryToCatch = require('try-to-catch');
4
+ const {
5
+ readFile,
6
+ writeFile,
7
+ unlink,
8
+ } = require('node:fs/promises');
4
9
 
5
10
  const {
6
11
  join,
7
12
  extname,
8
13
  basename,
9
- } = require('path');
14
+ } = require('node:path');
10
15
 
11
16
  const test = require('supertape');
12
17
  const processFile = require('putout/process-file');
@@ -20,6 +25,16 @@ const update = async (a, b) => {
20
25
  await write(a, b);
21
26
  };
22
27
 
28
+ const remove = async (a) => {
29
+ const remove = global.unlink || unlink;
30
+ await tryToCatch(remove, a);
31
+ };
32
+
33
+ const read = async (a, b) => {
34
+ const read = global.readFile || readFile;
35
+ return await read(a, b);
36
+ };
37
+
23
38
  const buildOptions = ({options, plugins, processors}) => ({
24
39
  ...options,
25
40
  plugins: plugins || options.plugins,
@@ -28,6 +43,11 @@ const buildOptions = ({options, plugins, processors}) => ({
28
43
 
29
44
  const addDot = (a) => a ? `.${a}` : '';
30
45
 
46
+ const fail = (t, message) => {
47
+ const {__putout_test_fail = t.fail} = global;
48
+ return __putout_test_fail(message);
49
+ };
50
+
31
51
  module.exports._addDot = addDot;
32
52
 
33
53
  module.exports.createTest = (dir, options) => {
@@ -40,10 +60,11 @@ module.exports.createTest = (dir, options) => {
40
60
 
41
61
  const createProcess = (dir, options) => (operator) => async (filename, plugins, processors) => {
42
62
  if (!isStr(filename))
43
- return operator.fail(`Expected filename to be string!`);
63
+ return fail(operator, `Expected filename to be string!`);
44
64
 
45
65
  const {
46
66
  processedSource,
67
+ rawSource,
47
68
  output,
48
69
  } = await process(
49
70
  filename,
@@ -58,6 +79,9 @@ const createProcess = (dir, options) => (operator) => async (filename, plugins,
58
79
  if (isUpdate())
59
80
  return operator.pass('fixtures updated');
60
81
 
82
+ if (rawSource === processedSource)
83
+ return fail(operator, `'input' === 'output', use 'noProcess()'`);
84
+
61
85
  return operator.equal(processedSource, output, 'fixtures should equal');
62
86
  };
63
87
 
@@ -98,16 +122,24 @@ const createComparePlaces = (dir, options) => (operator) => async (filename, exp
98
122
 
99
123
  module.exports._createComparePlaces = createComparePlaces;
100
124
 
101
- async function process(filename, dir, {printer, processors, plugins, extension, fix = true, noChange = false, processorRunners}) {
125
+ async function process(filename, dir, config) {
126
+ let {extension} = config;
127
+ const {
128
+ printer,
129
+ processors,
130
+ plugins,
131
+ fix = true,
132
+ noChange = false,
133
+ processorRunners,
134
+ } = config;
135
+
102
136
  extension = addDot(extname(filename).slice(1) || extension);
103
137
  filename = basename(filename, String(extension));
104
138
 
105
139
  const inputName = join(dir, 'fixture', `${filename}${extension}`);
106
140
  const outputName = join(dir, 'fixture', `${filename}-fix${extension}`);
107
141
 
108
- const rawSource = await readFile(inputName, 'utf8');
109
- const output = !fix || noChange ? '' : await readFile(outputName, 'utf8');
110
-
142
+ const rawSource = await read(inputName, 'utf8');
111
143
  const options = {
112
144
  dir,
113
145
  processors,
@@ -129,8 +161,13 @@ async function process(filename, dir, {printer, processors, plugins, extension,
129
161
  processorRunners,
130
162
  });
131
163
 
132
- if (isUpdate() && !noChange && fix)
133
- await update(outputName, processedSource);
164
+ if (isUpdate())
165
+ if (!noChange && fix)
166
+ await update(outputName, processedSource);
167
+ else if (noChange)
168
+ await remove(outputName);
169
+
170
+ const output = !fix || noChange ? '' : await read(outputName, 'utf8');
134
171
 
135
172
  return {
136
173
  processedSource,
package/lib/test.js CHANGED
@@ -36,6 +36,11 @@ global.__putout_test_fs = {
36
36
  const isUpdate = () => Boolean(Number(process.env.UPDATE));
37
37
  const getPrinter = () => process.env.PUTOUT_PRINTER;
38
38
 
39
+ const fail = (t, message) => {
40
+ const {__putout_test_fail = t.fail} = global;
41
+ return __putout_test_fail(message);
42
+ };
43
+
39
44
  const TS = {
40
45
  ENABLED: true,
41
46
  DISABLED: false,
@@ -344,6 +349,9 @@ const transform = currify((dir, options, t, name, transformed = null, addons = {
344
349
  }],
345
350
  });
346
351
 
352
+ if (input === code)
353
+ return fail(t, `'input' === 'output', use 'noTransform()'`);
354
+
347
355
  if (isUpdate() && !isStr) {
348
356
  writeFixture({
349
357
  full,
@@ -553,7 +561,6 @@ const noReportWithOptions = currify((dir, options, t, name, ruleOptions) => {
553
561
  rmFixture(`${full}-fix`);
554
562
 
555
563
  const rule = parseRule(options);
556
-
557
564
  const rules = {
558
565
  [rule]: ['on', ruleOptions],
559
566
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/test",
3
- "version": "9.0.1",
3
+ "version": "10.0.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Test runner for 🐊Putout plugins ",
@@ -66,8 +66,8 @@
66
66
  "@putout/plugin-remove-empty": "*",
67
67
  "@putout/plugin-remove-unused-variables": "*",
68
68
  "c8": "^9.0.0",
69
- "eslint": "^9.0.0-alpha.0",
70
- "eslint-plugin-n": "^17.0.0-0",
69
+ "eslint": "^9.0.0",
70
+ "eslint-plugin-n": "^17.0.0",
71
71
  "eslint-plugin-putout": "^22.0.0",
72
72
  "lerna": "^6.0.1",
73
73
  "madrun": "^10.0.0",