pdf.js/external/builder/test-fixtures_esprima.mjs

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

76 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

import { fileURLToPath } from "url";
import fs from "fs";
import path from "path";
import { preprocessPDFJSCode } from "./babel-plugin-pdfjs-preprocessor.mjs";
2016-05-11 08:05:29 +09:00
const __dirname = path.dirname(fileURLToPath(import.meta.url));
2016-05-11 08:05:29 +09:00
let errors = 0;
2016-05-11 08:05:29 +09:00
const baseDir = path.join(__dirname, "fixtures_esprima");
const files = fs
2017-05-04 02:16:37 +09:00
.readdirSync(baseDir)
.filter(function (name) {
2017-05-04 02:16:37 +09:00
return /-expected\./.test(name);
})
.map(function (name) {
2017-05-04 02:16:37 +09:00
return path.join(baseDir, name);
});
files.forEach(function (expectationFilename) {
const inFilename = expectationFilename.replace("-expected", "");
const expectation = fs
2017-05-04 02:16:37 +09:00
.readFileSync(expectationFilename)
.toString()
.trim()
.replaceAll("__filename", fs.realpathSync(inFilename));
const input = fs.readFileSync(inFilename).toString();
2016-05-11 08:05:29 +09:00
const defines = {
2016-05-11 08:05:29 +09:00
TRUE: true,
FALSE: false,
OBJ: { obj: { i: 1 }, j: 2 },
TEXT: "text",
2016-05-11 08:05:29 +09:00
};
const map = {
2017-05-31 10:38:22 +09:00
"import-alias": "import-name",
};
const ctx = {
defines,
map,
2016-05-11 08:05:29 +09:00
rootPath: __dirname + "/../..",
};
let out;
2016-05-11 08:05:29 +09:00
try {
out = preprocessPDFJSCode(ctx, input);
2016-05-11 08:05:29 +09:00
} catch (e) {
out = ("Error: " + e.message).replaceAll(/^/gm, "//");
2016-05-11 08:05:29 +09:00
}
if (out !== expectation) {
errors++;
// Allow regenerating the expected output using
// OVERWRITE=true node ./external/builder/test-fixtures_esprima.mjs
if (process.env.OVERWRITE) {
fs.writeFileSync(expectationFilename, out + "\n");
}
2017-05-04 02:16:37 +09:00
console.log("Assertion failed for " + inFilename);
console.log("--------------------------------------------------");
console.log("EXPECTED:");
console.log(expectation);
console.log("--------------------------------------------------");
console.log("ACTUAL");
console.log(out);
console.log("--------------------------------------------------");
console.log();
2016-05-11 08:05:29 +09:00
}
});
if (errors) {
2017-05-04 02:16:37 +09:00
console.error("Found " + errors + " expectation failures.");
process.exit(1);
2016-05-11 08:05:29 +09:00
} else {
2017-05-04 02:16:37 +09:00
console.log("All tests completed without errors.");
process.exit(0);
2016-05-11 08:05:29 +09:00
}