Skip to content

Commit 9f1cfac

Browse files
Merge pull request #1077 from reef-technologies/improve-pexpect-errors
Improve error messaging in autocomplete tests
2 parents be77460 + 86d83d6 commit 9f1cfac

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve error messaging in autocomplete integration tests.

test/integration/test_autocomplete.py

+37-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,41 @@
2727
"""
2828

2929

30+
def patched_spawn(*args, **kwargs):
31+
"""
32+
Patch pexpect.spawn to improve error messages
33+
"""
34+
35+
instance = pexpect.spawn(*args, **kwargs)
36+
37+
def _patch_expect(func):
38+
def _wrapper(pattern_list, **kwargs):
39+
try:
40+
return func(pattern_list, **kwargs)
41+
except pexpect.exceptions.TIMEOUT as exc:
42+
raise pexpect.exceptions.TIMEOUT(
43+
f'Timeout reached waiting for `{pattern_list}` to be autocompleted'
44+
) from exc
45+
except pexpect.exceptions.EOF as exc:
46+
raise pexpect.exceptions.EOF(
47+
f'Received EOF waiting for `{pattern_list}` to be autocompleted'
48+
) from exc
49+
except Exception as exc:
50+
raise RuntimeError(
51+
f'Unexpected error waiting for `{pattern_list}` to be autocompleted'
52+
) from exc
53+
54+
return _wrapper
55+
56+
instance.expect = _patch_expect(instance.expect)
57+
instance.expect_exact = _patch_expect(instance.expect_exact)
58+
59+
# capture child shell's output for debugging
60+
instance.logfile = sys.stdout.buffer
61+
62+
return instance
63+
64+
3065
@pytest.fixture(scope='session')
3166
def bashrc(homedir):
3267
bashrc_path = homedir / '.bashrc'
@@ -44,7 +79,7 @@ def autocomplete_installed(env, homedir, bashrc, cli_version, cli_command, is_ru
4479
if is_running_on_docker:
4580
pytest.skip('Not supported on Docker')
4681

47-
shell = pexpect.spawn(
82+
shell = patched_spawn(
4883
f'bash -i -c "{cli_command} install-autocomplete"', env=env, logfile=sys.stderr.buffer
4984
)
5085
try:
@@ -58,7 +93,7 @@ def autocomplete_installed(env, homedir, bashrc, cli_version, cli_command, is_ru
5893

5994
@pytest.fixture
6095
def shell(env):
61-
shell = pexpect.spawn('bash -i', env=env, maxread=1000)
96+
shell = patched_spawn('bash -i', env=env, maxread=1000)
6297
shell.setwinsize(100, 1000) # required to see all suggestions in tests
6398
yield shell
6499
shell.close()

0 commit comments

Comments
 (0)