From 2b973be0c13bc66f2c74916ef1ba14f1df93affa Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Tue, 1 Apr 2025 12:09:29 +0200 Subject: [PATCH] Add tests for glob butchering --- utils/modifycommand_test.go | 162 ++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) diff --git a/utils/modifycommand_test.go b/utils/modifycommand_test.go index 4cba676..d55aab7 100644 --- a/utils/modifycommand_test.go +++ b/utils/modifycommand_test.go @@ -3,6 +3,7 @@ package utils import ( "os" "path/filepath" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -1269,3 +1270,164 @@ func TestLoadCommandsFromCookFilesNoYamlFiles(t *testing.T) { // 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) +}