|
1 | | -import { describe, expect, test } from "bun:test" |
| 1 | +import { afterEach, describe, expect, test } from "bun:test" |
| 2 | +import fs from "fs/promises" |
| 3 | +import path from "path" |
2 | 4 | import { Bus } from "../../src/bus" |
3 | 5 | import { Instance } from "../../src/project/instance" |
4 | 6 | import { Pty } from "../../src/pty" |
5 | 7 | import type { PtyID } from "../../src/pty/schema" |
6 | 8 | import { tmpdir } from "../fixture/fixture" |
7 | 9 | import { setTimeout as sleep } from "node:timers/promises" |
8 | 10 |
|
| 11 | +const env = { |
| 12 | + HOME: process.env.HOME, |
| 13 | +} |
| 14 | + |
| 15 | +afterEach(() => { |
| 16 | + if (env.HOME === undefined) delete process.env.HOME |
| 17 | + else process.env.HOME = env.HOME |
| 18 | +}) |
| 19 | + |
9 | 20 | const wait = async (fn: () => boolean, ms = 5000) => { |
10 | 21 | const end = Date.now() + ms |
11 | 22 | while (Date.now() < end) { |
@@ -129,6 +140,43 @@ describe("pty", () => { |
129 | 140 | }) |
130 | 141 | }) |
131 | 142 |
|
| 143 | + test("keeps pty shell startup deterministic in sandbox mode", async () => { |
| 144 | + if (process.platform !== "darwin") return |
| 145 | + |
| 146 | + await using home = await tmpdir({ |
| 147 | + init: async (dir) => { |
| 148 | + await Bun.write(path.join(dir, ".bashrc"), 'printf hit > "$HOME/bashrc-hit"\n') |
| 149 | + }, |
| 150 | + }) |
| 151 | + await using dir = await tmpdir({ |
| 152 | + config: { |
| 153 | + experimental: { |
| 154 | + sandbox: { |
| 155 | + enabled: true, |
| 156 | + }, |
| 157 | + }, |
| 158 | + }, |
| 159 | + }) |
| 160 | + process.env.HOME = home.path |
| 161 | + |
| 162 | + await Instance.provide({ |
| 163 | + directory: dir.path, |
| 164 | + fn: async () => { |
| 165 | + const info = await Pty.create({ command: "/bin/bash", title: "bash" }) |
| 166 | + try { |
| 167 | + await sleep(150) |
| 168 | + const hit = await fs |
| 169 | + .access(path.join(home.path, "bashrc-hit")) |
| 170 | + .then(() => true) |
| 171 | + .catch(() => false) |
| 172 | + expect(hit).toBe(false) |
| 173 | + } finally { |
| 174 | + await Pty.remove(info.id) |
| 175 | + } |
| 176 | + }, |
| 177 | + }) |
| 178 | + }) |
| 179 | + |
132 | 180 | test("blocks excluded commands on initial pty spawn", async () => { |
133 | 181 | await using dir = await tmpdir({ |
134 | 182 | config: { |
|
0 commit comments