Files
sigil/internal/core/config_test.go
T

120 lines
2.7 KiB
Go

package core
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)
}
})
}
}