|
1 | | -import { buffer, describe, detect, expect, it, useCommand } from "./suite.ts"; |
2 | | -import { run } from "../mod.ts"; |
| 1 | +import { describe, expect, it, x } from "./suite.ts"; |
| 2 | +import { each, run, type Stream } from "../mod.ts"; |
| 3 | + |
| 4 | +function* detect(stream: Stream<string, void>, text: string) { |
| 5 | + for (const line of yield* each(stream)) { |
| 6 | + if (line.includes(text)) { |
| 7 | + return; |
| 8 | + } |
| 9 | + yield* each.next(); |
| 10 | + } |
| 11 | +} |
3 | 12 |
|
4 | 13 | describe("main", () => { |
5 | 14 | it("gracefully shuts down on SIGINT", async () => { |
6 | 15 | await run(function* () { |
7 | | - let daemon = yield* useCommand("deno", { |
8 | | - stdout: "piped", |
9 | | - args: ["run", "--allow-env", "test/main/ok.daemon.ts"], |
10 | | - }); |
11 | | - let stdout = yield* buffer(daemon.stdout); |
12 | | - yield* detect(stdout, "started"); |
| 16 | + let proc = yield* x("deno", ["run", "test/main/ok.daemon.ts"]); |
13 | 17 |
|
14 | | - daemon.kill("SIGINT"); |
| 18 | + yield* detect(proc.lines, "started"); |
15 | 19 |
|
16 | | - let status = yield* daemon.status; |
| 20 | + const { exitCode, stdout } = yield* proc.kill("SIGINT"); |
17 | 21 |
|
18 | | - expect(status.code).toBe(130); |
| 22 | + expect(stdout).toContain("gracefully stopped"); |
19 | 23 |
|
20 | | - yield* detect(stdout, "gracefully stopped"); |
| 24 | + expect(exitCode).toBe(130); |
21 | 25 | }); |
22 | 26 | }); |
23 | 27 |
|
24 | 28 | if (Deno.build.os !== "windows") { |
25 | 29 | it("gracefully shuts down on SIGTERM", async () => { |
26 | 30 | await run(function* () { |
27 | | - let daemon = yield* useCommand("deno", { |
28 | | - stdout: "piped", |
29 | | - args: ["run", "--allow-env", "test/main/ok.daemon.ts"], |
30 | | - }); |
31 | | - let stdout = yield* buffer(daemon.stdout); |
32 | | - yield* detect(stdout, "started"); |
| 31 | + let proc = yield* x("deno", ["run", "test/main/ok.daemon.ts"]); |
33 | 32 |
|
34 | | - daemon.kill("SIGTERM"); |
| 33 | + yield* detect(proc.lines, "started"); |
35 | 34 |
|
36 | | - let status = yield* daemon.status; |
| 35 | + const { exitCode, stdout } = yield* proc.kill("SIGTERM"); |
37 | 36 |
|
38 | | - expect(status.code).toBe(143); |
| 37 | + expect(stdout).toContain("gracefully stopped"); |
39 | 38 |
|
40 | | - yield* detect(stdout, "gracefully stopped"); |
| 39 | + expect(exitCode).toBe(143); |
41 | 40 | }); |
42 | 41 | }); |
43 | 42 | } |
44 | 43 |
|
45 | 44 | it("exits gracefully on explicit exit()", async () => { |
46 | 45 | await run(function* () { |
47 | | - let cmd = yield* useCommand("deno", { |
48 | | - stdout: "piped", |
49 | | - args: ["run", "--allow-env", "test/main/ok.exit.ts"], |
50 | | - }); |
| 46 | + let proc = yield* x("deno", ["run", "test/main/ok.exit.ts"]); |
51 | 47 |
|
52 | | - let stdout = yield* buffer(cmd.stdout); |
53 | | - |
54 | | - yield* detect(stdout, "goodbye.\nOk, computer."); |
| 48 | + yield* detect(proc.lines, "goodbye."); |
| 49 | + yield* detect(proc.lines, "Ok, computer."); |
55 | 50 | }); |
56 | 51 | }); |
57 | 52 |
|
58 | 53 | it("exits gracefully with 0 on implicit exit", async () => { |
59 | 54 | await run(function* () { |
60 | | - let cmd = yield* useCommand("deno", { |
61 | | - stdout: "piped", |
62 | | - args: ["run", "--allow-env", "test/main/ok.implicit.ts"], |
63 | | - }); |
| 55 | + let proc = yield* x("deno", ["run", "test/main/ok.implicit.ts"]); |
| 56 | + |
| 57 | + yield* detect(proc.lines, "goodbye."); |
64 | 58 |
|
65 | | - let stdout = yield* buffer(cmd.stdout); |
66 | | - let status = yield* cmd.status; |
| 59 | + const { exitCode } = yield* proc; |
67 | 60 |
|
68 | | - yield* detect(stdout, "goodbye."); |
69 | | - expect(status.code).toEqual(0); |
| 61 | + expect(exitCode).toEqual(0); |
70 | 62 | }); |
71 | 63 | }); |
72 | 64 |
|
73 | 65 | it("exits gracefully on explicit exit failure exit()", async () => { |
74 | 66 | await run(function* () { |
75 | | - let cmd = yield* useCommand("deno", { |
76 | | - stdout: "piped", |
77 | | - stderr: "piped", |
78 | | - args: ["run", "--allow-env", "test/main/fail.exit.ts"], |
79 | | - }); |
80 | | - let stdout = yield* buffer(cmd.stdout); |
81 | | - let stderr = yield* buffer(cmd.stderr); |
82 | | - let status = yield* cmd.status; |
| 67 | + let proc = yield* x("deno", ["run", "test/main/fail.exit.ts"]); |
| 68 | + |
| 69 | + const { stderr, exitCode, stdout } = yield* proc; |
83 | 70 |
|
84 | | - yield* detect(stdout, "graceful goodbye"); |
85 | | - yield* detect(stderr, "It all went horribly wrong"); |
86 | | - expect(status.code).toEqual(23); |
| 71 | + expect(stdout).toContain("graceful goodbye"); |
| 72 | + expect(stderr).toContain("It all went horribly wrong"); |
| 73 | + expect(exitCode).toEqual(23); |
87 | 74 | }); |
88 | 75 | }); |
89 | 76 |
|
90 | 77 | it("error exits gracefully on unexpected errors", async () => { |
91 | 78 | await run(function* () { |
92 | | - let cmd = yield* useCommand("deno", { |
93 | | - stdout: "piped", |
94 | | - stderr: "piped", |
95 | | - args: ["run", "--allow-env", "test/main/fail.unexpected.ts"], |
96 | | - }); |
| 79 | + let proc = yield* x("deno", ["run", "test/main/fail.unexpected.ts"]); |
97 | 80 |
|
98 | | - let stdout = yield* buffer(cmd.stdout); |
99 | | - let stderr = yield* buffer(cmd.stderr); |
100 | | - let status = yield* cmd.status; |
| 81 | + const { stderr, stdout, exitCode } = yield* proc; |
101 | 82 |
|
102 | | - yield* detect(stdout, "graceful goodbye"); |
103 | | - yield* detect(stderr, "Error: moo"); |
104 | | - expect(status.code).toEqual(1); |
| 83 | + expect(stdout).toContain("graceful goodbye"); |
| 84 | + expect(stderr).toContain("Error: moo"); |
| 85 | + expect(exitCode).toEqual(1); |
105 | 86 | }); |
106 | 87 | }); |
107 | 88 |
|
108 | 89 | it("works even if suspend is the only operation", async () => { |
109 | 90 | await run(function* () { |
110 | | - let process = yield* useCommand("deno", { |
111 | | - stdout: "piped", |
112 | | - args: ["run", "--allow-env", "test/main/just.suspend.ts"], |
113 | | - }); |
114 | | - let stdout = yield* buffer(process.stdout); |
115 | | - yield* detect(stdout, "started"); |
116 | | - |
117 | | - process.kill("SIGINT"); |
| 91 | + let proc = yield* x("deno", ["run", "test/main/just.suspend.ts"]); |
118 | 92 |
|
119 | | - let status = yield* process.status; |
| 93 | + yield* detect(proc.lines, "started"); |
120 | 94 |
|
121 | | - expect(status.code).toBe(130); |
| 95 | + const { exitCode, stdout } = yield* proc.kill("SIGINT"); |
122 | 96 |
|
123 | | - yield* detect(stdout, "gracefully stopped"); |
| 97 | + expect(exitCode).toBe(130); |
| 98 | + expect(stdout).toContain("gracefully stopped"); |
124 | 99 | }); |
125 | 100 | }); |
126 | 101 | }); |
0 commit comments