mirror of
https://github.com/hasso5703/alzeo.git
synced 2026-12-05 12:24:28 +01:00
tests: a file built on the shared tests of tests/ declares them too (run_suite.py, run_single.py)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fefcbd3f57
commit
d24a92b2fb
4 files changed
+67
-3
No files matched your search
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env python3
|
||||
"""How many tests a test file declares: the `def test_` written in it, and those of the shared classes of tests/ that its classes
|
||||
build on (vector_lanes.py's), once for each class that inherits them.
|
||||
|
||||
run_suite.py and run_single.py compare that number with what unittest ran, so that a file which stops early, or a test that never
|
||||
runs, fails the CI. Counted on the file's text alone, a file built on shared tests read as running more than it declares:
|
||||
test_embed_lane.py ran 40 of its 14, test_clipstral_lane.py 35 of its 9 (found in the review of v1.61.0, 2026-12-02)."""
|
||||
import ast
|
||||
import pathlib
|
||||
|
||||
# spelled in two parts: the CI counts that string to know how many tests a file declares
|
||||
TEST_DEF = "def " + "test_"
|
||||
|
||||
|
||||
def inherited(module, name, seen=None):
|
||||
"""The tests the class `name` of a shared module of tests/ declares, its bases in that module included. A test module's
|
||||
classes are its own file's: 0 for them, and for anything that is not a class of such a module."""
|
||||
module = pathlib.Path(module)
|
||||
if not module.is_file() or module.name.startswith("test_"):
|
||||
return 0
|
||||
try:
|
||||
classes = {c.name: c for c in ast.parse(module.read_text()).body if isinstance(c, ast.ClassDef)}
|
||||
except SyntaxError:
|
||||
return 0
|
||||
seen = set() if seen is None else seen
|
||||
if name not in classes or name in seen:
|
||||
return 0
|
||||
seen.add(name)
|
||||
cls = classes[name]
|
||||
n = sum(isinstance(f, (ast.FunctionDef, ast.AsyncFunctionDef)) and f.name.startswith("test_") for f in cls.body)
|
||||
return n + sum(inherited(module, b.id, seen) for b in cls.bases if isinstance(b, ast.Name))
|
||||
|
||||
|
||||
def declared(path):
|
||||
"""The tests the file at `path` declares (see the module's docstring)."""
|
||||
path = pathlib.Path(path)
|
||||
text = path.read_text()
|
||||
n = text.count(TEST_DEF)
|
||||
try:
|
||||
tree = ast.parse(text)
|
||||
except SyntaxError:
|
||||
return n
|
||||
for node in tree.body:
|
||||
if isinstance(node, ast.ClassDef):
|
||||
for base in node.bases:
|
||||
if isinstance(base, ast.Attribute) and isinstance(base.value, ast.Name):
|
||||
n += inherited(path.parent / f"{base.value.id}.py", base.attr)
|
||||
return n
|
||||
+4
-1
@@ -31,6 +31,9 @@ import sys
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
|
||||
from declared import declared as declared_tests # noqa: E402 (its own folder, which python -I leaves out)
|
||||
|
||||
REPO = pathlib.Path(__file__).resolve().parents[1]
|
||||
|
||||
# The pair's four files, each a process of its own, the longest first (seconds measured on the
|
||||
@@ -102,7 +105,7 @@ def groups(mods, own=OWN, rest=REST):
|
||||
|
||||
|
||||
def declared(mods, repo=REPO):
|
||||
return sum((repo / (m.replace(".", "/") + ".py")).read_text().count(TEST_DEF) for m in mods)
|
||||
return sum(declared_tests(repo / (m.replace(".", "/") + ".py")) for m in mods)
|
||||
|
||||
|
||||
def run_group(mods, repo=REPO):
|
||||
|
||||
+4
-1
@@ -33,6 +33,9 @@ import tempfile
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
|
||||
from declared import declared as declared_tests # noqa: E402 (its own folder, which python -I leaves out)
|
||||
|
||||
REPO = pathlib.Path(__file__).resolve().parents[1]
|
||||
|
||||
# The longest files start first, so that they do not end alone at the tail of the run.
|
||||
@@ -119,7 +122,7 @@ def run_one(f, probe, repo=REPO, timeout=1800):
|
||||
return verdict
|
||||
text = path.read_text()
|
||||
if TEST_DEF in text:
|
||||
declared = text.count(TEST_DEF)
|
||||
declared = declared_tests(path)
|
||||
ran = max([int(w.split()[1]) for w in out.splitlines() if w.startswith("Ran ") and w.split()[1].isdigit()],
|
||||
default=0)
|
||||
if ran != declared:
|
||||
|
||||
+11
-1
@@ -41,6 +41,11 @@ FILES = {
|
||||
"dashboard/tests/test_cockpit_side.py": "import unittest\n\nclass T(unittest.TestCase):\n def TEST_a(self):\n"
|
||||
" pass\n" + MAIN,
|
||||
# says where it is on stderr, as unittest does, then hangs
|
||||
# tests shared by two lanes' files, as vector_lanes.py's: a class of a module of tests/ that is no test file
|
||||
"tests/shared_tests.py": "class Shared:\n def TEST_x(self):\n pass\n\n def TEST_y(self):\n pass\n\n\n"
|
||||
"class More(Shared):\n def TEST_z(self):\n pass\n",
|
||||
"tests/test_builds_on_shared.py": "import unittest\nimport shared_tests\n\nclass T(shared_tests.More, unittest.TestCase):\n"
|
||||
" def TEST_own(self):\n pass\n" + MAIN,
|
||||
"tests/test_hangs.py": "import sys, time, unittest\n\nclass T(unittest.TestCase):\n def TEST_a(self):\n"
|
||||
" sys.stderr.write('hung in TEST_a\\n'); sys.stderr.flush(); time.sleep(60)\n" + MAIN,
|
||||
}
|
||||
@@ -77,6 +82,11 @@ class OnPlantedDefects(unittest.TestCase):
|
||||
v = self.verdict("tests/test_ok.py")
|
||||
self.assertEqual((v["rc"], v["problems"]), (0, []))
|
||||
|
||||
def test_a_file_built_on_shared_tests_declares_them_too(self):
|
||||
# its own test, and the three of the shared classes it inherits: counted on its text alone it read as running 4 of its 1
|
||||
v = self.verdict("tests/test_builds_on_shared.py")
|
||||
self.assertEqual((v["rc"], v["problems"]), (0, []))
|
||||
|
||||
def test_a_failing_test_is_named_with_its_output(self):
|
||||
v = self.verdict("tests/test_fails.py")
|
||||
self.assertNotEqual(v["rc"], 0)
|
||||
@@ -119,7 +129,7 @@ class OnPlantedDefects(unittest.TestCase):
|
||||
together, alone = rs.plan(rs.files(self.repo), repo=self.repo, first=[])
|
||||
self.assertEqual(alone, ["tests/test_takes_a_port.py"])
|
||||
self.assertNotIn("tests/test_takes_a_port.py", together)
|
||||
self.assertEqual(len(together) + len(alone), len(FILES))
|
||||
self.assertEqual(len(together) + len(alone), len([f for f in FILES if pathlib.Path(f).name.startswith("test_")]))
|
||||
|
||||
def test_a_longest_file_that_is_gone_stops_the_run(self):
|
||||
with self.assertRaisesRegex(SystemExit, "named as the longest, are gone"):
|
||||
|
||||
Reference in new issue
Block a user