package main import ( "bytes" "path/filepath" "strings" "testing" ) func TestRunDwimPrintsLargestTreesFirst(t *testing.T) { // dwim scans its root, then prints the trees report, the larger of // two duplicate-tree pairs first, and ends by saying how to print // the report again. t.Setenv(databaseEnv, testDBPath(t)) root := realTempDir(t) small := []string{ filepath.Dir(writeFile(t, root, "small1/f.bin", pattern(1, 300))), filepath.Dir(writeFile(t, root, "small2/f.bin", pattern(1, 300))), } large := []string{ filepath.Dir(writeFile(t, root, "large1/f.bin", pattern(2, 5000))), filepath.Dir(writeFile(t, root, "large2/f.bin", pattern(2, 5000))), } var stdout bytes.Buffer stderr := captureStderr(t) err := runDwim(t.Context(), root, &stdout) if err != nil { t.Fatalf("runDwim: %v; stderr: %s", err, stderr()) } lines := strings.Split(strings.TrimSuffix(stdout.String(), "\n"), "\n") if len(lines) != 3 { t.Fatalf("stdout = %q, want a header and two groups", stdout.String()) } wantFirst := large[0] + "\t" + large[1] + "\t1\t5000\t" if !strings.HasPrefix(lines[1], wantFirst) { t.Errorf("first group = %q, want it to start %q", lines[1], wantFirst) } wantSecond := small[0] + "\t" + small[1] + "\t1\t300\t" if !strings.HasPrefix(lines[2], wantSecond) { t.Errorf("second group = %q, want it to start %q", lines[2], wantSecond) } last := "\ndwim: run \"sfdupes trees\" to print this report again\n" if got := stderr(); !strings.HasSuffix(got, last) { t.Errorf("stderr = %q, want it to end %q", got, last) } } func TestRunDwimScanErrorPrintsNoReport(t *testing.T) { // A fatal scan error ends dwim before the report: a root that does // not exist fails the scan as it fails scan itself. t.Setenv(databaseEnv, testDBPath(t)) missing := filepath.Join(realTempDir(t), "missing") var stdout bytes.Buffer stderr := captureStderr(t) err := runDwim(t.Context(), missing, &stdout) if err == nil { t.Fatal("runDwim on a missing root: error = nil, want it fatal") } if got := stdout.String(); got != "" { t.Errorf("stdout = %q, want no report", got) } if got := stderr(); strings.Contains(got, "trees:") || strings.Contains(got, "dwim:") { t.Errorf("stderr = %q, want nothing from trees or dwim", got) } }