-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Expand file tree
/
Copy pathspawn_and_kill.ts
More file actions
53 lines (46 loc) · 1.41 KB
/
spawn_and_kill.ts
File metadata and controls
53 lines (46 loc) · 1.41 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
// Spawns a Deno server with DENO_COVERAGE_DIR set, waits for it to be
// ready, sends SIGTERM, then verifies coverage files were written.
//
// The server script to run is passed as the first argument.
const serverScript = Deno.args[0];
const covDir = Deno.cwd() + "/cov_output";
const child = new Deno.Command(Deno.execPath(), {
args: ["run", "--allow-net", "--allow-read", serverScript],
env: {
DENO_COVERAGE_DIR: covDir,
},
stdout: "piped",
stderr: "piped",
}).spawn();
// Wait for the server to be ready by reading stderr for the "Listening" message.
const decoder = new TextDecoder();
let stderr = "";
const reader = child.stderr.getReader();
while (true) {
const { value, done } = await reader.read();
if (done) break;
stderr += decoder.decode(value);
if (stderr.includes("Listening")) break;
}
reader.releaseLock();
// Send SIGTERM to the server process.
child.kill("SIGTERM");
const status = await child.status;
// The process should have been killed by SIGTERM.
console.log("signal:", status.signal);
// Check that coverage files were written.
const covFiles: string[] = [];
try {
for await (const entry of Deno.readDir(covDir)) {
if (entry.name.endsWith(".json")) {
covFiles.push(entry.name);
}
}
} catch {
// directory doesn't exist
}
if (covFiles.length > 0) {
console.log("coverage files written:", covFiles.length);
} else {
console.log("ERROR: no coverage files found");
}