Skip to content

Commit

Permalink
Test that Cleanup is properly called from BasicRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Jun 4, 2013
1 parent c4395e1 commit 36557e6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
16 changes: 16 additions & 0 deletions basic_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@ func TestBasicRunner_Run(t *testing.T) {
r := &BasicRunner{[]Step{stepA, stepB}}
r.Run(data)

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

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

func TestBasicRunner_Run_Halt(t *testing.T) {
Expand All @@ -37,9 +45,17 @@ func TestBasicRunner_Run_Halt(t *testing.T) {
r := &BasicRunner{[]Step{stepA, stepB, stepC}}
r.Run(data)

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

// Test cleanup data
expected = []string{"b", "a"}
results = data["cleanup"].([]string)
if !reflect.DeepEqual(results, expected) {
t.Errorf("unexpected result: %#v", results)
}
}
21 changes: 13 additions & 8 deletions multistep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ type TestStepAcc struct {
}

func (s TestStepAcc) Run(state map[string]interface{}) StepAction {
if _, ok := state["data"]; !ok {
state["data"] = make([]string, 0, 5)
}

data := state["data"].([]string)
data = append(data, s.Data)
state["data"] = data
s.insertData(state, "data")

if s.Halt {
return ActionHalt
Expand All @@ -27,5 +21,16 @@ func (s TestStepAcc) Run(state map[string]interface{}) StepAction {
return ActionContinue
}

func (s TestStepAcc) Cleanup(map[string]interface{}) {
func (s TestStepAcc) Cleanup(state map[string]interface{}) {
s.insertData(state, "cleanup")
}

func (s TestStepAcc) insertData(state map[string]interface{}, key string) {
if _, ok := state[key]; !ok {
state[key] = make([]string, 0, 5)
}

data := state[key].([]string)
data = append(data, s.Data)
state[key] = data
}

0 comments on commit 36557e6

Please sign in to comment.