diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-12-02 12:41:52 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-02 12:41:52 -0500 |
commit | f4b8c2ea7dc58034b70f32ff5481573168ba92b1 (patch) | |
tree | b17fb199710c3250a32c5a32b4f355153d613a03 /cli/tests/unit/test_util.ts | |
parent | b638bc183d3f092a79155426a58e64e631a3b6bf (diff) |
chore: fix flaky netListenUnrefAndRef (#16892)
Closes #16890
Diffstat (limited to 'cli/tests/unit/test_util.ts')
-rw-r--r-- | cli/tests/unit/test_util.ts | 47 |
1 files changed, 42 insertions, 5 deletions
diff --git a/cli/tests/unit/test_util.ts b/cli/tests/unit/test_util.ts index 9a1b13038..ce945c7db 100644 --- a/cli/tests/unit/test_util.ts +++ b/cli/tests/unit/test_util.ts @@ -29,16 +29,53 @@ export function pathToAbsoluteFileUrl(path: string): URL { return new URL(`file://${Deno.build.os === "windows" ? "/" : ""}${path}`); } -const decoder = new TextDecoder(); +export function execCode(code: string): Promise<readonly [number, string]> { + return execCode2(code).finished(); +} -export async function execCode(code: string): Promise<[number, string]> { - const output = await new Deno.Command(Deno.execPath(), { +export function execCode2(code: string) { + const command = new Deno.Command(Deno.execPath(), { args: [ "eval", "--unstable", "--no-check", code, ], - }).output(); - return [output.code, decoder.decode(output.stdout)]; + stdout: "piped", + stderr: "inherit", + }); + + const child = command.spawn(); + const stdout = child.stdout.pipeThrough(new TextDecoderStream()).getReader(); + let output = ""; + + return { + async waitStdoutText(text: string) { + while (true) { + const readData = await stdout.read(); + if (readData.value) { + output += readData.value; + if (output.includes(text)) { + return; + } + } + if (readData.done) { + throw new Error(`Did not find text '${text}' in stdout.`); + } + } + }, + async finished() { + while (true) { + const readData = await stdout.read(); + if (readData.value) { + output += readData.value; + } + if (readData.done) { + break; + } + } + const status = await child.status; + return [status.code, output] as const; + }, + }; } |