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 {