File size: 1,721 Bytes
3144483
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { expect, it } from "vitest";
import { runExec } from "./exec.js";

it.each([0, 7])(
  "observes both streams before settlement without changing exit %s",
  async (code) => {
    const received = { stdout: "", stderr: "" };
    let settled = false;
    const observations: boolean[] = [];
    const command = runExec(
      process.execPath,
      [
        "-e",
        `console.log('out'); console.error('err'); setTimeout(() => process.exit(${code}), 80)`,
      ],
      {
        timeoutMs: 2_000,
        logOutput: false,
        onOutputChunk: (chunk, stream) => {
          received[stream] += chunk.toString();
          observations.push(settled);
        },
      },
    );
    if (code === 0) {
      await expect(command).resolves.toEqual({ stdout: "out\n", stderr: "err\n" });
    } else {
      await expect(command).rejects.toMatchObject({ code, stdout: "out\n", stderr: "err\n" });
    }
    settled = true;
    expect(received).toEqual({ stdout: "out\n", stderr: "err\n" });
    expect(observations.length).toBeGreaterThanOrEqual(2);
    expect(observations).not.toContain(true);
  },
);

it.each([0, 7])(
  "keeps exit %s and buffered output when a diagnostic observer throws",
  async (code) => {
    const command = runExec(
      process.execPath,
      ["-e", `console.log('retained'); process.exitCode = ${code}`],
      {
        timeoutMs: 2_000,
        onOutputChunk: () => {
          throw new Error("diagnostic unavailable");
        },
      },
    );
    if (code === 0) {
      await expect(command).resolves.toEqual({ stdout: "retained\n", stderr: "" });
    } else {
      await expect(command).rejects.toMatchObject({ code, stdout: "retained\n", stderr: "" });
    }
  },
);