fix(test): replace Unix-only assumptions with cross-platform alternatives (#14906)
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import { describe, expect, test } from "bun:test"
|
import { describe, expect, test } from "bun:test"
|
||||||
|
import os from "os"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
import { BashTool } from "../../src/tool/bash"
|
import { BashTool } from "../../src/tool/bash"
|
||||||
import { Instance } from "../../src/project/instance"
|
import { Instance } from "../../src/project/instance"
|
||||||
@@ -138,14 +139,14 @@ describe("tool.bash permissions", () => {
|
|||||||
await bash.execute(
|
await bash.execute(
|
||||||
{
|
{
|
||||||
command: "ls",
|
command: "ls",
|
||||||
workdir: "/tmp",
|
workdir: os.tmpdir(),
|
||||||
description: "List /tmp",
|
description: "List temp dir",
|
||||||
},
|
},
|
||||||
testCtx,
|
testCtx,
|
||||||
)
|
)
|
||||||
const extDirReq = requests.find((r) => r.permission === "external_directory")
|
const extDirReq = requests.find((r) => r.permission === "external_directory")
|
||||||
expect(extDirReq).toBeDefined()
|
expect(extDirReq).toBeDefined()
|
||||||
expect(extDirReq!.patterns).toContain("/tmp/*")
|
expect(extDirReq!.patterns).toContain(path.join(os.tmpdir(), "*"))
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -366,7 +367,8 @@ describe("tool.bash truncation", () => {
|
|||||||
ctx,
|
ctx,
|
||||||
)
|
)
|
||||||
expect((result.metadata as any).truncated).toBe(false)
|
expect((result.metadata as any).truncated).toBe(false)
|
||||||
expect(result.output).toBe("hello\n")
|
const eol = process.platform === "win32" ? "\r\n" : "\n"
|
||||||
|
expect(result.output).toBe(`hello${eol}`)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ describe("tool.assertExternalDirectory", () => {
|
|||||||
|
|
||||||
const directory = "/tmp/project"
|
const directory = "/tmp/project"
|
||||||
const target = "/tmp/outside/file.txt"
|
const target = "/tmp/outside/file.txt"
|
||||||
const expected = path.join(path.dirname(target), "*")
|
const expected = path.join(path.dirname(target), "*").replaceAll("\\", "/")
|
||||||
|
|
||||||
await Instance.provide({
|
await Instance.provide({
|
||||||
directory,
|
directory,
|
||||||
@@ -91,7 +91,7 @@ describe("tool.assertExternalDirectory", () => {
|
|||||||
|
|
||||||
const directory = "/tmp/project"
|
const directory = "/tmp/project"
|
||||||
const target = "/tmp/outside"
|
const target = "/tmp/outside"
|
||||||
const expected = path.join(target, "*")
|
const expected = path.join(target, "*").replaceAll("\\", "/")
|
||||||
|
|
||||||
await Instance.provide({
|
await Instance.provide({
|
||||||
directory,
|
directory,
|
||||||
|
|||||||
@@ -293,19 +293,26 @@ describe("tool.write", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe("error handling", () => {
|
describe("error handling", () => {
|
||||||
test("throws error for paths outside project", async () => {
|
test("throws error when OS denies write access", async () => {
|
||||||
await using tmp = await tmpdir()
|
await using tmp = await tmpdir()
|
||||||
const outsidePath = "/etc/passwd"
|
const readonlyPath = path.join(tmp.path, "readonly.txt")
|
||||||
|
|
||||||
|
// Create a read-only file
|
||||||
|
await fs.writeFile(readonlyPath, "test", "utf-8")
|
||||||
|
await fs.chmod(readonlyPath, 0o444)
|
||||||
|
|
||||||
await Instance.provide({
|
await Instance.provide({
|
||||||
directory: tmp.path,
|
directory: tmp.path,
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
|
const { FileTime } = await import("../../src/file/time")
|
||||||
|
FileTime.read(ctx.sessionID, readonlyPath)
|
||||||
|
|
||||||
const write = await WriteTool.init()
|
const write = await WriteTool.init()
|
||||||
await expect(
|
await expect(
|
||||||
write.execute(
|
write.execute(
|
||||||
{
|
{
|
||||||
filePath: outsidePath,
|
filePath: readonlyPath,
|
||||||
content: "test",
|
content: "new content",
|
||||||
},
|
},
|
||||||
ctx,
|
ctx,
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in New Issue
Block a user