Przeglądaj źródła

fix(test): replace Unix-only assumptions with cross-platform alternatives (#14906)

Luke Parker 1 miesiąc temu
rodzic
commit
3d379c20c4

+ 6 - 4
packages/opencode/test/tool/bash.test.ts

@@ -1,4 +1,5 @@
 import { describe, expect, test } from "bun:test"
+import os from "os"
 import path from "path"
 import { BashTool } from "../../src/tool/bash"
 import { Instance } from "../../src/project/instance"
@@ -138,14 +139,14 @@ describe("tool.bash permissions", () => {
         await bash.execute(
           {
             command: "ls",
-            workdir: "/tmp",
-            description: "List /tmp",
+            workdir: os.tmpdir(),
+            description: "List temp dir",
           },
           testCtx,
         )
         const extDirReq = requests.find((r) => r.permission === "external_directory")
         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,
         )
         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}`)
       },
     })
   })

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

@@ -65,7 +65,7 @@ describe("tool.assertExternalDirectory", () => {
 
     const directory = "/tmp/project"
     const target = "/tmp/outside/file.txt"
-    const expected = path.join(path.dirname(target), "*")
+    const expected = path.join(path.dirname(target), "*").replaceAll("\\", "/")
 
     await Instance.provide({
       directory,
@@ -91,7 +91,7 @@ describe("tool.assertExternalDirectory", () => {
 
     const directory = "/tmp/project"
     const target = "/tmp/outside"
-    const expected = path.join(target, "*")
+    const expected = path.join(target, "*").replaceAll("\\", "/")
 
     await Instance.provide({
       directory,

+ 11 - 4
packages/opencode/test/tool/write.test.ts

@@ -293,19 +293,26 @@ describe("tool.write", () => {
   })
 
   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()
-      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({
         directory: tmp.path,
         fn: async () => {
+          const { FileTime } = await import("../../src/file/time")
+          FileTime.read(ctx.sessionID, readonlyPath)
+
           const write = await WriteTool.init()
           await expect(
             write.execute(
               {
-                filePath: outsidePath,
-                content: "test",
+                filePath: readonlyPath,
+                content: "new content",
               },
               ctx,
             ),