From 2999bbac13c8c51182ec203fa10a8bf0d7470120 Mon Sep 17 00:00:00 2001 From: closer-claudio Date: Sun, 15 Nov 2026 03:43:40 -0800 Subject: [PATCH] lint: wrapcheck leaves the backlog, its nine errors wrapped wrapcheck was off for git/commits.go and validate/runner.go as part of the onboarding backlog. It finds nine errors returned as another package gave them: git's exit status from `git version`, `git log --check` and `git show`, and the working directory, the chdir and the commit lookups of the runner. Each now says what was being done: "listing the commits of ", "entering ", "git show ". Wrapping `git.Check`'s error would have broken the dangling-whitespace rule, which told git's "whitespace errors" exit from a failure by the error's text (`err.Error() == "exit status 2"`). The rule now reads the exit status from the error (`errors.As` to `*exec.ExitError`), which the wrap keeps. No test held the rule's message: the command's contract test now does, and it fails on the rule as it was once the error is wrapped. The backlog's counts for the two files are lowered by what left it. For https://github.com/forkcloser/git-validation/issues/37. Co-Authored-By: Claude Fable 5.2 Signed-off-by: closer-claudio --- .lint-go.yaml | 6 ++---- cmd/git-validation/contract_test.go | 29 +++++++++++++++++++++++++++++ git/commits.go | 22 ++++++++++++++++++---- rules/danglingwhitespace/rule.go | 13 ++++++++++++- validate/runner.go | 12 ++++++------ 5 files changed, 67 insertions(+), 15 deletions(-) diff --git a/.lint-go.yaml b/.lint-go.yaml index 690fe5b..e772868 100644 --- a/.lint-go.yaml +++ b/.lint-go.yaml @@ -90,8 +90,7 @@ golangci: - gocognit # 1 - revive # 1 - path: ^git/commits\.go$ - linters: # 37 findings - - wrapcheck # 11 + linters: # 26 findings - noctx # 10 - gosec # 8 - varnamelen # 3 @@ -137,9 +136,8 @@ golangci: - paralleltest # 2 - testpackage # 1 - path: ^validate/runner\.go$ - linters: # 17 findings + linters: # 11 findings - forbidigo # 7 - - wrapcheck # 6 - varnamelen # 2 - err113 # 1 - gocognit # 1 diff --git a/cmd/git-validation/contract_test.go b/cmd/git-validation/contract_test.go index 06cb6fd..0a0bd95 100644 --- a/cmd/git-validation/contract_test.go +++ b/cmd/git-validation/contract_test.go @@ -211,6 +211,35 @@ func TestContractDefaultRunOverAHistory(t *testing.T) { } } +// TestContractWhitespaceErrorsAreNamed: a commit that adds trailing +// whitespace fails the dangling-whitespace rule with the command that shows +// them, and a commit that does not passes it. +// +//nolint:paralleltest // newRepo sets the environment with t.Setenv +func TestContractWhitespaceErrorsAreNamed(t *testing.T) { + r := newRepo(t) + + clean := r.commit("a", "a\n", "Clean change", true) + dirty := r.commit("b", "b \n", "Dirty change", true) + + code, out := r.run(nil, "--run", "dangling-whitespace", "-v") + if code != 1 { + t.Errorf("a commit with trailing whitespace: exit %d, want 1", code) + } + + for _, line := range []string{ + " * " + dirty + " \"Dirty change\" ... FAIL\n", + // The rule names the commit by its whole hash, which the short one starts. + " - FAIL - has whitespace errors. See `git show --check " + dirty, + " * " + clean + " \"Clean change\" ... PASS\n", + " - PASS - commit does not have any whitespace errors\n", + } { + if !strings.Contains(out, line) { + t.Errorf("output lacks %q:\n%s", line, out) + } + } +} + // TestContractVerboseAndQuiet: -v prints every rule's result under its // commit; -q prints nothing for a passing commit and one line per failing // rule, the hash and the reason, with -v or without; QUIET in the diff --git a/git/commits.go b/git/commits.go index 085d867..1caefec 100644 --- a/git/commits.go +++ b/git/commits.go @@ -149,7 +149,7 @@ func gitVersion() (string, error) { buf, err := cmd.Output() if err != nil { - return "", err + return "", fmt.Errorf("git version: %w", err) } return strings.Fields(string(buf))[2], nil @@ -226,7 +226,9 @@ func Check(commit string) ([]byte, error) { } if excludeEnvList := os.Getenv("GIT_CHECK_EXCLUDE"); excludeEnvList != "" { - gitNewEnough, err := gitVersionNewerThan(versionWithExcludes) + var gitNewEnough bool + + gitNewEnough, err = gitVersionNewerThan(versionWithExcludes) if err != nil { return nil, err } @@ -246,7 +248,14 @@ func Check(commit string) ([]byte, error) { cmd.Stderr = os.Stderr - return cmd.Output() + // The output holds what git found when it exits non-zero, which is how + // it says there are whitespace errors: both go back to the caller. + out, err := cmd.Output() + if err != nil { + return out, fmt.Errorf("git log --check %s: %w", commit, err) + } + + return out, nil } // Show returns the diff of a commit. @@ -257,7 +266,12 @@ func Show(commit string) ([]byte, error) { cmd.Stderr = os.Stderr - return cmd.Output() + out, err := cmd.Output() + if err != nil { + return out, fmt.Errorf("git show %s: %w", commit, err) + } + + return out, nil } // CommitEntry represents a single commit's information from `git`. diff --git a/rules/danglingwhitespace/rule.go b/rules/danglingwhitespace/rule.go index 8e15d73..151513f 100644 --- a/rules/danglingwhitespace/rule.go +++ b/rules/danglingwhitespace/rule.go @@ -1,10 +1,17 @@ package danglingwhitespace import ( + "errors" + "os/exec" + "github.com/forkcloser/git-validation/git" "github.com/forkcloser/git-validation/validate" ) +// whitespaceErrors is the status git log --check exits with when it finds +// whitespace errors. +const whitespaceErrors = 2 + // DanglingWhitespace is the rule for checking the presence of dangling // whitespaces on line endings. var DanglingWhitespace = validate.Rule{ @@ -27,7 +34,11 @@ func ValidateDanglingWhitespace(r validate.Rule, c git.CommitEntry) (vr validate _, err := git.Check(c["commit"]) if err != nil { vr.Pass = false - if err.Error() == "exit status 2" { + + // git log --check exits 2 when it finds whitespace errors; anything + // else is git failing. + var exit *exec.ExitError + if errors.As(err, &exit) && exit.ExitCode() == whitespaceErrors { vr.Msg = "has whitespace errors. See `git show --check " + c["commit"] + "`." } else { vr.Msg = "errored with: " + err.Error() diff --git a/validate/runner.go b/validate/runner.go index 14b3bf5..8f91a65 100644 --- a/validate/runner.go +++ b/validate/runner.go @@ -47,7 +47,7 @@ func NewRunner(root string, rules []Rule, commitrange string) (runner *Runner, e cwd, err = os.Getwd() if err != nil { - return nil, err + return nil, fmt.Errorf("finding the working directory: %w", err) } defer func() { if chdirErr := os.Chdir(cwd); chdirErr != nil { @@ -56,14 +56,14 @@ func NewRunner(root string, rules []Rule, commitrange string) (runner *Runner, e }() if err = os.Chdir(newroot); err != nil { - return nil, err + return nil, fmt.Errorf("entering %q: %w", newroot, err) } commitrange, err = git.FetchHeadCommit() if err != nil { commitrange, err = git.HeadCommit() if err != nil { - return nil, err + return nil, fmt.Errorf("finding a commit to validate from in %q: %w", newroot, err) } } @@ -82,7 +82,7 @@ func NewRunner(root string, rules []Rule, commitrange string) (runner *Runner, e func (r *Runner) Run() (err error) { cwd, err := os.Getwd() if err != nil { - return err + return fmt.Errorf("finding the working directory: %w", err) } defer func() { if chdirErr := os.Chdir(cwd); chdirErr != nil { @@ -91,13 +91,13 @@ func (r *Runner) Run() (err error) { }() if err = os.Chdir(r.Root); err != nil { - return err + return fmt.Errorf("entering %q: %w", r.Root, err) } // collect the entries c, err := git.Commits(r.CommitRange) if err != nil { - return err + return fmt.Errorf("listing the commits of %q: %w", r.CommitRange, err) } for _, commit := range c {