-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_test.ts
More file actions
84 lines (75 loc) · 1.93 KB
/
run_test.ts
File metadata and controls
84 lines (75 loc) · 1.93 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
import {
assertEquals,
assertThrowsAsync,
} from "https://deno.land/std@0.97.0/testing/asserts.ts";
import { join } from "https://deno.land/std@0.91.0/path/mod.ts";
import { getText } from "./map.ts";
import { run } from "./run.ts";
import { copy } from "https://deno.land/std@0.143.0/streams/conversion.ts";
const mockcliFile = "mockcli.ts";
const denoRun = ["deno", "run", "--allow-read"];
const mockcli = [...denoRun, mockcliFile];
async function answer() {
return await getText(run({
cmd: [...mockcli, "answer"],
}));
}
async function echo(input?: string | Uint8Array) {
return await getText(run({
cmd: [...mockcli, "echo"],
input,
}));
}
async function fail(input: string) {
return await run({
cmd: [...mockcli, "fail"],
input,
});
}
Deno.test("runs a command and returns the output", async () => {
assertEquals(
await answer(),
"42\n",
);
});
Deno.test("run passes string input to stdin", async () => {
assertEquals(
await echo("hello, world"),
"hello, world",
);
});
Deno.test("run passes buffer input to stdin", async () => {
const buf = new TextEncoder().encode("hello, world");
assertEquals(
await echo(buf),
"hello, world",
);
});
Deno.test("run throws error if process fails", async () => {
await assertThrowsAsync(
async () => await fail("oh no!"),
Error,
"oh no!",
);
});
Deno.test("run uses the specified CWD", async () => {
// For testing in another CWD we copy the mockcli file to the temporary working dir.
const cwd = await Deno.makeTempDir();
const tmpMockcli = join(cwd, mockcliFile);
const dst = await Deno.create(tmpMockcli);
const src = await Deno.open(mockcliFile);
try {
await copy(src, dst);
assertEquals(
await getText(run({
cmd: [...denoRun, tmpMockcli, "cwd"],
cwd,
})),
cwd + "\n",
);
} finally {
dst.close();
src.close();
await Deno.remove(cwd, { recursive: true });
}
});