diff options
Diffstat (limited to '')
-rw-r--r-- | src/bun.js/api/bun/subprocess.zig | 2 | ||||
-rw-r--r-- | test/bun.js/spawn.test.ts | 40 |
2 files changed, 40 insertions, 2 deletions
diff --git a/src/bun.js/api/bun/subprocess.zig b/src/bun.js/api/bun/subprocess.zig index 0444ef927..6bf839ca1 100644 --- a/src/bun.js/api/bun/subprocess.zig +++ b/src/bun.js/api/bun/subprocess.zig @@ -1188,7 +1188,7 @@ pub const Subprocess = struct { try actions.dup2(fd, std_fileno); }, .path => |pathlike| { - const flag = if (std_fileno == std.os.STDIN_FILENO) @as(u32, os.O.WRONLY) else @as(u32, std.os.O.RDONLY); + const flag = if (std_fileno == std.os.STDIN_FILENO) @as(u32, os.O.RDONLY) else @as(u32, std.os.O.WRONLY); try actions.open(std_fileno, pathlike.slice(), flag | std.os.O.CREAT, 0o664); }, .inherit => { diff --git a/test/bun.js/spawn.test.ts b/test/bun.js/spawn.test.ts index f7426660f..e03de2662 100644 --- a/test/bun.js/spawn.test.ts +++ b/test/bun.js/spawn.test.ts @@ -1,9 +1,47 @@ -import { readableStreamToText, spawn } from "bun"; +import { readableStreamToText, spawn, write } from "bun"; import { describe, expect, it } from "bun:test"; +import { rmdirSync, unlinkSync, rmSync } from "node:fs"; describe("spawn", () => { const hugeString = "hello".repeat(10000).slice(); + it("Bun.file() works as stdout", async () => { + rmSync("/tmp/out.123.txt", { force: true }); + const { exited } = spawn({ + cmd: ["echo", "hello"], + stdout: Bun.file("/tmp/out.123.txt"), + }); + + await exited; + expect(await Bun.file("/tmp/out.123.txt").text()).toBe("hello\n"); + }); + + it("Bun.file() works as stdin", async () => { + await write(Bun.file("/tmp/out.456.txt"), "hello there!"); + const { stdout } = spawn({ + cmd: ["cat"], + stdout: "pipe", + stdin: Bun.file("/tmp/out.456.txt"), + }); + + expect(await readableStreamToText(stdout)).toBe("hello there!"); + }); + + it("Bun.file() works as stdin and stdout", async () => { + await write(Bun.file("/tmp/out.456.txt"), "hello!"); + await write(Bun.file("/tmp/out.123.txt"), "wrong!"); + + const { exited } = spawn({ + cmd: ["cat"], + stdout: Bun.file("/tmp/out.123.txt"), + stdin: Bun.file("/tmp/out.456.txt"), + }); + + await exited; + expect(await Bun.file("/tmp/out.456.txt").text()).toBe("hello!"); + expect(await Bun.file("/tmp/out.123.txt").text()).toBe("hello!"); + }); + it("stdout can be read", async () => { await Bun.write("/tmp/out.txt", hugeString); const { stdout } = spawn({ |