Skip to content

Commit 3d379c2

Browse files
authored
fix(test): replace Unix-only assumptions with cross-platform alternatives (anomalyco#14906)
1 parent 06f25c7 commit 3d379c2

3 files changed

Lines changed: 19 additions & 10 deletions

File tree

packages/opencode/test/tool/bash.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, expect, test } from "bun:test"
2+
import os from "os"
23
import path from "path"
34
import { BashTool } from "../../src/tool/bash"
45
import { Instance } from "../../src/project/instance"
@@ -138,14 +139,14 @@ describe("tool.bash permissions", () => {
138139
await bash.execute(
139140
{
140141
command: "ls",
141-
workdir: "/tmp",
142-
description: "List /tmp",
142+
workdir: os.tmpdir(),
143+
description: "List temp dir",
143144
},
144145
testCtx,
145146
)
146147
const extDirReq = requests.find((r) => r.permission === "external_directory")
147148
expect(extDirReq).toBeDefined()
148-
expect(extDirReq!.patterns).toContain("/tmp/*")
149+
expect(extDirReq!.patterns).toContain(path.join(os.tmpdir(), "*"))
149150
},
150151
})
151152
})
@@ -366,7 +367,8 @@ describe("tool.bash truncation", () => {
366367
ctx,
367368
)
368369
expect((result.metadata as any).truncated).toBe(false)
369-
expect(result.output).toBe("hello\n")
370+
const eol = process.platform === "win32" ? "\r\n" : "\n"
371+
expect(result.output).toBe(`hello${eol}`)
370372
},
371373
})
372374
})

packages/opencode/test/tool/external-directory.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe("tool.assertExternalDirectory", () => {
6565

6666
const directory = "/tmp/project"
6767
const target = "/tmp/outside/file.txt"
68-
const expected = path.join(path.dirname(target), "*")
68+
const expected = path.join(path.dirname(target), "*").replaceAll("\\", "/")
6969

7070
await Instance.provide({
7171
directory,
@@ -91,7 +91,7 @@ describe("tool.assertExternalDirectory", () => {
9191

9292
const directory = "/tmp/project"
9393
const target = "/tmp/outside"
94-
const expected = path.join(target, "*")
94+
const expected = path.join(target, "*").replaceAll("\\", "/")
9595

9696
await Instance.provide({
9797
directory,

packages/opencode/test/tool/write.test.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,19 +293,26 @@ describe("tool.write", () => {
293293
})
294294

295295
describe("error handling", () => {
296-
test("throws error for paths outside project", async () => {
296+
test("throws error when OS denies write access", async () => {
297297
await using tmp = await tmpdir()
298-
const outsidePath = "/etc/passwd"
298+
const readonlyPath = path.join(tmp.path, "readonly.txt")
299+
300+
// Create a read-only file
301+
await fs.writeFile(readonlyPath, "test", "utf-8")
302+
await fs.chmod(readonlyPath, 0o444)
299303

300304
await Instance.provide({
301305
directory: tmp.path,
302306
fn: async () => {
307+
const { FileTime } = await import("../../src/file/time")
308+
FileTime.read(ctx.sessionID, readonlyPath)
309+
303310
const write = await WriteTool.init()
304311
await expect(
305312
write.execute(
306313
{
307-
filePath: outsidePath,
308-
content: "test",
314+
filePath: readonlyPath,
315+
content: "new content",
309316
},
310317
ctx,
311318
),

0 commit comments

Comments
 (0)