Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cmd/check_example/main_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package main

import (
"github.com/NETWAYS/go-check/testhelper"
"github.com/stretchr/testify/assert"
"os"
"regexp"
"testing"

"github.com/NETWAYS/go-check/testhelper"
"github.com/stretchr/testify/assert"
)

func TestMyMain(t *testing.T) {
stdout := testhelper.RunMainTest(main, "--help")
assert.Regexp(t, regexp.MustCompile(`would exit with code 3`), stdout)

stdout = testhelper.RunMainTest(main, "--warning", "20")
assert.Regexp(t, regexp.MustCompile(`^OK - value is 10\nwould exit with code 0\n$`), stdout)
assert.Regexp(t, regexp.MustCompile(`^\[OK\] - value is 10\nwould exit with code 0\n$`), stdout)

stdout = testhelper.RunMainTest(main, "--warning", "10", "--value", "11")
assert.Regexp(t, regexp.MustCompile(`^WARNING - value is 11\nwould exit with code 1\n$`), stdout)
assert.Regexp(t, regexp.MustCompile(`^\[WARNING\] - value is 11\nwould exit with code 1\n$`), stdout)
}

func TestMain(m *testing.M) {
Expand Down
2 changes: 1 addition & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ func ExampleConfig() {

Exitf(OK, "Everything is fine - answer=%d", 42)

// Output: OK - Everything is fine - answer=42
// Output: [OK] - Everything is fine - answer=42
// would exit with code 0
}
5 changes: 3 additions & 2 deletions exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package check

import (
"fmt"
"github.com/mitchellh/go-ps"
"os"
"runtime/debug"
"strconv"
"strings"

"github.com/mitchellh/go-ps"
)

// AllowExit lets you disable the call to os.Exit() in ExitXxx() functions of this package.
Expand Down Expand Up @@ -34,7 +35,7 @@ func Exitf(rc int, output string, args ...interface{}) {
func ExitRaw(rc int, output ...string) {
var text strings.Builder

text.WriteString(StatusText(rc) + " -")
text.WriteString("[" + StatusText(rc) + "] -")

for _, s := range output {
text.WriteString(" " + s)
Expand Down
10 changes: 5 additions & 5 deletions exit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,34 @@ import (

func ExampleExit() {
Exitf(OK, "Everything is fine - value=%d", 42)
// Output: OK - Everything is fine - value=42
// Output: [OK] - Everything is fine - value=42
// would exit with code 0
}

func ExampleExitf() {
Exitf(OK, "Everything is fine - value=%d", 42)
// Output: OK - Everything is fine - value=42
// Output: [OK] - Everything is fine - value=42
// would exit with code 0
}

func ExampleExitRaw() {
ExitRaw(OK, "Everything is fine")
// Output: OK - Everything is fine
// Output: [OK] - Everything is fine
// would exit with code 0
}

func ExampleExitError() {
err := fmt.Errorf("connection to %s has been timed out", "localhost:12345")
ExitError(err)
// Output: UNKNOWN - connection to localhost:12345 has been timed out (*errors.errorString)
// Output: [UNKNOWN] - connection to localhost:12345 has been timed out (*errors.errorString)
// would exit with code 3
}

func ExampleCatchPanic() {
defer CatchPanic()

panic("something bad happened")
// Output: UNKNOWN - Golang encountered a panic: something bad happened
// Output: [UNKNOWN] - Golang encountered a panic: something bad happened
// would exit with code 3
}

Expand Down