Repository: cli/cli
File: internal/prompter/accessible_prompter_test.go (Go)

Code before:
			require.NoError(t, err)

			// Wait for the retry prompt
			_, err = console.ExpectString("Paste your authentication token:")
			require.NoError(t, err)

			// Wait to ensure huh has time to set the echo mode
			time.Sleep(beforePasswordSendTimeout)

			// Now enter some dummy auth token to return control back to the test
			_, err = console.SendLine(dummyAuthTokenForAfterFailure)
			require.NoError(t, err)
		}()

		authValue, err := p.AuthToken()
		require.NoError(t, err)
		require.Equal(t, dummyAuthTokenForAfterFailure, authValue)

		// Ensure the dummy password is not printed to the screen,
		// asserting that echo mode is disabled.
		//
		// Note that since console.ExpectString returns successful if the
		// expected string matches any part of the stream, we have to use an
		// anchored regexp (i.e., with ^ and $) to make sure the password/token
		// is not printed at all.
		_, err = console.Expect(expect.Regexp(regexp.MustCompile("^ \r\n\r\n$")))
		require.NoError(t, err)
	})

	t.Run("ConfirmDeletion", func(t *testing.T) {
		console := newTestVirtualTerminal(t)
		p := newTestAccessiblePrompter(t, console)

		requiredValue := "test"
		go func() {
			// Wait for prompt to appear
			_, err := console.ExpectString(fmt.Sprintf("Type %q to confirm deletion", requiredValue))
			require.NoError(t, err)

			// Confirm
			_, err = console.SendLine(requiredValue)
			require.NoError(t, err)
		}()

		// An err indicates that the confirmation text sent did not match
		err := p.ConfirmDeletion(requiredValue)
		require.NoError(t, err)
	})

	t.Run("ConfirmDeletion - bad input", func(t *testing.T) {
		console := newTestVirtualTerminal(t)

Review comment (suggestion):
Suggestion: I think you might be able to use `expect.RegexpPattern` as a shortcut:

```Go
_, err = console.Expect(expect.RegexpPattern("^ \r\n\r\n$"))
```

Code after:

			// Wait for the retry prompt
			_, err = console.ExpectString("Paste your authentication token:")
			require.NoError(t, err)

			// Wait to ensure huh has time to set the echo mode
			time.Sleep(beforePasswordSendTimeout)

			// Now enter some dummy auth token to return control back to the test
			_, err = console.SendLine(dummyAuthTokenForAfterFailure)
			require.NoError(t, err)
		}()

		authValue, err := p.AuthToken()
		require.NoError(t, err)
		require.Equal(t, dummyAuthTokenForAfterFailure, authValue)

		// Ensure the dummy password is not printed to the screen,
		// asserting that echo mode is disabled.
		//
		// Note that since console.ExpectString returns successful if the
		// expected string matches any part of the stream, we have to use an
		// anchored regexp (i.e., with ^ and $) to make sure the password/token
		// is not printed at all.
		_, err = console.Expect(expect.RegexpPattern("^ \r\n\r\n$"))
		require.NoError(t, err)
	})

	t.Run("ConfirmDeletion", func(t *testing.T) {
		console := newTestVirtualTerminal(t)
		p := newTestAccessiblePrompter(t, console)

		requiredValue := "test"
		go func() {
			// Wait for prompt to appear
			_, err := console.ExpectString(fmt.Sprintf("Type %q to confirm deletion", requiredValue))
			require.NoError(t, err)

			// Confirm
			_, err = console.SendLine(requiredValue)
			require.NoError(t, err)
		}()

		// An err indicates that the confirmation text sent did not match
		err := p.ConfirmDeletion(requiredValue)
		require.NoError(t, err)
	})

	t.Run("ConfirmDeletion - bad input", func(t *testing.T) {
		console := newTestVirtualTerminal(t)
		p := newTestAccessiblePrompter(t, console)