|
| 1 | +import { execFile } from "node:child_process"; |
| 2 | +import { resolve } from "node:path"; |
| 3 | +import { promisify } from "node:util"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | + |
| 6 | +const execFileAsync = promisify(execFile); |
| 7 | +const binPath = resolve(__dirname, "../../dist/bin/bash-env.js"); |
| 8 | + |
| 9 | +async function runBin( |
| 10 | + args: string[], |
| 11 | +): Promise<{ stdout: string; stderr: string; exitCode: number }> { |
| 12 | + try { |
| 13 | + const { stdout, stderr } = await execFileAsync("node", [binPath, ...args]); |
| 14 | + return { stdout, stderr, exitCode: 0 }; |
| 15 | + } catch (error: unknown) { |
| 16 | + const e = error as { stdout?: string; stderr?: string; code?: number }; |
| 17 | + return { |
| 18 | + stdout: e.stdout ?? "", |
| 19 | + stderr: e.stderr ?? "", |
| 20 | + exitCode: e.code ?? 1, |
| 21 | + }; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +describe("bash-env bundled binary", () => { |
| 26 | + it("should show version", async () => { |
| 27 | + const result = await runBin(["--version"]); |
| 28 | + expect(result.stdout).toContain("bash-env"); |
| 29 | + expect(result.exitCode).toBe(0); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should show help", async () => { |
| 33 | + const result = await runBin(["--help"]); |
| 34 | + expect(result.stdout).toContain("Usage:"); |
| 35 | + expect(result.exitCode).toBe(0); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should execute echo command", async () => { |
| 39 | + const result = await runBin(["-c", "echo hello world"]); |
| 40 | + expect(result.stdout).toBe("hello world\n"); |
| 41 | + expect(result.exitCode).toBe(0); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should execute pipes", async () => { |
| 45 | + const result = await runBin(["-c", 'echo "line1\nline2\nline3" | wc -l']); |
| 46 | + expect(result.stdout.trim()).toBe("3"); |
| 47 | + expect(result.exitCode).toBe(0); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should handle file operations with --allow-write", async () => { |
| 51 | + const result = await runBin([ |
| 52 | + "-c", |
| 53 | + 'echo "test" > /tmp/test.txt && cat /tmp/test.txt', |
| 54 | + "--allow-write", |
| 55 | + ]); |
| 56 | + expect(result.stdout).toBe("test\n"); |
| 57 | + expect(result.exitCode).toBe(0); |
| 58 | + }); |
| 59 | + |
| 60 | + it("should support JSON output", async () => { |
| 61 | + const result = await runBin(["-c", "echo hello", "--json"]); |
| 62 | + const json = JSON.parse(result.stdout); |
| 63 | + expect(json.stdout).toBe("hello\n"); |
| 64 | + expect(json.stderr).toBe(""); |
| 65 | + expect(json.exitCode).toBe(0); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should lazy-load commands (grep)", async () => { |
| 69 | + const result = await runBin([ |
| 70 | + "-c", |
| 71 | + 'echo -e "foo\\nbar\\nbaz" | grep ba', |
| 72 | + "--allow-write", |
| 73 | + ]); |
| 74 | + expect(result.stdout).toContain("bar"); |
| 75 | + expect(result.stdout).toContain("baz"); |
| 76 | + expect(result.exitCode).toBe(0); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should lazy-load commands (sed)", async () => { |
| 80 | + const result = await runBin(["-c", "echo hello | sed 's/hello/world/'"]); |
| 81 | + expect(result.stdout).toBe("world\n"); |
| 82 | + expect(result.exitCode).toBe(0); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should lazy-load commands (awk)", async () => { |
| 86 | + const result = await runBin(["-c", "echo 'a b c' | awk '{print $2}'"]); |
| 87 | + expect(result.stdout).toBe("b\n"); |
| 88 | + expect(result.exitCode).toBe(0); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should handle errexit mode", async () => { |
| 92 | + const result = await runBin(["-e", "-c", "false; echo should not print"]); |
| 93 | + expect(result.stdout).not.toContain("should not print"); |
| 94 | + expect(result.exitCode).toBe(1); |
| 95 | + }); |
| 96 | +}); |
0 commit comments