/
NodeTestAPI.n4js
135 lines (117 loc) · 5.81 KB
/
NodeTestAPI.n4js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright (c) 2018 NumberFour AG.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* NumberFour AG - Initial API and implementation
*/
import debug from "debug";
import {NodeTestCLI} from "org/eclipse/n4js/mangelhaft/runner/node/NodeTestCLI";
import NodeTestOptions from "org/eclipse/n4js/mangelhaft/runner/node/NodeTestOptions";
import {TestInfos} from "org/eclipse/n4js/mangelhaft/types/TestInfos";
import {createTempDir} from "org/eclipse/n4js/mangelhaft/util/fs";
import {fsSafeAccessSync} from "org/eclipse/n4js/mangelhaft/util/fs";
import {readJsonFile} from "org/eclipse/n4js/mangelhaft/util/fs";
import {PACKAGE_JSON} from "org/eclipse/n4js/mangelhaft/util/npm";
import {PACKAGE_JSON_N4JS_SECTION_KEY} from "org/eclipse/n4js/mangelhaft/util/npm";
import {isHTTP_re} from "org/eclipse/n4js/mangelhaft/util/npm";
import * as child_process from "child_process";
import * as lib_fs from "fs";
import * as n4js_cli from "n4js-cli";
import * as lib_path from "path";
const log = debug("n4js-mangelhaft:api");
const rlog = debug("n4js-mangelhaft:test-run");
export default public class NodeTestAPI {
/**
* Executes a test run (might include compilation) with the given set of test options.
*/
public static async exec(options: NodeTestOptions): void {
log("options: %O", options);
let baseDir = options.compile;
if (baseDir) { // call n4jsc
const n4jscTempDir = await createTempDir();
if (isHTTP_re.test(options.testCatalog)) { // use temp file
options.testCatalog = lib_path.join(n4jscTempDir, "test-catalog.json");
} else {
options.testCatalog = lib_path.resolve(process.cwd(), options.testCatalog);
}
log("compiling", options.testCatalog);
await n4js_cli.n4jsc(n4js_cli.Goal.compile, baseDir, options);
log("generated test catalog:", options.testCatalog);
const testCatalog: TestInfos = options.emitProjectTestCatalogs ? JSON.parse(String(lib_fs.readFileSync(options.testCatalog, { encoding: "UTF-8" }))) as TestInfos : null;
const pathToPackageJson = new Map<string, Object+>();
for (const p of await lib_fs.readdir(baseDir)) {
const prj = lib_path.join(baseDir, p);
const packageJsonPath = lib_path.join(prj, PACKAGE_JSON);
if (fsSafeAccessSync(packageJsonPath)) {
const packageJson = readJsonFile(packageJsonPath);
if (packageJson && packageJson[PACKAGE_JSON_N4JS_SECTION_KEY]) { // only add if package.json has an "n4js" section
pathToPackageJson.set(prj, packageJson);
}
}
}
for (const [dir, packageJson] of pathToPackageJson) {
if (testCatalog && options.emitProjectTestCatalogs) {
const tests = testCatalog.testDescriptors.filter(test => test.origin === packageJson["name"] as string);
if (tests.length) {
const file = lib_path.join(dir, options.emitProjectTestCatalogs);
log("writing:", file);
lib_fs.writeFileSync(file, JSON.stringify({
testDescriptors: tests
}, null, 2), { encoding: "UTF-8" });
}
}
}
}
if (!isHTTP_re.test(options.testCatalog)) {
// try to resolve file:
const file = lib_path.resolve(process.cwd(), options.testCatalog);
if (fsSafeAccessSync(file)) {
options.testCatalog = file;
} else { // try to resolve via NPM
try {
options.testCatalog = require.resolve(options.testCatalog);
} catch (exc) {
throw new Error(`Cannot resolve test catalog: ${options.testCatalog}`);
}
}
}
log("testing with test catalog:", options.testCatalog);
await new Promise<any, any>((resolve, reject) => {
rlog("execPath:", process.execPath);
const modulePath = lib_path.join(__dirname, "NodeTestRunner.js");
rlog("modulePath:", modulePath);
const execArgv = process.execArgv.slice();
const execArgvOptions = execArgv.join(" ");
const nodeOptions = process.env.NODE_OPTIONS as string || "";
const checkOption = (str: string, opt: string): boolean => str.endsWith(opt) || str.indexOf(`${opt} `) >= 0;
const hasOption = (opt: string): boolean => checkOption(execArgvOptions, opt) || checkOption(nodeOptions, opt);
if (!["-r esm", "--require=esm", "--experimental-modules"].some(hasOption)) {
const esm = require.resolve("esm"); // use "own" esm version
execArgv.push("-r", esm);
}
if (options.inspectBrk) {
execArgv.push(`--inspect-brk=${options.inspectBrk}`);
}
rlog("execArgv:", execArgv);
const args = NodeTestCLI.toCommandLine(options); // fodder in same args
rlog("args:", args);
//rlog("env:", process.env);
child_process.fork(modulePath, args, {
stdio: "inherit",
execArgv: execArgv,
env: process.env
}).on("close", code => {
rlog("exit code:", code);
if (code === 0) {
resolve(code);
} else {
reject(code);
}
});
});
}
}