Skip to content

Commit bf82c66

Browse files
authored
chore: make commandWithCwdIsAsync test less flaky (denoland#26770)
1 parent b926213 commit bf82c66

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

tests/unit/command_test.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,31 @@ Deno.test(
1414
const enc = new TextEncoder();
1515
const cwd = await Deno.makeTempDir({ prefix: "deno_command_test" });
1616

17+
const exitCodeFileLock = "deno_was_here.lock";
1718
const exitCodeFile = "deno_was_here";
1819
const programFile = "poll_exit.ts";
1920
const program = `
21+
const file = await Deno.open("${exitCodeFileLock}", { write: true, create: true });
2022
async function tryExit() {
23+
await file.lock(true);
2124
try {
2225
const code = parseInt(await Deno.readTextFile("${exitCodeFile}"));
2326
Deno.exit(code);
2427
} catch {
2528
// Retry if we got here before deno wrote the file.
2629
setTimeout(tryExit, 0.01);
30+
} finally {
31+
await file.unlock();
2732
}
2833
}
2934
3035
tryExit();
3136
`;
32-
3337
Deno.writeFileSync(`${cwd}/${programFile}`, enc.encode(program));
3438

3539
const command = new Deno.Command(Deno.execPath(), {
3640
cwd,
37-
args: ["run", "--allow-read", programFile],
41+
args: ["run", "-RW", programFile],
3842
stdout: "inherit",
3943
stderr: "inherit",
4044
});
@@ -43,12 +47,18 @@ tryExit();
4347
// Write the expected exit code *after* starting deno.
4448
// This is how we verify that `Child` is actually asynchronous.
4549
const code = 84;
46-
Deno.writeFileSync(`${cwd}/${exitCodeFile}`, enc.encode(`${code}`));
4750

51+
await using file = await Deno.open(`${cwd}/${exitCodeFileLock}`, {
52+
write: true,
53+
create: true,
54+
});
55+
await file.lock(true);
56+
Deno.writeFileSync(`${cwd}/${exitCodeFile}`, enc.encode(`${code}`));
57+
await file.unlock();
4858
const status = await child.status;
4959
await Deno.remove(cwd, { recursive: true });
50-
assertEquals(status.success, false);
5160
assertEquals(status.code, code);
61+
assertEquals(status.success, false);
5262
assertEquals(status.signal, null);
5363
},
5464
);

0 commit comments

Comments
 (0)