import process from 'node:process'; import {join} from 'node:path'; import {once} from 'node:events'; import { readFileSync, writeFileSync, existsSync, unlinkSync, } from 'node:fs'; import test from 'supertape'; import putout from 'putout'; import currify from 'currify'; import {createProgress} from '@putout/engine-runner/progress'; import {createError} from './create-error.js'; import {preTest} from './pre-test.js'; import { format, noFormat, formatMany, } from './format/index.js'; import {maybeDirectory} from './maybe-directory.js'; import { readFixture, writeFixture, writeFixFixture, rmFixture, } from './fixture.js'; const {isArray} = Array; const isString = (a) => typeof a === 'string'; const isObject = (a) => typeof a === 'object'; const {keys} = Object; globalThis.__putout_test_fs = { readFileSync, writeFileSync, existsSync, unlinkSync, }; const isUpdate = () => Boolean(Number(process.env.UPDATE)); const fail = (t, message) => { const { __putout_test_fail = t.fail, } = globalThis; return __putout_test_fail(message); }; export default createTest; const parsePlugin = (plugins) => { if (isArray(plugins)) return plugins[0]; return plugins; }; export function createTest(url, maybeOptions, maybeExtends = {}) { const dir = join(maybeDirectory(url), 'fixture'); const { extension = '', extensionFix, lint = putout, ...options } = parseOptions(maybeOptions); const plugin = parsePlugin(options.plugins); const linterOptions = { lint, extension, extensionFix, }; preTest(test, plugin, options.runners); return test.extend({ transform: transform(dir, linterOptions, options), noTransform: noTransform(dir, linterOptions, options), transformCode: transformCode(linterOptions, options), noTransformCode: noTransformCode(linterOptions, options), progress: progress(dir, options), progressWithOptions: progressWithOptions(dir, options), transformWithOptions: transformWithOptions(dir, linterOptions, options), noTransformWithOptions: noTransformWithOptions(dir, linterOptions, options), report: report(dir, linterOptions, options), noReport: noReport(dir, linterOptions, options), noReportAfterTransform: noReportAfterTransform(dir, linterOptions, options), noReportAfterTransformWithOptions: noReportAfterTransformWithOptions(dir, linterOptions, options), reportWithOptions: reportWithOptions(dir, linterOptions, options), noReportWithOptions: noReportWithOptions(dir, linterOptions, options), reportCode: reportCode(lint, options), noReportCode: noReportCode(lint, options), format: format(dir, options), formatMany: formatMany(dir, options), noFormat: noFormat(dir, options), ...maybeExtends, }); } const toObject = (array) => { const result = {}; const first = parsePlugin(array); if (isObject(first) && !isArray(first)) return first; for (const [name, value] of array) { result[name] = value; } return result; }; const progress = (dir, options) => (t) => async (name, expected) => { checkProgress(name, expected); const full = join(dir, name); const [input, isTS] = readFixture(full); const progress = createProgress(); const [[result]] = await Promise.all([ once(progress, 'file'), putout(input, { progress, isTS, ...options, }), ]); return t.deepEqual(result, expected); }; const progressWithOptions = (dir, options) => (t) => async (name, pluginOptions, expected) => { const full = join(dir, name); const [input, isTS] = readFixture(full); const rule = parseRule(options); const rules = { [rule]: ['on', pluginOptions], }; const progress = createProgress(); const [[result]] = await Promise.all([ once(progress, 'file'), putout(input, { rules, progress, isTS, ...options, }), ]); return t.deepEqual(result, expected); }; const transform = currify((dir, linterOptions, options, t, name, transformed = null, addons = {}) => { const { lint, extension, extensionFix, } = linterOptions; const {plugins} = options; const full = join(dir, name); const isStr = isString(transformed); const [input, isTS, currentExtension] = readFixture(full, extension, extensionFix); if (!isStr) addons = transformed; addons = addons || {}; const {code} = lint(input, { isTS, ...options, plugins: [{ ...toObject(plugins), ...addons, }], }); if (input === code) return fail(t, `'input' === 'output', use 'noTransform()'`); if (isUpdate() && !isStr) { writeFixFixture({ full, code, extension: currentExtension, }); return t.pass('fixed fixture updated'); } const [output] = isStr ? [transformed] : readFixture(`${full}-fix`, currentExtension); return t.equal(code, output); }); const transformWithOptions = currify((dir, linterOptions, options, t, name, pluginOptions) => { const {lint, extension} = linterOptions; const full = join(dir, name); const [input, isTS, currentExtension] = readFixture(full, extension); const rule = parseRule(options); const rules = { [rule]: ['on', pluginOptions], }; const {code} = lint(input, { isTS, rules, ...options, }); if (isUpdate()) { writeFixFixture({ full, code, extension: currentExtension, }); return t.pass('fixed fixture updated'); } const [output] = readFixture(`${full}-fix`, extension); return t.equal(code, output); }); const parseRule = ({plugins}) => { const [plugin] = plugins; return plugin[0] || keys(plugin)[0]; }; const noTransformWithOptions = currify((dir, linterOptions, options, t, name, ruleOptions) => { const {lint, extension} = linterOptions; const full = join(dir, name); const [input, isTS, currentExtension] = readFixture(full, extension); rmFixture(`${full}-fix`); const rule = parseRule(options); const rules = { [rule]: ['on', ruleOptions], }; const {code} = lint(input, { isTS, rules, ...options, }); if (isUpdate()) { writeFixture({ full, code, extension: currentExtension, }); return t.pass('source fixture updated'); } return t.equal(code, input); }); const noTransform = currify((dir, linterOptions, options, t, name, addons = {}) => { const { lint, extension, extensionFix, } = linterOptions; const full = join(dir, name); const [input, isTS, currentExtension] = readFixture(full, extension, extensionFix); rmFixture(`${full}-fix`, currentExtension); const {plugins} = options; const {code} = lint(input, { isTS, ...options, plugins: [{ ...toObject(plugins), ...addons, }], }); if (isUpdate()) { writeFixture({ full, code, extension: currentExtension, }); return t.pass('source fixture updated'); } return t.equal(code, input); }); const transformCode = currify((linterOptions, options, t, input, output, isTS = false) => { const {lint} = linterOptions; const {code} = lint(input, { isTS, ...options, }); return t.equal(code, output); }); const noTransformCode = currify((linterOptions, options, t, input) => { const {lint} = linterOptions; const {code} = lint(input, options); return t.equal(code, input); }); const getMessage = ({message}) => message; const report = (dir, linterOptions, options) => (t) => (name, message, plugins) => { const {lint, extension} = linterOptions; checkReport(name, message); const full = join(dir, name); const [source, isTS] = readFixture(full, extension); const addT = reportCode(lint, { isTS, ...options, }); const run = addT(t); return run(source, message, plugins); }; const noReport = (dir, linterOptions, options) => (t) => (name, addons = []) => { const {plugins} = options; const {lint, extension} = linterOptions; const full = join(dir, name); const [source, isTS] = readFixture(full, extension); rmFixture(`${full}-fix`); return noReportCode(lint, { isTS, ...options, plugins: [{ ...toObject(plugins), ...toObject(addons), }], }, t, source); }; export const _createNoReport = noReport; const noReportAfterTransform = currify((dir, linterOptions, options, t, name, addons = {}) => { const {lint, extension} = linterOptions; const full = join(dir, name); const [source, isTS] = readFixture(full, extension); return noReportCodeAfterTransform(lint, { isTS, ...options, }, t, source, addons); }); export const _createNoReportAfterTransform = noReportAfterTransform; const noReportAfterTransformWithOptions = currify((dir, linterOptions, options, t, name, ruleOptions) => { const {lint, extension} = linterOptions; const full = join(dir, name); const [source, isTS] = readFixture(full, extension); const rule = parseRule(options); const rules = { [rule]: ['on', ruleOptions], }; return noReportCodeAfterTransform(lint, { isTS, ...options, rules: { ...options.rules, ...rules, }, }, t, source); }); export const _createNoReportAfterTransformWithOptions = noReportAfterTransformWithOptions; const reportWithOptions = currify((dir, linterOptions, options, t, name, message, ruleOptions) => { const {lint, extension} = linterOptions; const full = join(dir, name); const [source, isTS] = readFixture(full, extension); const rule = parseRule(options); const rules = { [rule]: ['on', ruleOptions], }; const addT = reportCode(lint, { ...options, rules, isTS, }); const run = addT(t); return run(source, message); }); const noReportWithOptions = currify((dir, linterOptions, options, t, name, ruleOptions) => { const {lint, extension} = linterOptions; const full = join(dir, name); const [source, isTS] = readFixture(full, extension); rmFixture(`${full}-fix`); const rule = parseRule(options); const rules = { [rule]: ['on', ruleOptions], }; return noReportCode(lint, { isTS, ...options, rules, }, t, source); }); const reportCode = (lint, options) => (t) => (source, message, addons) => { const {plugins} = options; const {places} = lint(source, { fix: false, ...options, plugins: [{ ...toObject(plugins), ...addons, }], }); const resultMessages = places.map(getMessage); if (isArray(message)) return t.deepEqual(resultMessages, message); return t.equal(resultMessages[0], message); }; const noReportCode = currify((lint, options, t, source) => { const {places} = lint(source, { fix: false, ...options, }); return t.deepEqual(places, [], 'should not report'); }); const noReportCodeAfterTransform = currify((lint, options, t, source, addons = {}) => { const {plugins} = options; const {places} = lint(source, { fix: true, ...options, plugins: [{ ...toObject(plugins), ...addons, }], }); return t.deepEqual(places, [], 'should not report after transform'); }); function parseOptions(plugin) { if (!plugin.plugins) return { plugins: [plugin], }; return plugin; } function checkReport(name, message) { if (!isString(message) && !message) { const help = `☝️ Looks like you forget to pass the 'message' for 'report()' operator`; const source = `report(name: string, message: string): Operator`; const values = { name, message, }; throw Error(createError(help, source, values)); } } function checkProgress(name, expected) { if (!isString(name)) { const message = `☝️ Looks like you forget to pass the 'name' of a fixture for 'progress()' operator.`; const signature = 'await progress(name: string, expected: ExpectedProgress): Operator'; const values = { name, expected, }; throw Error(createError(message, signature, values)); } }