Skip to content

Commit eecf26e

Browse files
authored
[mypyc] make --update-data work for mypyc tests (#22268)
Authored by Codex
1 parent 1107984 commit eecf26e

2 files changed

Lines changed: 7 additions & 30 deletions

File tree

‎mypyc/test/test_run.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,9 @@ def run_case_step(self, testcase: DataDrivenTestCase, incremental_step: int) ->
440440
outlines = [
441441
fix_native_line_number(line, testcase.file, testcase.line) for line in outlines
442442
]
443-
assert_test_output(testcase, outlines, msg, expected)
443+
assert_test_output(
444+
testcase, outlines, msg, expected, incremental_step=incremental_step
445+
)
444446

445447
if incremental_step > 1 and options.incremental:
446448
suffix = "" if incremental_step == 2 else str(incremental_step - 1)

‎mypyc/test/testutil.py‎

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from mypy.test.config import test_temp_dir
1919
from mypy.test.data import DataDrivenTestCase, DataSuite
2020
from mypy.test.helpers import assert_string_arrays_equal
21+
from mypy.test.update_data import update_testcase_output
2122
from mypy.types import Type
2223
from mypyc.analysis.ircheck import assert_func_ir_valid
2324
from mypyc.common import IS_32_BIT_PLATFORM, PLATFORM_SIZE
@@ -153,46 +154,20 @@ def build_ir_for_single_file2(
153154
return module, tree, result.types, mapper
154155

155156

156-
def update_testcase_output(testcase: DataDrivenTestCase, output: list[str]) -> None:
157-
# TODO: backport this to mypy
158-
assert testcase.old_cwd is not None, "test was not properly set up"
159-
testcase_path = os.path.join(testcase.old_cwd, testcase.file)
160-
with open(testcase_path) as f:
161-
data_lines = f.read().splitlines()
162-
163-
# We can't rely on the test line numbers to *find* the test, since
164-
# we might fix multiple tests in a run. So find it by the case
165-
# header. Give up if there are multiple tests with the same name.
166-
test_slug = f"[case {testcase.name}]"
167-
if data_lines.count(test_slug) != 1:
168-
return
169-
start_idx = data_lines.index(test_slug)
170-
stop_idx = start_idx + 11
171-
while stop_idx < len(data_lines) and not data_lines[stop_idx].startswith("[case "):
172-
stop_idx += 1
173-
174-
test = data_lines[start_idx:stop_idx]
175-
out_start = test.index("[out]")
176-
test[out_start + 1 :] = output
177-
data_lines[start_idx:stop_idx] = test + [""]
178-
data = "\n".join(data_lines)
179-
180-
with open(testcase_path, "w") as f:
181-
print(data, file=f)
182-
183-
184157
def assert_test_output(
185158
testcase: DataDrivenTestCase,
186159
actual: list[str],
187160
message: str,
188161
expected: list[str] | None = None,
189162
formatted: list[str] | None = None,
163+
*,
164+
incremental_step: int = 1,
190165
) -> None:
191166
__tracebackhide__ = True
192167

193168
expected_output = expected if expected is not None else testcase.output
194169
if expected_output != actual and testcase.config.getoption("--update-data", False):
195-
update_testcase_output(testcase, actual)
170+
update_testcase_output(testcase, actual, incremental_step=incremental_step)
196171

197172
assert_string_arrays_equal(
198173
expected_output, actual, f"{message} ({testcase.file}, line {testcase.line})"

0 commit comments

Comments
 (0)