mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-22 03:30:35 +00:00
Fix prompt default handling (#7300)
* Fix prompt default handling * Merge branch 'master' of github.com:prysmaticlabs/prysm into default_handling * added test * Merge branch 'master' of github.com:prysmaticlabs/prysm into default_handling * Merge branch 'master' of github.com:prysmaticlabs/prysm into default_handling
This commit is contained in:
parent
de3f112a05
commit
1a4129f5a6
@ -24,5 +24,8 @@ go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["validate_test.go"],
|
||||
embed = [":go_default_library"],
|
||||
deps = ["//shared/testutil/assert:go_default_library"],
|
||||
deps = [
|
||||
"//shared/testutil/assert:go_default_library",
|
||||
"//shared/testutil/require:go_default_library",
|
||||
],
|
||||
)
|
||||
|
@ -91,6 +91,9 @@ func DefaultAndValidatePrompt(promptText string, defaultValue string, validateFu
|
||||
if ok := scanner.Scan(); ok {
|
||||
item := scanner.Text()
|
||||
response = strings.TrimRight(item, "\r\n")
|
||||
if response == "" {
|
||||
return defaultValue, nil
|
||||
}
|
||||
if err := validateFunc(response); err != nil {
|
||||
fmt.Printf("Entry not valid: %s\n", au.BrightRed(err))
|
||||
} else {
|
||||
|
@ -1,9 +1,12 @@
|
||||
package promptutil
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
||||
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
||||
)
|
||||
|
||||
func TestValidatePasswordInput(t *testing.T) {
|
||||
@ -88,3 +91,67 @@ func TestIsValidUnicode(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultAndValidatePrompt(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
def string
|
||||
want string
|
||||
wantError bool
|
||||
}{
|
||||
{
|
||||
name: "number",
|
||||
input: "3",
|
||||
def: "0",
|
||||
want: "3",
|
||||
},
|
||||
{
|
||||
name: "empty return default",
|
||||
input: "",
|
||||
def: "0",
|
||||
want: "0",
|
||||
},
|
||||
{
|
||||
name: "empty return default no zero",
|
||||
input: "",
|
||||
def: "3",
|
||||
want: "3",
|
||||
},
|
||||
{
|
||||
name: "empty return default, no zero",
|
||||
input: "a",
|
||||
def: "0",
|
||||
want: "",
|
||||
wantError: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
content := []byte(tt.input + "\n")
|
||||
tmpfile, err := ioutil.TempFile("", "content")
|
||||
require.NoError(t, err)
|
||||
defer func() {
|
||||
err := os.Remove(tmpfile.Name())
|
||||
require.NoError(t, err)
|
||||
}()
|
||||
|
||||
_, err = tmpfile.Write(content)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = tmpfile.Seek(0, 0)
|
||||
require.NoError(t, err)
|
||||
oldStdin := os.Stdin
|
||||
defer func() { os.Stdin = oldStdin }() // Restore original Stdin
|
||||
os.Stdin = tmpfile
|
||||
got, err := DefaultAndValidatePrompt(tt.name, tt.def, ValidateNumber)
|
||||
if !tt.wantError {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
assert.Equal(t, tt.want, got)
|
||||
err = tmpfile.Close()
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user