Skip to content

Commit 52a51f8

Browse files
test(diff): add tests for gitignore pattern matching and mode getters
1 parent be470bb commit 52a51f8

1 file changed

Lines changed: 131 additions & 0 deletions

File tree

internal/diff/gitignore_test.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package diff
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
)
8+
9+
func TestExcludedDirs(t *testing.T) {
10+
dirs := ExcludedDirs()
11+
if len(dirs) == 0 {
12+
t.Fatal("ExcludedDirs should return non-empty list")
13+
}
14+
found := false
15+
for _, d := range dirs {
16+
if d == ".git/" {
17+
found = true
18+
break
19+
}
20+
}
21+
if !found {
22+
t.Error("ExcludedDirs should include .git/")
23+
}
24+
25+
dirs2 := ExcludedDirs()
26+
dirs[0] = "MUTATED"
27+
if dirs2[0] == "MUTATED" {
28+
t.Error("ExcludedDirs should return a copy, not the original slice")
29+
}
30+
}
31+
32+
func TestLoadGitignorePatterns(t *testing.T) {
33+
t.Run("valid gitignore", func(t *testing.T) {
34+
dir := t.TempDir()
35+
content := "*.log\n# comment\n\nnode_modules/\n*.tmp\n"
36+
if err := os.WriteFile(filepath.Join(dir, ".gitignore"), []byte(content), 0644); err != nil {
37+
t.Fatal(err)
38+
}
39+
patterns := LoadGitignorePatterns(dir)
40+
want := []string{"*.log", "node_modules/", "*.tmp"}
41+
if len(patterns) != len(want) {
42+
t.Fatalf("got %d patterns %v, want %d %v", len(patterns), patterns, len(want), want)
43+
}
44+
for i := range want {
45+
if patterns[i] != want[i] {
46+
t.Errorf("patterns[%d] = %q, want %q", i, patterns[i], want[i])
47+
}
48+
}
49+
})
50+
51+
t.Run("missing gitignore", func(t *testing.T) {
52+
dir := t.TempDir()
53+
patterns := LoadGitignorePatterns(dir)
54+
if patterns != nil {
55+
t.Errorf("expected nil for missing .gitignore, got %v", patterns)
56+
}
57+
})
58+
}
59+
60+
func TestIsPathExcluded(t *testing.T) {
61+
tests := []struct {
62+
name string
63+
relPath string
64+
patterns []string
65+
want bool
66+
}{
67+
{"hardcoded dir .git", ".git", nil, true},
68+
{"hardcoded dir prefix", ".git/config", nil, true},
69+
{"node_modules dir pattern", "node_modules/foo.js", []string{"node_modules/"}, true},
70+
{"gitignore pattern match", "debug.log", []string{"*.log"}, true},
71+
{"no match", "main.go", []string{"*.log"}, false},
72+
{"no patterns", "main.go", nil, false},
73+
}
74+
for _, tt := range tests {
75+
t.Run(tt.name, func(t *testing.T) {
76+
got := IsPathExcluded(".", tt.relPath, tt.patterns)
77+
if got != tt.want {
78+
t.Errorf("IsPathExcluded(%q, %v) = %v, want %v", tt.relPath, tt.patterns, got, tt.want)
79+
}
80+
})
81+
}
82+
}
83+
84+
func TestMatchGitignorePattern(t *testing.T) {
85+
tests := []struct {
86+
name string
87+
relPath string
88+
pattern string
89+
want bool
90+
}{
91+
{"basename glob match", "src/debug.log", "*.log", true},
92+
{"basename glob no match", "src/main.go", "*.log", false},
93+
{"directory pattern", "vendor/pkg/file.go", "vendor/", true},
94+
{"directory pattern nested", "a/vendor/b", "vendor/", true},
95+
{"directory pattern no match", "vendor_extra/file.go", "vendor/", false},
96+
{"full path glob", "docs/api.md", "docs/*.md", true},
97+
{"full path no match", "src/api.md", "docs/*.md", false},
98+
{"negation pattern", "important.log", "!important.log", false},
99+
{"path suffix match", "src/generated/api.go", "generated/api.go", true},
100+
}
101+
for _, tt := range tests {
102+
t.Run(tt.name, func(t *testing.T) {
103+
got := MatchGitignorePattern(tt.relPath, tt.pattern)
104+
if got != tt.want {
105+
t.Errorf("MatchGitignorePattern(%q, %q) = %v, want %v", tt.relPath, tt.pattern, got, tt.want)
106+
}
107+
})
108+
}
109+
}
110+
111+
func TestIsRangeMode(t *testing.T) {
112+
p := &Provider{mode: ModeRange}
113+
if !p.IsRangeMode() {
114+
t.Error("expected IsRangeMode() = true for ModeRange")
115+
}
116+
p.mode = ModeCommit
117+
if p.IsRangeMode() {
118+
t.Error("expected IsRangeMode() = false for ModeCommit")
119+
}
120+
}
121+
122+
func TestIsCommitMode(t *testing.T) {
123+
p := &Provider{mode: ModeCommit}
124+
if !p.IsCommitMode() {
125+
t.Error("expected IsCommitMode() = true for ModeCommit")
126+
}
127+
p.mode = ModeWorkspace
128+
if p.IsCommitMode() {
129+
t.Error("expected IsCommitMode() = false for ModeWorkspace")
130+
}
131+
}

0 commit comments

Comments
 (0)