|
1 | | -import { describe, expect, it } from "bun:test"; |
| 1 | +import { describe, expect, it, beforeEach, afterEach } from "bun:test"; |
2 | 2 | import * as os from "os"; |
3 | 3 | import * as path from "path"; |
| 4 | +import * as fs from "fs/promises"; |
4 | 5 | import { WorktreeRuntime } from "./WorktreeRuntime"; |
5 | 6 |
|
6 | 7 | describe("WorktreeRuntime constructor", () => { |
@@ -65,3 +66,82 @@ describe("WorktreeRuntime.resolvePath", () => { |
65 | 66 | expect(path.isAbsolute(resolved)).toBe(true); |
66 | 67 | }); |
67 | 68 | }); |
| 69 | + |
| 70 | +describe("WorktreeRuntime.exec bashrc sourcing", () => { |
| 71 | + // Use a temp directory as fake HOME with .mux/bashrc inside |
| 72 | + const testHome = path.join(os.tmpdir(), `mux-bashrc-test-${Date.now()}`); |
| 73 | + const testMuxDir = path.join(testHome, ".mux"); |
| 74 | + const testBashrcPath = path.join(testMuxDir, "bashrc"); |
| 75 | + const testWorkDir = path.join(os.tmpdir(), "mux-bashrc-workdir"); |
| 76 | + |
| 77 | + beforeEach(async () => { |
| 78 | + // Create test directories |
| 79 | + await fs.mkdir(testMuxDir, { recursive: true }); |
| 80 | + await fs.mkdir(testWorkDir, { recursive: true }); |
| 81 | + }); |
| 82 | + |
| 83 | + afterEach(async () => { |
| 84 | + // Clean up test files |
| 85 | + await fs.rm(testHome, { recursive: true, force: true }); |
| 86 | + await fs.rm(testWorkDir, { recursive: true, force: true }); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should source ~/.mux/bashrc and set environment variables", async () => { |
| 90 | + // Create a test bashrc that sets a unique environment variable |
| 91 | + const testEnvValue = `test_value_${Date.now()}`; |
| 92 | + await fs.writeFile(testBashrcPath, `export MUX_TEST_BASHRC_VAR="${testEnvValue}"\n`); |
| 93 | + |
| 94 | + const runtime = new WorktreeRuntime("/tmp"); |
| 95 | + // Pass HOME as environment variable so the child process uses our test home |
| 96 | + const stream = await runtime.exec("echo $MUX_TEST_BASHRC_VAR", { |
| 97 | + cwd: testWorkDir, |
| 98 | + timeout: 5, |
| 99 | + env: { HOME: testHome }, |
| 100 | + }); |
| 101 | + |
| 102 | + // Read stdout |
| 103 | + const reader = stream.stdout.getReader(); |
| 104 | + const decoder = new TextDecoder(); |
| 105 | + let output = ""; |
| 106 | + while (true) { |
| 107 | + const { done, value } = await reader.read(); |
| 108 | + if (done) break; |
| 109 | + output += decoder.decode(value, { stream: true }); |
| 110 | + } |
| 111 | + output += decoder.decode(); |
| 112 | + |
| 113 | + await stream.exitCode; |
| 114 | + |
| 115 | + // Should have the value set by our bashrc |
| 116 | + expect(output.trim()).toBe(testEnvValue); |
| 117 | + }); |
| 118 | + |
| 119 | + it("should work silently when ~/.mux/bashrc doesn't exist", async () => { |
| 120 | + // Don't create bashrc - just have empty .mux dir |
| 121 | + await fs.rm(testBashrcPath, { force: true }); |
| 122 | + |
| 123 | + const runtime = new WorktreeRuntime("/tmp"); |
| 124 | + const stream = await runtime.exec("echo hello", { |
| 125 | + cwd: testWorkDir, |
| 126 | + timeout: 5, |
| 127 | + env: { HOME: testHome }, |
| 128 | + }); |
| 129 | + |
| 130 | + // Read stdout |
| 131 | + const reader = stream.stdout.getReader(); |
| 132 | + const decoder = new TextDecoder(); |
| 133 | + let output = ""; |
| 134 | + while (true) { |
| 135 | + const { done, value } = await reader.read(); |
| 136 | + if (done) break; |
| 137 | + output += decoder.decode(value, { stream: true }); |
| 138 | + } |
| 139 | + output += decoder.decode(); |
| 140 | + |
| 141 | + const exitCode = await stream.exitCode; |
| 142 | + |
| 143 | + // Command should succeed with expected output |
| 144 | + expect(exitCode).toBe(0); |
| 145 | + expect(output.trim()).toBe("hello"); |
| 146 | + }); |
| 147 | +}); |
0 commit comments