Most of the command-line contract was already tested through run. This adds what was missing: report and trees with no database exit 1 with the message telling the user to run scan; a scan that skips an unreadable file prints its warning and counts the skip in its summary; the report and trees summary lines are checked exactly. The fatal-path test now takes its subcommands from the command tree, so a new subcommand wired without runE fails it. The nonexistent-operand test gets an accurate name. Model: opus-5-5
This commit is contained in:
@@ -29,6 +29,10 @@
|
||||
|
||||
# Completed Steps
|
||||
|
||||
- tests cover a missing database, the `scan` skip warning, the `report`
|
||||
and `trees` summary lines, and every subcommand going through `runE`
|
||||
(2026-10-04, https://git.eeqj.de/sneak/sfdupes/issues/16)
|
||||
|
||||
- `.golangci.yml` replaced with the current canonical copy, which uses
|
||||
`gomodguard_v2`, so lint no longer prints a deprecation warning
|
||||
(2026-10-04, https://git.eeqj.de/sneak/sfdupes/issues/26)
|
||||
|
||||
+93
-16
@@ -85,8 +85,9 @@ func makeReadOnly(t *testing.T, path string) {
|
||||
|
||||
// captureStderr redirects os.Stderr to a file for the rest of the test
|
||||
// and returns a function reading back everything written to it. scan
|
||||
// writes its warnings and summary straight to os.Stderr, not to the
|
||||
// stderr writer run is given.
|
||||
// writes its warnings and summary, and report and trees their
|
||||
// summaries, straight to os.Stderr, not to the stderr writer run is
|
||||
// given.
|
||||
func captureStderr(t *testing.T) func() string {
|
||||
t.Helper()
|
||||
|
||||
@@ -184,17 +185,18 @@ func TestRunFatalAfterOpenClosesDatabase(t *testing.T) {
|
||||
// sidecar check is evidence of the close only for scan: report and
|
||||
// trees only read a database that is out of WAL mode, which leaves
|
||||
// nothing on disk whether they close it or not.
|
||||
cases := map[string][]string{
|
||||
cmdScan: {cmdScan},
|
||||
cmdReport: {cmdReport},
|
||||
cmdTrees: {cmdTrees},
|
||||
}
|
||||
//
|
||||
// The subcommands come from the command tree, so a new one is
|
||||
// checked too: one wired with a bare RunE instead of runE reports
|
||||
// its failure as a usage error, exit 2 with the usage text.
|
||||
for _, cmd := range newRootCommand(io.Discard, io.Discard).Commands() {
|
||||
name := cmd.Name()
|
||||
|
||||
for name, args := range cases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
path := brokenDatabase(t)
|
||||
t.Setenv(databaseEnv, path)
|
||||
|
||||
args := []string{name}
|
||||
if name == cmdScan {
|
||||
args = append(args, t.TempDir())
|
||||
}
|
||||
@@ -219,7 +221,7 @@ func TestRunFatalAfterOpenClosesDatabase(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunMissingOperandIsFatalNotUsage(t *testing.T) {
|
||||
func TestRunNonexistentPathIsFatalNotUsage(t *testing.T) {
|
||||
// README §Error handling: a PATH operand that does not exist is a
|
||||
// fatal error (1), not a usage error (2) — and a runtime failure
|
||||
// must not dump the usage text.
|
||||
@@ -258,6 +260,34 @@ func assertFatalOutput(t *testing.T, stderr, stdout string) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunMissingDatabaseIsFatal(t *testing.T) {
|
||||
// README §Database: report and trees need an existing database; a
|
||||
// missing one exits 1 with a message telling the user to run scan.
|
||||
for _, name := range []string{cmdReport, cmdTrees} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
path := testDBPath(t)
|
||||
t.Setenv(databaseEnv, path)
|
||||
|
||||
var stdout, stderr bytes.Buffer
|
||||
|
||||
code := run([]string{name}, &stdout, &stderr)
|
||||
if code != exitFatal {
|
||||
t.Errorf("run(%s) = %d, want %d", name, code, exitFatal)
|
||||
}
|
||||
|
||||
want := "sfdupes: " + path + ": no database (run \"sfdupes " +
|
||||
"scan\" first, or set " + databaseEnv + ")\n"
|
||||
if got := stderr.String(); got != want {
|
||||
t.Errorf("stderr = %q, want %q", got, want)
|
||||
}
|
||||
|
||||
if got := stdout.String(); got != "" {
|
||||
t.Errorf("stdout = %q, want nothing (data only)", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunUsageErrors(t *testing.T) {
|
||||
// Usage errors keep exiting 2 with cobra's own report on stderr.
|
||||
cases := map[string]struct {
|
||||
@@ -453,10 +483,41 @@ func scanOK(t *testing.T, operands ...string) string {
|
||||
}
|
||||
|
||||
func TestRunScanSucceedsDespiteWarnings(t *testing.T) {
|
||||
// README §Error handling: a scan that skips a file it cannot read
|
||||
// warns, counts the skip in its summary, and still exits 0, which
|
||||
// scanOK checks along with the empty stdout.
|
||||
if os.Geteuid() == 0 {
|
||||
t.Skip("root ignores file permissions")
|
||||
}
|
||||
|
||||
path := testDBPath(t)
|
||||
t.Setenv(databaseEnv, path)
|
||||
|
||||
scanFixture(t)
|
||||
dir := t.TempDir()
|
||||
writeFile(t, dir, "a.bin", pattern(1, 300))
|
||||
|
||||
// Same size as a.bin, so the scan reads it, and the read fails.
|
||||
unreadable := writeFile(t, dir, "unreadable.bin", pattern(2, 300))
|
||||
|
||||
err := os.Chmod(unreadable, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
stderr := scanOK(t, dir)
|
||||
|
||||
warning := "hash " + unreadable + ": open " + unreadable +
|
||||
": permission denied\n"
|
||||
if !strings.Contains(stderr, warning) {
|
||||
t.Errorf("stderr = %q, want %q", stderr, warning)
|
||||
}
|
||||
|
||||
summary := "scan: 1 files seen (1 added, 0 updated, 0 removed, " +
|
||||
"0 unchanged), 1 skipped\n"
|
||||
if !strings.Contains(stderr, summary) {
|
||||
t.Errorf("stderr = %q, want %q", stderr, summary)
|
||||
}
|
||||
|
||||
assertNoSidecars(t, path)
|
||||
}
|
||||
|
||||
@@ -563,12 +624,14 @@ func TestRunReportSucceeds(t *testing.T) {
|
||||
|
||||
dupes := scanFixture(t)
|
||||
|
||||
var stdout, stderr bytes.Buffer
|
||||
var stdout bytes.Buffer
|
||||
|
||||
code := run([]string{cmdReport}, &stdout, &stderr)
|
||||
stderr := captureStderr(t)
|
||||
|
||||
code := run([]string{cmdReport}, &stdout, os.Stderr)
|
||||
if code != exitOK {
|
||||
t.Fatalf("run(report) = %d, want %d; stderr: %s",
|
||||
code, exitOK, stderr.String())
|
||||
code, exitOK, stderr())
|
||||
}
|
||||
|
||||
want := "first\tdupe\tsize\n" + dupes[0] + "\t" + dupes[1] + "\t300\n"
|
||||
@@ -576,6 +639,12 @@ func TestRunReportSucceeds(t *testing.T) {
|
||||
t.Errorf("stdout = %q, want %q", got, want)
|
||||
}
|
||||
|
||||
want = "report: 2 records read, 1 duplicate groups, 1 dupe files, " +
|
||||
"300 B reclaimable\n"
|
||||
if got := stderr(); got != want {
|
||||
t.Errorf("stderr = %q, want %q", got, want)
|
||||
}
|
||||
|
||||
assertNoSidecars(t, path)
|
||||
}
|
||||
|
||||
@@ -585,12 +654,14 @@ func TestRunTreesSucceeds(t *testing.T) {
|
||||
|
||||
dupes := scanFixture(t)
|
||||
|
||||
var stdout, stderr bytes.Buffer
|
||||
var stdout bytes.Buffer
|
||||
|
||||
code := run([]string{cmdTrees}, &stdout, &stderr)
|
||||
stderr := captureStderr(t)
|
||||
|
||||
code := run([]string{cmdTrees}, &stdout, os.Stderr)
|
||||
if code != exitOK {
|
||||
t.Fatalf("run(trees) = %d, want %d; stderr: %s",
|
||||
code, exitOK, stderr.String())
|
||||
code, exitOK, stderr())
|
||||
}
|
||||
|
||||
// The two directories holding the duplicate pair are duplicate
|
||||
@@ -601,6 +672,12 @@ func TestRunTreesSucceeds(t *testing.T) {
|
||||
t.Errorf("stdout = %q, want %q", got, want)
|
||||
}
|
||||
|
||||
want = "trees: 2 records read, 1 duplicate tree groups, 1 dupe trees, " +
|
||||
"300 B reclaimable\n"
|
||||
if got := stderr(); got != want {
|
||||
t.Errorf("stderr = %q, want %q", got, want)
|
||||
}
|
||||
|
||||
assertNoSidecars(t, path)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user