internal/ignore/ignore_test.go
Ref: Size: 1.9 KiB
package ignore
import (
"os"
"path/filepath"
"testing"
)
func writeGitignore(t *testing.T, dir, content string) {
t.Helper()
if err := os.WriteFile(filepath.Join(dir, ".gitignore"), []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
func TestHardExcludes(t *testing.T) {
dir := t.TempDir()
m, err := New(dir)
if err != nil {
t.Fatal(err)
}
cases := []struct {
path string
want bool
}{
{filepath.Join(dir, ".git", "HEAD"), true},
{filepath.Join(dir, "node_modules", "x", "y.js"), true},
{filepath.Join(dir, "dist", "out.js"), true},
{filepath.Join(dir, "build", "out.o"), true},
{filepath.Join(dir, "target", "x"), true},
{filepath.Join(dir, ".venv", "bin", "python"), true},
{filepath.Join(dir, "src", "foo.go"), false},
{filepath.Join(dir, "foo.swp"), true},
{filepath.Join(dir, "lock.lock"), true},
{filepath.Join(dir, "a.tmp"), true},
}
for _, c := range cases {
if got := m.ShouldIgnore(c.path); got != c.want {
t.Errorf("ShouldIgnore(%q) = %v, want %v", c.path, got, c.want)
}
}
}
func TestGitignorePatterns(t *testing.T) {
dir := t.TempDir()
writeGitignore(t, dir, "secret.txt\n*.bak\n")
m, err := New(dir)
if err != nil {
t.Fatal(err)
}
if !m.ShouldIgnore(filepath.Join(dir, "secret.txt")) {
t.Error("secret.txt should be ignored")
}
if !m.ShouldIgnore(filepath.Join(dir, "old.bak")) {
t.Error("old.bak should be ignored")
}
if m.ShouldIgnore(filepath.Join(dir, "ok.txt")) {
t.Error("ok.txt should NOT be ignored")
}
}
func TestReloadPicksUpNewPatterns(t *testing.T) {
dir := t.TempDir()
writeGitignore(t, dir, "")
m, err := New(dir)
if err != nil {
t.Fatal(err)
}
p := filepath.Join(dir, "fresh.txt")
if m.ShouldIgnore(p) {
t.Fatal("fresh.txt should not be ignored before reload")
}
writeGitignore(t, dir, "fresh.txt\n")
if err := m.Reload(); err != nil {
t.Fatal(err)
}
if !m.ShouldIgnore(p) {
t.Error("fresh.txt should be ignored after reload")
}
}