27
27
"""
28
28
29
29
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
+
30
65
@pytest .fixture (scope = 'session' )
31
66
def bashrc (homedir ):
32
67
bashrc_path = homedir / '.bashrc'
@@ -44,7 +79,7 @@ def autocomplete_installed(env, homedir, bashrc, cli_version, cli_command, is_ru
44
79
if is_running_on_docker :
45
80
pytest .skip ('Not supported on Docker' )
46
81
47
- shell = pexpect . spawn (
82
+ shell = patched_spawn (
48
83
f'bash -i -c "{ cli_command } install-autocomplete"' , env = env , logfile = sys .stderr .buffer
49
84
)
50
85
try :
@@ -58,7 +93,7 @@ def autocomplete_installed(env, homedir, bashrc, cli_version, cli_command, is_ru
58
93
59
94
@pytest .fixture
60
95
def shell (env ):
61
- shell = pexpect . spawn ('bash -i' , env = env , maxread = 1000 )
96
+ shell = patched_spawn ('bash -i' , env = env , maxread = 1000 )
62
97
shell .setwinsize (100 , 1000 ) # required to see all suggestions in tests
63
98
yield shell
64
99
shell .close ()
0 commit comments