2017-06-07 01:53:39 +03:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2017-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* @flow */
|
|
|
|
|
2017-08-24 04:04:48 +03:00
|
|
|
import { type CompilerDiagnostic, type ErrorHandlerResult, FatalError } from "../lib/errors.js";
|
2017-07-03 22:02:00 +03:00
|
|
|
import { prepackFileSync } from "../lib/prepack-node.js";
|
2017-06-07 01:53:39 +03:00
|
|
|
import invariant from "../lib/invariant.js";
|
|
|
|
|
|
|
|
let chalk = require("chalk");
|
2017-07-04 02:19:43 +03:00
|
|
|
let path = require("path");
|
|
|
|
let fs = require("fs");
|
2017-06-07 01:53:39 +03:00
|
|
|
|
|
|
|
function search(dir, relative) {
|
|
|
|
let tests = [];
|
|
|
|
|
|
|
|
if (fs.existsSync(dir)) {
|
|
|
|
for (let name of fs.readdirSync(dir)) {
|
|
|
|
let loc = path.join(dir, name);
|
|
|
|
let stat = fs.statSync(loc);
|
|
|
|
|
|
|
|
if (stat.isFile()) {
|
|
|
|
tests.push({
|
|
|
|
file: fs.readFileSync(loc, "utf8"),
|
2017-07-04 02:19:43 +03:00
|
|
|
name: path.join(relative, name),
|
2017-06-07 01:53:39 +03:00
|
|
|
});
|
|
|
|
} else if (stat.isDirectory()) {
|
|
|
|
tests = tests.concat(search(loc, path.join(relative, name)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tests;
|
|
|
|
}
|
|
|
|
|
|
|
|
let tests = search(`${__dirname}/../test/error-handler`, "test/error-handler");
|
|
|
|
|
2017-07-04 02:19:43 +03:00
|
|
|
function errorHandler(
|
|
|
|
retval: ErrorHandlerResult,
|
2017-07-14 02:42:31 +03:00
|
|
|
errors: Array<CompilerDiagnostic>,
|
|
|
|
error: CompilerDiagnostic
|
2017-07-04 02:19:43 +03:00
|
|
|
): ErrorHandlerResult {
|
2017-06-07 01:53:39 +03:00
|
|
|
errors.push(error);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
function runTest(name: string, code: string): boolean {
|
|
|
|
console.log(chalk.inverse(name));
|
|
|
|
|
|
|
|
let recover = code.includes("// recover-from-errors");
|
2018-02-01 01:22:24 +03:00
|
|
|
let compatibility = code.includes("// jsc") ? "jsc-600-1-4-17" : undefined;
|
2017-06-07 01:53:39 +03:00
|
|
|
|
|
|
|
let expectedErrors = code.match(/\/\/\s*expected errors:\s*(.*)/);
|
|
|
|
invariant(expectedErrors);
|
|
|
|
invariant(expectedErrors.length > 1);
|
|
|
|
expectedErrors = expectedErrors[1];
|
|
|
|
expectedErrors = eval(expectedErrors); // eslint-disable-line no-eval
|
|
|
|
invariant(expectedErrors.constructor === Array);
|
|
|
|
|
|
|
|
let errors = [];
|
|
|
|
try {
|
2017-07-31 23:19:08 +03:00
|
|
|
let options = {
|
2017-06-12 20:12:10 +03:00
|
|
|
internalDebug: false,
|
2017-06-07 01:53:39 +03:00
|
|
|
mathRandomSeed: "0",
|
2017-08-12 00:54:04 +03:00
|
|
|
errorHandler: errorHandler.bind(null, recover ? "Recover" : "Fail", errors),
|
2017-06-07 01:53:39 +03:00
|
|
|
serialize: true,
|
2018-08-04 03:11:00 +03:00
|
|
|
instantRender: false,
|
2018-02-01 01:22:24 +03:00
|
|
|
compatibility,
|
2017-07-31 23:19:08 +03:00
|
|
|
};
|
2018-08-04 03:11:00 +03:00
|
|
|
|
|
|
|
if (code.includes("// instant render")) options.instantRender = true;
|
2017-08-15 08:28:11 +03:00
|
|
|
prepackFileSync([name], options);
|
2017-06-14 00:09:18 +03:00
|
|
|
if (!recover) {
|
2017-12-13 03:17:55 +03:00
|
|
|
console.error(chalk.red("Serialization succeeded though it should have failed"));
|
2017-06-14 00:09:18 +03:00
|
|
|
return false;
|
|
|
|
}
|
2017-06-07 01:53:39 +03:00
|
|
|
} catch (e) {
|
2017-08-24 04:04:48 +03:00
|
|
|
if (!(e instanceof FatalError)) {
|
2017-12-13 03:17:55 +03:00
|
|
|
console.error(chalk.red(`Unexpected error: ${e.message}`));
|
2018-03-02 01:17:55 +03:00
|
|
|
return false;
|
2017-08-24 04:04:48 +03:00
|
|
|
}
|
2017-06-07 01:53:39 +03:00
|
|
|
}
|
|
|
|
if (errors.length !== expectedErrors.length) {
|
2017-12-13 03:17:55 +03:00
|
|
|
console.error(chalk.red(`Expected ${expectedErrors.length} errors, but found ${errors.length}`));
|
2017-06-07 01:53:39 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < expectedErrors.length; ++i) {
|
|
|
|
for (let prop in expectedErrors[i]) {
|
2017-06-14 00:09:18 +03:00
|
|
|
let expected = expectedErrors[i][prop];
|
|
|
|
let actual = (errors[i]: any)[prop];
|
|
|
|
if (prop === "location") {
|
2017-07-31 23:19:08 +03:00
|
|
|
if (actual) delete actual.filename;
|
2017-06-14 00:09:18 +03:00
|
|
|
actual = JSON.stringify(actual);
|
|
|
|
expected = JSON.stringify(expected);
|
|
|
|
}
|
|
|
|
if (expected !== actual) {
|
2017-12-13 03:17:55 +03:00
|
|
|
console.error(chalk.red(`Error ${i + 1}: Expected ${expected} errors, but found ${actual}`));
|
2017-06-07 01:53:39 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function run() {
|
|
|
|
let failed = 0;
|
|
|
|
let passed = 0;
|
2017-07-04 02:19:43 +03:00
|
|
|
let total = 0;
|
2017-06-07 01:53:39 +03:00
|
|
|
|
|
|
|
for (let test of tests) {
|
|
|
|
// filter hidden files
|
|
|
|
if (path.basename(test.name)[0] === ".") continue;
|
|
|
|
if (test.name.endsWith("~")) continue;
|
|
|
|
|
|
|
|
total++;
|
2017-07-04 02:19:43 +03:00
|
|
|
if (runTest(test.name, test.file)) passed++;
|
|
|
|
else failed++;
|
2017-06-07 01:53:39 +03:00
|
|
|
}
|
|
|
|
|
2018-06-06 22:18:39 +03:00
|
|
|
console.log("Passed:", `${passed}/${total}`, (Math.floor((passed / total) * 100) || 0) + "%");
|
2017-06-07 01:53:39 +03:00
|
|
|
return failed === 0;
|
|
|
|
}
|
|
|
|
|
2017-07-04 02:19:43 +03:00
|
|
|
if (!run()) process.exit(1);
|