Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9cad48eddd |
@@ -29,9 +29,9 @@
|
|||||||
|
|
||||||
# Completed Steps
|
# Completed Steps
|
||||||
|
|
||||||
- tests cover a missing database, the `scan` skip warning, the `report`
|
- tests cover a missing database, `scan` keeping stdout empty, its skip
|
||||||
and `trees` summary lines, and every subcommand going through `runE`
|
warning, the `report` and `trees` summary lines, and every subcommand
|
||||||
(2026-10-04, https://git.eeqj.de/sneak/sfdupes/issues/16)
|
going through `runE` (2026-10-04, https://git.eeqj.de/sneak/sfdupes/issues/16)
|
||||||
|
|
||||||
- `.golangci.yml` replaced with the current canonical copy, which uses
|
- `.golangci.yml` replaced with the current canonical copy, which uses
|
||||||
`gomodguard_v2`, so lint no longer prints a deprecation warning
|
`gomodguard_v2`, so lint no longer prints a deprecation warning
|
||||||
|
|||||||
+15
-5
@@ -344,13 +344,14 @@ func storedPaths(t *testing.T, path string) []string {
|
|||||||
|
|
||||||
// TestRunScanInterrupted calls the scan entrypoint with a context that
|
// TestRunScanInterrupted calls the scan entrypoint with a context that
|
||||||
// is already cancelled, as when a signal arrives at once. It must return
|
// is already cancelled, as when a signal arrives at once. It must return
|
||||||
// errInterrupted promptly with its one line on stderr, leave the
|
// errInterrupted promptly with its one line on stderr and nothing on
|
||||||
// database valid and as it was, and leave nothing in the way of the
|
// stdout, leave the database valid and as it was, and leave nothing in
|
||||||
// next scan, which must bring the database up to date.
|
// the way of the next scan, which must bring the database up to date.
|
||||||
func TestRunScanInterrupted(t *testing.T) {
|
func TestRunScanInterrupted(t *testing.T) {
|
||||||
path := testDBPath(t)
|
path := testDBPath(t)
|
||||||
t.Setenv(databaseEnv, path)
|
t.Setenv(databaseEnv, path)
|
||||||
|
|
||||||
|
stdout := captureStdout(t)
|
||||||
stderr := captureStderr(t)
|
stderr := captureStderr(t)
|
||||||
dir := buildSmokeTree(t)
|
dir := buildSmokeTree(t)
|
||||||
|
|
||||||
@@ -393,6 +394,10 @@ func TestRunScanInterrupted(t *testing.T) {
|
|||||||
t.Errorf("stderr = %q, want %q", got, want)
|
t.Errorf("stderr = %q, want %q", got, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if got := stdout(); got != "" {
|
||||||
|
t.Errorf("stdout = %q, want nothing (data only)", got)
|
||||||
|
}
|
||||||
|
|
||||||
assertNoSidecars(t, path)
|
assertNoSidecars(t, path)
|
||||||
|
|
||||||
if got := storedPaths(t, path); !slices.Equal(got, before) {
|
if got := storedPaths(t, path); !slices.Equal(got, before) {
|
||||||
@@ -415,12 +420,13 @@ func TestRunScanInterrupted(t *testing.T) {
|
|||||||
// TestRunScanInterruptedMidHash interrupts the scan entrypoint part-way
|
// TestRunScanInterruptedMidHash interrupts the scan entrypoint part-way
|
||||||
// through its hash phase, after the database is open. It must return
|
// through its hash phase, after the database is open. It must return
|
||||||
// errInterrupted, release the lock, end stderr with its line counting
|
// errInterrupted, release the lock, end stderr with its line counting
|
||||||
// every file the walk reached, close the database out of WAL mode, and
|
// every file the walk reached, write nothing to stdout, close the
|
||||||
// keep the records it hashed.
|
// database out of WAL mode, and keep the records it hashed.
|
||||||
func TestRunScanInterruptedMidHash(t *testing.T) {
|
func TestRunScanInterruptedMidHash(t *testing.T) {
|
||||||
path := testDBPath(t)
|
path := testDBPath(t)
|
||||||
t.Setenv(databaseEnv, path)
|
t.Setenv(databaseEnv, path)
|
||||||
|
|
||||||
|
stdout := captureStdout(t)
|
||||||
stderr := captureStderr(t)
|
stderr := captureStderr(t)
|
||||||
dir := buildWalkCancelTree(t)
|
dir := buildWalkCancelTree(t)
|
||||||
|
|
||||||
@@ -438,6 +444,10 @@ func TestRunScanInterruptedMidHash(t *testing.T) {
|
|||||||
t.Errorf("stderr = %q, want it to end with %q", got, want)
|
t.Errorf("stderr = %q, want it to end with %q", got, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if got := stdout(); got != "" {
|
||||||
|
t.Errorf("stdout = %q, want nothing (data only)", got)
|
||||||
|
}
|
||||||
|
|
||||||
assertNoSidecars(t, path)
|
assertNoSidecars(t, path)
|
||||||
|
|
||||||
db, err := openReportDatabase(t.Context(), path)
|
db, err := openReportDatabase(t.Context(), path)
|
||||||
|
|||||||
+41
-17
@@ -91,16 +91,35 @@ func makeReadOnly(t *testing.T, path string) {
|
|||||||
func captureStderr(t *testing.T) func() string {
|
func captureStderr(t *testing.T) func() string {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
f, err := os.Create(filepath.Join(t.TempDir(), "stderr"))
|
return capture(t, &os.Stderr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// captureStdout does for os.Stdout what captureStderr does for
|
||||||
|
// os.Stderr. scan is never given run's stdout writer, so anything it
|
||||||
|
// printed would go straight to os.Stdout. The scan tests pass os.Stdout
|
||||||
|
// as run's stdout too, so the one capture sees both.
|
||||||
|
func captureStdout(t *testing.T) func() string {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
return capture(t, &os.Stdout)
|
||||||
|
}
|
||||||
|
|
||||||
|
// capture redirects *std, which is os.Stdout or os.Stderr, to a file
|
||||||
|
// for the rest of the test and returns a function reading back
|
||||||
|
// everything written to it.
|
||||||
|
func capture(t *testing.T, std **os.File) func() string {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
f, err := os.Create(filepath.Join(t.TempDir(), "output"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
saved := os.Stderr
|
saved := *std
|
||||||
os.Stderr = f
|
*std = f
|
||||||
|
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
os.Stderr = saved
|
*std = saved
|
||||||
|
|
||||||
_ = f.Close()
|
_ = f.Close()
|
||||||
})
|
})
|
||||||
@@ -201,15 +220,17 @@ func TestRunFatalAfterOpenClosesDatabase(t *testing.T) {
|
|||||||
args = append(args, t.TempDir())
|
args = append(args, t.TempDir())
|
||||||
}
|
}
|
||||||
|
|
||||||
var stdout, stderr bytes.Buffer
|
stdout := captureStdout(t)
|
||||||
|
|
||||||
code := run(args, &stdout, &stderr)
|
var stderr bytes.Buffer
|
||||||
|
|
||||||
|
code := run(args, os.Stdout, &stderr)
|
||||||
if code != exitFatal {
|
if code != exitFatal {
|
||||||
t.Errorf("run(%v) = %d, want %d", args, code, exitFatal)
|
t.Errorf("run(%v) = %d, want %d", args, code, exitFatal)
|
||||||
}
|
}
|
||||||
|
|
||||||
assertNoSidecars(t, path)
|
assertNoSidecars(t, path)
|
||||||
assertFatalOutput(t, stderr.String(), stdout.String())
|
assertFatalOutput(t, stderr.String(), stdout())
|
||||||
|
|
||||||
// Proof that the failure happened after the open: only a
|
// Proof that the failure happened after the open: only a
|
||||||
// query against the opened database can report this.
|
// query against the opened database can report this.
|
||||||
@@ -227,16 +248,18 @@ func TestRunNonexistentPathIsFatalNotUsage(t *testing.T) {
|
|||||||
// must not dump the usage text.
|
// must not dump the usage text.
|
||||||
t.Setenv(databaseEnv, testDBPath(t))
|
t.Setenv(databaseEnv, testDBPath(t))
|
||||||
|
|
||||||
var stdout, stderr bytes.Buffer
|
stdout := captureStdout(t)
|
||||||
|
|
||||||
|
var stderr bytes.Buffer
|
||||||
|
|
||||||
missing := filepath.Join(t.TempDir(), "nope")
|
missing := filepath.Join(t.TempDir(), "nope")
|
||||||
|
|
||||||
code := run([]string{cmdScan, missing}, &stdout, &stderr)
|
code := run([]string{cmdScan, missing}, os.Stdout, &stderr)
|
||||||
if code != exitFatal {
|
if code != exitFatal {
|
||||||
t.Errorf("run(scan %s) = %d, want %d", missing, code, exitFatal)
|
t.Errorf("run(scan %s) = %d, want %d", missing, code, exitFatal)
|
||||||
}
|
}
|
||||||
|
|
||||||
assertFatalOutput(t, stderr.String(), stdout.String())
|
assertFatalOutput(t, stderr.String(), stdout())
|
||||||
}
|
}
|
||||||
|
|
||||||
// assertFatalOutput checks that a fatal error was reported the way
|
// assertFatalOutput checks that a fatal error was reported the way
|
||||||
@@ -465,17 +488,16 @@ func scanFixture(t *testing.T) []string {
|
|||||||
func scanOK(t *testing.T, operands ...string) string {
|
func scanOK(t *testing.T, operands ...string) string {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
var stdout bytes.Buffer
|
stdout := captureStdout(t)
|
||||||
|
|
||||||
stderr := captureStderr(t)
|
stderr := captureStderr(t)
|
||||||
|
|
||||||
code := run(append([]string{cmdScan}, operands...), &stdout, os.Stderr)
|
code := run(append([]string{cmdScan}, operands...), os.Stdout, os.Stderr)
|
||||||
if code != exitOK {
|
if code != exitOK {
|
||||||
t.Fatalf("run(scan %q) = %d, want %d; stderr: %s",
|
t.Fatalf("run(scan %q) = %d, want %d; stderr: %s",
|
||||||
operands, code, exitOK, stderr())
|
operands, code, exitOK, stderr())
|
||||||
}
|
}
|
||||||
|
|
||||||
if got := stdout.String(); got != "" {
|
if got := stdout(); got != "" {
|
||||||
t.Errorf("scan stdout = %q, want nothing (data only)", got)
|
t.Errorf("scan stdout = %q, want nothing (data only)", got)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -807,9 +829,11 @@ func TestRunSecondScanFails(t *testing.T) {
|
|||||||
|
|
||||||
holdScanLock(t, path)
|
holdScanLock(t, path)
|
||||||
|
|
||||||
var stdout, stderr bytes.Buffer
|
stdout := captureStdout(t)
|
||||||
|
|
||||||
code := run([]string{cmdScan, t.TempDir()}, &stdout, &stderr)
|
var stderr bytes.Buffer
|
||||||
|
|
||||||
|
code := run([]string{cmdScan, t.TempDir()}, os.Stdout, &stderr)
|
||||||
if code != exitFatal {
|
if code != exitFatal {
|
||||||
t.Errorf("run(scan) = %d, want %d", code, exitFatal)
|
t.Errorf("run(scan) = %d, want %d", code, exitFatal)
|
||||||
}
|
}
|
||||||
@@ -820,7 +844,7 @@ func TestRunSecondScanFails(t *testing.T) {
|
|||||||
t.Errorf("stderr = %q, want %q", got, want)
|
t.Errorf("stderr = %q, want %q", got, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
if got := stdout.String(); got != "" {
|
if got := stdout(); got != "" {
|
||||||
t.Errorf("stdout = %q, want nothing (data only)", got)
|
t.Errorf("stdout = %q, want nothing (data only)", got)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user