github.com/cosmos/cosmos-sdk@v0.50.10/x/gov/client/cli/prompt_test.go (about) 1 //go:build !race 2 // +build !race 3 4 // Disabled -race because the package github.com/manifoldco/promptui@v0.9.0 5 // has a data race and this code exposes it, but fixing it would require 6 // holding up the associated change to this. 7 8 package cli_test 9 10 import ( 11 "fmt" 12 "math" 13 "os" 14 "testing" 15 16 "github.com/chzyer/readline" 17 "github.com/stretchr/testify/assert" 18 "github.com/stretchr/testify/require" 19 20 "github.com/cosmos/cosmos-sdk/x/gov/client/cli" 21 ) 22 23 type st struct { 24 I int 25 } 26 27 // Tests that we successfully report overflows in parsing ints 28 // See https://github.com/cosmos/cosmos-sdk/issues/13346 29 func TestPromptIntegerOverflow(t *testing.T) { 30 // Intentionally sending values out of the range of int. 31 intOverflowers := []string{ 32 "-9223372036854775809", 33 "9223372036854775808", 34 "9923372036854775809", 35 "-9923372036854775809", 36 "18446744073709551616", 37 "-18446744073709551616", 38 } 39 40 for _, intOverflower := range intOverflowers { 41 overflowStr := intOverflower 42 t.Run(overflowStr, func(t *testing.T) { 43 origStdin := readline.Stdin 44 defer func() { 45 readline.Stdin = origStdin 46 }() 47 48 fin, fw := readline.NewFillableStdin(os.Stdin) 49 readline.Stdin = fin 50 fw.Write([]byte(overflowStr + "\n")) 51 52 v, err := cli.Prompt(st{}, "") 53 assert.Equal(t, st{}, v, "expected a value of zero") 54 require.NotNil(t, err, "expected a report of an overflow") 55 require.Contains(t, err.Error(), "range") 56 }) 57 } 58 } 59 60 func TestPromptParseInteger(t *testing.T) { 61 // Intentionally sending a value out of the range of 62 values := []struct { 63 in string 64 want int 65 }{ 66 {fmt.Sprintf("%d", math.MinInt), math.MinInt}, 67 {"19991", 19991}, 68 {"991000000199", 991000000199}, 69 } 70 71 for _, tc := range values { 72 tc := tc 73 t.Run(tc.in, func(t *testing.T) { 74 origStdin := readline.Stdin 75 defer func() { 76 readline.Stdin = origStdin 77 }() 78 79 fin, fw := readline.NewFillableStdin(os.Stdin) 80 readline.Stdin = fin 81 fw.Write([]byte(tc.in + "\n")) 82 83 v, err := cli.Prompt(st{}, "") 84 assert.Nil(t, err, "expected a nil error") 85 assert.Equal(t, tc.want, v.I, "expected %d = %d", tc.want, v.I) 86 }) 87 } 88 }