|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { spawn } from "node:child_process"; |
| 3 | +import { test } from "node:test"; |
| 4 | +import { deployedUrlsFromOutput } from "../deployment.mjs"; |
| 5 | +import { runCommand } from "../run.mjs"; |
| 6 | + |
| 7 | +const RUN_MODULE = new URL("../run.mjs", import.meta.url).href; |
| 8 | + |
| 9 | +// Runs a fake command through runCommand in a separate Node process, so its |
| 10 | +// fd 1 writes can be observed as they happen. Resolves with every stdout chunk |
| 11 | +// the wrapper wrote, each stamped with its arrival time, and the result. |
| 12 | +function runFake(fakeScript, { capture = true } = {}) { |
| 13 | + const wrapper = ` |
| 14 | + import { runCommand } from ${JSON.stringify(RUN_MODULE)}; |
| 15 | + import { writeFileSync } from "node:fs"; |
| 16 | + const result = await runCommand(process.execPath, ["-e", ${JSON.stringify(fakeScript)}], { cwd: process.cwd(), capture: ${capture} }); |
| 17 | + writeFileSync(3, JSON.stringify({ ...result, error: result.error?.code })); |
| 18 | + `; |
| 19 | + const child = spawn(process.execPath, ["--input-type=module", "-e", wrapper], { |
| 20 | + stdio: ["ignore", "pipe", "inherit", "pipe"], |
| 21 | + }); |
| 22 | + const writes = []; |
| 23 | + let resultJson = ""; |
| 24 | + child.stdout.on("data", (chunk) => writes.push({ at: Date.now(), text: chunk.toString() })); |
| 25 | + child.stdio[3].on("data", (chunk) => { |
| 26 | + resultJson += chunk; |
| 27 | + }); |
| 28 | + return new Promise((done) => { |
| 29 | + child.on("close", () => done({ writes, result: JSON.parse(resultJson) })); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +test("writes each stdout line to the log before the command exits", async () => { |
| 34 | + const { writes, result } = await runFake( |
| 35 | + 'console.log("first"); setTimeout(() => console.log("second"), 500);', |
| 36 | + ); |
| 37 | + const first = writes.find((w) => w.text.includes("first")); |
| 38 | + const second = writes.find((w) => w.text.includes("second")); |
| 39 | + assert.ok(first && second); |
| 40 | + assert.ok(!first.text.includes("second"), "first and second arrived in one write"); |
| 41 | + assert.ok(second.at - first.at >= 300, `second arrived ${second.at - first.at}ms after first`); |
| 42 | + assert.equal(result.stdout, "first\nsecond\n"); |
| 43 | + assert.equal(result.status, 0); |
| 44 | +}); |
| 45 | + |
| 46 | +test("collects a result line split across writes and parses it as before", async () => { |
| 47 | + const line = JSON.stringify({ |
| 48 | + kind: "result", |
| 49 | + envelope: { |
| 50 | + commandId: "deploy", |
| 51 | + result: { |
| 52 | + summary: { |
| 53 | + nodes: [{ address: "app", entities: [{ kind: "compute-service", url: "https://abc.ewr.prisma.build" }] }], |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }); |
| 58 | + const half = Math.floor(line.length / 2); |
| 59 | + const { writes, result } = await runFake( |
| 60 | + `process.stdout.write(${JSON.stringify(`deploying\n${line.slice(0, half)}`)}); |
| 61 | + setTimeout(() => process.stdout.write(${JSON.stringify(line.slice(half))}), 200);`, |
| 62 | + ); |
| 63 | + assert.equal(result.stdout, `deploying\n${line}`); |
| 64 | + assert.deepEqual(deployedUrlsFromOutput(result.stdout), { |
| 65 | + urls: { app: "https://abc.ewr.prisma.build" }, |
| 66 | + url: "https://abc.ewr.prisma.build", |
| 67 | + }); |
| 68 | + // The partial last line is ended in the log, but not in the collected stdout. |
| 69 | + assert.equal(writes.map((w) => w.text).join(""), `deploying\n${line}\n`); |
| 70 | +}); |
| 71 | + |
| 72 | +test("returns the exit status of a failing command", async () => { |
| 73 | + const { result } = await runFake('console.log("boom"); process.exit(3);'); |
| 74 | + assert.equal(result.status, 3); |
| 75 | + assert.equal(result.stdout, "boom\n"); |
| 76 | +}); |
| 77 | + |
| 78 | +test("returns the signal of a killed command", async () => { |
| 79 | + const { result } = await runFake('process.kill(process.pid, "SIGTERM");'); |
| 80 | + assert.equal(result.status, null); |
| 81 | + assert.equal(result.signal, "SIGTERM"); |
| 82 | +}); |
| 83 | + |
| 84 | +test("returns no stdout when output is not captured", async () => { |
| 85 | + const { writes, result } = await runFake('console.log("inherited");', { capture: false }); |
| 86 | + assert.equal(result.stdout, ""); |
| 87 | + assert.equal(writes.map((w) => w.text).join(""), "inherited\n"); |
| 88 | +}); |
| 89 | + |
| 90 | +test("returns the spawn error for a missing command", async () => { |
| 91 | + const result = await runCommand("no-such-command-for-run-test", [], { cwd: process.cwd(), capture: true }); |
| 92 | + assert.equal(result.error?.code, "ENOENT"); |
| 93 | + assert.equal(result.stdout, ""); |
| 94 | +}); |
0 commit comments