|
| 1 | +/* |
| 2 | + * exec-process-utf8-chunks.test.ts |
| 3 | + * |
| 4 | + * Regression test for #15341: execProcess must keep a multi-byte UTF-8 |
| 5 | + * character whole when the subprocess output splits it across two read |
| 6 | + * chunks. The child writes a long run of 3-byte characters, so read |
| 7 | + * boundaries (64 KiB is not a multiple of 3) fall inside a character. |
| 8 | + * |
| 9 | + * Copyright (C) 2026 Posit Software, PBC |
| 10 | + */ |
| 11 | + |
| 12 | +import { unitTest } from "../test.ts"; |
| 13 | +import { assertEquals } from "../asserts.ts"; |
| 14 | +import { execProcess } from "../../src/core/process.ts"; |
| 15 | +import { execPath } from "../../src/deno_ral/process.ts"; |
| 16 | + |
| 17 | +const kChar = "“"; |
| 18 | +const kCount = 100_000; |
| 19 | +const expected = kChar.repeat(kCount); |
| 20 | + |
| 21 | +// Child-side helpers: write all bytes to an fd, and sleep between writes. |
| 22 | +const prelude = `import { writeSync } from "node:fs"; |
| 23 | +const w = (fd, b) => { let o = 0; while (o < b.length) o += writeSync(fd, b, o); }; |
| 24 | +const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); |
| 25 | +`; |
| 26 | + |
| 27 | +const childCmd = (script: string) => ({ |
| 28 | + cmd: execPath(), |
| 29 | + args: ["eval", prelude + script], |
| 30 | + stdout: "piped" as const, |
| 31 | + stderr: "piped" as const, |
| 32 | +}); |
| 33 | + |
| 34 | +const largeCmd = (fd: 1 | 2) => |
| 35 | + childCmd(`w(${fd}, new TextEncoder().encode("${kChar}".repeat(${kCount})));`); |
| 36 | + |
| 37 | +// 3 rounds: stdout [E2]; stderr "”"; stdout [80 9C]. Each stdout character |
| 38 | +// is split across two writes with the other stream's output in between. |
| 39 | +const interleavedCmd = childCmd(`for (let i = 0; i < 3; i++) { |
| 40 | + w(1, Uint8Array.of(0xE2)); |
| 41 | + await sleep(100); |
| 42 | + w(2, new TextEncoder().encode("”")); |
| 43 | + await sleep(100); |
| 44 | + w(1, Uint8Array.of(0x80, 0x9C)); |
| 45 | + await sleep(100); |
| 46 | +}`); |
| 47 | + |
| 48 | +const partialStderrCmd = childCmd(`w(2, Uint8Array.of(0xE2, 0x80));`); |
| 49 | + |
| 50 | +const truncatedCmd = childCmd(`w(1, Uint8Array.of(0x61, 0xE2, 0x80));`); |
| 51 | + |
| 52 | +unitTest("execProcess - stdout keeps characters split across chunks", async () => { |
| 53 | + const result = await execProcess(largeCmd(1)); |
| 54 | + assertEquals(result.stdout, expected); |
| 55 | +}); |
| 56 | + |
| 57 | +unitTest("execProcess - stderr keeps characters split across chunks", async () => { |
| 58 | + const result = await execProcess(largeCmd(2)); |
| 59 | + assertEquals(result.stderr, expected); |
| 60 | +}); |
| 61 | + |
| 62 | +unitTest( |
| 63 | + "execProcess - filtered stderr keeps characters split across chunks", |
| 64 | + async () => { |
| 65 | + const result = await execProcess( |
| 66 | + largeCmd(2), |
| 67 | + undefined, |
| 68 | + undefined, |
| 69 | + (output) => output, |
| 70 | + ); |
| 71 | + assertEquals(result.stderr, expected); |
| 72 | + }, |
| 73 | +); |
| 74 | + |
| 75 | +unitTest( |
| 76 | + "execProcess - merged output keeps characters split across chunks", |
| 77 | + async () => { |
| 78 | + const result = await execProcess( |
| 79 | + largeCmd(2), |
| 80 | + undefined, |
| 81 | + "stderr>stdout", |
| 82 | + (output) => output, |
| 83 | + ); |
| 84 | + assertEquals(result.stdout, expected); |
| 85 | + }, |
| 86 | +); |
| 87 | + |
| 88 | +unitTest( |
| 89 | + "execProcess - merged output interleaving streams keeps characters whole", |
| 90 | + async () => { |
| 91 | + const result = await execProcess(interleavedCmd, undefined, "stderr>stdout"); |
| 92 | + // Mux order across sources is not a contract, so compare sorted characters. |
| 93 | + assertEquals([...result.stdout!].sort().join(""), "“““”””"); |
| 94 | + }, |
| 95 | +); |
| 96 | + |
| 97 | +unitTest("execProcess - filter never sees empty text", async () => { |
| 98 | + const seen: string[] = []; |
| 99 | + const result = await execProcess( |
| 100 | + partialStderrCmd, |
| 101 | + undefined, |
| 102 | + undefined, |
| 103 | + (s) => { |
| 104 | + seen.push(s); |
| 105 | + return s; |
| 106 | + }, |
| 107 | + ); |
| 108 | + assertEquals(seen, ["�"]); |
| 109 | + assertEquals(result.stderr, "�"); |
| 110 | +}); |
| 111 | + |
| 112 | +unitTest("execProcess - truncated tail decodes to one U+FFFD", async () => { |
| 113 | + const result = await execProcess(truncatedCmd); |
| 114 | + assertEquals(result.stdout, "a�"); |
| 115 | +}); |
0 commit comments