feat(01-03): add deterministic rich CLI output
- Inject bonfire.Output from stdin/stdout/stderr with stdlib widgets and x/term - Degrade spinner, progress, table and prompts without a TTY or color - Reject plugin command names that are not namespace:verb
This commit is contained in:
92
bonfire/prompts_test.go
Normal file
92
bonfire/prompts_test.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package bonfire
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfirmUsesDefaultWhenNonTTY(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
out := NewOutput(strings.NewReader("n\n"), &buf, &buf)
|
||||
got, err := out.Confirm("continue?", true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !got {
|
||||
t.Fatal("non-tty confirm should use default true without reading stdin")
|
||||
}
|
||||
if buf.Len() != 0 {
|
||||
t.Fatalf("confirm wrote %q", buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAskReadsStdinLine(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
out := NewOutput(strings.NewReader("alice\n"), &buf, &buf)
|
||||
got, err := out.Ask("name", "bob")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != "alice" {
|
||||
t.Fatalf("ask = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAskEOFUsesDefault(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
out := NewOutput(strings.NewReader(""), &buf, &buf)
|
||||
got, err := out.Ask("name", "bob")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != "bob" {
|
||||
t.Fatalf("ask eof = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChoiceReadsStdinLine(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
out := NewOutput(strings.NewReader("2\n"), &buf, &buf)
|
||||
got, err := out.Choice("pick", []string{"a", "b", "c"}, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != "b" {
|
||||
t.Fatalf("choice = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSecretReadsPlainStdinLine(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
out := NewOutput(strings.NewReader("s3cret\n"), &buf, &buf)
|
||||
got, err := out.Secret("token")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != "s3cret" {
|
||||
t.Fatalf("secret = %q", got)
|
||||
}
|
||||
if strings.Contains(buf.String(), "s3cret") {
|
||||
t.Fatal("secret value was written to output")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClosedStdinDoesNotHang(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
out := NewOutput(io.NopCloser(strings.NewReader("")), &buf, &buf)
|
||||
if _, err := out.Ask("name", "def"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ok, err := out.Confirm("ok?", false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ok {
|
||||
t.Fatal("confirm default false")
|
||||
}
|
||||
if _, err := out.Secret("pw"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user