Add tests for glob butchering

This commit is contained in:
2025-04-01 12:09:29 +02:00
parent d21e20d387
commit 2b973be0c1

View File

@@ -3,6 +3,7 @@ package utils
import ( import (
"os" "os"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@@ -1269,3 +1270,164 @@ func TestLoadCommandsFromCookFilesNoYamlFiles(t *testing.T) {
// t.Errorf("Expected 0 commands, got: %d", len(commands)) // t.Errorf("Expected 0 commands, got: %d", len(commands))
// } // }
// } // }
// Absolute path without wildcards returns correct root and pattern
func TestFigureOutGlobRootWithAbsolutePathNoWildcards(t *testing.T) {
// Setup
absPath := filepath.Join("C:\\Users", "test", "documents", "file.txt")
// Execute
root, pattern, err := FigureOutGlobRoot(absPath)
// Assert
assert.NoError(t, err)
expectedRoot := filepath.Clean(filepath.Join("C:\\Users", "test", "documents"))
assert.Equal(t, expectedRoot, root)
assert.Equal(t, "file.txt", pattern)
}
// Empty input pattern handling - we expect our "Patter", such as it is, to be a folder
func TestFigureOutGlobRootWithEmptyPattern(t *testing.T) {
// Setup
emptyPattern := ""
// Execute
root, pattern, err := FigureOutGlobRoot(emptyPattern)
// Assert
assert.NoError(t, err)
cwd, _ := os.Getwd()
cwdParts := strings.Split(filepath.Clean(cwd), "\\")
expectedRoot := strings.Join(cwdParts[:len(cwdParts)-1], "\\")
expectedPattern := cwdParts[len(cwdParts)-1]
assert.Equal(t, expectedRoot, root)
assert.Equal(t, expectedPattern, pattern)
}
// Relative path is correctly joined with current working directory
func TestFigureOutGlobRootWithRelativePath(t *testing.T) {
// Setup
cwd, err := os.Getwd()
assert.NoError(t, err)
relPath := "test/documents/file.txt"
// Execute
root, pattern, err := FigureOutGlobRoot(relPath)
// Assert
assert.NoError(t, err)
expectedRoot := filepath.Clean(filepath.Join(cwd, "test/documents"))
assert.Equal(t, expectedRoot, root)
assert.Equal(t, "file.txt", pattern)
}
// Path with wildcards correctly identifies the last non-wildcard directory as root
func TestFigureOutGlobRootWithWildcards(t *testing.T) {
// Setup
inputPattern := filepath.Join("C:\\Users", "test", "documents", "*", "file.txt")
// Execute
root, pattern, err := FigureOutGlobRoot(inputPattern)
// Assert
assert.NoError(t, err)
expectedRoot := filepath.Clean(filepath.Join("C:\\Users", "test", "documents"))
assert.Equal(t, expectedRoot, root)
assert.Equal(t, "*/file.txt", pattern)
}
// Windows-style paths are properly handled and converted
func TestFigureOutGlobRootWithRelativePathAndWildcards(t *testing.T) {
// Setup
inputPattern := "documents\\*\\file?.txt"
cwd, _ := os.Getwd()
// Execute
root, pattern, err := FigureOutGlobRoot(inputPattern)
// Assert
assert.NoError(t, err)
expectedRoot := filepath.Clean(filepath.Join(cwd, "documents"))
assert.Equal(t, expectedRoot, root)
assert.Equal(t, "*/file?.txt", pattern)
}
// Path with only wildcards (e.g., "*" or "**")
func TestFigureOutGlobRootWithOnlyWildcards(t *testing.T) {
// Setup
wildcardPattern := "*"
// Execute
root, pattern, err := FigureOutGlobRoot(wildcardPattern)
// Assert
assert.NoError(t, err)
expectedRoot, _ := os.Getwd()
assert.Equal(t, expectedRoot, root)
assert.Equal(t, "*", pattern)
}
// Multiple path segments are correctly processed and joined
func TestFigureOutGlobRootWithRelativePathAndWildcards2(t *testing.T) {
// Setup
cwd, err := os.Getwd()
assert.NoError(t, err)
relPath := filepath.Join("test", "data", "*", "file?.txt")
// Execute
root, pattern, err := FigureOutGlobRoot(relPath)
// Assert
assert.NoError(t, err)
expectedRoot := filepath.Clean(filepath.Join(cwd, "test", "data"))
assert.Equal(t, expectedRoot, root)
assert.Equal(t, "*/file?.txt", pattern)
}
// Path with mixed forward and backward slashes
func TestFigureOutGlobRootWithMixedSlashes(t *testing.T) {
// Setup
mixedPath := "C:\\Users/test\\documents\\file.txt"
// Execute
root, pattern, err := FigureOutGlobRoot(mixedPath)
// Assert
assert.NoError(t, err)
expectedRoot := filepath.Clean(filepath.Join("C:\\Users", "test", "documents"))
assert.Equal(t, expectedRoot, root)
assert.Equal(t, "file.txt", pattern)
}
// Path with wildcards in the first segment
func TestFigureOutGlobRootWithWildcardInFirstSegment(t *testing.T) {
// Setup
inputPattern := "*\\Users\\test\\documents\\file.txt"
cwd, _ := os.Getwd()
// Execute
root, pattern, err := FigureOutGlobRoot(inputPattern)
// Assert
assert.NoError(t, err)
expectedRoot := filepath.Clean(cwd)
assert.Equal(t, expectedRoot, root)
assert.Equal(t, "*/Users/test/documents/file.txt", pattern)
}
// Handling of relative paths with ".." or "." components
func TestFigureOutGlobRootWithRelativePathAndDotComponents(t *testing.T) {
// Setup
cwd, err := os.Getwd()
assert.NoError(t, err)
relPath := filepath.Join("..", ".", "test", "documents", "file.txt")
// Execute
root, pattern, err := FigureOutGlobRoot(relPath)
// Assert
assert.NoError(t, err)
expectedRoot := filepath.Clean(filepath.Join(cwd, "..", "test", "documents"))
assert.Equal(t, expectedRoot, root)
assert.Equal(t, "file.txt", pattern)
}