TestConfirmTo ensures that the confirmation prompt works as expected.
(t *testing.T)
| 137 | |
| 138 | // TestConfirmTo ensures that the confirmation prompt works as expected. |
| 139 | func TestConfirmTo(t *testing.T) { |
| 140 | assert := asrt.New(t) |
| 141 | |
| 142 | // Unset the env var, then reset after the test |
| 143 | t.Setenv("DDEV_NONINTERACTIVE", "") |
| 144 | |
| 145 | // test a given input against a default value |
| 146 | testInput := func(input string, defaultTo bool, expected bool) { |
| 147 | text := util.RandString(32) |
| 148 | scanner := bufio.NewScanner(strings.NewReader(input)) |
| 149 | util.SetInputScanner(scanner) |
| 150 | |
| 151 | getOutput := util.CaptureStdOut() |
| 152 | resp := util.ConfirmTo(text, defaultTo) |
| 153 | if expected { |
| 154 | assert.True(resp) |
| 155 | } else { |
| 156 | assert.False(resp) |
| 157 | } |
| 158 | assert.Contains(getOutput(), text) |
| 159 | } |
| 160 | |
| 161 | yesses := []string{"YES", "Yes", "yes", "Y", "y"} |
| 162 | for _, y := range yesses { |
| 163 | testInput(y, true, true) |
| 164 | testInput(y, false, true) |
| 165 | } |
| 166 | |
| 167 | nos := []string{"NO", "No", "no", "N", "n"} |
| 168 | for _, n := range nos { |
| 169 | testInput(n, true, false) |
| 170 | testInput(n, false, false) |
| 171 | } |
| 172 | |
| 173 | // Test that junk answers (not yes, no, or <enter>) eventually return a false |
| 174 | junkText := "a\nb\na\nb\na\nb\na\nb\na\nb\n" |
| 175 | testInput(junkText, true, false) |
| 176 | testInput(junkText, false, false) |
| 177 | |
| 178 | // Test that <enter> returns the defaultTo value |
| 179 | enter := "\n" |
| 180 | testInput(enter, true, true) |
| 181 | testInput(enter, false, false) |
| 182 | } |
| 183 | |
| 184 | // TestSliceToUniqueSlice tests SliceToUniqueSlice |
| 185 | func TestSliceToUniqueSlice(t *testing.T) { |
nothing calls this directly
no test coverage detected