initial commit
[govpp.git] / vendor / github.com / onsi / gomega / gexec / exit_matcher.go
1 package gexec
2
3 import (
4         "fmt"
5
6         "github.com/onsi/gomega/format"
7 )
8
9 /*
10 The Exit matcher operates on a session:
11
12         Ω(session).Should(Exit(<optional status code>))
13
14 Exit passes if the session has already exited.
15
16 If no status code is provided, then Exit will succeed if the session has exited regardless of exit code.
17 Otherwise, Exit will only succeed if the process has exited with the provided status code.
18
19 Note that the process must have already exited.  To wait for a process to exit, use Eventually:
20
21         Eventually(session, 3).Should(Exit(0))
22 */
23 func Exit(optionalExitCode ...int) *exitMatcher {
24         exitCode := -1
25         if len(optionalExitCode) > 0 {
26                 exitCode = optionalExitCode[0]
27         }
28
29         return &exitMatcher{
30                 exitCode: exitCode,
31         }
32 }
33
34 type exitMatcher struct {
35         exitCode       int
36         didExit        bool
37         actualExitCode int
38 }
39
40 type Exiter interface {
41         ExitCode() int
42 }
43
44 func (m *exitMatcher) Match(actual interface{}) (success bool, err error) {
45         exiter, ok := actual.(Exiter)
46         if !ok {
47                 return false, fmt.Errorf("Exit must be passed a gexec.Exiter (Missing method ExitCode() int) Got:\n%s", format.Object(actual, 1))
48         }
49
50         m.actualExitCode = exiter.ExitCode()
51
52         if m.actualExitCode == -1 {
53                 return false, nil
54         }
55
56         if m.exitCode == -1 {
57                 return true, nil
58         }
59         return m.exitCode == m.actualExitCode, nil
60 }
61
62 func (m *exitMatcher) FailureMessage(actual interface{}) (message string) {
63         if m.actualExitCode == -1 {
64                 return "Expected process to exit.  It did not."
65         } else {
66                 return format.Message(m.actualExitCode, "to match exit code:", m.exitCode)
67         }
68 }
69
70 func (m *exitMatcher) NegatedFailureMessage(actual interface{}) (message string) {
71         if m.actualExitCode == -1 {
72                 return "you really shouldn't be able to see this!"
73         } else {
74                 if m.exitCode == -1 {
75                         return "Expected process not to exit.  It did."
76                 } else {
77                         return format.Message(m.actualExitCode, "not to match exit code:", m.exitCode)
78                 }
79         }
80 }
81
82 func (m *exitMatcher) MatchMayChangeInTheFuture(actual interface{}) bool {
83         session, ok := actual.(*Session)
84         if ok {
85                 return session.ExitCode() == -1
86         }
87         return true
88 }