Skip to content

Commit

Permalink
Add exitOnError flag
Browse files Browse the repository at this point in the history
  • Loading branch information
karlseguin committed Mar 21, 2021
1 parent f817a5a commit 822e764
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
7 changes: 5 additions & 2 deletions posthandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
)

var (
SuccessHandler = &SuccessPostHandler{}
SuccessHandler = &SuccessPostHandler{}
FailureHandlerFactory = func(expected, actual interface{}) PostHandler {
return &FailurePostHandler{expected, actual}
}
)

type PostHandler interface {
Expand All @@ -19,7 +22,7 @@ type FailurePostHandler struct {
}

func NewFailureHandler(expected, actual interface{}) PostHandler {
return &FailurePostHandler{expected, actual}
return FailureHandlerFactory(expected, actual)
}

func (h *FailurePostHandler) Message(format string, args ...interface{}) {
Expand Down
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ Run tests as you normally would via `go test`. However, to run specific tests, u

go test -m AddsTwo

### Exit On Error
Use the -e flag to exit on a failed assertion.

## Expectations

Two similar syntaxes of expectations are supported
Expand Down
13 changes: 13 additions & 0 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var (
loaded = false
loadLock = new(sync.Mutex)
showStdout = flag.Bool("vv", false, "turn on stdout")
exitOnFailure = flag.Bool("e", false, "exit on failures")
matchFlag = flag.String("m", "", "Regular expression selecting which tests to run")
notMatchFlag = flag.String("M", "", "Regular expression selecting which tests not to run")
summaryPath = flag.String("summary", "", "Path to write a summary file to")
Expand Down Expand Up @@ -52,6 +53,18 @@ func Expectify(suite interface{}, t *testing.T) {
}
os.Stdout = silentOut
loaded = true

if *exitOnFailure {
FailureHandlerFactory = func(actual, expected interface{}) PostHandler {
for _, res := range runner.results {
if !res.Passed() || testing.Verbose() {
res.Report()
}
}
os.Exit(1)
return nil
}
}
}
loadLock.Unlock()

Expand Down

0 comments on commit 822e764

Please sign in to comment.