diff options
| author | Paul Buetow <paul@buetow.org> | 2026-07-22 18:53:43 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-07-22 18:53:43 +0300 |
| commit | 456cb2d3be55d431ba507367764ccfd13b13b4b4 (patch) | |
| tree | 71c241dcf9fd963661023779e8edefc30ad7a4be /internal/release/release_prompt_test.go | |
| parent | 23ecaa2c7f731a4f7188aec2404594641faf2f7f (diff) | |
fix(errcheck): handle all unchecked error returns without changing behaviormain
errcheck ./... flagged unchecked HTTP response Close(), file Close(),
os.RemoveAll(), fmt.Scanln(), and fmt.Fprintf() calls across codeberg,
github, release, showcase, and sync. None of these were bugs causing
incorrect behavior today, but leaving them unchecked hid real failure
modes (e.g. a lagging NFS mount failing a file Close() after writes,
which this codebase has hit before per commit 23ecaa2).
- internal/codeberg/codeberg.go, internal/github/github.go,
internal/release/release.go: added a small closeResponseBody(resp)
helper per package and used it for all deferred resp.Body.Close()
calls. The body is always fully read (or abandoned on an earlier
error) by the time Close() runs, so the error is intentionally
discarded - matching the explicit `_ = ...` discard convention
already used elsewhere in this repo (e.g. showcase.go's
os.RemoveAll on the worktree-add failure path).
- internal/release/release.go: the two fmt.Scanln(&response) prompts
now explicitly discard the return values; a Scanln error already
leaves response == "", which the existing y/yes check already
treats as a safe decline, so behavior is unchanged.
- internal/showcase/code_extractor.go, images.go, language_detector.go:
added a shared closeFile(*os.File) helper (package showcase) for the
read-only file Close() calls, matching the same discard rationale.
copyFile's destination Close() is the one write-side case where a
close failure is real data-loss information, so it now uses a named
return to surface it via err without masking any earlier error.
- internal/showcase/showcase.go: the deferred os.RemoveAll(tempRoot)
now explicitly discards its error, matching the sibling `_ =` call
three lines above in the same function.
- internal/sync/branch_analyzer.go: GenerateDeleteScript's per-repository
fmt.Fprintf(file, ...) calls are now checked and wrapped with
fmt.Errorf(...: %w), matching the error-wrapping convention already
used by writeBranchDeletionBlock right below it in the same file
(the repeated repo-header writes were pulled into a new
writeDeleteScriptRepoHeader helper to keep this readable). The
script file's defer file.Close() now uses a named return so a close
failure is reported instead of silently discarded.
Added focused tests for the two behavior-relevant paths: copyFile's
missing-source/success paths after the named-return change, and
PromptConfirmation's empty-input-declines/explicit-yes behavior after
touching the Scanln call. Trivial defer-Close discards elsewhere are
not additionally unit tested per the task's guidance against
overengineering.
Verified: go build ./..., go vet ./..., errcheck ./... (clean), and
go test ./... all pass.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Diffstat (limited to 'internal/release/release_prompt_test.go')
| -rw-r--r-- | internal/release/release_prompt_test.go | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/internal/release/release_prompt_test.go b/internal/release/release_prompt_test.go new file mode 100644 index 0000000..fdb1d27 --- /dev/null +++ b/internal/release/release_prompt_test.go @@ -0,0 +1,74 @@ +package release + +import ( + "os" + "testing" +) + +// withStdin temporarily replaces os.Stdin with r for the duration of fn, then +// restores the original value. Not run in parallel with other tests since +// os.Stdin is a shared global. +func withStdin(t *testing.T, r *os.File, fn func()) { + t.Helper() + + original := os.Stdin + os.Stdin = r + defer func() { os.Stdin = original }() + + fn() +} + +// TestPromptConfirmation_EmptyInputDeclines verifies that when Scanln +// returns an error (e.g. a bare newline, which fmt.Scanln reports as +// "unexpected newline"), PromptConfirmation still safely defaults to +// declining rather than panicking or hanging - this is the discarded-error +// behavior that PromptConfirmation's fmt.Scanln call relies on. +func TestPromptConfirmation_EmptyInputDeclines(t *testing.T) { + r, w, err := os.Pipe() + if err != nil { + t.Fatalf("failed to create pipe: %v", err) + } + defer func() { _ = r.Close() }() + + if _, err := w.WriteString("\n"); err != nil { + t.Fatalf("failed to write to pipe: %v", err) + } + if err := w.Close(); err != nil { + t.Fatalf("failed to close pipe writer: %v", err) + } + + var got bool + withStdin(t, r, func() { + got = PromptConfirmation("Proceed?") + }) + + if got { + t.Fatal("PromptConfirmation() = true for empty input, want false") + } +} + +// TestPromptConfirmation_YesInputConfirms is a regression check that the +// happy path (explicit "y") still works after touching the Scanln call. +func TestPromptConfirmation_YesInputConfirms(t *testing.T) { + r, w, err := os.Pipe() + if err != nil { + t.Fatalf("failed to create pipe: %v", err) + } + defer func() { _ = r.Close() }() + + if _, err := w.WriteString("y\n"); err != nil { + t.Fatalf("failed to write to pipe: %v", err) + } + if err := w.Close(); err != nil { + t.Fatalf("failed to close pipe writer: %v", err) + } + + var got bool + withStdin(t, r, func() { + got = PromptConfirmation("Proceed?") + }) + + if !got { + t.Fatal("PromptConfirmation() = false for \"y\" input, want true") + } +} |
