forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_stage.go
73 lines (56 loc) · 1.53 KB
/
fake_stage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package fakes
import (
biui "github.com/cloudfoundry/bosh-cli/ui"
)
type FakeStage struct {
PerformCalls []*PerformCall
SubStages []*FakeStage
}
type PerformCall struct {
Name string
Error error
SkipError error
Stage *FakeStage
}
func NewFakeStage() *FakeStage {
return &FakeStage{}
}
func (s *FakeStage) Perform(name string, closure func() error) error {
call := &PerformCall{Name: name}
// lazily instantiate to make matching sub-stages easier
if s.PerformCalls == nil {
s.PerformCalls = []*PerformCall{}
}
s.PerformCalls = append(s.PerformCalls, call) //We want to record the calls in the same order as the real implementation would print them
err := closure()
call.Error = err
if err != nil {
if skipErr, isSkipError := err.(biui.SkipStageError); isSkipError {
call.SkipError = skipErr
err = nil
}
}
return err
}
func (s *FakeStage) PerformComplex(name string, closure func(biui.Stage) error) error {
subStage := NewFakeStage()
// lazily instantiate to make matching simple stages easier
if s.SubStages == nil {
s.SubStages = []*FakeStage{}
}
s.SubStages = append(s.SubStages, subStage)
err := closure(subStage)
call := &PerformCall{Name: name, Error: err, Stage: subStage}
if err != nil {
if skipErr, isSkipError := err.(biui.SkipStageError); isSkipError {
call.SkipError = skipErr
err = nil
}
}
// lazily instantiate to make matching sub-stages easier
if s.PerformCalls == nil {
s.PerformCalls = []*PerformCall{}
}
s.PerformCalls = append(s.PerformCalls, call)
return err
}