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
+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):