devbox/internal/ignore/ignore_test.go
shoemoney 84a4ff53e8 feat(cli): doctor --json, conflicts --rm, status sync-age, opt-in default ignores 🧰🙈
- 🩺 `doctor --json`: structured check report for cron/monitoring; still exits
  non-zero on ❌ (collects checks into a slice, JSON path suppresses the human lines).
- 🗑️ `conflicts --rm`: delete every `.conflict-` copy after review (lazy cleanup,
  not a resolver). Only touches conflict copies — real files untouched.
- ⏱️ `status` shows per-mount last-sync age ("synced 12s ago" / "not synced yet")
  from a daemon-tracked timestamp (control.MountState.LastSyncUnix); also in --json.
- 🙈 the reversed no-go: `settings.sync.ignore_defaults` (opt-in, default off) layers
  a built-in junk set (node_modules, .git, target, dist, build, .venv, __pycache__,
  .next, .DS_Store) onto the matcher — don't ship gigabytes of regenerable junk over
  the now-live WAN. Reuses round-1 --exclude plumbing; honored by daemon + publish.

Tests: doctor --json valid+exit-code, conflicts --rm deletes only conflicts,
ignore.Defaults match. go test ./... -race + vet clean.
2026-06-30 13:26:24 -05:00

100 lines
2 KiB
Go

package ignore
import (
"strings"
"testing"
)
// TestDefaultsMatch proves the opt-in default-ignore set matches common junk
// dirs but leaves real source alone.
func TestDefaultsMatch(t *testing.T) {
m, err := Compile(Defaults)
if err != nil {
t.Fatal(err)
}
for _, dir := range []string{"node_modules", ".git", "__pycache__", "target", "dist"} {
if !m.Match(dir, true) {
t.Errorf("Defaults should ignore %q/", dir)
}
}
if m.Match("src", true) || m.Match("main.go", false) {
t.Error("Defaults must not ignore real source")
}
}
const devignore = `
node_modules/
dist/
build/
.next/
target/
*.log
*.tmp
.DS_Store
.env
.env.*
*.pem
*.key
secrets/
!.env.example
`
func newMatcher(t *testing.T) *Matcher {
t.Helper()
m, err := Compile(strings.Split(devignore, "\n"))
if err != nil {
t.Fatalf("Compile: %v", err)
}
return m
}
func TestMatch(t *testing.T) {
m := newMatcher(t)
cases := []struct {
path string
isDir bool
want bool
}{
// node_modules at any depth
{"node_modules", true, true},
{"node_modules/x", false, true},
{"a/b/node_modules/c", false, true},
// other dir patterns
{"dist/x", false, true},
{"build", true, true},
{".next", true, true},
{"target/y", false, true},
// extension / name patterns at any depth
{"app.log", false, true},
{"a/b/c.tmp", false, true},
{".DS_Store", false, true},
{"sub/.DS_Store", false, true},
// secrets + env + keys
{".env", false, true},
{".env.local", false, true},
{"key.pem", false, true},
{"id.key", false, true},
{"secrets/aws", false, true},
// negation beats .env.*
{".env.example", false, false},
// .devbox always ignored
{".devbox/anything", false, true},
{".devbox/hooks/post-pull", false, true},
// not ignored
{"src/main.go", false, false},
{"README.md", false, false},
}
for _, c := range cases {
if got := m.Match(c.path, c.isDir); got != c.want {
t.Errorf("Match(%q, isDir=%v) = %v, want %v", c.path, c.isDir, got, c.want)
}
}
}