Path: blob/main/vendor/github.com/onsi/gomega/gexec/exit_matcher.go
2880 views
// untested sections: 212package gexec34import (5"fmt"67"github.com/onsi/gomega/format"8)910/*11The Exit matcher operates on a session:1213Expect(session).Should(Exit(<optional status code>))1415Exit passes if the session has already exited.1617If no status code is provided, then Exit will succeed if the session has exited regardless of exit code.18Otherwise, Exit will only succeed if the process has exited with the provided status code.1920Note that the process must have already exited. To wait for a process to exit, use Eventually:2122Eventually(session, 3).Should(Exit(0))23*/24func Exit(optionalExitCode ...int) *exitMatcher {25exitCode := -126if len(optionalExitCode) > 0 {27exitCode = optionalExitCode[0]28}2930return &exitMatcher{31exitCode: exitCode,32}33}3435type exitMatcher struct {36exitCode int37didExit bool38actualExitCode int39}4041type Exiter interface {42ExitCode() int43}4445func (m *exitMatcher) Match(actual any) (success bool, err error) {46exiter, ok := actual.(Exiter)47if !ok {48return false, fmt.Errorf("Exit must be passed a gexec.Exiter (Missing method ExitCode() int) Got:\n%s", format.Object(actual, 1))49}5051m.actualExitCode = exiter.ExitCode()5253if m.actualExitCode == -1 {54return false, nil55}5657if m.exitCode == -1 {58return true, nil59}60return m.exitCode == m.actualExitCode, nil61}6263func (m *exitMatcher) FailureMessage(actual any) (message string) {64if m.actualExitCode == -1 {65return "Expected process to exit. It did not."66}67return format.Message(m.actualExitCode, "to match exit code:", m.exitCode)68}6970func (m *exitMatcher) NegatedFailureMessage(actual any) (message string) {71if m.actualExitCode == -1 {72return "you really shouldn't be able to see this!"73} else {74if m.exitCode == -1 {75return "Expected process not to exit. It did."76}77return format.Message(m.actualExitCode, "not to match exit code:", m.exitCode)78}79}8081func (m *exitMatcher) MatchMayChangeInTheFuture(actual any) bool {82session, ok := actual.(*Session)83if ok {84return session.ExitCode() == -185}86return true87}888990