prepack/scripts/test-internal.js

123 lines
3.6 KiB
JavaScript
Raw Normal View History

2015-10-15 02:59:41 +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 */
import { CompilerDiagnostic, type ErrorHandlerResult, FatalError } from "../lib/errors.js";
Early termination for fatal errors Summary: If an error is reported to the host application and it returns a value that indicates that the error is fatal, the evaluator now throws a FatalError exception rather than causing an IntrospectionError. This terminates things quickly and cleanly but uncovered a bug in the way effect tracking is cleaned up in the face of exceptions that are not throw completions. To fix this, the code that pops an evaluation context off the stack now checks if there are any dangling effects in the context and, if there are, it folds them into the effects of the outer context. The effects then propagate to the most closely nested evaluateForEffects call, where they are rolled back from the global state and incorporated into the effects returned from the call. When they propagate all the way to a test runner, as happens when there is an IntrospectionError in global code, the state is never rolled back and thus the error can be logged in the state that applied when it was created. DEPRECATED API The InitializationError constructor in prepack-standalone.js is going to go away in a future release. Please use FatalError instead. For now, InitializationError.prototype is on the prototype chain of FatalError.prototype, so instances of FatalError will still be instances of InitializationError, so this should not be a breaking change in the next release. Closes https://github.com/facebook/prepack/pull/740 Differential Revision: D5286107 Pulled By: hermanventer fbshipit-source-id: 05c7f9197acaa0ba922d136f122968b2f41a4b82
2017-06-21 06:18:43 +03:00
import type { BabelNodeSourceLocation } from "babel-types";
import { prepackSources } from "../lib/prepack-standalone.js";
2015-10-15 02:59:41 +03:00
let chalk = require("chalk");
let path = require("path");
let fs = require("fs");
2015-10-15 02:59:41 +03:00
function search(dir, relative) {
let tests = [];
2017-05-10 20:51:06 +03:00
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"),
name: path.join(relative, name),
2017-05-10 20:51:06 +03:00
});
} else if (stat.isDirectory()) {
tests = tests.concat(search(loc, path.join(relative, name)));
}
2015-10-15 02:59:41 +03:00
}
}
return tests;
}
let tests = search(`${__dirname}/../facebook/test`, "facebook/test");
2015-10-15 02:59:41 +03:00
let errors: Map<BabelNodeSourceLocation, CompilerDiagnostic>;
let errorList: Array<CompilerDiagnostic>;
function errorHandler(diagnostic: CompilerDiagnostic): ErrorHandlerResult {
if (diagnostic.location) errors.set(diagnostic.location, diagnostic);
else errorList.push(diagnostic);
return "Recover";
Early termination for fatal errors Summary: If an error is reported to the host application and it returns a value that indicates that the error is fatal, the evaluator now throws a FatalError exception rather than causing an IntrospectionError. This terminates things quickly and cleanly but uncovered a bug in the way effect tracking is cleaned up in the face of exceptions that are not throw completions. To fix this, the code that pops an evaluation context off the stack now checks if there are any dangling effects in the context and, if there are, it folds them into the effects of the outer context. The effects then propagate to the most closely nested evaluateForEffects call, where they are rolled back from the global state and incorporated into the effects returned from the call. When they propagate all the way to a test runner, as happens when there is an IntrospectionError in global code, the state is never rolled back and thus the error can be logged in the state that applied when it was created. DEPRECATED API The InitializationError constructor in prepack-standalone.js is going to go away in a future release. Please use FatalError instead. For now, InitializationError.prototype is on the prototype chain of FatalError.prototype, so instances of FatalError will still be instances of InitializationError, so this should not be a breaking change in the next release. Closes https://github.com/facebook/prepack/pull/740 Differential Revision: D5286107 Pulled By: hermanventer fbshipit-source-id: 05c7f9197acaa0ba922d136f122968b2f41a4b82
2017-06-21 06:18:43 +03:00
}
2015-10-15 02:59:41 +03:00
function runTest(name: string, code: string): boolean {
console.log(chalk.inverse(name));
try {
errors = new Map();
errorList = [];
let modelName = name + ".model";
let sourceMapName = name + ".map";
let sourceCode = fs.readFileSync(name, "utf8");
let modelCode = fs.existsSync(modelName) ? fs.readFileSync(modelName, "utf8") : undefined;
let sourceMap = fs.existsSync(sourceMapName) ? fs.readFileSync(sourceMapName, "utf8") : undefined;
let sources = [];
if (modelCode) {
sources.push({ filePath: modelName, fileContents: modelCode });
}
sources.push({ filePath: name, fileContents: sourceCode, sourceMapContents: sourceMap });
let options = {
internalDebug: true,
compatibility: "jsc-600-1-4-17",
delayUnsupportedRequires: true,
accelerateUnsupportedRequires: true,
mathRandomSeed: "0",
errorHandler,
serialize: true,
initializeMoreModules: !modelCode,
sourceMaps: !!sourceMap,
};
let serialized = prepackSources(sources, options);
let new_map = serialized.map; // force source maps to get computed
if (!new_map) console.error(chalk.red("No source map"));
if (!serialized) {
console.error(chalk.red("Error during serialization"));
return false;
} else {
return true;
}
} catch (e) {
if (!(e instanceof FatalError)) console.error(e);
2015-10-15 02:59:41 +03:00
return false;
Early termination for fatal errors Summary: If an error is reported to the host application and it returns a value that indicates that the error is fatal, the evaluator now throws a FatalError exception rather than causing an IntrospectionError. This terminates things quickly and cleanly but uncovered a bug in the way effect tracking is cleaned up in the face of exceptions that are not throw completions. To fix this, the code that pops an evaluation context off the stack now checks if there are any dangling effects in the context and, if there are, it folds them into the effects of the outer context. The effects then propagate to the most closely nested evaluateForEffects call, where they are rolled back from the global state and incorporated into the effects returned from the call. When they propagate all the way to a test runner, as happens when there is an IntrospectionError in global code, the state is never rolled back and thus the error can be logged in the state that applied when it was created. DEPRECATED API The InitializationError constructor in prepack-standalone.js is going to go away in a future release. Please use FatalError instead. For now, InitializationError.prototype is on the prototype chain of FatalError.prototype, so instances of FatalError will still be instances of InitializationError, so this should not be a breaking change in the next release. Closes https://github.com/facebook/prepack/pull/740 Differential Revision: D5286107 Pulled By: hermanventer fbshipit-source-id: 05c7f9197acaa0ba922d136f122968b2f41a4b82
2017-06-21 06:18:43 +03:00
} finally {
for (let [loc, error] of errors) {
console.error(
`${error.severity}: ${loc.source || ""} ${loc.start.line}:${loc.start.column + 1} ${error.errorCode} ${
error.message
}`
);
Early termination for fatal errors Summary: If an error is reported to the host application and it returns a value that indicates that the error is fatal, the evaluator now throws a FatalError exception rather than causing an IntrospectionError. This terminates things quickly and cleanly but uncovered a bug in the way effect tracking is cleaned up in the face of exceptions that are not throw completions. To fix this, the code that pops an evaluation context off the stack now checks if there are any dangling effects in the context and, if there are, it folds them into the effects of the outer context. The effects then propagate to the most closely nested evaluateForEffects call, where they are rolled back from the global state and incorporated into the effects returned from the call. When they propagate all the way to a test runner, as happens when there is an IntrospectionError in global code, the state is never rolled back and thus the error can be logged in the state that applied when it was created. DEPRECATED API The InitializationError constructor in prepack-standalone.js is going to go away in a future release. Please use FatalError instead. For now, InitializationError.prototype is on the prototype chain of FatalError.prototype, so instances of FatalError will still be instances of InitializationError, so this should not be a breaking change in the next release. Closes https://github.com/facebook/prepack/pull/740 Differential Revision: D5286107 Pulled By: hermanventer fbshipit-source-id: 05c7f9197acaa0ba922d136f122968b2f41a4b82
2017-06-21 06:18:43 +03:00
}
for (let error of errorList) {
console.error(`${error.severity}: ${error.errorCode} ${error.message}`);
}
2015-10-15 02:59:41 +03:00
}
}
function run() {
let failed = 0;
let passed = 0;
let total = 0;
2015-10-15 02:59:41 +03:00
for (let test of tests) {
if (!test.name.endsWith(".js")) continue;
2015-10-15 02:59:41 +03:00
total++;
if (runTest(test.name, test.file)) passed++;
else failed++;
2015-10-15 02:59:41 +03:00
}
console.log("Passed:", `${passed}/${total}`, (Math.floor(passed / total * 100) || 0) + "%");
2015-10-15 02:59:41 +03:00
return failed === 0;
}
if (!run()) process.exit(1);