Answer principles round 1: 1 🔴, 7 🟡 (0 misread); every effect is handed in as an argument, no test patches a global, and the traces principle scopes tools that run on their user's machine
CI / tests (pull_request) Successful in 3m43s

This commit is contained in:
2026-11-19 12:42:03 +01:00
parent 8ca612b7a4
commit cb3135bc8c
124 changed files with 1437 additions and 793 deletions
@@ -0,0 +1,36 @@
import ast
import pathlib
import subprocess
import sys
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from repo_checks import ROOT
def patches(source: str) -> list[int]:
"""The line numbers where source names mock's patch, as an attribute of mock or imported from it."""
found = []
for node in ast.walk(ast.parse(source)):
if isinstance(node, ast.Attribute) and node.attr == "patch" and ast.unparse(node.value).split(".")[-1] == "mock":
found.append(node.lineno)
elif isinstance(node, ast.ImportFrom) and node.module == "unittest.mock" and any(alias.name == "patch" for alias in node.names):
found.append(node.lineno)
return sorted(found)
class NoPatchedGlobals(unittest.TestCase):
def test_no_python_file_under_bin_patches_a_global(self):
# A file not yet staged counts too, so a new suite is checked before its first commit.
listed = subprocess.run(["git", "ls-files", "--cached", "--others", "--exclude-standard", "bin/*.py"],capture_output=True, check=True, cwd=ROOT, text=True).stdout.split()
self.assertIn("bin/repo_checks/test_no_patched_globals.py", listed)
patching = {name for name in listed if patches((ROOT / name).read_text(encoding="utf-8"))}
self.assertEqual(patching, set(), "hand the code under test its effect as an argument instead")
def test_each_spelling_of_patch_is_found(self):
source = "import unittest.mock\nfrom unittest import mock\nfrom unittest.mock import Mock, patch\n\nunittest.mock.patch.dict(a, b)\nmock.patch.object(a, 'b')\nmock.patch('a.b')\nmock.Mock()\n"
self.assertEqual(patches(source), [3, 5, 6, 7])
if __name__ == "__main__":
unittest.main()
+21 -21
View File
@@ -81,6 +81,13 @@ def cycle(imported: dict[str, set[str]]) -> list[str]:
return []
def tree(folder: pathlib.Path, sources: dict[str, str]) -> None:
"""Writes each source to its path under folder."""
for path, source in sources.items():
(folder / path).parent.mkdir(parents=True, exist_ok=True)
(folder / path).write_text(source, encoding="utf-8")
class PackageCycles(unittest.TestCase):
def test_no_package_under_bin_imports_one_that_imports_it_back(self):
self.assertEqual(cycle(package_imports(ROOT / "bin")), [], "move what both packages need into a package that imports neither")
@@ -94,17 +101,13 @@ class PackageCycles(unittest.TestCase):
def test_an_import_of_the_layer_above_is_found_in_tests_too(self):
with tempfile.TemporaryDirectory() as temp:
folder = pathlib.Path(temp)
for package, name, source in (("low", "mod.py", "import other\n"), ("low", "test_mod.py", "from high.mod import x\n"), ("low", "run.py", "def f():\n\timport high\n"), ("high", "mod.py", "import low\n")):
(folder / package).mkdir(exist_ok=True)
(folder / package / name).write_text(source)
tree(folder, {"low/mod.py": "import other\n", "low/test_mod.py": "from high.mod import x\n", "low/run.py": "def f():\n\timport high\n", "high/mod.py": "import low\n"})
self.assertEqual(upward_imports(folder, {"low": ("high",)}), ["low/run.py imports high", "low/test_mod.py imports high"])
def test_a_driver_imported_from_another_package_is_found_in_tests_too(self):
with tempfile.TemporaryDirectory() as temp:
folder = pathlib.Path(temp)
for package, name, source in (("a", "mod.py", "import a.other\n"), ("b", "test_mod.py", "from a.mod import x\n"), ("b", "mod.py", "def f():\n\timport a\n"), ("c", "mod.py", "import b\n")):
(folder / package).mkdir(exist_ok=True)
(folder / package / name).write_text(source)
tree(folder, {"a/mod.py": "import a.other\n", "b/test_mod.py": "from a.mod import x\n", "b/mod.py": "def f():\n\timport a\n", "c/mod.py": "import b\n"})
self.assertEqual(driver_imports(folder, ("a",)), ["b/mod.py imports a", "b/test_mod.py imports a"])
def test_no_module_imports_a_test_module_of_another_package(self):
@@ -113,26 +116,23 @@ class PackageCycles(unittest.TestCase):
def test_a_test_module_imported_from_another_package_is_found_however_it_is_spelled(self):
with tempfile.TemporaryDirectory() as temp:
folder = pathlib.Path(temp)
sources = (
("a", "test_mod.py", "from a.test_other import x\nfrom test_env import y\nfrom a.doubles import z\n"),
("b", "test_mod.py", "from a.test_mod import x\n"),
("b", "mod.py", "def f():\n\timport a.deep.test_far\n"),
("c", "test_mod.py", "from a import test_mod\nfrom a.test_mod import x, y\n"),
)
for package, name, source in sources:
(folder / package).mkdir(exist_ok=True)
(folder / package / name).write_text(source)
(folder / "test_root.py").write_text("from a.test_mod import x\n")
tree(folder, {
"a/test_mod.py": "from a.test_other import x\nfrom test_env import y\nfrom a.doubles import z\n",
"b/test_mod.py": "from a.test_mod import x\n",
"b/mod.py": "def f():\n\timport a.deep.test_far\n",
"c/test_mod.py": "from a import test_mod\nfrom a.test_mod import x, y\n",
"test_root.py": "from a.test_mod import x\n",
})
self.assertEqual(imports_of_test_modules(folder), ["b/mod.py imports a.deep.test_far", "b/test_mod.py imports a.test_mod", "c/test_mod.py imports a.test_mod", "test_root.py imports a.test_mod"])
def test_reads_an_import_deferred_into_a_function_and_leaves_out_tests(self):
with tempfile.TemporaryDirectory() as temp:
folder = pathlib.Path(temp)
for package, source in (("a", "def f():\n\tfrom b.mod import x\n"), ("b", "import a.mod\n"), ("c", "")):
(folder / package).mkdir()
(folder / package / "__init__.py").write_text("")
(folder / package / "mod.py").write_text(source)
(folder / "c" / "test_mod.py").write_text("import a\n")
tree(folder, {
"a/__init__.py": "", "a/mod.py": "def f():\n\tfrom b.mod import x\n",
"b/__init__.py": "", "b/mod.py": "import a.mod\n",
"c/__init__.py": "", "c/mod.py": "", "c/test_mod.py": "import a\n",
})
self.assertEqual(package_imports(folder), {"a": {"b"}, "b": {"a"}, "c": set()})
def test_a_cycle_through_a_deferred_import_is_found(self):