mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-07-28 01:27:58 +00:00
Merge pull request #3158 from danmobot/fix/sandbox-fs-windows-paths
test: cover sandbox fs Windows path handling
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"maps"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
@@ -1064,8 +1064,8 @@ func (r *sandboxFs) execute(path string, fn func(root *os.Root, relPath string)
|
||||
return err
|
||||
}
|
||||
|
||||
// os.Root api on windows only accept forward slashes (/)
|
||||
relPath = filepath.ToSlash(relPath)
|
||||
// os.Root API on Windows only accepts forward slashes (/).
|
||||
relPath = normalizeRootRelPath(relPath)
|
||||
|
||||
return fn(root, relPath)
|
||||
}
|
||||
@@ -1230,6 +1230,17 @@ func buildFs(workspace string, restrict bool, patterns []*regexp.Regexp) fileSys
|
||||
return sandbox
|
||||
}
|
||||
|
||||
func normalizeRootRelPath(relPath string) string {
|
||||
return normalizeRootRelPathForSeparator(relPath, os.PathSeparator)
|
||||
}
|
||||
|
||||
func normalizeRootRelPathForSeparator(relPath string, sep rune) string {
|
||||
if sep == '\\' {
|
||||
return strings.ReplaceAll(relPath, `\`, `/`)
|
||||
}
|
||||
return relPath
|
||||
}
|
||||
|
||||
// Helper to get a safe relative path for os.Root usage
|
||||
func getSafeRelPath(workspace, path string) (string, error) {
|
||||
if workspace == "" {
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package fstools
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNormalizeRootRelPathForWindowsSeparator(t *testing.T) {
|
||||
got := normalizeRootRelPathForSeparator(`aaa\bbb\file.txt`, '\\')
|
||||
want := "aaa/bbb/file.txt"
|
||||
|
||||
if got != want {
|
||||
t.Fatalf("normalizeRootRelPathForSeparator() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeRootRelPathForUnixSeparatorLeavesBackslashUnchanged(t *testing.T) {
|
||||
input := `aaa\bbb\file.txt`
|
||||
got := normalizeRootRelPathForSeparator(input, '/')
|
||||
|
||||
if got != input {
|
||||
t.Fatalf("normalizeRootRelPathForSeparator() = %q, want %q", got, input)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user