fix(cli): reset stdin on nil override
All checks were successful
Security Scan / security (push) Successful in 20s
All checks were successful
Security Scan / security (push) Successful in 20s
This commit is contained in:
parent
32342dfd31
commit
27e44f069a
2 changed files with 20 additions and 1 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue