From 27e44f069a1a04f4e93f9bdcfceab97d1effb8ec Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 03:42:16 +0000 Subject: [PATCH] fix(cli): reset stdin on nil override --- pkg/cli/prompt.go | 9 ++++++++- pkg/cli/prompt_test.go | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/cli/prompt.go b/pkg/cli/prompt.go index 09a383c..61f806b 100644 --- a/pkg/cli/prompt.go +++ b/pkg/cli/prompt.go @@ -13,7 +13,14 @@ import ( var stdin io.Reader = os.Stdin // SetStdin overrides the default stdin reader for testing. -func SetStdin(r io.Reader) { stdin = r } +// Pass nil to restore the real os.Stdin reader. +func SetStdin(r io.Reader) { + if r == nil { + stdin = os.Stdin + return + } + stdin = r +} // newReader wraps stdin in a bufio.Reader if it isn't one already. func newReader() *bufio.Reader { diff --git a/pkg/cli/prompt_test.go b/pkg/cli/prompt_test.go index bad3048..63e0d59 100644 --- a/pkg/cli/prompt_test.go +++ b/pkg/cli/prompt_test.go @@ -1,6 +1,7 @@ package cli import ( + "os" "strings" "testing" @@ -50,3 +51,14 @@ func TestMultiSelect_Good(t *testing.T) { assert.NoError(t, err) assert.Equal(t, []string{"a", "c"}, vals) } + +func TestSetStdin_Good_ResetNil(t *testing.T) { + original := stdin + t.Cleanup(func() { stdin = original }) + + SetStdin(strings.NewReader("hello\n")) + assert.NotSame(t, os.Stdin, stdin) + + SetStdin(nil) + assert.Same(t, os.Stdin, stdin) +}