From c98a83e1c7fc4218a8745a87b456d5cb78ec5a36 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Mon, 16 Oct 2023 08:55:44 -0400 Subject: [PATCH] ensure stderr contains error reports (#29) Signed-off-by: Alex Goodman --- application.go | 3 ++- application_test.go | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/application.go b/application.go index aaab8c5..2adb046 100644 --- a/application.go +++ b/application.go @@ -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 } } diff --git a/application_test.go b/application_test.go index a97967d..1a53d04 100644 --- a/application_test.go +++ b/application_test.go @@ -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)