|
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 { LocalRuntime } from "./LocalRuntime"; |
5 | 6 |
|
6 | 7 | describe("LocalRuntime constructor", () => { |
@@ -30,6 +31,85 @@ describe("LocalRuntime constructor", () => { |
30 | 31 | }); |
31 | 32 | }); |
32 | 33 |
|
| 34 | +describe("LocalRuntime.exec bashrc sourcing", () => { |
| 35 | + // Use a temp directory as fake HOME with .mux/bashrc inside |
| 36 | + const testHome = path.join(os.tmpdir(), `mux-bashrc-test-${Date.now()}`); |
| 37 | + const testMuxDir = path.join(testHome, ".mux"); |
| 38 | + const testBashrcPath = path.join(testMuxDir, "bashrc"); |
| 39 | + const testWorkDir = path.join(os.tmpdir(), "mux-bashrc-workdir"); |
| 40 | + |
| 41 | + beforeEach(async () => { |
| 42 | + // Create test directories |
| 43 | + await fs.mkdir(testMuxDir, { recursive: true }); |
| 44 | + await fs.mkdir(testWorkDir, { recursive: true }); |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(async () => { |
| 48 | + // Clean up test files |
| 49 | + await fs.rm(testHome, { recursive: true, force: true }); |
| 50 | + await fs.rm(testWorkDir, { recursive: true, force: true }); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should source ~/.mux/bashrc and set environment variables", async () => { |
| 54 | + // Create a test bashrc that sets a unique environment variable |
| 55 | + const testEnvValue = `test_value_${Date.now()}`; |
| 56 | + await fs.writeFile(testBashrcPath, `export MUX_TEST_BASHRC_VAR="${testEnvValue}"\n`); |
| 57 | + |
| 58 | + const runtime = new LocalRuntime("/tmp"); |
| 59 | + // Pass HOME as environment variable so the child process uses our test home |
| 60 | + const stream = await runtime.exec("echo $MUX_TEST_BASHRC_VAR", { |
| 61 | + cwd: testWorkDir, |
| 62 | + timeout: 5, |
| 63 | + env: { HOME: testHome }, |
| 64 | + }); |
| 65 | + |
| 66 | + // Read stdout |
| 67 | + const reader = stream.stdout.getReader(); |
| 68 | + const decoder = new TextDecoder(); |
| 69 | + let output = ""; |
| 70 | + while (true) { |
| 71 | + const { done, value } = await reader.read(); |
| 72 | + if (done) break; |
| 73 | + output += decoder.decode(value, { stream: true }); |
| 74 | + } |
| 75 | + output += decoder.decode(); |
| 76 | + |
| 77 | + await stream.exitCode; |
| 78 | + |
| 79 | + // Should have the value set by our bashrc |
| 80 | + expect(output.trim()).toBe(testEnvValue); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should work silently when ~/.mux/bashrc doesn't exist", async () => { |
| 84 | + // Don't create bashrc - just have empty .mux dir |
| 85 | + await fs.rm(testBashrcPath, { force: true }); |
| 86 | + |
| 87 | + const runtime = new LocalRuntime("/tmp"); |
| 88 | + const stream = await runtime.exec("echo hello", { |
| 89 | + cwd: testWorkDir, |
| 90 | + timeout: 5, |
| 91 | + env: { HOME: testHome }, |
| 92 | + }); |
| 93 | + |
| 94 | + // Read stdout |
| 95 | + const reader = stream.stdout.getReader(); |
| 96 | + const decoder = new TextDecoder(); |
| 97 | + let output = ""; |
| 98 | + while (true) { |
| 99 | + const { done, value } = await reader.read(); |
| 100 | + if (done) break; |
| 101 | + output += decoder.decode(value, { stream: true }); |
| 102 | + } |
| 103 | + output += decoder.decode(); |
| 104 | + |
| 105 | + const exitCode = await stream.exitCode; |
| 106 | + |
| 107 | + // Command should succeed with expected output |
| 108 | + expect(exitCode).toBe(0); |
| 109 | + expect(output.trim()).toBe("hello"); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
33 | 113 | describe("LocalRuntime.resolvePath", () => { |
34 | 114 | it("should expand tilde to home directory", async () => { |
35 | 115 | const runtime = new LocalRuntime("/tmp"); |
|
0 commit comments