Skip to content

Commit f3cf86c

Browse files
authored
More accurate test dependencies (#7261)
1 parent cdc7842 commit f3cf86c

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

scripts/update_lib/cmd_todo.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ def get_all_tests(cpython_prefix: str) -> list[str]:
146146
tests = set()
147147
for entry in test_dir.iterdir():
148148
# Skip non-test items
149-
if not entry.name.startswith(("_test", "test_")):
149+
if "test" not in entry.name:
150+
continue
151+
152+
# Exclude special cases
153+
if "regrtest" in entry.name:
150154
continue
151155

152156
if entry.is_file() and entry.suffix == ".py":
@@ -333,8 +337,16 @@ def compute_test_todo_list(
333337
# Get order from DEPENDENCIES
334338
test_order = lib_test_order[lib_name].index(test_name)
335339
else:
336-
# Extract lib name from test name (test_foo -> foo)
337-
lib_name = test_name.removeprefix("test_").removeprefix("_test")
340+
# Extract lib name from test name:
341+
# - test_foo -> foo
342+
# - datetimetester -> datetime
343+
# - xmltests -> xml
344+
lib_name = (
345+
test_name.removeprefix("test_")
346+
.removeprefix("_test")
347+
.removesuffix("tester")
348+
.removesuffix("tests")
349+
)
338350
test_order = 0 # Default order for tests not in DEPENDENCIES
339351

340352
# Check if corresponding lib is up-to-date

scripts/update_lib/deps.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ def test_imports(self) -> frozenset[str]:
5353
@property
5454
def lib_imports(self) -> frozenset[str]:
5555
return frozenset(
56-
# module.split(".", 1)[0]
57-
module
58-
for module in self.__imports
59-
if not module.startswith("test.")
56+
module for module in self.__imports if not module.startswith("test.")
6057
)
6158

6259
def visit_Import(self, node):
@@ -119,7 +116,7 @@ def visit_Call(self, node) -> None:
119116
self.__imports.add(f"test.{target}")
120117

121118

122-
def parse_test_imports(content: str) -> set[str]:
119+
def parse_test_imports(content: str) -> frozenset[str]:
123120
"""Parse test file content and extract test package dependencies."""
124121
if not (tree := safe_parse_ast(content)):
125122
return set()
@@ -129,7 +126,7 @@ def parse_test_imports(content: str) -> set[str]:
129126
return visitor.test_imports
130127

131128

132-
def parse_lib_imports(content: str) -> set[str]:
129+
def parse_lib_imports(content: str) -> frozenset[str]:
133130
"""Parse library file and extract all imported module names."""
134131
if not (tree := safe_parse_ast(content)):
135132
return set()
@@ -147,8 +144,7 @@ def parse_lib_imports(content: str) -> set[str]:
147144
def filter_rustpython_todo(content: str) -> str:
148145
"""Remove lines containing RustPython TODO markers."""
149146
lines = content.splitlines(keepends=True)
150-
filtered = [line for line in lines if TODO_MARKER not in line]
151-
return "".join(filtered)
147+
return "".join(line for line in lines if TODO_MARKER not in line)
152148

153149

154150
def count_rustpython_todo(content: str) -> int:
@@ -342,7 +338,7 @@ def clear_import_graph_caches() -> None:
342338
},
343339
"codecs": {
344340
"test": [
345-
"test_codecs.py",
341+
"test_charmapcodec.py",
346342
"test_codeccallbacks.py",
347343
"test_codecencodings_cn.py",
348344
"test_codecencodings_hk.py",
@@ -355,8 +351,9 @@ def clear_import_graph_caches() -> None:
355351
"test_codecmaps_jp.py",
356352
"test_codecmaps_kr.py",
357353
"test_codecmaps_tw.py",
358-
"test_charmapcodec.py",
354+
"test_codecs.py",
359355
"test_multibytecodec.py",
356+
"testcodec.py",
360357
],
361358
},
362359
# Non-pattern hard_deps (can't be auto-detected)
@@ -423,6 +420,7 @@ def clear_import_graph_caches() -> None:
423420
"test_multiprocessing_forkserver",
424421
"test_multiprocessing_spawn",
425422
"test_multiprocessing_main_handling.py",
423+
"_test_multiprocessing.py",
426424
],
427425
},
428426
"urllib": {
@@ -745,12 +743,9 @@ def resolve_hard_dep_parent(name: str, cpython_prefix: str) -> str | None:
745743
# Auto-detect _py{module} or _py_{module} patterns
746744
# Only if the parent module actually exists
747745
if name.startswith("_py"):
748-
if name.startswith("_py_"):
749-
# _py_abc -> abc
750-
parent = name[4:]
751-
else:
752-
# _pydatetime -> datetime
753-
parent = name[3:]
746+
# _py_abc -> abc
747+
# _pydatetime -> datetime
748+
parent = name.removeprefix("_py_").removeprefix("_py")
754749

755750
# Verify the parent module exists
756751
lib_dir = pathlib.Path(cpython_prefix) / "Lib"
@@ -781,7 +776,7 @@ def resolve_test_to_lib(test_name: str) -> str | None:
781776
tests = dep_info.get("test", [])
782777
for test_path in tests:
783778
# test_path is like "test_urllib2.py" or "test_multiprocessing_fork"
784-
path_stem = test_path[:-3] if test_path.endswith(".py") else test_path
779+
path_stem = test_path.removesuffix(".py")
785780
if path_stem == test_name:
786781
return lib_name
787782

0 commit comments

Comments
 (0)