Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions test/js/bun/shell/file-io.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,32 @@ describe("IOWriter file output redirection", () => {
// command caused the same file descriptor to be closed twice, resulting
// in an EBADF error. The issue was that two separate IOWriter instances
// were created for the same fd when both stdout and stderr were redirected.
TestBuilder.command`pwd &> pwd_output.txt`.exitCode(0).runAsTest("builtin pwd with &> redirect");
TestBuilder.command`pwd &> pwd_output.txt`
.exitCode(0)
.fileEquals("pwd_output.txt", "$TEMP_DIR\n")
.runAsTest("builtin pwd with &> redirect");

TestBuilder.command`echo "hello" &> echo_output.txt`
.exitCode(0)
.fileEquals("echo_output.txt", "hello\n")
.runAsTest("builtin echo with &> redirect");

TestBuilder.command`pwd &>> append_output.txt`.exitCode(0).runAsTest("builtin pwd with &>> append redirect");
TestBuilder.command`pwd extra-arg &> stderr_output.txt`
.exitCode(1)
.stderr("")
.fileEquals("stderr_output.txt", "pwd: too many arguments\n")
.runAsTest("builtin stderr with &> redirect");

TestBuilder.command`pwd &>> append_output.txt`
.file("append_output.txt", "existing line\n")
.exitCode(0)
.fileEquals("append_output.txt", "existing line\n$TEMP_DIR\n")
.runAsTest("builtin pwd with &>> append redirect");

TestBuilder.command`pwd &>> new_append_output.txt`
.exitCode(0)
.fileEquals("new_append_output.txt", "$TEMP_DIR\n")
.runAsTest("builtin pwd with &>> redirect creates a missing file");
});
});

Expand Down
9 changes: 7 additions & 2 deletions test/js/bun/shell/test_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ export function createTestBuilder(path: string) {
return this;
}

/**
* Expect a file in the temp directory to have the given contents. Like
* `stdout()`, a string `expected` has `$TEMP_DIR` replaced with the temp directory.
*/
fileEquals(filename: string, expected: string | (() => string | Promise<string>)): this {
this.getTempDir();
this.file_equals[filename] = expected;
Expand Down Expand Up @@ -228,7 +232,8 @@ export function createTestBuilder(path: string) {
} else if (typeof this.expected_exit_code === "function") this.expected_exit_code(exitCode);

for (const [filename, expected_raw] of Object.entries(this.file_equals)) {
const expected = typeof expected_raw === "string" ? expected_raw : await expected_raw();
const expected =
typeof expected_raw === "string" ? expected_raw.replaceAll("$TEMP_DIR", tempdir) : await expected_raw();
const actual = await Bun.file(join(this.tempdir!, filename)).text();
expect(actual).toEqual(expected);
}
Expand Down Expand Up @@ -256,7 +261,7 @@ export function createTestBuilder(path: string) {
if (stdout === undefined || stderr === undefined || exitCode === undefined) {
throw err_;
}
this.doChecks(stdout, stderr, exitCode);
await this.doChecks(stdout, stderr, exitCode);
return;
}
if (this.expected_error === true) return undefined;
Expand Down