Skip to content

Commit

Permalink
Call DebugFn before cleanup of the last step that does not succeed.
Browse files Browse the repository at this point in the history
  • Loading branch information
orivej committed Oct 4, 2015
1 parent 22a40b8 commit e02bce9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
13 changes: 8 additions & 5 deletions debug_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ func (r *DebugRunner) Run(state StateBag) {
pauseFn = DebugPauseDefault
}

// Rebuild the steps so that we insert the pause step after each
steps := make([]Step, len(r.Steps)*2)
// Wrap steps to call PauseFn after each run and before each cleanup
steps := make([]Step, len(r.Steps))
for i, step := range r.Steps {
steps[i*2] = step
steps[(i*2)+1] = &debugStepPause{
steps[i] = &debugStepPause{
reflect.Indirect(reflect.ValueOf(step)).Type().Name(),
step,
pauseFn,
}
}
Expand Down Expand Up @@ -97,14 +97,17 @@ func DebugPauseDefault(loc DebugLocation, name string, state StateBag) {

type debugStepPause struct {
StepName string
Step Step
PauseFn DebugPauseFn
}

func (s *debugStepPause) Run(state StateBag) StepAction {
action := s.Step.Run(state)
s.PauseFn(DebugLocationAfterRun, s.StepName, state)
return ActionContinue
return action
}

func (s *debugStepPause) Cleanup(state StateBag) {
s.PauseFn(DebugLocationBeforeCleanup, s.StepName, state)
s.Step.Cleanup(state)
}
37 changes: 37 additions & 0 deletions debug_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,40 @@ func TestDebugPauseDefault(t *testing.T) {
t.Fatal("didn't complete")
}
}

// confirm that a halting step is debuggable before cleanup
func TestDebugRunner_Halt(t *testing.T) {
data := new(BasicStateBag)
stepA := &TestStepAcc{Data: "a"}
stepB := &TestStepAcc{Data: "b", Halt: true}
stepC := &TestStepAcc{Data: "c"}

key := "data"
pauseFn := func(loc DebugLocation, name string, state StateBag) {
direction := "Run"
if loc == DebugLocationBeforeCleanup {
direction = "Cleanup"
}

if _, ok := state.GetOk(key); !ok {
state.Put(key, []string{})
}

data := state.Get(key).([]string)
state.Put(key, append(data, direction))
}

r := &DebugRunner{
Steps: []Step{stepA, stepB, stepC},
PauseFn: pauseFn,
}

r.Run(data)

// Test data
expected := []string{"a", "Run", "b", "Run", "Cleanup", "Cleanup"}
results := data.Get("data").([]string)
if !reflect.DeepEqual(results, expected) {
t.Errorf("unexpected results: %#v", results)
}
}

0 comments on commit e02bce9

Please sign in to comment.