62159
Sy
symb / files
symb / internal / filesearch / gitignore_test.go
2.0 KB
1package filesearch 2 3import "testing" 4 5func TestGitignoreMatcher(t *testing.T) { 6 tests := []struct { 7 pattern string 8 path string 9 isDir bool 10 want bool 11 }{ 12 // Simple patterns 13 {"*.log", "test.log", false, true}, 14 {"*.log", "test.txt", false, false}, 15 {"*.log", "logs/test.log", false, true}, 16 17 // Directory patterns 18 {"node_modules/", "node_modules", true, true}, 19 {"node_modules/", "node_modules/package.json", false, true}, 20 {"node_modules/", "src/node_modules", true, true}, 21 22 // Wildcard patterns 23 {"build/*", "build/output.txt", false, true}, 24 {"build/*", "build", true, false}, 25 {"build/*", "src/build/output.txt", false, true}, 26 27 // Negation patterns 28 {"!important.log", "important.log", false, false}, 29 30 // Double asterisk 31 {"**/temp", "temp", false, true}, 32 {"**/temp", "src/temp", false, true}, 33 {"**/temp", "src/lib/temp", false, true}, 34 35 // Leading slash (root-only) 36 {"/root.txt", "root.txt", false, true}, 37 {"/root.txt", "src/root.txt", false, false}, 38 } 39 40 for _, tt := range tests { 41 pattern := parseGitignorePattern(tt.pattern) 42 if pattern == nil { 43 t.Errorf("failed to parse pattern: %s", tt.pattern) 44 continue 45 } 46 47 matcher := &GitignoreMatcher{patterns: []*gitignorePattern{pattern}} 48 got := matcher.Matches(tt.path, tt.isDir) 49 50 if got != tt.want { 51 t.Errorf("pattern %q, path %q (isDir=%v): got %v, want %v", 52 tt.pattern, tt.path, tt.isDir, got, tt.want) 53 } 54 } 55} 56 57func TestGitignoreMultiplePatterns(t *testing.T) { 58 patterns := []string{ 59 "*.log", 60 "!important.log", 61 } 62 63 matcher := &GitignoreMatcher{} 64 for _, p := range patterns { 65 pattern := parseGitignorePattern(p) 66 if pattern != nil { 67 matcher.patterns = append(matcher.patterns, pattern) 68 } 69 } 70 71 tests := []struct { 72 path string 73 want bool 74 }{ 75 {"test.log", true}, // Ignored by *.log 76 {"important.log", false}, // Un-ignored by !important.log 77 {"other.txt", false}, // Not matched 78 } 79 80 for _, tt := range tests { 81 got := matcher.Matches(tt.path, false) 82 if got != tt.want { 83 t.Errorf("path %q: got %v, want %v", tt.path, got, tt.want) 84 } 85 } 86}