X Tutup
Skip to content

Commit 1f4bd80

Browse files
committed
Fix test flaky due to race in showing/hiding cursor
https://github.com/cli/cli/pull/3787/checks?check_run_id=2793254411
1 parent e1b5f78 commit 1f4bd80

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

pkg/surveyext/editor_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ func Test_GhEditor_Prompt_skip(t *testing.T) {
7474
// wait until the prompt is rendered and send the Enter key
7575
go func() {
7676
pty.WaitForOutput("Body")
77-
assert.Equal(t, "\x1b[0G\x1b[2K\x1b[0;1;92m? \x1b[0m\x1b[0;1;99mBody \x1b[0m\x1b[0;36m[(e) to launch vim, enter to skip] \x1b[0m\x1b[?25l", pty.Output())
77+
assert.Equal(t, "\x1b[0G\x1b[2K\x1b[0;1;92m? \x1b[0m\x1b[0;1;99mBody \x1b[0m\x1b[0;36m[(e) to launch vim, enter to skip] \x1b[0m", normalizeANSI(pty.Output()))
7878
pty.ResetOutput()
7979
assert.NoError(t, pty.SendKey('\n'))
8080
}()
8181

8282
res, err := e.Prompt(defaultPromptConfig())
8383
assert.NoError(t, err)
8484
assert.Equal(t, "initial value", res)
85-
assert.Equal(t, "\x1b[?25h", pty.Output())
85+
assert.Equal(t, "", normalizeANSI(pty.Output()))
8686
}
8787

8888
func Test_GhEditor_Prompt_editorAppend(t *testing.T) {
@@ -105,15 +105,15 @@ func Test_GhEditor_Prompt_editorAppend(t *testing.T) {
105105
// wait until the prompt is rendered and send the 'e' key
106106
go func() {
107107
pty.WaitForOutput("Body")
108-
assert.Equal(t, "\x1b[0G\x1b[2K\x1b[0;1;92m? \x1b[0m\x1b[0;1;99mBody \x1b[0m\x1b[0;36m[(e) to launch vim, enter to skip] \x1b[0m\x1b[?25l", pty.Output())
108+
assert.Equal(t, "\x1b[0G\x1b[2K\x1b[0;1;92m? \x1b[0m\x1b[0;1;99mBody \x1b[0m\x1b[0;36m[(e) to launch vim, enter to skip] \x1b[0m", normalizeANSI(pty.Output()))
109109
pty.ResetOutput()
110110
assert.NoError(t, pty.SendKey('e'))
111111
}()
112112

113113
res, err := e.Prompt(defaultPromptConfig())
114114
assert.NoError(t, err)
115115
assert.Equal(t, "initial value - added by vim", res)
116-
assert.Equal(t, "\x1b[?25h\x1b[?25h", pty.Output())
116+
assert.Equal(t, "", normalizeANSI(pty.Output()))
117117
}
118118

119119
func Test_GhEditor_Prompt_editorTruncate(t *testing.T) {
@@ -136,15 +136,15 @@ func Test_GhEditor_Prompt_editorTruncate(t *testing.T) {
136136
// wait until the prompt is rendered and send the 'e' key
137137
go func() {
138138
pty.WaitForOutput("Body")
139-
assert.Equal(t, "\x1b[0G\x1b[2K\x1b[0;1;92m? \x1b[0m\x1b[0;1;99mBody \x1b[0m\x1b[0;36m[(e) to launch nano, enter to skip] \x1b[0m\x1b[?25l", pty.Output())
139+
assert.Equal(t, "\x1b[0G\x1b[2K\x1b[0;1;92m? \x1b[0m\x1b[0;1;99mBody \x1b[0m\x1b[0;36m[(e) to launch nano, enter to skip] \x1b[0m", normalizeANSI(pty.Output()))
140140
pty.ResetOutput()
141141
assert.NoError(t, pty.SendKey('e'))
142142
}()
143143

144144
res, err := e.Prompt(defaultPromptConfig())
145145
assert.NoError(t, err)
146146
assert.Equal(t, "", res)
147-
assert.Equal(t, "\x1b[?25h\x1b[?25h", pty.Output())
147+
assert.Equal(t, "", normalizeANSI(pty.Output()))
148148
}
149149

150150
// survey doesn't expose this
@@ -275,3 +275,10 @@ func (f *teeWriter) Reset() {
275275
f.buf.Reset()
276276
f.mu.Unlock()
277277
}
278+
279+
// strips some ANSI escape sequences that we do not want tests to be concerned with
280+
func normalizeANSI(t string) string {
281+
t = strings.ReplaceAll(t, "\x1b[?25h", "") // strip sequence that shows cursor
282+
t = strings.ReplaceAll(t, "\x1b[?25l", "") // strip sequence that hides cursor
283+
return t
284+
}

0 commit comments

Comments
 (0)
X Tutup