diff --git a/.lint-go.yaml b/.lint-go.yaml index e772868..c54236f 100644 --- a/.lint-go.yaml +++ b/.lint-go.yaml @@ -90,8 +90,7 @@ golangci: - gocognit # 1 - revive # 1 - path: ^git/commits\.go$ - linters: # 26 findings - - noctx # 10 + linters: # 16 findings - gosec # 8 - varnamelen # 3 - gochecknoglobals # 2 diff --git a/cmd/git-validation/main.go b/cmd/git-validation/main.go index 01a0f9b..c5eee8c 100644 --- a/cmd/git-validation/main.go +++ b/cmd/git-validation/main.go @@ -62,12 +62,12 @@ func main() { // shutdown runs the command and exits with the status an ExitError asks // for, silently, and 1 for any other error, which it logs. - shutdown.Run(shutdown.SetDefaults(context.Background()), func(context.Context) error { - return run(&flags) + shutdown.Run(shutdown.SetDefaults(context.Background()), func(ctx context.Context) error { + return run(ctx, &flags) }) } -func run(flags *cli) error { +func run(ctx context.Context, flags *cli) error { if flags.ListRules { for _, r := range validate.RegisteredRules { fmt.Printf("%q -- %s\n", r.Name, r.Description) @@ -101,7 +101,7 @@ func run(flags *cli) error { } } - slog.Debug("rules to run", "rules", ruleNames(rules)) + slog.DebugContext(ctx, "rules to run", "rules", ruleNames(rules)) commitRange := flags.Range if commitRange == "" { @@ -118,9 +118,9 @@ func run(flags *cli) error { } } - slog.Debug("using commit range", "range", commitRange) + slog.DebugContext(ctx, "using commit range", "range", commitRange) - runner, err := validate.NewRunner(flags.Dir, rules, commitRange) + runner, err := validate.NewRunner(ctx, flags.Dir, rules, commitRange) if err != nil { return fmt.Errorf("preparing the run: %w", err) } @@ -129,7 +129,7 @@ func run(flags *cli) error { report(os.Stdout, commit, results, flags.Verbose, flags.Quiet) } - if err := runner.Run(); err != nil { + if err := runner.Run(ctx); err != nil { return fmt.Errorf("validating commits: %w", err) } diff --git a/git/commits.go b/git/commits.go index 1caefec..d1ebffa 100644 --- a/git/commits.go +++ b/git/commits.go @@ -1,6 +1,7 @@ package git import ( + "context" "errors" "fmt" "log/slog" @@ -15,16 +16,18 @@ import ( // If commitrange is a git still range 12345...54321, then it will be isolated set of commits. // If commitrange is a single commit, all ancestor commits up through the hash provided. // If commitrange is an empty commit range, then nil is returned. -func Commits(commitrange string) ([]CommitEntry, error) { +func Commits(ctx context.Context, commitrange string) ([]CommitEntry, error) { // TEST if it has _enough_ of a range from rev-list - commitrange, err := checkRevList(commitrange) + commitrange, err := checkRevList(ctx, commitrange) if err != nil { return nil, fmt.Errorf("validating the commit range: %w", err) } cmdArgs := []string{"git", "rev-list", commitrange} - output, err := command(cmdArgs[1:]...).Output() + output, err := command(ctx, cmdArgs[1:]...).Output() + + err = ended(ctx, err) if err != nil { return nil, fmt.Errorf("%s: %w", strings.Join(cmdArgs, " "), err) } @@ -37,7 +40,7 @@ func Commits(commitrange string) ([]CommitEntry, error) { commits := make([]CommitEntry, len(commitHashes)) for i, commitHash := range commitHashes { - c, err := LogCommit(commitHash) + c, err := LogCommit(ctx, commitHash) if err != nil { return commits, err } @@ -57,14 +60,21 @@ var errRange = errors.New("the commit range does not resolve") // what the repository had, so a typo, or a shallow clone whose depth did not // reach the range's start, validated the whole history and passed. In a // shallow clone the error says what to fetch. -func checkRevList(commitrange string) (string, error) { +func checkRevList(ctx context.Context, commitrange string) (string, error) { cmdArgs := []string{"git", "rev-list", commitrange} - _, err := command(cmdArgs[1:]...).Output() + _, err := command(ctx, cmdArgs[1:]...).Output() + + err = ended(ctx, err) if err == nil { return commitrange, nil } + // A git that was stopped says nothing of the range. + if ctx.Err() != nil { + return "", fmt.Errorf("%s: %w", strings.Join(cmdArgs, " "), err) + } + // The git command that failed is named, and its exit unwraps, as every // git failure here does; git's own words follow. detail := "" @@ -73,12 +83,12 @@ func checkRevList(commitrange string) (string, error) { detail = " (" + strings.TrimSpace(string(exitErr.Stderr)) + ")" } - if shallowRepository() { + if shallowRepository(ctx) { detail += "; the repository is a shallow clone" // A typo and an end past the depth look the same from here: both // are not in the clone. - switch missing := missingEnds(commitrange); len(missing) { + switch missing := missingEnds(ctx, commitrange); len(missing) { case 0: case 1: detail += " and " + missing[0] + " is not in it" @@ -94,7 +104,7 @@ func checkRevList(commitrange string) (string, error) { // missingEnds returns the ends of a range (a..b, a...b, or a single revision) // that do not name a commit the repository has. -func missingEnds(commitrange string) []string { +func missingEnds(ctx context.Context, commitrange string) []string { var missing []string for end := range strings.SplitSeq(strings.ReplaceAll(commitrange, "...", ".."), "..") { @@ -102,7 +112,8 @@ func missingEnds(commitrange string) []string { continue } - if err := command("rev-parse", "--verify", "--quiet", end+"^{commit}").Run(); err != nil { + err := command(ctx, "rev-parse", "--verify", "--quiet", end+"^{commit}").Run() + if err != nil { missing = append(missing, end) } } @@ -112,8 +123,9 @@ func missingEnds(commitrange string) []string { // shallowRepository reports whether the repository was cloned with a depth, // so that its history may not reach a range's start. -func shallowRepository() bool { - output, err := command("rev-parse", "--is-shallow-repository").Output() +func shallowRepository(ctx context.Context) bool { + output, err := command(ctx, "rev-parse", "--is-shallow-repository").Output() + err = ended(ctx, err) return err == nil && strings.TrimSpace(string(output)) == "true" } @@ -143,11 +155,13 @@ var FieldNames = map[string]string{ "%G?": "verification_flag", } -func gitVersion() (string, error) { - cmd := command("version") +func gitVersion(ctx context.Context) (string, error) { + cmd := command(ctx, "version") cmd.Stderr = os.Stderr buf, err := cmd.Output() + + err = ended(ctx, err) if err != nil { return "", fmt.Errorf("git version: %w", err) } @@ -158,8 +172,8 @@ func gitVersion() (string, error) { // https://github.com/vbatts/git-validation/issues/37 var versionWithExcludes = "1.9.5" -func gitVersionNewerThan(otherV string) (bool, error) { - gv, err := gitVersion() +func gitVersionNewerThan(ctx context.Context, otherV string) (bool, error) { + gv, err := gitVersion(ctx) if err != nil { return false, err } @@ -208,14 +222,19 @@ func versionNumbers(gitVersion string) ([]int, error) { // Check warns if changes introduce whitespace errors. // Returns non-zero if any issues are found. -func Check(commit string) ([]byte, error) { - logs, err := LogUntil(commit) +func Check(ctx context.Context, commit string) ([]byte, error) { + logs, err := LogUntil(ctx, commit) if err != nil { return nil, err } if len(logs) == 1 { - slog.Debug("skipping the whitespace check of the initial commit, which has no parent", "commit", commit) + slog.DebugContext( + ctx, + "skipping the whitespace check of the initial commit, which has no parent", + "commit", + commit, + ) return []byte{}, nil } @@ -228,7 +247,7 @@ func Check(commit string) ([]byte, error) { if excludeEnvList := os.Getenv("GIT_CHECK_EXCLUDE"); excludeEnvList != "" { var gitNewEnough bool - gitNewEnough, err = gitVersionNewerThan(versionWithExcludes) + gitNewEnough, err = gitVersionNewerThan(ctx, versionWithExcludes) if err != nil { return nil, err } @@ -244,13 +263,15 @@ func Check(commit string) ([]byte, error) { } } - cmd := command(args...) + cmd := command(ctx, args...) cmd.Stderr = os.Stderr // 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() + + err = ended(ctx, err) if err != nil { return out, fmt.Errorf("git log --check %s: %w", commit, err) } @@ -261,12 +282,14 @@ func Check(commit string) ([]byte, error) { // Show returns the diff of a commit. // // NOTE: This could be expensive for very large commits. -func Show(commit string) ([]byte, error) { - cmd := command("--no-pager", "show", commit) +func Show(ctx context.Context, commit string) ([]byte, error) { + cmd := command(ctx, "--no-pager", "show", commit) cmd.Stderr = os.Stderr out, err := cmd.Output() + + err = ended(ctx, err) if err != nil { return out, fmt.Errorf("git show %s: %w", commit, err) } @@ -279,12 +302,14 @@ func Show(commit string) ([]byte, error) { type CommitEntry map[string]string // LogUntil returns the array of oneline commits from initial until the provided commit. -func LogUntil(commit string) ([]string, error) { - cmd := command("--no-pager", "log", "--oneline", "--no-show-signature", commit) +func LogUntil(ctx context.Context, commit string) ([]string, error) { + cmd := command(ctx, "--no-pager", "log", "--oneline", "--no-show-signature", commit) cmd.Stderr = os.Stderr out, err := cmd.Output() + + err = ended(ctx, err) if err != nil { return nil, fmt.Errorf("%s: %w", strings.Join(cmd.Args, " "), err) } @@ -293,15 +318,17 @@ func LogUntil(commit string) ([]string, error) { } // LogCommit assembles the full information on a commit from its commit hash. -func LogCommit(commit string) (*CommitEntry, error) { +func LogCommit(ctx context.Context, commit string) (*CommitEntry, error) { c := CommitEntry{} for k, v := range FieldNames { - cmd := command("--no-pager", "log", "-1", `--pretty=format:`+k+``, commit) + cmd := command(ctx, "--no-pager", "log", "-1", `--pretty=format:`+k+``, commit) cmd.Stderr = os.Stderr out, err := cmd.Output() + + err = ended(ctx, err) if err != nil { return nil, fmt.Errorf("%s: %w", strings.Join(cmd.Args, " "), err) } @@ -314,10 +341,12 @@ func LogCommit(commit string) (*CommitEntry, error) { } // FetchHeadCommit returns the hash of FETCH_HEAD. -func FetchHeadCommit() (string, error) { +func FetchHeadCommit(ctx context.Context) (string, error) { cmdArgs := []string{"git", "--no-pager", "rev-parse", "--verify", "FETCH_HEAD"} - output, err := command(cmdArgs[1:]...).Output() + output, err := command(ctx, cmdArgs[1:]...).Output() + + err = ended(ctx, err) if err != nil { return "", fmt.Errorf("%s: %w", strings.Join(cmdArgs, " "), err) } @@ -326,10 +355,12 @@ func FetchHeadCommit() (string, error) { } // HeadCommit returns the hash of HEAD. -func HeadCommit() (string, error) { +func HeadCommit(ctx context.Context) (string, error) { cmdArgs := []string{"git", "--no-pager", "rev-parse", "--verify", "HEAD"} - output, err := command(cmdArgs[1:]...).Output() + output, err := command(ctx, cmdArgs[1:]...).Output() + + err = ended(ctx, err) if err != nil { return "", fmt.Errorf("%s: %w", strings.Join(cmdArgs, " "), err) } @@ -337,9 +368,21 @@ func HeadCommit() (string, error) { return strings.TrimSpace(string(output)), nil } -// command returns the git command for args, recorded at debug level. -func command(args ...string) *exec.Cmd { - slog.Debug("running git", "cmd", "git "+strings.Join(args, " ")) +// ended is err, or the context's error with it when the context is done: a +// git that was killed because its context ended says "signal: killed", and +// the caller asked whether its context ended. +func ended(ctx context.Context, err error) error { + if err == nil || ctx.Err() == nil { + return err + } + + return fmt.Errorf("%w: %w", ctx.Err(), err) +} + +// command returns the git command for args, recorded at debug level. git is +// killed when ctx ends. +func command(ctx context.Context, args ...string) *exec.Cmd { + slog.DebugContext(ctx, "running git", "cmd", "git "+strings.Join(args, " ")) - return exec.Command("git", args...) + return exec.CommandContext(ctx, "git", args...) } diff --git a/git/commits_test.go b/git/commits_test.go index 087b8e7..065cbcf 100644 --- a/git/commits_test.go +++ b/git/commits_test.go @@ -19,18 +19,18 @@ import ( func TestCommitEntry(t *testing.T) { newRepository(t, "first commit", "second commit") - head, err := HeadCommit() + head, err := HeadCommit(t.Context()) if err != nil { t.Fatal(err) } - commits, err := Commits(head) + commits, err := Commits(t.Context(), head) if err != nil { t.Fatal(err) } if len(commits) != 2 { - t.Fatalf("Commits(HEAD) = %d commits, want 2", len(commits)) + t.Fatalf("Commits(t.Context(), HEAD) = %d commits, want 2", len(commits)) } for _, commit := range commits { @@ -64,12 +64,12 @@ func TestCommandsAreRecordedAtDebug(t *testing.T) { slog.SetDefault(slog.New(slog.NewTextHandler(&records, &slog.HandlerOptions{Level: slog.LevelDebug}))) t.Cleanup(func() { slog.SetDefault(previous) }) - head, err := HeadCommit() + head, err := HeadCommit(t.Context()) if err != nil { t.Fatal(err) } - if _, err := Check(head); err != nil { + if _, err := Check(t.Context(), head); err != nil { t.Fatal(err) } @@ -187,7 +187,7 @@ func TestCommitsOutsideARepository(t *testing.T) { // No repository above it either. t.Setenv("GIT_CEILING_DIRECTORIES", filepath.Dir(dir)) - _, err := Commits("HEAD~1..HEAD") + _, err := Commits(t.Context(), "HEAD~1..HEAD") if err == nil { t.Fatal("Commits outside a repository succeeded") } diff --git a/git/context_test.go b/git/context_test.go new file mode 100644 index 0000000..30aebad --- /dev/null +++ b/git/context_test.go @@ -0,0 +1,163 @@ +package git_test + +import ( + "context" + "errors" + "io" + "os" + "os/exec" + "path/filepath" + "runtime" + "testing" + "time" + + "github.com/forkcloser/git-validation/git" +) + +// fakeGitMarker names, in the environment of a process that stands in for +// git, the file it writes once it runs. +const fakeGitMarker = "GIT_VALIDATION_FAKE_GIT" + +// TestMain makes the test binary stand in for git when it is run as one: it +// says it has started and then stays, as a git that never ends would, until +// it is killed. +func TestMain(m *testing.M) { + if marker := os.Getenv(fakeGitMarker); marker != "" { + if err := os.WriteFile(marker, []byte("started"), 0o600); err != nil { + os.Exit(1) + } + + time.Sleep(10 * time.Minute) + + return + } + + m.Run() +} + +// neverEndingGit puts a git that never ends first on PATH, and returns the +// file it writes once it runs. +func neverEndingGit(t *testing.T) string { + t.Helper() + + self, err := os.Executable() + if err != nil { + t.Fatal(err) + } + + dir := t.TempDir() + + name := "git" + if runtime.GOOS == "windows" { + name = "git.exe" + } + + source, err := os.Open(self) + if err != nil { + t.Fatal(err) + } + + defer func() { _ = source.Close() }() + + target, err := os.OpenFile(filepath.Join(dir, name), os.O_CREATE|os.O_WRONLY, 0o700) + if err != nil { + t.Fatal(err) + } + + if _, err := io.Copy(target, source); err != nil { + t.Fatal(err) + } + + if err := target.Close(); err != nil { + t.Fatal(err) + } + + marker := filepath.Join(dir, "started") + + t.Setenv("PATH", dir) + t.Setenv(fakeGitMarker, marker) + + return marker +} + +// TestContractAContextStopsARunningGit: when its context ends, a git that is +// running is killed, and the call returns the context's error with git's +// own: it does not wait for git to end. +// +//nolint:paralleltest // it sets PATH for the process +func TestContractAContextStopsARunningGit(t *testing.T) { + marker := neverEndingGit(t) + + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + + go func() { + // Cancel once git is running, not before it starts. + for { + if _, err := os.Stat(marker); err == nil { + cancel() + + return + } + + select { + case <-ctx.Done(): + return + case <-time.After(10 * time.Millisecond): + } + } + }() + + start := time.Now() + head, err := git.HeadCommit(ctx) + + if !errors.Is(err, context.Canceled) || head != "" { + t.Fatalf("HeadCommit under a context cancelled while git runs: %q, %v; want context.Canceled", head, err) + } + + // The git that stood in ends by itself only after ten minutes, with + // success: an exit that is no success is the kill. + var exit *exec.ExitError + if !errors.As(err, &exit) || exit.Success() { + t.Errorf("the error does not carry git's end by a kill: %v", err) + } + + if _, statErr := os.Stat(marker); statErr != nil { + t.Errorf("git was not running when the context ended: %v", statErr) + } + + if waited := time.Since(start); waited > time.Minute { + t.Errorf("the call returned after %v", waited) + } +} + +// TestContractADoneContextRunsNoGit: under a context that is already done, +// no git is started, and every call returns the context's error. +// +//nolint:paralleltest // it sets PATH for the process +func TestContractADoneContextRunsNoGit(t *testing.T) { + marker := neverEndingGit(t) + + done, cancel := context.WithCancel(t.Context()) + cancel() + + calls := map[string]func() error{ + "Commits": func() error { _, err := git.Commits(done, "HEAD"); return err }, + "Check": func() error { _, err := git.Check(done, "HEAD"); return err }, + "Show": func() error { _, err := git.Show(done, "HEAD"); return err }, + "LogUntil": func() error { _, err := git.LogUntil(done, "HEAD"); return err }, + "LogCommit": func() error { _, err := git.LogCommit(done, "HEAD"); return err }, + "FetchHeadCommit": func() error { _, err := git.FetchHeadCommit(done); return err }, + "HeadCommit": func() error { _, err := git.HeadCommit(done); return err }, + } + + for name, call := range calls { + if err := call(); !errors.Is(err, context.Canceled) { + t.Errorf("%s under a done context: %v; want context.Canceled", name, err) + } + } + + if _, err := os.Stat(marker); !errors.Is(err, os.ErrNotExist) { + t.Errorf("a git was started under a done context: %v", err) + } +} diff --git a/git/contract_test.go b/git/contract_test.go index a84adb9..e9e77e9 100644 --- a/git/contract_test.go +++ b/git/contract_test.go @@ -102,7 +102,7 @@ func TestContractLogCommitIsGitsView(t *testing.T) { ) second := r.commit("b", []byte("b\n"), "Second subject") - e, err := git.LogCommit(first) + e, err := git.LogCommit(t.Context(), first) if err != nil { t.Fatal(err) } @@ -133,7 +133,7 @@ func TestContractLogCommitIsGitsView(t *testing.T) { t.Errorf("tree, dates: %q %q %q", (*e)["abbreviated_tree"], (*e)["author_date"], (*e)["committer_date"]) } - e2, err := git.LogCommit(second) + e2, err := git.LogCommit(t.Context(), second) if err != nil { t.Fatal(err) } @@ -153,7 +153,7 @@ func TestContractLogCommitIsGitsView(t *testing.T) { r.git("merge", "-q", "--no-ff", "--no-edit", "side") merge := r.git("rev-parse", "HEAD") - m, err := git.LogCommit(merge) + m, err := git.LogCommit(t.Context(), merge) if err != nil { t.Fatal(err) } @@ -200,7 +200,7 @@ func TestContractCommitsListsARange(t *testing.T) { // with its whole history; it used to validate the whole history. {"nonesuch..HEAD", nil, true}, } { - got, err := git.Commits(c.rng) + got, err := git.Commits(t.Context(), c.rng) if c.refused { // The range, and git's own words on it. if err == nil || !strings.Contains(err.Error(), c.rng) || !strings.Contains(err.Error(), "fatal:") { @@ -221,11 +221,11 @@ func TestContractCommitsListsARange(t *testing.T) { } } - if head, err := git.HeadCommit(); err != nil || head != hashes[3] { + if head, err := git.HeadCommit(t.Context()); err != nil || head != hashes[3] { t.Errorf("HeadCommit %q %v, want %s", head, err, hashes[3]) } - if _, err := git.FetchHeadCommit(); err == nil { + if _, err := git.FetchHeadCommit(t.Context()); err == nil { t.Error("FetchHeadCommit with no fetch ever made: no error") } } @@ -248,15 +248,15 @@ func TestContractCheckFindsWhitespaceErrors(t *testing.T) { vendored := r.commit(filepath.Join("vendor", "d.txt"), []byte("vendored \n"), "Vendored") - if _, err := git.Check(root); err != nil { + if _, err := git.Check(t.Context(), root); err != nil { t.Errorf("the root commit: %v, want it skipped", err) } - if _, err := git.Check(clean); err != nil { + if _, err := git.Check(t.Context(), clean); err != nil { t.Errorf("a clean commit: %v", err) } - _, err := git.Check(dirty) + _, err := git.Check(t.Context(), dirty) if err == nil { t.Fatal("a commit adding trailing whitespace: no error") } @@ -266,17 +266,17 @@ func TestContractCheckFindsWhitespaceErrors(t *testing.T) { t.Errorf("a commit adding trailing whitespace: %v, want git's exit status 2", err) } - if _, err := git.Check(vendored); err == nil { + if _, err := git.Check(t.Context(), vendored); err == nil { t.Error("a vendored file with trailing whitespace, no exclusion: no error") } t.Setenv("GIT_CHECK_EXCLUDE", "./vendor::./nonesuch") - if _, err := git.Check(vendored); err != nil { + if _, err := git.Check(t.Context(), vendored); err != nil { t.Errorf("a vendored file with trailing whitespace, vendor excluded: %v", err) } - if _, err := git.Check(dirty); err == nil { + if _, err := git.Check(t.Context(), dirty); err == nil { t.Error("the dirty commit outside the excluded paths: no error") } } diff --git a/rules/danglingwhitespace/rule.go b/rules/danglingwhitespace/rule.go index 151513f..59f2be0 100644 --- a/rules/danglingwhitespace/rule.go +++ b/rules/danglingwhitespace/rule.go @@ -1,6 +1,7 @@ package danglingwhitespace import ( + "context" "errors" "os/exec" @@ -26,12 +27,12 @@ func init() { } // ValidateDanglingWhitespace runs Git's check to look for whitespace errors. -func ValidateDanglingWhitespace(r validate.Rule, c git.CommitEntry) (vr validate.Result) { +func ValidateDanglingWhitespace(ctx context.Context, r validate.Rule, c git.CommitEntry) (vr validate.Result) { vr.CommitEntry = c vr.Msg = "commit does not have any whitespace errors" vr.Pass = true - _, err := git.Check(c["commit"]) + _, err := git.Check(ctx, c["commit"]) if err != nil { vr.Pass = false diff --git a/rules/dco/dco.go b/rules/dco/dco.go index 1f6f6b1..131a02f 100644 --- a/rules/dco/dco.go +++ b/rules/dco/dco.go @@ -1,6 +1,7 @@ package dco import ( + "context" "regexp" "strings" @@ -25,7 +26,7 @@ var ( ) // ValidateDCO checks that the commit has been signed off, per the DCO process. -func ValidateDCO(r validate.Rule, c git.CommitEntry) (vr validate.Result) { +func ValidateDCO(_ context.Context, r validate.Rule, c git.CommitEntry) (vr validate.Result) { vr.CommitEntry = c if len(strings.Split(c["parent"], " ")) > 1 { vr.Pass = true diff --git a/rules/messageregexp/rule.go b/rules/messageregexp/rule.go index cc627c3..adf61a8 100644 --- a/rules/messageregexp/rule.go +++ b/rules/messageregexp/rule.go @@ -1,6 +1,7 @@ package messageregexp import ( + "context" "fmt" "regexp" "strings" @@ -33,7 +34,7 @@ func CheckValue(value string) error { } // ValidateMessageRegexp is the message regex func to run. -func ValidateMessageRegexp(r validate.Rule, c git.CommitEntry) (vr validate.Result) { +func ValidateMessageRegexp(_ context.Context, r validate.Rule, c git.CommitEntry) (vr validate.Result) { if r.Value == "" { vr.Pass = true vr.Msg = "noop: message_regexp value is blank" diff --git a/rules/messageregexp/rule_test.go b/rules/messageregexp/rule_test.go index de6bbfc..608aa90 100644 --- a/rules/messageregexp/rule_test.go +++ b/rules/messageregexp/rule_test.go @@ -41,14 +41,14 @@ func TestRunWithoutCheckFailsTheCommit(t *testing.T) { rule := validate.Rule{Name: "message_regexp", Value: "("} commit := git.CommitEntry{"commit": "1111", "subject": "(", "body": "", "parent": "0000"} - got := messageregexp.ValidateMessageRegexp(rule, commit) + got := messageregexp.ValidateMessageRegexp(t.Context(), rule, commit) if got.Pass || !strings.Contains(got.Msg, "message_regexp value does not compile") { t.Errorf("Run with \"(\": pass=%v msg=%q", got.Pass, got.Msg) } rule.Value = "^\\(" - if got := messageregexp.ValidateMessageRegexp(rule, commit); !got.Pass { + if got := messageregexp.ValidateMessageRegexp(t.Context(), rule, commit); !got.Pass { t.Errorf("Run with a matching value: pass=%v msg=%q", got.Pass, got.Msg) } } diff --git a/rules/shortsubject/shortsubject.go b/rules/shortsubject/shortsubject.go index 726b341..3b4a186 100644 --- a/rules/shortsubject/shortsubject.go +++ b/rules/shortsubject/shortsubject.go @@ -1,6 +1,7 @@ package shortsubject import ( + "context" "strings" "unicode/utf8" @@ -22,7 +23,7 @@ func init() { // ValidateShortSubject checks that the commit's subject is strictly less than // 90 characters (preferably not more than 72 chars). -func ValidateShortSubject(r validate.Rule, c git.CommitEntry) (vr validate.Result) { +func ValidateShortSubject(_ context.Context, r validate.Rule, c git.CommitEntry) (vr validate.Result) { if len(strings.Split(c["parent"], " ")) > 1 { vr.Pass = true vr.Msg = "merge commits do not require length check" diff --git a/validate/contract_test.go b/validate/contract_test.go index 0f9c34e..0a253fd 100644 --- a/validate/contract_test.go +++ b/validate/contract_test.go @@ -147,7 +147,7 @@ func TestContractDCO(t *testing.T) { subject = "Signed-off-by: Ada Lovelace " } - r := dco.ValidateDCO(dco.DcoRule, entry(subject, c.body, parentA)) + r := dco.ValidateDCO(t.Context(), dco.DcoRule, entry(subject, c.body, parentA)) if r.Pass != c.pass { t.Errorf("pass %v (%q), want %v", r.Pass, r.Msg, c.pass) } @@ -158,7 +158,7 @@ func TestContractDCO(t *testing.T) { }) } - if r := dco.ValidateDCO(dco.DcoRule, entry("Merge branch", "", parentA, parentB)); !r.Pass { + if r := dco.ValidateDCO(t.Context(), dco.DcoRule, entry("Merge branch", "", parentA, parentB)); !r.Pass { t.Errorf("an unsigned merge commit: %q, want a pass", r.Msg) } } @@ -210,7 +210,7 @@ func TestContractShortSubject(t *testing.T) { {90, false, "exceeds 90"}, {200, false, "exceeds 90"}, } { - r := shortsubject.ValidateShortSubject( + r := shortsubject.ValidateShortSubject(t.Context(), shortsubject.ShortSubjectRule, entry(strings.Repeat("s", c.length), "", parentA), ) @@ -220,7 +220,7 @@ func TestContractShortSubject(t *testing.T) { } long := strings.Repeat("s", 200) - if r := shortsubject.ValidateShortSubject( + if r := shortsubject.ValidateShortSubject(t.Context(), shortsubject.ShortSubjectRule, entry(long, "", parentA, parentB), ); !r.Pass { @@ -247,18 +247,18 @@ func TestContractMessageRegexp(t *testing.T) { {"nothing matches", "fix", "details", false}, {"match not at a line start", "see JIRA-12", "", false}, } { - r := messageregexp.ValidateMessageRegexp(rule, entry(c.subject, c.body, parentA)) + r := messageregexp.ValidateMessageRegexp(t.Context(), rule, entry(c.subject, c.body, parentA)) if r.Pass != c.pass { t.Errorf("%s: pass %v (%q), want %v", c.name, r.Pass, r.Msg, c.pass) } } - if r := messageregexp.ValidateMessageRegexp(rule, entry("fix", "", parentA, parentB)); !r.Pass { + if r := messageregexp.ValidateMessageRegexp(t.Context(), rule, entry("fix", "", parentA, parentB)); !r.Pass { t.Errorf("a merge commit: %q, want a pass", r.Msg) } blank := messageregexp.RegexpRule - if r := messageregexp.ValidateMessageRegexp( + if r := messageregexp.ValidateMessageRegexp(t.Context(), blank, entry("fix", "", parentA), ); !r.Pass || @@ -278,7 +278,7 @@ func TestContractResultsCount(t *testing.T) { rules := validate.FilterRules(validate.RegisteredRules, validate.SanitizeFilters("DCO,short-subject")) - results := validate.Commit(entry(strings.Repeat("s", 95), "", parentA), rules) + results := validate.Commit(t.Context(), entry(strings.Repeat("s", 95), "", parentA), rules) if len(results) != 2 { t.Fatalf("%d results for two rules", len(results)) } @@ -287,12 +287,12 @@ func TestContractResultsCount(t *testing.T) { t.Errorf("pass %d fail %d, want 0 and 2", pass, fail) } - mixed := validate.Commit(entry("short", "Signed-off-by: A ", parentA), rules) + mixed := validate.Commit(t.Context(), entry("short", "Signed-off-by: A ", parentA), rules) if pass, fail := mixed.PassFail(); pass != 2 || fail != 0 { t.Errorf("pass %d fail %d for two passing rules, want 2 and 0", pass, fail) } - half := validate.Commit(entry("short", "", parentA), rules) + half := validate.Commit(t.Context(), entry("short", "", parentA), rules) if pass, fail := half.PassFail(); pass != 1 || fail != 1 { t.Errorf("pass %d fail %d for one of two rules failing, want 1 and 1", pass, fail) } @@ -344,8 +344,8 @@ func TestContractCommitsToFixCountsCommits(t *testing.T) { var results validate.Results - results = append(results, validate.Commit(bad, rules)...) - results = append(results, validate.Commit(good, rules)...) + results = append(results, validate.Commit(t.Context(), bad, rules)...) + results = append(results, validate.Commit(t.Context(), good, rules)...) if n := results.CommitsToFix(); n != 1 { t.Errorf("CommitsToFix %d for one commit failing two rules beside a clean one, want 1", n) diff --git a/validate/rules.go b/validate/rules.go index 2d6d2bd..65badaf 100644 --- a/validate/rules.go +++ b/validate/rules.go @@ -1,6 +1,7 @@ package validate import ( + "context" "errors" "fmt" "slices" @@ -36,7 +37,7 @@ type Rule struct { Name string // short name for reference in the `-run=...` flag Value string // value to configure for the rule (i.e. a regexp to check for in the commit message) Description string // longer Description for readability - Run func(Rule, git.CommitEntry) Result + Run func(context.Context, Rule, git.CommitEntry) Result Default bool // whether the registered rule is run by default // Check refuses a Value the rule cannot run with, before any commit is // read (NewRunner calls it); nil for a rule that takes no value. @@ -45,11 +46,11 @@ type Rule struct { // Commit processes the given rules on the provided commit, and returns the // result set, every result carrying the commit (not every rule sets it). -func Commit(c git.CommitEntry, rules []Rule) Results { +func Commit(ctx context.Context, c git.CommitEntry, rules []Rule) Results { results := Results{} for _, r := range rules { - res := r.Run(r, c) + res := r.Run(ctx, r, c) res.CommitEntry = c results = append(results, res) } diff --git a/validate/rules_test.go b/validate/rules_test.go index ef54d6b..b5a7d04 100644 --- a/validate/rules_test.go +++ b/validate/rules_test.go @@ -1,6 +1,7 @@ package validate import ( + "context" "errors" "slices" "strings" @@ -155,10 +156,10 @@ func TestCommitsToFix(t *testing.T) { // return. func TestCommitStampsTheEntry(t *testing.T) { entry := git.CommitEntry{"commit": "1111"} - fail := func(Rule, git.CommitEntry) Result { return Result{Pass: false, Msg: "no"} } + fail := func(context.Context, Rule, git.CommitEntry) Result { return Result{Pass: false, Msg: "no"} } rules := []Rule{{Name: "a", Run: fail}, {Name: "b", Run: fail}} - results := Commit(entry, rules) + results := Commit(t.Context(), entry, rules) if len(results) != 2 { t.Fatalf("Commit returned %d results, want 2", len(results)) } diff --git a/validate/runner.go b/validate/runner.go index 8f91a65..9767d94 100644 --- a/validate/runner.go +++ b/validate/runner.go @@ -1,6 +1,7 @@ package validate import ( + "context" "errors" "fmt" "log/slog" @@ -26,7 +27,7 @@ type Runner struct { // NewRunner returns an initiallized Runner. A rule whose Check refuses its // Value is refused here, before any commit is read. -func NewRunner(root string, rules []Rule, commitrange string) (runner *Runner, err error) { +func NewRunner(ctx context.Context, root string, rules []Rule, commitrange string) (runner *Runner, err error) { for _, rule := range rules { if rule.Check == nil { continue @@ -59,15 +60,15 @@ func NewRunner(root string, rules []Rule, commitrange string) (runner *Runner, e return nil, fmt.Errorf("entering %q: %w", newroot, err) } - commitrange, err = git.FetchHeadCommit() + commitrange, err = git.FetchHeadCommit(ctx) if err != nil { - commitrange, err = git.HeadCommit() + commitrange, err = git.HeadCommit(ctx) if err != nil { return nil, fmt.Errorf("finding a commit to validate from in %q: %w", newroot, err) } } - slog.Debug("no commit range given, validating from", "commit", commitrange) + slog.DebugContext(ctx, "no commit range given, validating from", "commit", commitrange) } return &Runner{ @@ -78,8 +79,10 @@ func NewRunner(root string, rules []Rule, commitrange string) (runner *Runner, e } // Run processes the rules for each commit in the range provided, appending -// every result to Results. -func (r *Runner) Run() (err error) { +// every result to Results. When ctx ends it stops and returns the context's +// error: Results holds the commits validated whole before that, and nothing +// of the one it was at. +func (r *Runner) Run(ctx context.Context) (err error) { cwd, err := os.Getwd() if err != nil { return fmt.Errorf("finding the working directory: %w", err) @@ -95,13 +98,24 @@ func (r *Runner) Run() (err error) { } // collect the entries - c, err := git.Commits(r.CommitRange) + c, err := git.Commits(ctx, r.CommitRange) if err != nil { return fmt.Errorf("listing the commits of %q: %w", r.CommitRange, err) } for _, commit := range c { - vr := Commit(commit, r.Rules) + if err = ctx.Err(); err != nil { + return fmt.Errorf("stopped before %s: %w", commit["abbreviated_commit"], err) + } + + vr := Commit(ctx, commit, r.Rules) + + // A rule that was stopped has not judged the commit: what it + // returned is the stop, and is not a result. + if err = ctx.Err(); err != nil { + return fmt.Errorf("stopped at %s: %w", commit["abbreviated_commit"], err) + } + r.Results = append(r.Results, vr...) if r.Report != nil { diff --git a/validate/runner_test.go b/validate/runner_test.go index 9b62b58..2919444 100644 --- a/validate/runner_test.go +++ b/validate/runner_test.go @@ -2,14 +2,17 @@ package validate_test import ( "bytes" + "context" "errors" "log/slog" "os" "os/exec" "path/filepath" + "runtime" "slices" "strings" "testing" + "time" gitpkg "github.com/forkcloser/git-validation/git" "github.com/forkcloser/git-validation/validate" @@ -72,7 +75,7 @@ func TestNewRunnerRecordsTheCommitItStartsFrom(t *testing.T) { slog.SetDefault(slog.New(slog.NewTextHandler(&records, &slog.HandlerOptions{Level: slog.LevelDebug}))) t.Cleanup(func() { slog.SetDefault(previous) }) - runner, err := validate.NewRunner(dir, nil, "") + runner, err := validate.NewRunner(t.Context(), dir, nil, "") if err != nil { t.Fatal(err) } @@ -108,7 +111,7 @@ func TestNewRunnerRefusesAValueItsRuleCannotRun(t *testing.T) { }, } - runner, err := validate.NewRunner(t.TempDir(), []validate.Rule{rule}, "HEAD") + runner, err := validate.NewRunner(t.Context(), t.TempDir(), []validate.Rule{rule}, "HEAD") if !errors.Is(err, errValueDoesNotCompile) || runner != nil { t.Fatalf("NewRunner with a refused value: %v, %v", runner, err) } @@ -121,7 +124,7 @@ func TestNewRunnerRefusesAValueItsRuleCannotRun(t *testing.T) { rules := []validate.Rule{rule, {Name: "unchecked"}} - if _, err := validate.NewRunner(t.TempDir(), rules, "HEAD"); err != nil { + if _, err := validate.NewRunner(t.Context(), t.TempDir(), rules, "HEAD"); err != nil { t.Fatalf("NewRunner with an accepted value and an unchecked rule: %v", err) } } @@ -143,10 +146,10 @@ func TestRunIsSilentAndReportsEachCommit(t *testing.T) { git("commit", "-q", "--allow-empty", "-m", "second") rules := []validate.Rule{ - {Name: "is-first", Run: func(_ validate.Rule, c gitpkg.CommitEntry) validate.Result { + {Name: "is-first", Run: func(_ context.Context, _ validate.Rule, c gitpkg.CommitEntry) validate.Result { return validate.Result{Pass: c["subject"] == "first", Msg: "is-first on " + c["subject"]} }}, - {Name: "always", Run: func(_ validate.Rule, c gitpkg.CommitEntry) validate.Result { + {Name: "always", Run: func(_ context.Context, _ validate.Rule, c gitpkg.CommitEntry) validate.Result { return validate.Result{Pass: true, Msg: "always on " + c["subject"]} }}, } @@ -154,7 +157,7 @@ func TestRunIsSilentAndReportsEachCommit(t *testing.T) { for _, quiet := range []string{"", "1"} { t.Setenv("QUIET", quiet) - runner, err := validate.NewRunner(dir, rules, base+"..HEAD") + runner, err := validate.NewRunner(t.Context(), dir, rules, base+"..HEAD") if err != nil { t.Fatal(err) } @@ -181,7 +184,7 @@ func TestRunIsSilentAndReportsEachCommit(t *testing.T) { } printed := stdoutOf(t, func() { - if err := runner.Run(); err != nil { + if err := runner.Run(t.Context()); err != nil { t.Fatal(err) } }) @@ -203,12 +206,12 @@ func TestRunIsSilentAndReportsEachCommit(t *testing.T) { } } - runner, err := validate.NewRunner(dir, rules, base+"..HEAD") + runner, err := validate.NewRunner(t.Context(), dir, rules, base+"..HEAD") if err != nil { t.Fatal(err) } - if err := runner.Run(); err != nil || len(runner.Results) != 4 { + if err := runner.Run(t.Context()); err != nil || len(runner.Results) != 4 { t.Errorf("Run with no Report: %v, %d results; want 4", err, len(runner.Results)) } } @@ -242,3 +245,227 @@ func stdoutOf(t *testing.T, run func()) string { return string(out) } + +// ctxKey is the key of a value the context test puts in its context. +type ctxKey struct{} + +// TestRunPassesItsContextAndStopsWhenItEnds: Run hands its context to every +// rule; under a context that is done it lists no commit, runs no rule, and +// returns the context's error. +// +//nolint:paralleltest // newRepo sets the environment with t.Setenv +func TestRunPassesItsContextAndStopsWhenItEnds(t *testing.T) { + dir, git := newRepo(t) + + git("init", "-q") + git("commit", "-q", "--allow-empty", "-m", "base") + git("commit", "-q", "--allow-empty", "-m", "first") + + ran := 0 + rules := []validate.Rule{ + {Name: "sees", Run: func(ctx context.Context, _ validate.Rule, _ gitpkg.CommitEntry) validate.Result { + ran++ + + return validate.Result{Pass: ctx.Value(ctxKey{}) == "the caller's"} + }}, + } + + runner, err := validate.NewRunner(t.Context(), dir, rules, "HEAD") + if err != nil { + t.Fatal(err) + } + + if err = runner.Run(context.WithValue(t.Context(), ctxKey{}, "the caller's")); err != nil { + t.Fatal(err) + } + + if _, fail := runner.Results.PassFail(); ran != 2 || fail != 0 { + t.Errorf("the rule ran %d times and %d did not see the caller's context; want 2 and 0", ran, fail) + } + + done, cancel := context.WithCancel(t.Context()) + cancel() + + stopped, err := validate.NewRunner(t.Context(), dir, rules, "HEAD") + if err != nil { + t.Fatal(err) + } + + if err = stopped.Run(done); !errors.Is(err, context.Canceled) { + t.Errorf("Run under a done context: %v; want context.Canceled", err) + } + + if ran != 2 || len(stopped.Results) != 0 { + t.Errorf("under a done context the rule ran %d more times and Results holds %d", ran-2, len(stopped.Results)) + } + + if _, err = validate.NewRunner(done, dir, rules, ""); !errors.Is(err, context.Canceled) { + t.Errorf("NewRunner with no range, under a done context: %v; want context.Canceled", err) + } +} + +// TestRunStopsAmongTheCommits: a context that ends while Run is among the +// commits stops it there. Run returns the context's error, Results holds +// the commits validated whole and nothing of the one a rule was stopped at, +// Report is called for those alone, and no rule runs for a later commit. +// +//nolint:paralleltest // newRepo sets the environment with t.Setenv +func TestRunStopsAmongTheCommits(t *testing.T) { + dir, git := newRepo(t) + + git("init", "-q") + git("commit", "-q", "--allow-empty", "-m", "base") + + for _, subject := range []string{"one", "two", "three", "four", "five"} { + git("commit", "-q", "--allow-empty", "-m", subject) + } + + for _, stopAt := range []struct { + name string + // The run is stopped by the rule, at its nth call, or by Report, + // after its nth commit. + inRule, inReport int + results int + }{ + {"in a rule, at the first commit", 1, 0, 0}, + {"in a rule, at the third commit", 3, 0, 4}, + {"between the second commit and the third", 0, 2, 4}, + } { + ctx, cancel := context.WithCancel(t.Context()) + + calls := 0 + rules := []validate.Rule{ + {Name: "stops", Run: func(_ context.Context, _ validate.Rule, _ gitpkg.CommitEntry) validate.Result { + calls++ + if calls == stopAt.inRule { + cancel() + } + + return validate.Result{Pass: true} + }}, + {Name: "asks git", Run: func(ctx context.Context, _ validate.Rule, c gitpkg.CommitEntry) validate.Result { + _, err := gitpkg.Show(ctx, c["commit"]) + + return validate.Result{Pass: err == nil} + }}, + } + + runner, err := validate.NewRunner(t.Context(), dir, rules, "HEAD~5..HEAD") + if err != nil { + t.Fatal(err) + } + + reported := 0 + runner.Report = func(gitpkg.CommitEntry, validate.Results) { + reported++ + if reported == stopAt.inReport { + cancel() + } + } + + if err = runner.Run(ctx); !errors.Is(err, context.Canceled) { + t.Errorf("%s: Run = %v; want context.Canceled", stopAt.name, err) + } + + if _, fail := runner.Results.PassFail(); len(runner.Results) != stopAt.results || fail != 0 { + t.Errorf("%s: Results holds %d results, %d of them failures; want %d and none", + stopAt.name, len(runner.Results), fail, stopAt.results) + } + + if want := stopAt.results / len(rules); reported != want { + t.Errorf("%s: Report was called %d times, want %d", stopAt.name, reported, want) + } + + if want := max(stopAt.inRule, stopAt.inReport); calls != want { + t.Errorf("%s: the first rule ran %d times, want %d", stopAt.name, calls, want) + } + + cancel() + } +} + +// fakeGitMarker names, in the environment of a process that stands in for +// git, the file it writes once it runs. +const fakeGitMarker = "GIT_VALIDATION_FAKE_GIT" + +// TestMain makes the test binary stand in for a git that never ends when it +// is run as one. +func TestMain(m *testing.M) { + if marker := os.Getenv(fakeGitMarker); marker != "" { + if err := os.WriteFile(marker, []byte("started"), 0o600); err != nil { + os.Exit(1) + } + + time.Sleep(10 * time.Minute) + + return + } + + m.Run() +} + +// TestNewRunnerStopsLookingWhenItsContextEnds: with no range given, +// NewRunner asks git where to validate from; when its context ends while +// git runs, it returns the context's error and does not wait for git. +func TestNewRunnerStopsLookingWhenItsContextEnds(t *testing.T) { + self, err := os.Executable() + if err != nil { + t.Fatal(err) + } + + content, err := os.ReadFile(self) + if err != nil { + t.Fatal(err) + } + + dir := t.TempDir() + + name := "git" + if runtime.GOOS == "windows" { + name = "git.exe" + } + + if err = os.WriteFile(filepath.Join(dir, name), content, 0o700); err != nil { + t.Fatal(err) + } + + marker := filepath.Join(dir, "started") + + t.Setenv("PATH", dir) + t.Setenv(fakeGitMarker, marker) + + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + + go func() { + for { + if _, statErr := os.Stat(marker); statErr == nil { + cancel() + + return + } + + select { + case <-ctx.Done(): + return + case <-time.After(10 * time.Millisecond): + } + } + }() + + returned := make(chan error, 1) + + go func() { + _, runnerErr := validate.NewRunner(ctx, t.TempDir(), nil, "") + returned <- runnerErr + }() + + select { + case err = <-returned: + if !errors.Is(err, context.Canceled) { + t.Errorf("NewRunner under a context cancelled while git runs: %v; want context.Canceled", err) + } + case <-time.After(time.Minute): + t.Fatal("NewRunner is still waiting for git a minute after its context ended") + } +}