Skip to content

Commit b578c0d

Browse files
committed
Try to ignore more carriage returns in test_repl
1 parent de8f942 commit b578c0d

File tree

1 file changed

+36
-27
lines changed
  • graalpython/com.oracle.graal.python.test/src/tests

1 file changed

+36
-27
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_repl.py

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,19 @@
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
3939

40-
import sys
40+
import os
4141
import platform
42+
import re
43+
import select
44+
import subprocess
45+
import sys
46+
import tempfile
4247
import unittest
48+
from dataclasses import dataclass
49+
from textwrap import dedent
4350

44-
if (sys.platform != 'win32' and (sys.platform != 'linux' or platform.machine() != 'aarch64')) and (sys.implementation.name != 'graalpy' or __graalpython__.posix_module_backend() != 'java'):
45-
import os
46-
import re
47-
import select
48-
import subprocess
49-
import tempfile
50-
import termios
51-
from textwrap import dedent
51+
if (sys.platform != 'win32' and (sys.platform != 'linux' or platform.machine() != 'aarch64')) and (
52+
sys.implementation.name != 'graalpy' or __graalpython__.posix_module_backend() != 'java'):
5253

5354
# The terminal tests can be flaky
5455
def autoretry(fn):
@@ -63,16 +64,25 @@ def decorated(*args, **kwargs):
6364
print("Retrying test")
6465
continue
6566
fn(*args, **kwargs)
67+
6668
return decorated
6769

6870

71+
@dataclass
72+
class ExpectedInOutItem:
73+
prompt: str
74+
input: str
75+
output: str
76+
77+
6978
@autoretry
7079
def validate_repl(stdin, python_args=(), ignore_preamble=True):
7180
env = os.environ.copy()
7281
env['TERM'] = 'ansi'
7382
env['PYTHONIOENCODING'] = 'utf-8'
7483
pty_parent, pty_child = os.openpty()
7584
try:
85+
import termios
7686
termios.tcsetwinsize(pty_parent, (60, 80))
7787
proc = subprocess.Popen(
7888
[sys.executable, '-I', *python_args],
@@ -82,36 +92,35 @@ def validate_repl(stdin, python_args=(), ignore_preamble=True):
8292
stderr=pty_child,
8393
)
8494
out = ''
85-
input_and_output = []
95+
input_and_output: list[ExpectedInOutItem] = []
8696
expected_preamble = ''
87-
in_matches = list(re.finditer(r'^(>>>|\.\.\.) (.*)', stdin, flags=re.MULTILINE))
97+
in_matches = list(re.finditer(r'^(>>>|\.\.\.) ?(.*)', stdin, flags=re.MULTILINE))
8898
for i, match in enumerate(in_matches):
8999
if i == 0:
90100
expected_preamble = stdin[:match.start() - 1] if match.start() else ''
91-
input_and_output.append((
92-
match.group(1),
93-
match.group(2),
94-
stdin[match.end():in_matches[i + 1].start() - 1 if i + 1 < len(in_matches) else -1],
95-
))
101+
prompt = match.group(1)
102+
expected_input = match.group(2)
103+
expected_output = stdin[match.end():in_matches[i + 1].start() - 1 if i + 1 < len(in_matches) else -1]
104+
input_and_output.append(ExpectedInOutItem(prompt, expected_input, expected_output))
96105
index = -1
97106
whole_out = ''
98107
while True:
99108
rlist, _, _ = select.select([pty_parent], [], [], 60)
100109
assert pty_parent in rlist, f"Timed out waiting for REPL output. Output: {whole_out}{out}"
101110
out += os.read(pty_parent, 1024).decode('utf-8')
102111
out = re.sub(r'\x1b\[(?:\?2004[hl]|\d+[A-G])', '', out)
103-
out = out.replace('\r\n', '\n')
112+
out = re.sub(r'\r+\n', '\n', out)
104113
if out == '>>> ' or out.endswith(('\n>>> ', '\n... ')):
105114
prompt = out[:3]
106115
actual = out[:-5]
107116
if index >= 0:
108-
expected_prompt, current_in, expected_out = input_and_output[index]
109-
assert prompt == expected_prompt
110-
expected = f'{expected_prompt} {current_in}{expected_out}'
117+
current = input_and_output[index]
118+
assert prompt == current.prompt, f"Actual prompt: {prompt}\nExpected prompt: {current.prompt}"
119+
expected = f'{current.prompt} {current.input}{current.output}'
111120
else:
112121
expected = expected_preamble
113122
if index >= 0 or not ignore_preamble:
114-
assert actual == expected, f'Actual:\n{actual!r}\nExpected:\n{expected!r}'
123+
assert actual == expected, f'Actual:\n{actual!r}\nExpected:\n{expected!r}\nWhole output:\n{whole_out}{out}'
115124
index += 1
116125
whole_out += out[:-4]
117126
out = out[-4:]
@@ -123,7 +132,7 @@ def validate_repl(stdin, python_args=(), ignore_preamble=True):
123132
assert not out.strip(), f"Garbage after EOF:\n{out!r}"
124133
return
125134
else:
126-
_, next_in, _ = input_and_output[index]
135+
next_in = input_and_output[index].input
127136
os.write(pty_parent, next_in.encode('utf-8') + b'\r')
128137
finally:
129138
os.close(pty_child)
@@ -147,21 +156,21 @@ def test_continuation():
147156
>>> def foo():
148157
... a = 1
149158
... return a
150-
...
159+
...
151160
>>> class Foo:
152161
... def meth(self):
153162
... return 1
154-
...
163+
...
155164
>>> from functools import wraps
156165
>>> @wraps
157166
... def foo(fn):
158167
... return fn
159-
...
168+
...
160169
>>> from contextlib import contextmanager
161170
>>> @contextmanager
162171
... class Foo:
163172
... pass
164-
...
173+
...
165174
>>> """
166175
... asdf
167176
... """
@@ -182,7 +191,7 @@ def test_exceptions():
182191
>>> class BrokenRepr:
183192
... def __repr__(self):
184193
... asdf
185-
...
194+
...
186195
>>> BrokenRepr()
187196
Traceback (most recent call last):
188197
File "<stdin>", line 1, in <module>

0 commit comments

Comments
 (0)