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