Rework input args to be glob instead of files
So we don't have to do the whole find | shit
This commit is contained in:
89
glob_test.go
Normal file
89
glob_test.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGlobExpansion(t *testing.T) {
|
||||
// Create a temporary directory structure for testing
|
||||
tmpDir, err := os.MkdirTemp("", "glob-test")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp dir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
// Create some test files
|
||||
testFiles := []string{
|
||||
filepath.Join(tmpDir, "file1.xml"),
|
||||
filepath.Join(tmpDir, "file2.xml"),
|
||||
filepath.Join(tmpDir, "test.txt"),
|
||||
filepath.Join(tmpDir, "subdir", "file3.xml"),
|
||||
filepath.Join(tmpDir, "subdir", "file4.txt"),
|
||||
filepath.Join(tmpDir, "subdir", "nested", "file5.xml"),
|
||||
}
|
||||
|
||||
// Create the directory structure
|
||||
err = os.MkdirAll(filepath.Join(tmpDir, "subdir", "nested"), 0755)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create subdirectories: %v", err)
|
||||
}
|
||||
|
||||
// Create the files
|
||||
for _, file := range testFiles {
|
||||
dir := filepath.Dir(file)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
t.Fatalf("Failed to create directory %s: %v", dir, err)
|
||||
}
|
||||
if err := os.WriteFile(file, []byte("test content"), 0644); err != nil {
|
||||
t.Fatalf("Failed to create file %s: %v", file, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Change to the temporary directory to use relative paths
|
||||
origDir, _ := os.Getwd()
|
||||
os.Chdir(tmpDir)
|
||||
defer os.Chdir(origDir)
|
||||
|
||||
// Test cases
|
||||
tests := []struct {
|
||||
name string
|
||||
patterns []string
|
||||
expected int
|
||||
}{
|
||||
{
|
||||
name: "Simple pattern",
|
||||
patterns: []string{"*.xml"},
|
||||
expected: 2, // file1.xml, file2.xml
|
||||
},
|
||||
{
|
||||
name: "Multiple patterns",
|
||||
patterns: []string{"*.xml", "*.txt"},
|
||||
expected: 3, // file1.xml, file2.xml, test.txt
|
||||
},
|
||||
{
|
||||
name: "Directory globbing",
|
||||
patterns: []string{"subdir/*.xml"},
|
||||
expected: 1, // subdir/file3.xml
|
||||
},
|
||||
{
|
||||
name: "Doublestar pattern",
|
||||
patterns: []string{"**/*.xml"},
|
||||
expected: 4, // All XML files in all directories
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
files, err := expandFilePatterns(tc.patterns)
|
||||
if err != nil {
|
||||
t.Fatalf("expandFilePatterns failed: %v", err)
|
||||
}
|
||||
|
||||
if len(files) != tc.expected {
|
||||
t.Errorf("Expected %d files, got %d: %v", tc.expected, len(files), files)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user