test(fs): cover root path separator normalization
This commit is contained in:
parent
46ffda264f
commit
acc2c5c6aa
2 changed files with 24 additions and 49 deletions
|
|
@ -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 == "" {
|
||||||
|
|
|
||||||
|
|
@ -1,57 +1,21 @@
|
||||||
package fstools
|
package fstools
|
||||||
|
|
||||||
import (
|
import "testing"
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSandboxFsReadDirAcceptsOSSpecificRelativePaths(t *testing.T) {
|
func TestNormalizeRootRelPathForWindowsSeparator(t *testing.T) {
|
||||||
workspace := t.TempDir()
|
got := normalizeRootRelPathForSeparator(`aaa\bbb\file.txt`, '\\')
|
||||||
if err := os.MkdirAll(filepath.Join(workspace, "aaa", "bbb", "ccc"), 0o755); err != nil {
|
want := "aaa/bbb/file.txt"
|
||||||
t.Fatalf("create nested directory: %v", err)
|
|
||||||
}
|
|
||||||
if err := os.WriteFile(filepath.Join(workspace, "aaa", "bbb", "file.txt"), []byte("hello"), 0o600); err != nil {
|
|
||||||
t.Fatalf("create nested file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fsys := &sandboxFs{workspace: workspace}
|
if got != want {
|
||||||
entries, err := fsys.ReadDir(filepath.Join("aaa", "bbb"))
|
t.Fatalf("normalizeRootRelPathForSeparator() = %q, want %q", got, want)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("ReadDir with OS-specific relative path returned error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
seen := map[string]bool{}
|
|
||||||
for _, entry := range entries {
|
|
||||||
seen[entry.Name()] = true
|
|
||||||
}
|
|
||||||
if !seen["ccc"] || !seen["file.txt"] {
|
|
||||||
t.Fatalf("ReadDir entries = %v, want ccc and file.txt", seen)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSandboxFsOpenAcceptsOSSpecificRelativePaths(t *testing.T) {
|
func TestNormalizeRootRelPathForUnixSeparatorLeavesBackslashUnchanged(t *testing.T) {
|
||||||
workspace := t.TempDir()
|
input := `aaa\bbb\file.txt`
|
||||||
if err := os.MkdirAll(filepath.Join(workspace, "aaa", "bbb"), 0o755); err != nil {
|
got := normalizeRootRelPathForSeparator(input, '/')
|
||||||
t.Fatalf("create nested directory: %v", err)
|
|
||||||
}
|
|
||||||
if err := os.WriteFile(filepath.Join(workspace, "aaa", "bbb", "file.txt"), []byte("hello"), 0o600); err != nil {
|
|
||||||
t.Fatalf("create nested file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fsys := &sandboxFs{workspace: workspace}
|
if got != input {
|
||||||
file, err := fsys.Open(filepath.Join("aaa", "bbb", "file.txt"))
|
t.Fatalf("normalizeRootRelPathForSeparator() = %q, want %q", got, input)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Open with OS-specific relative path returned error: %v", err)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
data, err := io.ReadAll(file)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("read opened file: %v", err)
|
|
||||||
}
|
|
||||||
if string(data) != "hello" {
|
|
||||||
t.Fatalf("file content = %q, want %q", string(data), "hello")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue