diff --git a/config/configure.go b/config/configure.go index 205026a5b..24e679994 100644 --- a/config/configure.go +++ b/config/configure.go @@ -33,12 +33,13 @@ var hookSaveConfiguration = func(fn func(config *Configuration) error) func(conf return fn } +var stdin io.Reader = os.Stdin + func loadConfiguration() (*Configuration, error) { return hookLoadConfiguration(LoadConfiguration)(GetConfigPath() + "/" + configFile) } func NewConfigureCommand() *cli.Command { - c := &cli.Command{ Name: "configure", Short: i18n.T( @@ -287,14 +288,14 @@ func configureOIDC(w io.Writer, cp *Profile) error { func ReadInput(defaultValue string) string { var s string - scanner := bufio.NewScanner(os.Stdin) + scanner := bufio.NewScanner(stdin) if scanner.Scan() { s = scanner.Text() } if s == "" { return defaultValue } - return s + return strings.TrimSpace(s) } func MosaicString(s string, lastChars int) string { diff --git a/config/configure_test.go b/config/configure_test.go index 50cc909e0..3539a19a8 100644 --- a/config/configure_test.go +++ b/config/configure_test.go @@ -15,6 +15,7 @@ package config import ( "bytes" + "os" "runtime" "strings" "testing" @@ -314,7 +315,19 @@ func TestConfigureOIDC(t *testing.T) { } func TestReadInput(t *testing.T) { + defer func() { + stdin = os.Stdin + }() + // read empty string, return default value + stdin = strings.NewReader("") assert.Equal(t, "default", ReadInput("default")) + // read input, return input + stdin = strings.NewReader("input from stdion\n") + assert.Equal(t, "input from stdion", ReadInput("default")) + + // read input with spaces + stdin = strings.NewReader("input from stdion \n") + assert.Equal(t, "input from stdion", ReadInput("default")) } func TestMosaicString(t *testing.T) {