From f61cf7ece27ccff8014e28cb0287a0f1df766446 Mon Sep 17 00:00:00 2001 From: Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> Date: Thu, 7 Nov 2024 15:02:33 -0800 Subject: [PATCH] chore: make commandWithCwdIsAsync test less flaky (#26770) --- tests/unit/command_test.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/unit/command_test.ts b/tests/unit/command_test.ts index 51bbdd8600..8345548f85 100644 --- a/tests/unit/command_test.ts +++ b/tests/unit/command_test.ts @@ -14,27 +14,31 @@ Deno.test( const enc = new TextEncoder(); const cwd = await Deno.makeTempDir({ prefix: "deno_command_test" }); + const exitCodeFileLock = "deno_was_here.lock"; const exitCodeFile = "deno_was_here"; const programFile = "poll_exit.ts"; const program = ` +const file = await Deno.open("${exitCodeFileLock}", { write: true, create: true }); async function tryExit() { + await file.lock(true); try { const code = parseInt(await Deno.readTextFile("${exitCodeFile}")); Deno.exit(code); } catch { // Retry if we got here before deno wrote the file. setTimeout(tryExit, 0.01); + } finally { + await file.unlock(); } } tryExit(); `; - Deno.writeFileSync(`${cwd}/${programFile}`, enc.encode(program)); const command = new Deno.Command(Deno.execPath(), { cwd, - args: ["run", "--allow-read", programFile], + args: ["run", "-RW", programFile], stdout: "inherit", stderr: "inherit", }); @@ -43,12 +47,18 @@ tryExit(); // Write the expected exit code *after* starting deno. // This is how we verify that `Child` is actually asynchronous. const code = 84; - Deno.writeFileSync(`${cwd}/${exitCodeFile}`, enc.encode(`${code}`)); + await using file = await Deno.open(`${cwd}/${exitCodeFileLock}`, { + write: true, + create: true, + }); + await file.lock(true); + Deno.writeFileSync(`${cwd}/${exitCodeFile}`, enc.encode(`${code}`)); + await file.unlock(); const status = await child.status; await Deno.remove(cwd, { recursive: true }); - assertEquals(status.success, false); assertEquals(status.code, code); + assertEquals(status.success, false); assertEquals(status.signal, null); }, );