-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_runner.js
181 lines (181 loc) · 6.27 KB
/
test_runner.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const pc = require("picocolors");
const process = require("process");
const { pathToFileURL } = require("url");
const { testDefinitions } = require("@deno/shim-deno/test-internals");
const filePaths = [];
async function main() {
const testContext = {
process,
pc,
};
for (const [i, filePath] of filePaths.entries()) {
if (i > 0) {
console.log("");
}
const esmPath = "./esm/" + filePath;
console.log("\nRunning tests in " + pc.underline(esmPath) + "...\n");
process.chdir(__dirname + "/esm");
const esmTestContext = {
origin: pathToFileURL(filePath).toString(),
...testContext,
};
await import(esmPath);
await runTestDefinitions(testDefinitions.splice(0, testDefinitions.length), esmTestContext);
}
}
async function runTestDefinitions(testDefinitions, options) {
const testFailures = [];
for (const definition of testDefinitions) {
options.process.stdout.write("test " + definition.name + " ...");
if (definition.ignore) {
options.process.stdout.write(` ${options.pc.gray("ignored")}\n`);
continue;
}
const context = getTestContext(definition, undefined);
let pass = false;
try {
await definition.fn(context);
if (context.hasFailingChild) {
testFailures.push({
name: definition.name,
err: new Error("Had failing test step.")
});
}
else {
pass = true;
}
}
catch (err) {
testFailures.push({
name: definition.name,
err
});
}
const testStepOutput = context.getOutput();
if (testStepOutput.length > 0) {
options.process.stdout.write(testStepOutput);
}
else {
options.process.stdout.write(" ");
}
options.process.stdout.write(getStatusText(pass ? "ok" : "fail"));
options.process.stdout.write("\n");
}
if (testFailures.length > 0) {
options.process.stdout.write("\nFAILURES");
for (const failure of testFailures) {
options.process.stdout.write("\n\n");
options.process.stdout.write(failure.name + "\n");
options.process.stdout.write(indentText((failure.err?.stack ?? failure.err).toString(), 1));
}
options.process.exit(1);
}
function getTestContext(definition, parent) {
return {
name: definition.name,
parent,
origin: options.origin,
/** @type {any} */ err: undefined,
status: "ok",
children: [],
get hasFailingChild() {
return this.children.some((c) => c.status === "fail" || c.status === "pending");
},
getOutput() {
let output = "";
if (this.parent) {
output += "test " + this.name + " ...";
}
if (this.children.length > 0) {
output += "\n" + this.children.map((c) => indentText(c.getOutput(), 1)).join("\n") + "\n";
}
else if (!this.err) {
output += " ";
}
if (this.parent && this.err) {
output += "\n";
}
if (this.err) {
output += indentText((this.err.stack ?? this.err).toString(), 1);
if (this.parent) {
output += "\n";
}
}
if (this.parent) {
output += getStatusText(this.status);
}
return output;
},
async step(nameOrTestDefinition, fn) {
const definition = getDefinition();
const context = getTestContext(definition, this);
context.status = "pending";
this.children.push(context);
if (definition.ignore) {
context.status = "ignored";
return false;
}
try {
await definition.fn(context);
context.status = "ok";
if (context.hasFailingChild) {
context.status = "fail";
return false;
}
return true;
}
catch (err) {
context.status = "fail";
context.err = err;
return false;
}
/** @returns {TestDefinition} */ function getDefinition() {
if (typeof nameOrTestDefinition === "string") {
if (!(fn instanceof Function)) {
throw new TypeError("Expected function for second argument.");
}
return {
name: nameOrTestDefinition,
fn
};
}
else if (typeof nameOrTestDefinition === "object") {
return nameOrTestDefinition;
}
else {
throw new TypeError("Expected a test definition or name and function.");
}
}
}
};
}
function getStatusText(status) {
switch (status) {
case "ok":
return options.pc.green(status);
case "fail":
case "pending":
return options.pc.red(status);
case "ignored":
return options.pc.gray(status);
default:
{
const _assertNever = status;
return status;
}
}
}
function indentText(text, indentLevel) {
if (text === undefined) {
text = "[undefined]";
}
else if (text === null) {
text = "[null]";
}
else {
text = text.toString();
}
return text.split(/\r?\n/).map((line) => " ".repeat(indentLevel) + line).join("\n");
}
}
main();