fix(summer): send a delegate's stderr to the root's error writer through bonfire.ErrWriter (IN-13, quick-261028-rqd)

This commit is contained in:
Jakub Zych
2026-10-28 20:50:39 +01:00
parent f3f87f3cc4
commit 4914cade5a
8 changed files with 145 additions and 29 deletions

View File

@@ -4,9 +4,13 @@ import (
"bytes"
"context"
"errors"
"io"
"os"
"slices"
"strings"
"testing"
"github.com/spf13/cobra"
)
func TestNonTTYSpinner(t *testing.T) {
@@ -387,3 +391,51 @@ func TestHelpUsesSharedAdapter(t *testing.T) {
t.Fatalf("help = %q", got)
}
}
// stubOutput is an Output that is not a bonfire console.
type stubOutput struct{ Output }
// TestErrWriter (IN-13): ErrWriter returns the stream the root's error
// block goes to: os.Stderr for NewRoot on os.Stdout, the error writer of
// NewRootIO and NewOutput (stdout when NewOutput gets none), and os.Stderr
// for an Output that is not a bonfire console.
func TestErrWriter(t *testing.T) {
outputOf := func(t *testing.T, build func([]Command) (*cobra.Command, error)) Output {
t.Helper()
var got Output
cmd := Command{Name: "acme:grab", Description: "Grab the output", Run: func(_ context.Context, _ Input, out Output) error {
got = out
return nil
}}
root, err := build([]Command{cmd})
if err != nil {
t.Fatal(err)
}
root.SetArgs([]string{"acme:grab"})
if err := root.Execute(); err != nil {
t.Fatal(err)
}
return got
}
out := outputOf(t, func(c []Command) (*cobra.Command, error) { return NewRoot("acme", c, os.Stdout) })
if w := ErrWriter(out); w != os.Stderr {
t.Errorf("NewRoot on os.Stdout: ErrWriter = %v, want os.Stderr", w)
}
var stdout, stderr bytes.Buffer
out = outputOf(t, func(c []Command) (*cobra.Command, error) {
return NewRootIO("acme", c, strings.NewReader(""), &stdout, &stderr)
})
if w := ErrWriter(out); w != io.Writer(&stderr) {
t.Errorf("NewRootIO: ErrWriter = %v, want the error writer", w)
}
if w := ErrWriter(NewOutput(strings.NewReader(""), &stdout, &stderr)); w != io.Writer(&stderr) {
t.Errorf("NewOutput: ErrWriter = %v, want the error writer", w)
}
if w := ErrWriter(NewOutput(strings.NewReader(""), &stdout, nil)); w != io.Writer(&stdout) {
t.Errorf("NewOutput without an error writer: ErrWriter = %v, want stdout", w)
}
if w := ErrWriter(stubOutput{}); w != os.Stderr {
t.Errorf("a non-bonfire Output: ErrWriter = %v, want os.Stderr", w)
}
}