Skip to content

Commit d87feaa

Browse files
asmeurercodex
andcommitted
Fix prompt-toolkit pipe input tests
Co-authored-by: OpenAI Codex <codex@openai.com>
1 parent 2f4dd2c commit d87feaa

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

‎mypython/tests/test_completion.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from ..dircompletion import DirCompleter
88

9-
from .test_mypython import _run_session_with_text, _build_test_session
9+
from .test_mypython import _run_session_with_text, _build_test_session, _terminal_input
1010

1111
import flaky
1212
retry = flaky.flaky(max_runs=5)
@@ -32,7 +32,7 @@ def _input_with_tabs(text, _input, sleep_time=0.8):
3232
for t in UP_TO_TAB.findall(text):
3333
if not t:
3434
continue
35-
_input.send_text(t)
35+
_input.send_text(_terminal_input(t))
3636
time.sleep(sleep_time)
3737

3838
def _test_completion(text, min_time=0.1, max_time=2, runs=3):

‎mypython/tests/test_mypython.py‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,21 @@ def write(self, data):
3535
def write_raw(self, data):
3636
self.written_raw_data += data
3737

38+
def _terminal_input(text):
39+
"""
40+
Convert readable test input to terminal input.
41+
42+
In the real key map, raw LF is Shift-Enter. Tests write commands with
43+
ordinary newlines for readability, so send CR to exercise Enter.
44+
"""
45+
return text.replace('\n', '\r')
46+
3847
def _run_session_with_text(session, text, close=False):
3948
if text:
4049
assert text.endswith('\n')
4150
if '\n' in text[:-1]:
4251
assert text.endswith('\n\n')
43-
session.input.send_text(text)
52+
session.input.send_text(_terminal_input(text))
4453

4554
try:
4655
# with session._auto_refresh_context():
@@ -90,8 +99,9 @@ def _test_output(text, *, doctest_mode=False, remove_terminal_sequences=True):
9099
IMPORTANT: Only things printed directly to stdout/stderr are tested.
91100
Things printed via prompt_toolkit (e.g., print_tokens) are not caught.
92101
93-
For now, the input must be a single command. Use \x1b\n (M-Enter) to keep
94-
multiple lines in the same input.
102+
For now, the input must be a single command. Use \x1b\n to keep
103+
multiple lines in the same input; _run_session_with_text() translates
104+
it to the terminal's M-Enter sequence.
95105
96106
"""
97107
mypython.doctest_mode(doctest_mode)

0 commit comments

Comments
 (0)