Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Unify logic for running parser tests from external suites (#10444)
  • Loading branch information
nicolo-ribaudo committed Dec 3, 2019
1 parent 5440ae1 commit 7195f0d
Show file tree
Hide file tree
Showing 11 changed files with 581 additions and 756 deletions.
8 changes: 4 additions & 4 deletions Makefile
Expand Up @@ -159,13 +159,13 @@ bootstrap-flow:
cd build/flow && git checkout $(FLOW_COMMIT)

test-flow:
node scripts/tests/flow/run_babel_parser_flow_tests.js
node scripts/tests/flow

test-flow-ci: build-bundle-ci bootstrap-flow
$(MAKE) test-flow

test-flow-update-whitelist:
node scripts/tests/flow/run_babel_parser_flow_tests.js --update-whitelist
node scripts/tests/flow --update-whitelist

bootstrap-test262:
rm -rf build/test262
Expand All @@ -174,13 +174,13 @@ bootstrap-test262:
cd build/test262 && git checkout $(TEST262_COMMIT)

test-test262:
node scripts/tests/test262/run_babel_parser_test262.js
node scripts/tests/test262

test-test262-ci: build-bundle-ci bootstrap-test262
$(MAKE) test-test262

test-test262-update-whitelist:
node scripts/tests/test262/run_babel_parser_test262.js --update-whitelist
node scripts/tests/test262 --update-whitelist

# Does not work on Windows
clone-license:
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -60,6 +60,7 @@
"lerna-changelog": "^0.5.0",
"lint-staged": "^9.2.0",
"lodash": "^4.17.13",
"mergeiterator": "^1.2.5",
"output-file-sync": "^2.0.0",
"prettier": "^1.19.1",
"pump": "^3.0.0",
Expand Down
132 changes: 132 additions & 0 deletions scripts/tests/flow/index.js
@@ -0,0 +1,132 @@
const fs = require("fs").promises;
const path = require("path");
const merge = require("mergeiterator");
const TestRunner = require("../utils/parser-test-runner");

const flowOptionsMapping = {
esproposal_class_instance_fields: "classProperties",
esproposal_class_static_fields: "classProperties",
esproposal_export_star_as: "exportNamespaceFrom",
esproposal_decorators: "decorators-legacy",
esproposal_nullish_coalescing: "nullishCoalescingOperator",
esproposal_optional_chaining: "optionalChaining",
types: "flowComments",
intern_comments: false,
};

function getPlugins(test) {
const plugins = [
"dynamicImport",
["flow", { all: true }],
"flowComments",
"jsx",
"classProperties",
"classPrivateProperties",
"classPrivateMethods",
"bigInt",
"numericSeparator",
];

if (!test.options) return plugins;

for (const [option, enabled] of Object.entries(test.options)) {
if (!enabled) {
const idx = plugins.indexOf(flowOptionsMapping[option]);
if (idx !== -1) plugins.splice(idx, 1);
} else if (!(option in flowOptionsMapping)) {
throw new Error("Parser options not mapped " + option);
} else if (flowOptionsMapping[option]) {
plugins.push(flowOptionsMapping[option]);
}
}

return plugins;
}

async function* readdirRecursive(root, dir = ".") {
const names = await fs.readdir(path.join(root, dir));

const dirs = [];

for (const name of names) {
const file = path.join(dir, name);
const stats = await fs.stat(path.join(root, file));
if (!stats.isDirectory()) {
if (!file) continue;
yield file;
} else {
dirs.push(readdirRecursive(root, file));
}
}

yield* merge(dirs);
}

async function* loadTests(root) {
for await (const file of readdirRecursive(root)) {
if (file.slice(-3) === ".js") {
const noExt = path.join(root, file).slice(0, -3);

const [contents, tree, options] = await Promise.all([
fs.readFile(noExt + ".js", "utf8"),
fs.readFile(noExt + ".tree.json", "utf8").catch(() => null),
fs.readFile(noExt + ".options.json", "utf8").catch(() => null),
]);

yield {
file,
contents,
tree: JSON.parse(tree),
options: JSON.parse(options),
};
}
}
}

const runner = new TestRunner({
testDir: path.join(__dirname, "../../../build/flow/src/parser/test/flow"),
whitelist: path.join(__dirname, "whitelist.txt"),
shouldUpdate: process.argv.includes("--update-whitelist"),

async *getTests() {
for await (const test of loadTests(this.testDir)) {
const shouldSuccess =
test.tree && (!test.tree.errors || !test.tree.errors.length);

yield {
contents: test.contents,
fileName: test.file,
id: test.file,
expectedError: !shouldSuccess,
plugins: getPlugins(test),
};
}
},

parse(test, parser) {
try {
parser(test.contents, {
sourceType: "module",
plugins: test.plugins,
});
} catch (e) {
// lets retry in script mode
if (!test.expectedError) {
try {
parser(test.contents, {
sourceType: "script",
plugins: test.plugins,
});
return;
} catch {}
}

throw e;
}
},
});

runner.run().catch(err => {
console.error(err);
process.exitCode = 1;
});

0 comments on commit 7195f0d

Please sign in to comment.