Merge pull request #3158 from danmobot/fix/sandbox-fs-windows-paths
test: cover sandbox fs Windows path handling
This commit is contained in:
commit
79ae6bf97f
3 changed files with 35 additions and 2 deletions
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"maps"
|
"maps"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
|
||||||
|
|
@ -1064,8 +1064,8 @@ func (r *sandboxFs) execute(path string, fn func(root *os.Root, relPath string)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// os.Root api on windows only accept forward slashes (/)
|
// os.Root API on Windows only accepts forward slashes (/).
|
||||||
relPath = filepath.ToSlash(relPath)
|
relPath = normalizeRootRelPath(relPath)
|
||||||
|
|
||||||
return fn(root, relPath)
|
return fn(root, relPath)
|
||||||
}
|
}
|
||||||
|
|
@ -1230,6 +1230,17 @@ func buildFs(workspace string, restrict bool, patterns []*regexp.Regexp) fileSys
|
||||||
return sandbox
|
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
|
// Helper to get a safe relative path for os.Root usage
|
||||||
func getSafeRelPath(workspace, path string) (string, error) {
|
func getSafeRelPath(workspace, path string) (string, error) {
|
||||||
if workspace == "" {
|
if workspace == "" {
|
||||||
|
|
|
||||||
21
pkg/tools/fs/filesystem_windows_path_test.go
Normal file
21
pkg/tools/fs/filesystem_windows_path_test.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue