add tests for path and config functions
This commit is contained in:
+119
@@ -0,0 +1,119 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGlobToRegexp(t *testing.T) {
|
||||
tests := []struct {
|
||||
pattern string
|
||||
matches []string
|
||||
noMatch []string
|
||||
}{
|
||||
{
|
||||
pattern: "*.txt",
|
||||
matches: []string{"file.txt", "foo.txt"},
|
||||
noMatch: []string{"file.log", "dir/file.txt", "file.txt.bak"},
|
||||
},
|
||||
{
|
||||
pattern: "**/*.txt",
|
||||
matches: []string{"dir/file.txt", "a/b/c/file.txt"},
|
||||
noMatch: []string{"file.txt", "file.log", "file.txt.bak"},
|
||||
},
|
||||
{
|
||||
pattern: "cache/**",
|
||||
matches: []string{"cache/file", "cache/a/b/c"},
|
||||
noMatch: []string{"my-cache/file", "cache"},
|
||||
},
|
||||
{
|
||||
pattern: "?.log",
|
||||
matches: []string{"a.log", "1.log"},
|
||||
noMatch: []string{"file.log", ".log", "ab.log"},
|
||||
},
|
||||
{
|
||||
pattern: "**/.DS_Store",
|
||||
matches: []string{"foo/.DS_Store", "a/b/.DS_Store"},
|
||||
noMatch: []string{".DS_Store", ".DS_Store.bak", "foo/.DS_Store.txt"},
|
||||
},
|
||||
{
|
||||
pattern: "./file.txt",
|
||||
matches: []string{"file.txt"},
|
||||
noMatch: []string{"./file.txt"}, // leading ./ is stripped
|
||||
},
|
||||
{
|
||||
pattern: "/absolute",
|
||||
matches: []string{"absolute"},
|
||||
noMatch: []string{"/absolute"}, // leading / is stripped
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.pattern, func(t *testing.T) {
|
||||
re, err := globToRegexp(tt.pattern)
|
||||
if err != nil {
|
||||
t.Fatalf("globToRegexp(%q) error: %v", tt.pattern, err)
|
||||
}
|
||||
|
||||
for _, m := range tt.matches {
|
||||
if !re.MatchString(m) {
|
||||
t.Errorf("pattern %q should match %q, but didn't", tt.pattern, m)
|
||||
}
|
||||
}
|
||||
|
||||
for _, m := range tt.noMatch {
|
||||
if re.MatchString(m) {
|
||||
t.Errorf("pattern %q should NOT match %q, but did", tt.pattern, m)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestShouldIgnorePath(t *testing.T) {
|
||||
cfg := &packageConfig{
|
||||
compiledIgnores: []*regexp.Regexp{},
|
||||
}
|
||||
|
||||
// Test with nil config
|
||||
if shouldIgnorePath("foo.txt", nil) {
|
||||
t.Error("shouldIgnorePath with nil config should return false")
|
||||
}
|
||||
|
||||
// Test with empty ignores
|
||||
if shouldIgnorePath("foo.txt", cfg) {
|
||||
t.Error("shouldIgnorePath with empty ignores should return false")
|
||||
}
|
||||
|
||||
// Add some patterns and re-test
|
||||
patterns := []string{"*.log", "cache/**", "**/.DS_Store"}
|
||||
compiled, err := compileIgnorePatterns(patterns)
|
||||
if err != nil {
|
||||
t.Fatalf("compileIgnorePatterns error: %v", err)
|
||||
}
|
||||
cfg.compiledIgnores = compiled
|
||||
|
||||
tests := []struct {
|
||||
path string
|
||||
ignored bool
|
||||
}{
|
||||
{"debug.log", true},
|
||||
{"app.log", true},
|
||||
{"file.txt", false},
|
||||
{"cache/foo", true},
|
||||
{"cache/a/b", true},
|
||||
{"my-cache/foo", false},
|
||||
{".DS_Store", false}, // root-level .DS_Store not matched by **/.DS_Store
|
||||
{"foo/.DS_Store", true},
|
||||
{"main.go", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.path, func(t *testing.T) {
|
||||
got := shouldIgnorePath(tt.path, cfg)
|
||||
if got != tt.ignored {
|
||||
t.Errorf("shouldIgnorePath(%q) = %v, want %v", tt.path, got, tt.ignored)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user