fix: path resolution for outside dirs to throw in miro test
This commit is contained in:
+1
-3
@@ -1,8 +1,6 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"miro/internal/miro"
|
"miro/internal/miro"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@@ -15,7 +13,7 @@ func newTestCommand() *cobra.Command {
|
|||||||
RunE: func(_ *cobra.Command, args []string) error {
|
RunE: func(_ *cobra.Command, args []string) error {
|
||||||
path := ""
|
path := ""
|
||||||
if len(args) == 1 {
|
if len(args) == 1 {
|
||||||
path = filepath.Clean(args[0])
|
path = args[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
return miro.RunTests(path)
|
return miro.RunTests(path)
|
||||||
|
|||||||
@@ -17,23 +17,41 @@ func resolvePathWithinTestDir(testDir, path, pathType string) (string, error) {
|
|||||||
return "", fmt.Errorf("failed to resolve test directory path: %v", err)
|
return "", fmt.Errorf("failed to resolve test directory path: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ensureWithinTestDir := func(candidate string) (string, bool) {
|
||||||
|
relToTestDir, relErr := filepath.Rel(absTestDir, candidate)
|
||||||
|
if relErr != nil || !isWithinBase(relToTestDir) {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
return candidate, true
|
||||||
|
}
|
||||||
|
|
||||||
|
if filepath.IsAbs(path) {
|
||||||
|
absPath := filepath.Clean(path)
|
||||||
|
if candidate, ok := ensureWithinTestDir(absPath); ok {
|
||||||
|
return candidate, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", fmt.Errorf("%s path %q must be inside test directory %q", pathType, path, absTestDir)
|
||||||
|
}
|
||||||
|
|
||||||
absPath, err := filepath.Abs(path)
|
absPath, err := filepath.Abs(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to resolve %s path: %v", pathType, err)
|
return "", fmt.Errorf("failed to resolve %s path: %v", pathType, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
relToTestDir, err := filepath.Rel(absTestDir, absPath)
|
if candidate, ok := ensureWithinTestDir(absPath); ok {
|
||||||
if err == nil && isWithinBase(relToTestDir) {
|
return candidate, nil
|
||||||
return filepath.Join(absTestDir, relToTestDir), nil
|
}
|
||||||
|
|
||||||
|
candidate := filepath.Clean(filepath.Join(absTestDir, path))
|
||||||
|
if candidate, ok := ensureWithinTestDir(candidate); ok {
|
||||||
|
return candidate, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if filepath.IsAbs(path) {
|
|
||||||
return "", fmt.Errorf("%s path %q must be inside test directory %q", pathType, path, absTestDir)
|
return "", fmt.Errorf("%s path %q must be inside test directory %q", pathType, path, absTestDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
return filepath.Join(absTestDir, path), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func isWithinBase(rel string) bool {
|
func isWithinBase(rel string) bool {
|
||||||
return rel == "." || (rel != ".." && !strings.HasPrefix(rel, ".."+string(os.PathSeparator)))
|
return rel == "." || (rel != ".." && !strings.HasPrefix(rel, ".."+string(os.PathSeparator)))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,6 +73,19 @@ func TestResolvePathWithinTestDirRejectsAbsolutePathOutsideTestDir(t *testing.T)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestResolvePathWithinTestDirRejectsRelativePathOutsideTestDir(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
testDir := filepath.Join(root, "e2e")
|
||||||
|
|
||||||
|
_, err := resolvePathWithinTestDir(testDir, filepath.Join("..", "outside"), "record")
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("resolvePathWithinTestDir() error = nil, want error")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "must be inside test directory") {
|
||||||
|
t.Fatalf("resolvePathWithinTestDir() error = %q, want inside test directory error", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestResolvePathWithinTestDirAllowsPathCurrentDirectory(t *testing.T) {
|
func TestResolvePathWithinTestDirAllowsPathCurrentDirectory(t *testing.T) {
|
||||||
root := t.TempDir()
|
root := t.TempDir()
|
||||||
testDir := filepath.Join(root, "e2e")
|
testDir := filepath.Join(root, "e2e")
|
||||||
|
|||||||
Reference in New Issue
Block a user