diff options
Diffstat (limited to 'internal/apply/swap_test.go')
| -rw-r--r-- | internal/apply/swap_test.go | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/internal/apply/swap_test.go b/internal/apply/swap_test.go index c185686..ccac945 100644 --- a/internal/apply/swap_test.go +++ b/internal/apply/swap_test.go @@ -3,6 +3,7 @@ package apply import ( + "context" "os" "path/filepath" "strings" @@ -144,7 +145,7 @@ func TestChainLoggedReportsEachStepBeforeTheNext(t *testing.T) { plan.Step{Kind: plan.Rename, Src: moved, Dst: renamed}, ) var calls []int - res, err := ChainLogged(c, func(i int, sr StepResult) error { + res, err := ChainLogged(context.Background(), c, func(i int, sr StepResult) error { calls = append(calls, i) if i == 0 { if _, err := os.Lstat(moved); err != nil { @@ -185,3 +186,34 @@ func TestChainRefusesToDisplaceANonRegularTarget(t *testing.T) { t.Errorf("the directory was displaced: %v", err) } } + +// TestChainLoggedStopsBetweenStepsWhenInterrupted: once the context is +// cancelled, the chain finishes the step it is on and skips the rest, so an +// interrupt stops after the current step, not after the file's whole chain +// (re-review pa F7). +func TestChainLoggedStopsBetweenStepsWhenInterrupted(t *testing.T) { + root := t.TempDir() + p := filepath.Join(root, "a.pdf") + moved := filepath.Join(root, "W", "a.pdf") + c := planned(t, root, "a.pdf", "body", + plan.Step{Kind: plan.Move, Src: p, Dst: moved}, + plan.Step{Kind: plan.Rename, Src: moved, Dst: filepath.Join(root, "W", "b.pdf")}, + ) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + res, err := ChainLogged(ctx, c, func(i int, sr StepResult) error { + if i == 0 { + cancel() + } + return nil + }) + if err != nil { + t.Fatal(err) + } + if res[0].Status != "ok" || res[1].Status != "skipped" || res[1].Detail != "interrupted" { + t.Errorf("steps = %s, %s %q; want ok, then skipped as interrupted", res[0].Status, res[1].Status, res[1].Detail) + } + if _, err := os.Lstat(moved); err != nil { + t.Errorf("the finished step was undone or never ran: %v", err) + } +} |
