Skip to content

Commit

Permalink
ensure stderr contains error reports (#29)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <[email protected]>
  • Loading branch information
wagoodman authored Oct 16, 2023
1 parent 3238921 commit c98a83e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion application.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ func (a *application) Run() {
}()

if err := a.root.Execute(); err != nil {
color.Red.Println(strings.TrimSpace(err.Error()))
msg := color.Red.Render(strings.TrimSpace(err.Error()))
fmt.Fprintln(os.Stderr, msg)
exitCode = 1
}
}
Expand Down
8 changes: 8 additions & 0 deletions application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,17 @@ func Test_RunExitError(t *testing.T) {

cmd := exec.Command(os.Args[0], "-test.run=Test_RunExitError")
cmd.Env = append(os.Environ(), "CLIO_RUN_EXIT_ERROR=YES")

var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr

err := cmd.Run()
var e *exec.ExitError
if errors.As(err, &e) && !e.Success() {
// ensure that errors are reported to stderr, not stdout
assert.Contains(t, stderr.String(), "an error occurred")
assert.NotContains(t, stdout.String(), "an error occurred")
return
}
t.Fatalf("process ran with err %v, want exit status 1", err)
Expand Down

0 comments on commit c98a83e

Please sign in to comment.