-
Notifications
You must be signed in to change notification settings - Fork 4
/
have_killed.go
48 lines (38 loc) · 1.2 KB
/
have_killed.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
package fake_command_runner_matchers // import "code.cloudfoundry.org/commandrunner/fake_command_runner/matchers"
import (
"fmt"
"os/exec"
"code.cloudfoundry.org/commandrunner/fake_command_runner"
)
func HaveKilled(spec fake_command_runner.CommandSpec) *HaveKilledMatcher {
return &HaveKilledMatcher{Spec: spec}
}
type HaveKilledMatcher struct {
Spec fake_command_runner.CommandSpec
killed []*exec.Cmd
}
func (m *HaveKilledMatcher) Match(actual interface{}) (bool, error) {
runner, ok := actual.(*fake_command_runner.FakeCommandRunner)
if !ok {
return false, fmt.Errorf("Not a fake command runner: %#v.", actual)
}
m.killed = runner.KilledCommands()
matched := false
for _, cmd := range m.killed {
if m.Spec.Matches(cmd) {
matched = true
break
}
}
if matched {
return true, nil
} else {
return false, nil
}
}
func (m *HaveKilledMatcher) FailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("Expected to kill:%s\n\nActually killed:%s", prettySpec(m.Spec), prettyCommands(m.killed))
}
func (m *HaveKilledMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("Expected to not kill the following commands:%s", prettySpec(m.Spec))
}