Make tests less flaky
This commit is contained in:
@@ -63,7 +63,7 @@ class PrintsErrorRunner(Runner):
|
||||
with self._onerror():
|
||||
for _ in self.poll_until_timeout(timeout):
|
||||
screen = self.screenshot()
|
||||
if text in screen:
|
||||
if text in screen: # pragma: no branch
|
||||
return
|
||||
raise AssertionError(
|
||||
f'Timeout while waiting for text {text!r} to appear',
|
||||
@@ -86,11 +86,23 @@ class PrintsErrorRunner(Runner):
|
||||
w, h = self.tmux.execute_command(*cmd).split()
|
||||
return int(w), int(h)
|
||||
|
||||
def get_cursor_position(self):
|
||||
def _get_cursor_position(self):
|
||||
cmd = ('display', '-t0', '-p', '#{cursor_x}\t#{cursor_y}')
|
||||
x, y = self.tmux.execute_command(*cmd).split()
|
||||
return int(x), int(y)
|
||||
|
||||
def await_cursor_position(self, x, y):
|
||||
with self._onerror():
|
||||
for _ in self.poll_until_timeout():
|
||||
pos = self._get_cursor_position()
|
||||
if pos == (x, y): # pragma: no branch
|
||||
return
|
||||
|
||||
raise AssertionError(
|
||||
f'Timeout while waiting for cursor to reach {(x, y)}\n'
|
||||
f'Last cursor position: {pos}',
|
||||
)
|
||||
|
||||
@contextlib.contextmanager
|
||||
def resize(self, width, height):
|
||||
current_w, current_h = self.get_pane_size()
|
||||
@@ -227,50 +239,50 @@ def test_arrow_key_movement(tmpdir):
|
||||
)
|
||||
with run(str(f)) as h, and_exit(h):
|
||||
h.await_text('short')
|
||||
assert h.get_cursor_position() == (0, 1)
|
||||
h.await_cursor_position(0, 1)
|
||||
# should not go off the beginning of the file
|
||||
h.press('Left')
|
||||
assert h.get_cursor_position() == (0, 1)
|
||||
h.await_cursor_position(0, 1)
|
||||
h.press('Up')
|
||||
assert h.get_cursor_position() == (0, 1)
|
||||
h.await_cursor_position(0, 1)
|
||||
# left and right should work
|
||||
h.press('Right')
|
||||
h.press('Right')
|
||||
assert h.get_cursor_position() == (2, 1)
|
||||
h.await_cursor_position(2, 1)
|
||||
h.press('Left')
|
||||
assert h.get_cursor_position() == (1, 1)
|
||||
h.await_cursor_position(1, 1)
|
||||
# up should still be a noop on line 1
|
||||
h.press('Up')
|
||||
assert h.get_cursor_position() == (1, 1)
|
||||
h.await_cursor_position(1, 1)
|
||||
# down once should put it on the beginning of the second line
|
||||
h.press('Down')
|
||||
assert h.get_cursor_position() == (0, 2)
|
||||
h.await_cursor_position(0, 2)
|
||||
# down again should restore the x positon on the next line
|
||||
h.press('Down')
|
||||
assert h.get_cursor_position() == (1, 3)
|
||||
h.await_cursor_position(1, 3)
|
||||
# down once more should put it on the special end-of-file line
|
||||
h.press('Down')
|
||||
assert h.get_cursor_position() == (0, 4)
|
||||
h.await_cursor_position(0, 4)
|
||||
# should not go off the end of the file
|
||||
h.press('Down')
|
||||
assert h.get_cursor_position() == (0, 4)
|
||||
h.await_cursor_position(0, 4)
|
||||
h.press('Right')
|
||||
assert h.get_cursor_position() == (0, 4)
|
||||
h.await_cursor_position(0, 4)
|
||||
# left should put it at the end of the line
|
||||
h.press('Left')
|
||||
assert h.get_cursor_position() == (19, 3)
|
||||
h.await_cursor_position(19, 3)
|
||||
# right should put it to the next line
|
||||
h.press('Right')
|
||||
assert h.get_cursor_position() == (0, 4)
|
||||
h.await_cursor_position(0, 4)
|
||||
# if the hint-x is too high it should not go past the end of line
|
||||
h.press('Left')
|
||||
h.press('Up')
|
||||
h.press('Up')
|
||||
assert h.get_cursor_position() == (5, 1)
|
||||
h.await_cursor_position(5, 1)
|
||||
# and moving back down should still retain the hint-x
|
||||
h.press('Down')
|
||||
h.press('Down')
|
||||
assert h.get_cursor_position() == (19, 3)
|
||||
h.await_cursor_position(19, 3)
|
||||
|
||||
|
||||
def test_scrolling_arrow_key_movement(tmpdir):
|
||||
@@ -283,11 +295,11 @@ def test_scrolling_arrow_key_movement(tmpdir):
|
||||
for _ in range(7):
|
||||
h.press('Down')
|
||||
h.await_text('line_0')
|
||||
assert h.get_cursor_position() == (0, 8)
|
||||
h.await_cursor_position(0, 8)
|
||||
# but this should scroll down
|
||||
h.press('Down')
|
||||
h.await_text('line_8')
|
||||
assert h.get_cursor_position() == (0, 4)
|
||||
h.await_cursor_position(0, 4)
|
||||
assert h.screenshot().splitlines()[4] == 'line_8'
|
||||
# we should not have scrolled after 3 up presses
|
||||
for _ in range(3):
|
||||
@@ -305,11 +317,11 @@ def test_end_key(tmpdir, k):
|
||||
|
||||
with run(str(f)) as h, and_exit(h):
|
||||
h.await_text('hello world')
|
||||
assert h.get_cursor_position() == (0, 1)
|
||||
h.await_cursor_position(0, 1)
|
||||
h.press(k)
|
||||
assert h.get_cursor_position() == (11, 1)
|
||||
h.await_cursor_position(11, 1)
|
||||
h.press('Down')
|
||||
assert h.get_cursor_position() == (11, 2)
|
||||
h.await_cursor_position(11, 2)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('k', ('Home', 'C-a'))
|
||||
@@ -321,11 +333,11 @@ def test_home_key(tmpdir, k):
|
||||
h.await_text('hello world')
|
||||
h.press('Down')
|
||||
h.press('Left')
|
||||
assert h.get_cursor_position() == (11, 1)
|
||||
h.await_cursor_position(11, 1)
|
||||
h.press(k)
|
||||
assert h.get_cursor_position() == (0, 1)
|
||||
h.await_cursor_position(0, 1)
|
||||
h.press('Down')
|
||||
assert h.get_cursor_position() == (0, 2)
|
||||
h.await_cursor_position(0, 2)
|
||||
|
||||
|
||||
def test_resize_scrolls_up(tmpdir):
|
||||
@@ -337,19 +349,19 @@ def test_resize_scrolls_up(tmpdir):
|
||||
|
||||
for _ in range(7):
|
||||
h.press('Down')
|
||||
assert h.get_cursor_position() == (0, 8)
|
||||
h.await_cursor_position(0, 8)
|
||||
|
||||
# a resize to a height of 10 should not scroll
|
||||
with h.resize(80, 10):
|
||||
h.await_text_missing('line_8')
|
||||
assert h.get_cursor_position() == (0, 8)
|
||||
h.await_cursor_position(0, 8)
|
||||
|
||||
h.await_text('line_8')
|
||||
|
||||
# but a resize to smaller should
|
||||
with h.resize(80, 9):
|
||||
h.await_text_missing('line_0')
|
||||
assert h.get_cursor_position() == (0, 3)
|
||||
h.await_cursor_position(0, 3)
|
||||
# make sure we're still on the same line
|
||||
assert h.screenshot().splitlines()[3] == 'line_7'
|
||||
|
||||
@@ -378,7 +390,7 @@ def test_horizontal_scrolling(tmpdir):
|
||||
h.press('Right')
|
||||
h.await_text('«77777778')
|
||||
h.await_text('344444444445»')
|
||||
assert h.get_cursor_position() == (7, 2)
|
||||
h.await_cursor_position(7, 2)
|
||||
for _ in range(71):
|
||||
h.press('Right')
|
||||
h.await_text('«77777778')
|
||||
@@ -397,10 +409,10 @@ def test_horizontal_scrolling_exact_width(tmpdir):
|
||||
for _ in range(78):
|
||||
h.press('Right')
|
||||
h.await_text_missing('»')
|
||||
assert h.get_cursor_position() == (78, 1)
|
||||
h.await_cursor_position(78, 1)
|
||||
h.press('Right')
|
||||
h.await_text('«0000000')
|
||||
assert h.get_cursor_position() == (7, 1)
|
||||
h.await_cursor_position(7, 1)
|
||||
|
||||
|
||||
def test_horizontal_scrolling_narrow_window(tmpdir):
|
||||
@@ -414,7 +426,7 @@ def test_horizontal_scrolling_narrow_window(tmpdir):
|
||||
h.press('Right')
|
||||
h.await_text('0000»')
|
||||
h.press('Right')
|
||||
assert h.get_cursor_position() == (3, 1)
|
||||
h.await_cursor_position(3, 1)
|
||||
h.await_text('«000»')
|
||||
for _ in range(6):
|
||||
h.press('Right')
|
||||
@@ -431,7 +443,7 @@ def test_window_width_1(tmpdir):
|
||||
for _ in range(3):
|
||||
h.press('Right')
|
||||
h.await_text('hello')
|
||||
assert h.get_cursor_position() == (3, 1)
|
||||
h.await_cursor_position(3, 1)
|
||||
|
||||
|
||||
def test_basic_text_editing(tmpdir):
|
||||
@@ -462,10 +474,10 @@ def test_backspace_joins_lines(tmpdir):
|
||||
h.press('Bspace')
|
||||
h.await_text('foobar')
|
||||
h.await_text('f *')
|
||||
assert h.get_cursor_position() == (3, 1)
|
||||
h.await_cursor_position(3, 1)
|
||||
# pressing down should retain the X position
|
||||
h.press('Down')
|
||||
assert h.get_cursor_position() == (3, 2)
|
||||
h.await_cursor_position(3, 2)
|
||||
|
||||
|
||||
def test_backspace_at_end_of_file_still_allows_scrolling_down(tmpdir):
|
||||
@@ -477,7 +489,7 @@ def test_backspace_at_end_of_file_still_allows_scrolling_down(tmpdir):
|
||||
h.press('Down')
|
||||
h.press('Bspace')
|
||||
h.press('Down')
|
||||
assert h.get_cursor_position() == (0, 2)
|
||||
h.await_cursor_position(0, 2)
|
||||
assert '*' not in h.screenshot()
|
||||
|
||||
|
||||
@@ -492,7 +504,7 @@ def test_backspace_deletes_text(tmpdir):
|
||||
h.press('Bspace')
|
||||
h.await_text('ohi')
|
||||
h.await_text('f *')
|
||||
assert h.get_cursor_position() == (2, 1)
|
||||
h.await_cursor_position(2, 1)
|
||||
|
||||
|
||||
def test_delete_at_end_of_file(tmpdir):
|
||||
@@ -535,7 +547,7 @@ def test_press_enter_beginning_of_file(tmpdir):
|
||||
h.await_text('hello world')
|
||||
h.press('Enter')
|
||||
h.await_text('\n\nhello world')
|
||||
assert h.get_cursor_position() == (0, 2)
|
||||
h.await_cursor_position(0, 2)
|
||||
h.await_text('f *')
|
||||
|
||||
|
||||
@@ -549,6 +561,6 @@ def test_press_enter_mid_line(tmpdir):
|
||||
h.press('Right')
|
||||
h.press('Enter')
|
||||
h.await_text('hello\n world')
|
||||
assert h.get_cursor_position() == (0, 2)
|
||||
h.await_cursor_position(0, 2)
|
||||
h.press('Up')
|
||||
assert h.get_cursor_position() == (0, 1)
|
||||
h.await_cursor_position(0, 1)
|
||||
|
||||
Reference in New Issue
Block a user