Skip to content

Commit 4e4e936

Browse files
git: surface cancellation after verify on final attempt
When verify reports not clean on the last attempt and the context is cancelled during verify, return a cancellation error instead of ErrRewriteAttemptsExceeded. Add a regression test. Co-authored-by: Ben Schellenberger <bschellenberger2600@users.noreply.github.com>
1 parent e8f8fef commit 4e4e936

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

‎git/rewrite_scenario.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ func RunRewriteScenario(ctx context.Context, opts RewriteScenarioOptions) (Rewri
125125
result.Clean = true
126126
return result, nil
127127
}
128+
129+
if err := ctx.Err(); err != nil {
130+
result.Attempts = attempt
131+
return result, fmt.Errorf("rewrite scenario cancelled after verify on attempt %d: %w", attempt, err)
132+
}
128133
}
129134

130135
result.Attempts = opts.MaxAttempts

‎git/rewrite_scenario_test.go‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,39 @@ func TestRunRewriteScenario_CancelledAfterInterveneSkipsVerify(t *testing.T) {
415415
t.Fatalf("verifyCalls = %d, want 0", verifyCalls)
416416
}
417417
}
418+
419+
func TestRunRewriteScenario_CancelledInVerifyOnLastAttempt(t *testing.T) {
420+
ctx, cancel := context.WithCancel(context.Background())
421+
422+
result, err := RunRewriteScenario(ctx, RewriteScenarioOptions{
423+
MaxAttempts: 1,
424+
Detect: func(_ context.Context, _ int) (bool, error) {
425+
return true, nil
426+
},
427+
Intervene: func(_ context.Context, _ int) error {
428+
return nil
429+
},
430+
Verify: func(_ context.Context, _ int) (bool, error) {
431+
cancel()
432+
return false, nil
433+
},
434+
})
435+
if err == nil {
436+
t.Fatal("RunRewriteScenario() error = nil, want cancellation error")
437+
}
438+
if !errors.Is(err, context.Canceled) {
439+
t.Fatalf("RunRewriteScenario() error = %v, want context.Canceled", err)
440+
}
441+
if !strings.Contains(err.Error(), "cancelled after verify") {
442+
t.Fatalf("error = %v, want message about post-verify cancellation", err)
443+
}
444+
if result.Attempts != 1 {
445+
t.Fatalf("result.Attempts = %d, want 1", result.Attempts)
446+
}
447+
if len(result.Passes) != 1 || !result.Passes[0].Intervened || result.Passes[0].CleanAfterVerify {
448+
t.Fatalf("passes = %+v, want one pass with verify outcome recorded", result.Passes)
449+
}
450+
if errors.Is(err, ErrRewriteAttemptsExceeded) {
451+
t.Fatal("error should not be ErrRewriteAttemptsExceeded when context was cancelled")
452+
}
453+
}

0 commit comments

Comments
 (0)