Files
test-repo/scripts/sync-flow-tests.cjs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

132 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-05-21 21:11:09 +02:00
"use strict";
2017-04-08 16:52:38 +02:00
const fs = require("fs");
2020-06-26 20:30:21 +08:00
const path = require("path");
2017-04-08 16:52:38 +02:00
const flowParser = require("flow-parser");
2022-02-05 16:24:10 +03:00
const fastGlob = require("fast-glob");
2017-04-08 16:52:38 +02:00
const DEFAULT_SPEC_CONTENT = "runFormatTest(import.meta);\n";
2017-04-08 16:52:38 +02:00
const SPEC_FILE_NAME = "format.test.js";
const FLOW_TESTS_DIR = path.join(__dirname, "../tests/format/flow/flow-repo");
2017-04-08 16:52:38 +02:00
function tryParse(file, content) {
// Keep this sync with `/src/language-js/parse/flow.js`
2017-04-08 16:52:38 +02:00
const ast = flowParser.parse(content, {
comments: false,
enums: true,
2026-01-15 10:24:52 +08:00
match: true,
components: true,
// assert_operator: true,
esproposal_decorators: true,
2026-01-15 10:24:52 +08:00
pattern_matching: true,
records: true,
2017-04-08 16:52:38 +02:00
});
if (ast.errors.length > 0) {
2020-01-14 23:30:18 +08:00
const { line, column } = ast.errors[0].loc.start;
const { message } = ast.errors[0];
2017-04-08 16:52:38 +02:00
return `${file}:${line}:${column}: ${message}`;
}
return null;
}
function syncTests(syncDir) {
2022-02-05 16:24:10 +03:00
const specFiles = fastGlob.sync(
path.join(FLOW_TESTS_DIR, "**", SPEC_FILE_NAME),
);
2022-02-05 16:24:10 +03:00
const filesToCopy = fastGlob.sync(path.join(syncDir, "**/*.js"));
2017-04-08 16:52:38 +02:00
if (filesToCopy.length === 0) {
throw new Error(
[
"Couldn't find any files to copy.",
2017-12-05 21:14:03 +11:00
`Please make sure that \`${syncDir}\` exists and contains the flow tests.`,
2017-04-08 16:52:38 +02:00
].join("\n"),
2017-05-21 17:13:11 +02:00
);
2017-04-08 16:52:38 +02:00
}
const specContents = specFiles.reduce((obj, specFile) => {
obj[specFile] = fs.readFileSync(specFile, "utf8");
return obj;
}, {});
const skipped = [];
2022-10-23 22:23:30 +09:00
fs.rmSync(FLOW_TESTS_DIR);
2017-04-08 16:52:38 +02:00
2021-01-24 19:26:42 +08:00
for (const file of filesToCopy) {
2017-04-08 16:52:38 +02:00
const content = fs.readFileSync(file, "utf8");
const parseError = tryParse(file, content);
if (parseError) {
skipped.push(parseError);
2021-01-24 19:26:42 +08:00
continue;
2017-04-08 16:52:38 +02:00
}
const newFile = path.join(FLOW_TESTS_DIR, path.relative(syncDir, file));
const dirname = path.dirname(newFile);
const specFile = path.join(dirname, SPEC_FILE_NAME);
const specContent = specContents[specFile] || DEFAULT_SPEC_CONTENT;
fs.mkdirSync(dirname, { recursive: true });
2017-04-08 16:52:38 +02:00
fs.writeFileSync(newFile, content);
fs.writeFileSync(specFile, specContent);
2021-01-24 19:26:42 +08:00
}
2017-04-08 16:52:38 +02:00
return skipped;
}
function run(argv) {
if (argv.length !== 1) {
console.error(
[
"You must provide the path to a flow tests directory to sync from!",
2022-02-12 15:39:16 +08:00
"Example: node scripts/sync-flow-tests.cjs ../flow/tests/",
2017-04-08 16:52:38 +02:00
].join("\n"),
);
return 1;
}
const syncDir = argv[0];
let skipped = [];
try {
skipped = syncTests(syncDir);
} catch (error) {
console.error(`Failed to sync.\n${error}`);
return 1;
}
if (skipped.length > 0) {
console.log(
[
"Some files were skipped due to syntax errors.",
"This is expected since flow tests for handling invalid code,",
"but that's not interesting for Prettier's tests.",
"This is the skipped stuff:",
"",
2021-01-26 09:46:14 +08:00
...skipped,
"",
].join("\n"),
2017-04-08 16:52:38 +02:00
);
}
console.log(
[
"Done syncing! Now you need to:",
"",
`1. Optional: Adjust some ${SPEC_FILE_NAME} files.`,
"2. Run `jest -u` to create snapshots.",
"3. Run `git diff` to check how tests and snapshots have changed",
"4. Take a look at new snapshots to see if they're OK.",
].join("\n"),
);
return 0;
}
if (require.main === module) {
const exitCode = run(process.argv.slice(2));
process.exit(exitCode);
}