Merge pull request #452 from nguerrera/mocha-improvements

Small test infrastructure improvements
This commit is contained in:
Nick Guerrera 2021-04-15 13:35:38 -07:00 коммит произвёл GitHub
Родитель ca772c1b3e 73f89b51a0
Коммит 7d13fa1a53
6 изменённых файлов: 41 добавлений и 16 удалений

Просмотреть файл

@ -36,6 +36,6 @@
"devDependencies": {
"@types/node": "~14.0.27",
"prettier": "~2.2.1",
"typescript": "~4.1.5"
"typescript": "~4.2.4"
}
}

Просмотреть файл

@ -1,24 +1,19 @@
import fs from "fs";
import url, { fileURLToPath, pathToFileURL } from "url";
import { fileURLToPath, pathToFileURL, URL } from "url";
import { SymbolTable } from "./binder.js";
import { createDiagnostic, Diagnostic, DiagnosticError } from "./diagnostics.js";
import { CompilerHost, Sym } from "./types";
import { stat, readFile, mkdtemp, readdir, rmdir, realpath, writeFile } from "fs/promises";
import { stat, readFile, readdir, realpath, writeFile } from "fs/promises";
import { join, resolve } from "path";
export const adlVersion = getVersion();
function getVersion(): string {
const packageJsonPath = resolvePath(import.meta.url, "../../package.json");
const packageJsonPath = fileURLToPath(new URL("../../package.json", import.meta.url));
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
return packageJson.version;
}
export function resolvePath(basePath: string, ...parts: string[]): string {
const resolvedPath = new url.URL(parts.join(""), basePath);
return url.fileURLToPath(resolvedPath);
}
export function reportDuplicateSymbols(symbols: SymbolTable) {
let reported = new Set<Sym>();
let diagnostics = new Array<Diagnostic>();

Просмотреть файл

@ -36,7 +36,7 @@
"compile": "tsc -p .",
"watch": "tsc -p . --watch",
"dogfood": "node scripts/dogfood.js",
"test": "mocha 'dist/test/**/*.js'",
"test": "mocha --require source-map-support/register 'dist/test/**/*.js'",
"regen-samples": "node scripts/regen-samples.js"
},
"dependencies": {
@ -54,8 +54,9 @@
"@types/resolve": "~1.20.0",
"@types/yargs": "~15.0.12",
"grammarkdown": "~3.1.2",
"mocha": "~7.1.2",
"mocha": "~8.3.2",
"prettier": "~2.2.1",
"source-map-support": "~0.5.19",
"typescript": "~4.2.4"
}
}

Просмотреть файл

@ -1,3 +1,5 @@
import resolve from "resolve";
import { fileURLToPath, URL } from "url";
import { createProgram } from "../../compiler/program.js";
import { NodeHost } from "../../compiler/util.js";
@ -8,8 +10,11 @@ describe("libraries", () => {
describe(lib, () => {
it("compiles without error", async () => {
try {
const mainFile = fileURLToPath(
new URL(`../../../test/libraries/${lib}/main.adl`, import.meta.url)
);
await createProgram(NodeHost, {
mainFile: "test/libraries/" + lib + "/main.adl",
mainFile,
noEmit: true,
});
} catch (e) {

Просмотреть файл

@ -0,0 +1,20 @@
import { WriteLine } from "../compiler/diagnostics.js";
/**
* Verbose output is enabled by default for runs in mocha explorer in VS Code,
* where the output is nicely associated with the individual test, and disabled
* by default for command line runs where we don't want to spam the console.
*
* If the steps taken to produce the message are expensive, pass a callback
* instead of producing the message then passing it here only to be dropped
* when verbose output is disabled.
*/
export function logVerboseTestOutput(messageOrCallback: string | ((log: WriteLine) => void)) {
if (process.env.ADL_VERBOSE_TEST_OUTPUT) {
if (typeof messageOrCallback === "string") {
console.log(messageOrCallback);
} else {
messageOrCallback(console.log);
}
}
}

Просмотреть файл

@ -1,6 +1,7 @@
import * as assert from "assert";
import { parse } from "../compiler/parser.js";
import { SyntaxKind } from "../compiler/types.js";
import { logVerboseTestOutput } from "./test-helpers.js";
describe("syntax", () => {
describe("import statements", () => {
@ -223,10 +224,13 @@ function parseErrorEach(cases: string[]) {
}
function dumpAST(astNode: any) {
const replacer = function (this: any, key: string, value: any) {
return key == "kind" ? SyntaxKind[value] : value;
};
//console.log(JSON.stringify(astNode, replacer, 4));
logVerboseTestOutput((log) => {
const replacer = function (this: any, key: string, value: any) {
return key == "kind" ? SyntaxKind[value] : value;
};
const json = JSON.stringify(astNode, replacer, 4);
log(json);
});
}
function shorten(code: string) {