1package survey 2 3import ( 4 "fmt" 5 "testing" 6 7 expect "github.com/Netflix/go-expect" 8 "github.com/stretchr/testify/assert" 9 "gopkg.in/AlecAivazis/survey.v1/core" 10) 11 12func init() { 13 // disable color output for all prompts to simplify testing 14 core.DisableColor = true 15} 16 17func TestPasswordRender(t *testing.T) { 18 19 tests := []struct { 20 title string 21 prompt Password 22 data PasswordTemplateData 23 expected string 24 }{ 25 { 26 "Test Password question output", 27 Password{Message: "Tell me your secret:"}, 28 PasswordTemplateData{}, 29 fmt.Sprintf("%s Tell me your secret: ", core.QuestionIcon), 30 }, 31 { 32 "Test Password question output with help hidden", 33 Password{Message: "Tell me your secret:", Help: "This is helpful"}, 34 PasswordTemplateData{}, 35 fmt.Sprintf("%s Tell me your secret: [%s for help] ", core.QuestionIcon, string(core.HelpInputRune)), 36 }, 37 { 38 "Test Password question output with help shown", 39 Password{Message: "Tell me your secret:", Help: "This is helpful"}, 40 PasswordTemplateData{ShowHelp: true}, 41 fmt.Sprintf("%s This is helpful\n%s Tell me your secret: ", core.HelpIcon, core.QuestionIcon), 42 }, 43 } 44 45 for _, test := range tests { 46 test.data.Password = test.prompt 47 actual, err := core.RunTemplate( 48 PasswordQuestionTemplate, 49 &test.data, 50 ) 51 assert.Nil(t, err, test.title) 52 assert.Equal(t, test.expected, actual, test.title) 53 } 54} 55 56func TestPasswordPrompt(t *testing.T) { 57 tests := []PromptTest{ 58 { 59 "Test Password prompt interaction", 60 &Password{ 61 Message: "Please type your password", 62 }, 63 func(c *expect.Console) { 64 c.ExpectString("Please type your password") 65 c.Send("secret") 66 c.SendLine("") 67 c.ExpectEOF() 68 }, 69 "secret", 70 }, 71 { 72 "Test Password prompt interaction with help", 73 &Password{ 74 Message: "Please type your password", 75 Help: "It's a secret", 76 }, 77 func(c *expect.Console) { 78 c.ExpectString("Please type your password") 79 c.SendLine("?") 80 c.ExpectString("It's a secret") 81 c.Send("secret") 82 c.SendLine("") 83 c.ExpectEOF() 84 }, 85 "secret", 86 }, 87 } 88 89 for _, test := range tests { 90 t.Run(test.name, func(t *testing.T) { 91 RunPromptTest(t, test) 92 }) 93 } 94} 95