package assets import ( "bytes" "os" "path/filepath" "testing" ) func touch(t *testing.T, path string) { t.Helper() if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(path, []byte("x"), 0o644); err != nil { t.Fatal(err) } } func TestCheckDupes(t *testing.T) { root := t.TempDir() touch(t, filepath.Join(root, "tex", "Foo.tga")) touch(t, filepath.Join(root, "plc", "foo.tga")) // basename collision (case-insensitive) var out, errw bytes.Buffer if code := runCheckDupes([]string{root}, &out, &errw); code != exitFail { t.Fatalf("collision exit = %d, want %d\n%s", code, exitFail, errw.String()) } clean := t.TempDir() touch(t, filepath.Join(clean, "a.tga")) touch(t, filepath.Join(clean, "sub", "b.tga")) out.Reset() errw.Reset() if code := runCheckDupes([]string{clean}, &out, &errw); code != exitOK { t.Fatalf("no-collision exit = %d, want %d\n%s", code, exitOK, errw.String()) } } func TestCleanDupes(t *testing.T) { primary := t.TempDir() clean := t.TempDir() touch(t, filepath.Join(primary, "keep.tga")) collide := filepath.Join(clean, "sub", "Keep.tga") survive := filepath.Join(clean, "unique.tga") touch(t, collide) touch(t, survive) // dry-run removes nothing. var out, errw bytes.Buffer if code := runCleanDupes([]string{"--dry-run", primary, clean}, &out, &errw); code != exitOK { t.Fatalf("dry-run exit = %d\n%s", code, errw.String()) } if _, err := os.Stat(collide); err != nil { t.Fatal("dry-run deleted a file") } // real run deletes the collision, keeps the unique file. out.Reset() errw.Reset() if code := runCleanDupes([]string{primary, clean}, &out, &errw); code != exitOK { t.Fatalf("clean exit = %d\n%s", code, errw.String()) } if _, err := os.Stat(collide); !os.IsNotExist(err) { t.Fatal("collision file was not deleted") } if _, err := os.Stat(survive); err != nil { t.Fatal("unique file was wrongly deleted") } } func TestCleanDupesRejectsOverlap(t *testing.T) { root := t.TempDir() sub := filepath.Join(root, "child") touch(t, filepath.Join(sub, "x.tga")) var out, errw bytes.Buffer if code := runCleanDupes([]string{root, sub}, &out, &errw); code != exitUsage { t.Fatalf("overlap exit = %d, want %d", code, exitUsage) } }