/
cli_test.ts
53 lines (45 loc) · 1.33 KB
/
cli_test.ts
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
import { assert, assertEquals } from "../testing/asserts.ts";
import { dirname, fromFileUrl } from "../path/mod.ts";
/** The utility for running "node/main.ts" with the given args */
async function runNodeMain(...args: string[]) {
const p = Deno.run({
cmd: [
Deno.execPath(),
"run",
"--allow-read",
"--unstable",
"cli.ts",
...args,
],
stdout: "piped",
stderr: "piped",
cwd: dirname(fromFileUrl(new URL(import.meta.url))),
});
const [status, output, stderrOutput] = await Promise.all([
p.status(),
p.output(),
p.stderrOutput(),
]);
const decoder = new TextDecoder();
p.close();
return {
status,
output: decoder.decode(output),
stderrOutput: decoder.decode(stderrOutput),
};
}
Deno.test("[node/main] run node.js script", async () => {
const { status, output } = await runNodeMain("_module/example.js");
assertEquals(status.code, 0);
assertEquals(output, " foo\n");
});
Deno.test("[node/main] --help", async () => {
const { status, output } = await runNodeMain("--help");
assertEquals(status.code, 0);
assert(output.includes("Usage:"));
});
Deno.test("[node/main] exec with no arg", async () => {
const { status, output } = await runNodeMain();
assertEquals(status.code, 1); // Error
assert(output.includes("Usage:")); // Shows the usage
});