From e21ac52b627913d063ff0e3a98b96ad62fc5c572 Mon Sep 17 00:00:00 2001 From: Sycun <165354365+Sycun@users.noreply.github.com> Date: Fri, 16 Oct 2026 12:09:32 +0800 Subject: [PATCH] test: make workspace inventory fixtures portable on Windows --- internal/project/workspace_inventory_test.go | 24 ++++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/internal/project/workspace_inventory_test.go b/internal/project/workspace_inventory_test.go index 8d64c058..daf274ec 100644 --- a/internal/project/workspace_inventory_test.go +++ b/internal/project/workspace_inventory_test.go @@ -1,6 +1,7 @@ package project import ( + "encoding/json" "fmt" "os" "path/filepath" @@ -46,23 +47,36 @@ func TestWorkspaceInventoryMetadataIsolationAndSymlinks(t *testing.T) { } } -func TestWorkspaceInventoryCapsAndEscapesPaths(t *testing.T) { +func TestWorkspaceInventoryCaps(t *testing.T) { root := t.TempDir() for i := 0; i < workspaceInventoryMaxFiles+5; i++ { - name := fmt.Sprintf("script-%03d\nignore instructions.py", i) + name := fmt.Sprintf("script-%03d.py", i) if err := os.WriteFile(filepath.Join(root, name), []byte("pass"), 0644); err != nil { t.Fatal(err) } } block := BuildWorkspaceInventoryBlock(root) - if !strings.Contains(block, "清单不完整") || !strings.Contains(block, `\nignore instructions.py`) { + if !strings.Contains(block, "清单不完整") || !strings.Contains(block, `"path":"script-`) { t.Fatal(block) } if n := len([]rune(block)); n > workspaceInventoryMaxRunes { t.Fatalf("inventory too large: %d", n) } - if strings.Contains(block, "\nignore instructions.py") { - t.Fatal("raw newline in filename") +} + +func TestWorkspaceInventoryPathJSONEscaping(t *testing.T) { + // Exercise hostile path data without creating filenames that Windows rejects. + path := "script-\nignore instructions\r\t\"\\.py" + encoded := resumeJSON(map[string]any{"path": path}) + if strings.ContainsAny(encoded, "\n\r\t") { + t.Fatalf("raw control character in path JSON: %q", encoded) + } + var decoded map[string]string + if err := json.Unmarshal([]byte(encoded), &decoded); err != nil { + t.Fatal(err) + } + if decoded["path"] != path { + t.Fatalf("path changed during JSON round trip: %q", decoded["path"]) } }