From d2a54529b88814f2c7fce8285953c033b34b4016 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 6 Jul 2023 22:13:45 +0300 Subject: [PATCH] feature: @putout/test: drop formatSave, formatManySave --- packages/test/lib/eslint/eslint.spec.mjs | 47 ++++---- packages/test/lib/fixture/var-format.js | 5 - packages/test/lib/test.js | 25 ++-- packages/test/lib/test.spec.mjs | 5 + packages/test/test/fixture/empty-format.js | 0 packages/test/test/fixture/empty.js | 1 + packages/test/test/format.mjs | 132 ++++++++++++--------- packages/test/test/transform.js | 61 ++++------ packages/test/test/update.js | 5 + 9 files changed, 145 insertions(+), 136 deletions(-) create mode 100644 packages/test/test/fixture/empty-format.js create mode 100644 packages/test/test/fixture/empty.js create mode 100644 packages/test/test/update.js diff --git a/packages/test/lib/eslint/eslint.spec.mjs b/packages/test/lib/eslint/eslint.spec.mjs index f8fc4d883d..452c97c7f3 100644 --- a/packages/test/lib/eslint/eslint.spec.mjs +++ b/packages/test/lib/eslint/eslint.spec.mjs @@ -5,11 +5,18 @@ import { import {fileURLToPath} from 'url'; import {stub} from 'supertape'; import {createTest} from './eslint.mjs'; +import {createUpdate} from '../../test/update.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const test = createTest(import.meta.url); +const NOT_CHECK_ASSERTIONS_COUNT = { + checkAssertionsCount: false, +}; + +const update = createUpdate(); + test('test: eslint: export', async ({equal}) => { const eslint = await import('./eslint.mjs'); const testEslint = await import('@putout/test/eslint'); @@ -22,57 +29,49 @@ test('test: eslint: process', async ({process}) => { }); test('test: eslint: process: UPDATE', async ({process}) => { - global.process.env.UPDATE = '1'; + update(1); await process('operator-linebreak'); - delete global.process.env.UPDATE; -}, { - checkAssertionsCount: false, -}); + update(); +}, NOT_CHECK_ASSERTIONS_COUNT); test('test: eslint: process: UPDATE: stub', async ({process, calledWith}) => { - global.process.env.UPDATE = '1'; + update(1); const writeFileSync = stub(); global.writeFileSync = writeFileSync; await process('operator-linebreak'); - delete global.process.env.UPDATE; + update(); delete global.writeFileSync; const name = join(__dirname, 'fixture', 'operator-linebreak-fix.js'); const data = 'const a = 5;\n\n'; calledWith(writeFileSync, [name, data]); -}, { - checkAssertionsCount: false, -}); +}, NOT_CHECK_ASSERTIONS_COUNT); test('test: eslint: noProcess: UPDATE', async ({noProcess}) => { - global.process.env.UPDATE = '1'; + update(1); await noProcess('operator-linebreak-fix'); - delete global.process.env.UPDATE; -}, { - checkAssertionsCount: false, -}); + update(); +}, NOT_CHECK_ASSERTIONS_COUNT); test('test: eslint: noProcess: UPDATE: stub', async ({noProcess, calledWith}) => { - global.process.env.UPDATE = '1'; + update(1); const unlinkSync = stub(); global.unlinkSync = unlinkSync; await noProcess('operator-linebreak-fix'); - delete global.process.env.UPDATE; + update(); delete global.unlinkSync; const name = join(__dirname, 'fixture', 'operator-linebreak-fix-fix.js'); calledWith(unlinkSync, [name]); -}, { - checkAssertionsCount: false, -}); +}, NOT_CHECK_ASSERTIONS_COUNT); test('test: eslint: noProcess', async ({noProcess}) => { await noProcess('operator-linebreak-fix'); @@ -126,9 +125,7 @@ test('test: eslint: report: no plugins', (t) => { t.calledWith(failStub, expected); t.end(); -}, { - checkAssertionsCount: false, -}); +}, NOT_CHECK_ASSERTIONS_COUNT); test('test: eslint: transform: no plugins', (t) => { const failStub = stub().returns({ @@ -143,6 +140,4 @@ test('test: eslint: transform: no plugins', (t) => { t.calledWith(failStub, expected); t.end(); -}, { - checkAssertionsCount: false, -}); +}, NOT_CHECK_ASSERTIONS_COUNT); diff --git a/packages/test/lib/fixture/var-format.js b/packages/test/lib/fixture/var-format.js index d14d67ac96..e69de29bb2 100644 --- a/packages/test/lib/fixture/var-format.js +++ b/packages/test/lib/fixture/var-format.js @@ -1,5 +0,0 @@ -var - 1:8 error Avoid 'console' call remove-console - -✖ 1 errors in 1 files - fixable with the `--fix` option diff --git a/packages/test/lib/test.js b/packages/test/lib/test.js index eda3281509..032d73e3e8 100644 --- a/packages/test/lib/test.js +++ b/packages/test/lib/test.js @@ -90,8 +90,6 @@ const parsePlugin = (plugins) => { }; function createTest(dir, maybeOptions) { - const update = isUpdate(); - dir = join(dir, 'fixture'); const options = parseOptions(maybeOptions); @@ -113,12 +111,9 @@ function createTest(dir, maybeOptions) { noReportAfterTransform: noReportAfterTransform(dir, options), reportWithOptions: reportWithOptions(dir, options), noReportWithOptions: noReportWithOptions(dir, options), - reportCode: reportCode(options), - - formatSave: formatSave(dir, options), - format: update ? formatSave : format(dir, options), - formatManySave: formatManySave(dir, options), - formatMany: update ? formatManySave : formatMany(dir, options), + reportCode: reportCode(options), //formatSave: formatSave(dir, options), + format: formatSave(dir, options), //formatManySave: formatManySave(dir, options), + formatMany: formatManySave(dir, options), noFormat: noFormat(dir, options), }); } @@ -221,10 +216,15 @@ const formatMany = currify((dir, options, t) => async (formatter, names, formatt }); const formatManySave = currify((dir, options, t) => async (formatter, names, options = {}) => { + const runFormat = await formatMany(dir, options, t); + + if (!isUpdate()) + return await runFormat(formatter, names, options); + const {existsSync, writeFileSync} = global.__putout_test_fs; if (!isArray(names)) - throw Error(`☝️ Looks like 'formatManySave()' received 'names' with type: '${typeof names}', expected: 'array'`); + throw Error(`☝️ Looks like 'formatMany()' received 'names' with type: '${typeof names}', expected: 'array'`); const name = `${names.join('-')}-format.js`; const outputName = join(dir, name); @@ -232,7 +232,6 @@ const formatManySave = currify((dir, options, t) => async (formatter, names, opt if (!existsSync(outputName)) writeFileSync(outputName, ''); - const runFormat = await formatMany(dir, options, t); const {result} = await runFormat(formatter, names, options); writeFileSync(outputName, result); @@ -241,6 +240,11 @@ const formatManySave = currify((dir, options, t) => async (formatter, names, opt }); const formatSave = currify((dir, options, t) => async (formatter, name, options = {}) => { + const runFormat = format(dir, options, t); + + if (!isUpdate()) + return await runFormat(formatter, name, options); + const {existsSync, writeFileSync} = global.__putout_test_fs; const full = join(dir, name); @@ -249,7 +253,6 @@ const formatSave = currify((dir, options, t) => async (formatter, name, options if (!existsSync(outputName)) writeFileSync(outputName, ''); - const runFormat = format(dir, options, t); const {result} = await runFormat(formatter, name, options); writeFileSync(outputName, result); diff --git a/packages/test/lib/test.spec.mjs b/packages/test/lib/test.spec.mjs index 4e5061468d..d14be5541a 100644 --- a/packages/test/lib/test.spec.mjs +++ b/packages/test/lib/test.spec.mjs @@ -2,6 +2,9 @@ import removeConsoleFixture from '../test/fixture/remove-console.js'; import _createTest, {createTest} from './test.mjs'; import cjsTest from './test.js'; import formatter from '@putout/formatter-dump'; +import {createUpdate} from '../test/update.js'; + +const update = createUpdate(); const test = createTest(import.meta.url, { 'remove-console': removeConsoleFixture, @@ -12,5 +15,7 @@ test('test: esm: default export same as commonjs', ({equal}) => { }); test('test: esm: format', async ({format}) => { + update(1); await format(formatter, 'var'); + update(); }); diff --git a/packages/test/test/fixture/empty-format.js b/packages/test/test/fixture/empty-format.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/test/test/fixture/empty.js b/packages/test/test/fixture/empty.js new file mode 100644 index 0000000000..af75d47562 --- /dev/null +++ b/packages/test/test/fixture/empty.js @@ -0,0 +1 @@ +var s = fn(); diff --git a/packages/test/test/format.mjs b/packages/test/test/format.mjs index aebe8b22cf..517403fb4c 100644 --- a/packages/test/test/format.mjs +++ b/packages/test/test/format.mjs @@ -6,29 +6,36 @@ import removeConsoleFixture from './fixture/remove-console.js'; import {createTest} from '../lib/test.mjs'; import formatter from '@putout/formatter-dump'; import formatterProgress from '@putout/formatter-progress'; +import {createUpdate} from './update.js'; + +const update = createUpdate(); + +const NO_CHECK_ASSERTIONS_COUNT = { + checkAssertionsCount: false, +}; const test = createTest(import.meta.url, { 'remove-console': removeConsoleFixture, }); -test('test: format', async ({format}) => { - await format(formatter, 'var'); +test('test: format: no update', async ({format}) => { + update(0); + await format(formatter, 'empty'); + update(); }); test('test: format: options', async ({format}) => { + update(1); await format(formatterProgress, 'var', { minCount: 10, }); + update(); }); test('test: no format', async ({noFormat}) => { await noFormat(formatter, 'declared'); }); -test('test: formatMany', async ({formatMany}) => { - await formatMany(formatter, ['var', 'var']); -}); - const {existsSync, writeFileSync} = fs; const existsSyncStub = stub().returns(false); @@ -46,88 +53,105 @@ global.__putout_test_fs = { writeFileSync: writeFileSyncStub, }; -testUpdate('test: formatSave', async ({ok, formatSave}) => { - await formatSave(formatter, 'var'); +testUpdate('test: formatSave', async ({ok, format}) => { + update(1); + await format(formatter, 'var'); + update(); ok(writeFileSyncStub.called); -}, { - checkAssertionsCount: false, -}); +}, NO_CHECK_ASSERTIONS_COUNT); -testUpdate('test: formatSave: success', async ({equal, formatSave}) => { - const {message} = await formatSave(formatter, 'var'); +testUpdate('test: formatSave: success', async ({equal, format}) => { + update(1); + + const {message} = await format(formatter, 'var'); + + update(); equal(message, 'fixed fixture updated'); -}, { - checkAssertionsCount: false, -}); +}, NO_CHECK_ASSERTIONS_COUNT); -testUpdate('test: formatManySave: success', async ({equal, formatManySave}) => { - const {message} = await formatManySave(formatter, ['var']); +testUpdate('test: formatManySave: success', async ({equal, formatMany}) => { + update(1); + + const {message} = await formatMany(formatter, ['var']); + + update(); equal(message, 'fixed fixture updated'); -}, { - checkAssertionsCount: false, -}); +}, NO_CHECK_ASSERTIONS_COUNT); -testUpdate('test: formatManySave: not array', async ({equal, formatManySave}) => { - const [error] = await tryToCatch(formatManySave, formatter, 'var'); - equal(error.message, `☝️ Looks like 'formatManySave()' received 'names' with type: 'string', expected: 'array'`); -}, { - checkAssertionsCount: false, -}); +testUpdate('test: formatManySave: not array', async ({equal, formatMany}) => { + update(1); + + const [error] = await tryToCatch(formatMany, formatter, 'var'); + + update(); + equal(error.message, `☝️ Looks like 'formatMany()' received 'names' with type: 'string', expected: 'array'`); +}, NO_CHECK_ASSERTIONS_COUNT); testUpdate('test: formatMany: not array', async ({equal, formatMany}) => { const [error] = await tryToCatch(formatMany, formatter, 'var'); equal(error.message, `☝️ Looks like 'formatMany()' received 'names' with type: 'string', expected: 'array'`); -}, { - checkAssertionsCount: false, -}); +}, NO_CHECK_ASSERTIONS_COUNT); testUpdate('test: format: with UPDATE env variable', async ({ok, format}) => { + update(1); + await format(formatter, 'var'); + update(); + + ok(writeFileSyncStub.called); +}, NO_CHECK_ASSERTIONS_COUNT); + +testUpdate('test: format: with UPDATE=0 env variable', async ({ok, format}) => { + update(1); + await format(formatter, 'var'); + update(); + global.__putout_test_update = true; + ok(writeFileSyncStub.called); +}, NO_CHECK_ASSERTIONS_COUNT); + +testUpdate('test: format: with UPDATE=1 env variable', async ({ok, format}) => { + update(1); await format(formatter, 'var'); + update(); - global.__putout_test_update = false; ok(writeFileSyncStub.called); -}, { - checkAssertionsCount: false, -}); +}, NO_CHECK_ASSERTIONS_COUNT); -testUpdate('test: formatManySave', async ({ok, formatManySave}) => { - await formatManySave(formatter, ['var', 'var']); +testUpdate('test: formatMany: update', async ({ok, formatMany}) => { + update(1); + await formatMany(formatter, ['var', 'var']); + update(); ok(writeFileSyncStub.called); -}, { - checkAssertionsCount: false, -}); +}, NO_CHECK_ASSERTIONS_COUNT); testUpdate('test: formatMany: with UPDATE env variable', async ({ok, formatMany}) => { - global.__putout_test_update = true; + update(1); await formatMany(formatter, ['var', 'var']); + update(); - global.__putout_test_update = false; ok(writeFileSyncStub.called); -}, { - checkAssertionsCount: false, -}); +}, NO_CHECK_ASSERTIONS_COUNT); -testUpdate('test: formatSave: exists', async ({ok, formatSave}) => { +testUpdate('test: formatSave: exists', async ({ok, format}) => { existsSyncStub.returns(true); - await formatSave(formatter, 'var'); + update(1); + await format(formatter, 'var'); + update(0); ok(writeFileSyncStub.called); -}, { - checkAssertionsCount: false, -}); +}, NO_CHECK_ASSERTIONS_COUNT); -testUpdate('test: formatManySave: exists', async ({ok, formatManySave}) => { +testUpdate('test: formatManySave: exists', async ({ok, formatMany}) => { existsSyncStub.returns(true); - await formatManySave(formatter, ['var', 'var']); + update(1); + await formatMany(formatter, ['var', 'var']); + update(); ok(writeFileSyncStub.called); -}, { - checkAssertionsCount: false, -}); +}, NO_CHECK_ASSERTIONS_COUNT); fs.existsSync = existsSync; fs.writeFileSync = writeFileSync; diff --git a/packages/test/test/transform.js b/packages/test/test/transform.js index 64127e6fd6..ae2823e178 100644 --- a/packages/test/test/transform.js +++ b/packages/test/test/transform.js @@ -3,10 +3,12 @@ const {stub} = require('supertape'); const fs = require('fs'); const {reRequire} = require('mock-require'); + +const {createUpdate} = require('./update'); const {writeFileSync} = fs; fs.writeFileSync = stub(); -process.env.UPDATE = 1; +const update = createUpdate(); const NO_CHECK_ASSERTIONS_COUNT = { checkAssertionsCount: false, @@ -32,9 +34,7 @@ test('transform: with PUTOUT_PRINTER: env variable', (t) => { }); test('transform: with UPDATE env variable', (t) => { - const {UPDATE} = process.env; - - process.env.UPDATE = 1; + update(1); const {writeFileSync} = global.__putout_test_fs; const writeFileSyncStub = stub(); @@ -43,7 +43,7 @@ test('transform: with UPDATE env variable', (t) => { t.transform('typescript'); - process.env.UPDATE = UPDATE; + update(); global.__putout_test_fs.writeFileSync = writeFileSync; t.ok(writeFileSyncStub.called, 'should write fixture'); @@ -51,9 +51,7 @@ test('transform: with UPDATE env variable', (t) => { }, NO_CHECK_ASSERTIONS_COUNT); test('transform: noTransformWithOptions: with UPDATE env variable', (t) => { - const {UPDATE} = process.env; - - process.env.UPDATE = 1; + update(1); const {writeFileSync} = global.__putout_test_fs; const writeFileSyncStub = stub(); @@ -62,7 +60,7 @@ test('transform: noTransformWithOptions: with UPDATE env variable', (t) => { t.noTransformWithOptions('no-transform-with-options', {}); - process.env.UPDATE = UPDATE; + update(); global.__putout_test_fs.writeFileSync = writeFileSync; t.ok(writeFileSyncStub.called, 'should write fixture'); @@ -70,9 +68,7 @@ test('transform: noTransformWithOptions: with UPDATE env variable', (t) => { }, NO_CHECK_ASSERTIONS_COUNT); test('transform: with UPDATE env variable: pass', (t) => { - const {UPDATE} = process.env; - - process.env.UPDATE = 1; + update(1); const {writeFileSync} = global.__putout_test_fs; const writeFileSyncStub = stub(); @@ -81,7 +77,7 @@ test('transform: with UPDATE env variable: pass', (t) => { const result = t.transform('typescript'); - process.env.UPDATE = UPDATE; + update(); global.__putout_test_fs.writeFileSync = writeFileSync; t.equal(result.message, 'fixed fixture updated'); @@ -89,9 +85,7 @@ test('transform: with UPDATE env variable: pass', (t) => { }, NO_CHECK_ASSERTIONS_COUNT); test('test: no transform: with UPDATE env variable: pass', (t) => { - const {UPDATE} = process.env; - - process.env.UPDATE = 1; + update(1); const {writeFileSync} = global.__putout_test_fs; const writeFileSyncStub = stub(); @@ -100,7 +94,7 @@ test('test: no transform: with UPDATE env variable: pass', (t) => { const result = t.noTransform('no-transform'); - process.env.UPDATE = UPDATE; + update(); global.__putout_test_fs.writeFileSync = writeFileSync; t.equal(result.message, 'source fixture updated'); @@ -108,9 +102,7 @@ test('test: no transform: with UPDATE env variable: pass', (t) => { }, NO_CHECK_ASSERTIONS_COUNT); test('transform: with UPDATE env variable: js', (t) => { - const {UPDATE} = process.env; - - process.env.UPDATE = 1; + update(1); const {writeFileSync} = global.__putout_test_fs; const writeFileSyncStub = stub(); @@ -119,7 +111,7 @@ test('transform: with UPDATE env variable: js', (t) => { t.transform('update'); - process.env.UPDATE = UPDATE; + update(); global.__putout_test_fs.writeFileSync = writeFileSync; t.ok(writeFileSyncStub.called, 'should write fixture'); @@ -127,9 +119,7 @@ test('transform: with UPDATE env variable: js', (t) => { }, NO_CHECK_ASSERTIONS_COUNT); test('transform: with UPDATE env variable: with arg', (t) => { - const {UPDATE} = process.env; - - process.env.UPDATE = 1; + update(1); const {writeFileSync} = global.__putout_test_fs; const writeFileSyncStub = stub(); @@ -138,7 +128,7 @@ test('transform: with UPDATE env variable: with arg', (t) => { t.transform('typescript', '\n'); - process.env.UPDATE = UPDATE; + update(); global.__putout_test_fs.writeFileSync = writeFileSync; t.notCalled(writeFileSyncStub); @@ -146,9 +136,7 @@ test('transform: with UPDATE env variable: with arg', (t) => { }, NO_CHECK_ASSERTIONS_COUNT); test('noTransform: with UPDATE env variable', (t) => { - const {UPDATE} = process.env; - - process.env.UPDATE = 1; + update(1); const {writeFileSync, unlinkSync} = global.__putout_test_fs; @@ -160,7 +148,7 @@ test('noTransform: with UPDATE env variable', (t) => { t.noTransform('const'); - process.env.UPDATE = UPDATE; + update(); global.__putout_test_fs.writeFileSync = writeFileSync; global.__putout_test_fs.unlinkSync = unlinkSync; @@ -169,9 +157,7 @@ test('noTransform: with UPDATE env variable', (t) => { }, NO_CHECK_ASSERTIONS_COUNT); test('transformWithOptions: with UPDATE env variable', (t) => { - const {UPDATE} = process.env; - - process.env.UPDATE = 1; + update(1); const {writeFileSync, unlinkSync} = global.__putout_test_fs; @@ -183,7 +169,7 @@ test('transformWithOptions: with UPDATE env variable', (t) => { t.transformWithOptions('const', {}); - process.env.UPDATE = UPDATE; + update(); global.__putout_test_fs.writeFileSync = writeFileSync; global.__putout_test_fs.unlinkSync = unlinkSync; @@ -192,10 +178,7 @@ test('transformWithOptions: with UPDATE env variable', (t) => { }, NO_CHECK_ASSERTIONS_COUNT); test('transformWithOptions: with UPDATE env variable: pass', (t) => { - const {UPDATE} = process.env; - - process.env.UPDATE = 1; - + update(1); const {writeFileSync, unlinkSync} = global.__putout_test_fs; const unlinkSyncStub = stub(); @@ -206,7 +189,7 @@ test('transformWithOptions: with UPDATE env variable: pass', (t) => { const result = t.transformWithOptions('const', {}); - process.env.UPDATE = UPDATE; + update(); global.__putout_test_fs.writeFileSync = writeFileSync; global.__putout_test_fs.unlinkSync = unlinkSync; @@ -215,5 +198,3 @@ test('transformWithOptions: with UPDATE env variable: pass', (t) => { }, NO_CHECK_ASSERTIONS_COUNT); fs.writeFileSync = writeFileSync; - -delete process.env.UPDATE; diff --git a/packages/test/test/update.js b/packages/test/test/update.js new file mode 100644 index 0000000000..09b03758a1 --- /dev/null +++ b/packages/test/test/update.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports.createUpdate = (UPDATE = process.env.UPDATE) => (...args) => { + process.env.UPDATE = args.length ? args[0] : UPDATE; +};