Repository navigation
Expand file tree
/
Copy pathrunner.js
More file actions
262 lines (222 loc) · 6.53 KB
/
Copy pathrunner.js
File metadata and controls
262 lines (222 loc) · 6.53 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// SPDX-FileCopyrightText: 2026 Aleksandr Mezin <mezin.alexander@gmail.com>
//
// SPDX-License-Identifier: MIT
/** @module runner.js */
import Gio from 'gi://Gio';
import System from 'system';
/** @import {Reporter} from './reporter.js' */
import {TapReporter} from './reporter.js';
/**
* A base class for tests and test suites.
*/
export class Node {
#name;
#parent;
/**
* Create a new Node.
*
* @param {string} name - Name of the node.
* @param {Suite | null} parent - The suite that contains this Node.
*/
constructor(name, parent) {
this.#name = name;
this.#parent = parent;
}
/**
* Short name of the Node.
*
* @returns {string} The name as string.
*/
get name() {
return this.#name;
}
/**
* Full name of the Node, including the containing suite name.
*
* @returns {string} Full name as string.
*/
get fullName() {
if (!this.#parent)
return this.name;
return `${this.#parent.fullName} / ${this.name}`;
}
/**
* Run the test or suite.
*
* @abstract
* @param {Reporter} reporter - The Reporter to output test report with.
* @returns {Promise<void>} No return value.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
run(reporter) {
throw new TypeError('Not implemented in the base class');
}
}
/**
* A test.
*/
export class Test extends Node {
#fn;
#running = false;
/**
* Create a new Test.
*
* @param {Suite | null} parent - The suite that contains this test.
* @param {string} name - Test name.
* @param {function(this: Test, Test): void | Promise<void>} fn - The function that
* implements the test.
*/
constructor(parent, name, fn) {
super(name, parent);
this.#fn = fn;
}
/**
* Run the test.
*
* @override
* @param {Reporter} reporter - The Reporter to output test status to.
* @returns {Promise<void>}
*/
async run(reporter) {
if (this.#running)
throw new Error('Recursive Test.run() calls are not allowed');
/** @type {function(this: Test, Test): unknown} */
const unknownFn = this.#fn;
let result;
this.#running = true;
try {
reporter.testStart(this);
result = await Promise.resolve(this).then(unknownFn.bind(this));
if (result !== undefined)
throw new Error(`Test function must not return a value, but it returned ${result}`);
reporter.testEnd(this, null);
} catch (ex) {
reporter.testEnd(this, ex);
} finally {
this.#running = false;
}
}
}
/**
* A collection of tests and other suites.
*/
export class Suite extends Node {
#populated = false;
#adding = false;
#running = false;
/** @type {Node[]} */
#children = [];
/**
* Create a new Suite.
*
* @param {Suite | null} parent - The suite that contains this suite.
* @param {string} name - Suite name.
* @param {function(this: Suite, Suite): void} fn - The function that populates the suite.
*/
constructor(parent, name, fn) {
super(name, parent);
/** @type {function(this: Suite, Suite): unknown} */
const unknownFn = fn;
let result;
try {
result = unknownFn.call(this, this);
} finally {
this.#populated = true;
}
if (result !== undefined) {
throw new TypeError(
`suite() callback must be synchronous and must not return a value, but it returned ${result}`
);
}
}
/**
* Add a test.
*
* @param {string} name - Test name.
* @param {function(Test): void | Promise<void>} fn - The function that implements the test.
* @returns {void}
*/
test(name, fn) {
if (this.#populated || this.#adding)
throw new Error('Tests can be added only from suite() callback directly');
this.#adding = true;
try {
this.#children.push(new Test(this, name, fn));
} finally {
this.#adding = false;
}
}
/**
* Add a nested test suite.
*
* @param {string} name - Test suite name.
* @param {function(Suite): void} fn - The function that populates the suite.
* @returns {void}
*/
suite(name, fn) {
if (this.#populated || this.#adding)
throw new Error('Nested test suites can be added only from suite() callback directly');
this.#adding = true;
try {
this.#children.push(new Suite(this, name, fn));
} finally {
this.#adding = false;
}
}
/**
* Run the test suite.
*
* @override
* @param {Reporter} reporter - The Reporter to output test status to.
* @returns {Promise<void>}
*/
async run(reporter) {
if (!this.#populated)
throw new Error('Suite.run() must not be called from suite() callback');
if (this.#running)
throw new Error('Recursive Suite.run() calls are not allowed');
this.#running = true;
try {
for (const child of this.#children) {
// eslint-disable-next-line no-await-in-loop
await child.run(reporter);
}
} finally {
this.#running = false;
}
}
/**
* Setup a Gio.Application to run the test suite.
*
* @param {Gio.Application} app - The Application object.
*/
connectApplication(app) {
app.connect('activate', () => {
const reporter = new TapReporter();
reporter.start();
app.hold();
this.run(reporter).finally(() => {
app.release();
reporter.end();
}).catch(logError);
});
}
/**
* Run the test suite, using the specified Gio.Application object for command line parsing
* and the event loop.
*
* @param {Gio.Application} app - The Application object.
* @param {string[]} argv - Command line argument array.
* @returns {Promise<void>}
*/
runApplication(
app = new Gio.Application(),
argv = [System.programInvocationName, ...System.programArgs]
) {
this.connectApplication(app);
return app.runAsync(argv).catch(/** @param {unknown} ex */ ex => {
logError(ex);
return 1;
}).then(exitCode => System.exit(exitCode));
}
}