...

Source file src/github.com/onsi/gomega/gexec/exit_matcher.go

Documentation: github.com/onsi/gomega/gexec

     1  // untested sections: 2
     2  
     3  package gexec
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"github.com/onsi/gomega/format"
     9  )
    10  
    11  /*
    12  The Exit matcher operates on a session:
    13  
    14  	Expect(session).Should(Exit(<optional status code>))
    15  
    16  Exit passes if the session has already exited.
    17  
    18  If no status code is provided, then Exit will succeed if the session has exited regardless of exit code.
    19  Otherwise, Exit will only succeed if the process has exited with the provided status code.
    20  
    21  Note 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  */
    25  func 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  
    36  type exitMatcher struct {
    37  	exitCode       int
    38  	didExit        bool
    39  	actualExitCode int
    40  }
    41  
    42  type Exiter interface {
    43  	ExitCode() int
    44  }
    45  
    46  func (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  
    64  func (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  
    71  func (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  
    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  }
    89  

View as plain text