Skip to content

Commit

Permalink
Support defer: for deferring steps in a runbook
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Dec 17, 2024
1 parent ea242a3 commit fa3ed07
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 44 deletions.
3 changes: 3 additions & 0 deletions defer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package runn

const deferSectionKey = "defer"
79 changes: 79 additions & 0 deletions defer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package runn

import (
"context"
"testing"
)

func TestDeferRun(t *testing.T) {
book := "testdata/book/defer.yml"
ctx := context.Background()
o, err := New(Book(book))
if err != nil {
t.Fatal(err)
}
if err := o.Run(ctx); err == nil {
t.Fatal("expected error")
}
if want := 8; len(o.store.steps) != want {
t.Errorf("o.store.steps got %v, want %v", len(o.store.steps), want)
}
r := o.Result()
if want := 8; len(r.StepResults) != want {
t.Errorf("r.StepResults got %v, want %v", len(r.StepResults), want)
}

t.Run("main steps", func(t *testing.T) {
wantResults := []struct {
desc string
skipped bool
err bool
}{
{"step 1", false, false},
{"include step", false, false},
{"step 2", false, false},
{"step 3", false, true},
{"step 4", true, false},
{"defererd step c", false, false},
{"defererd step b", false, true},
{"defererd step a", false, false},
}
for i, want := range wantResults {
got := r.StepResults[i]
if got.Desc != want.desc {
t.Errorf("got %v, want %v", got.Desc, want.desc)
}
if got.Skipped != want.skipped {
t.Errorf("got %v, want %v", got.Skipped, want.skipped)
}
if (got.Err == nil) == want.err {
t.Errorf("got %v, want %v", got.Err, want.err)
}
}
})

t.Run("include steps", func(t *testing.T) {
wantResults := []struct {
desc string
skipped bool
err bool
}{
{"included step 1", false, false},
{"included step 2", false, false},
{"included defererd step d", false, false},
}

for i, want := range wantResults {
got := r.StepResults[1].IncludedRunResults[0].StepResults[i]
if got.Desc != want.desc {
t.Errorf("got %v, want %v", got.Desc, want.desc)
}
if got.Skipped != want.skipped {
t.Errorf("got %v, want %v", got.Skipped, want.skipped)
}
if (got.Err == nil) == want.err {
t.Errorf("got %v, want %v", got.Err, want.err)
}
}
})
}
1 change: 1 addition & 0 deletions include.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ func (o *operator) newNestedOperator(parent *step, opts ...Option) (*operator, e
oo.store.runNIndex = o.store.runNIndex
oo.dbg = o.dbg
oo.nm = o.nm
oo.deferred = o.deferred
return oo, nil
}

Expand Down
Loading

0 comments on commit fa3ed07

Please sign in to comment.