Make the test of the store key's code files follow every kind of import, plain, absolute and inside functions, starting from build.py too, and add the three modules build.py imports that it left out: fingerprint, redirects and site
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0bb20c60dd
commit
622ffc74be
2 files changed
+40
-10
No files matched your search
+5
-1
@@ -35,7 +35,11 @@ CODE_FILES = [os.path.join(HERE, "build.py"),
|
|||||||
# what those import (a test, test_build.py, keeps this list closed under imports)
|
# what those import (a test, test_build.py, keeps this list closed under imports)
|
||||||
os.path.join(HERE, "olt", "words.py"),
|
os.path.join(HERE, "olt", "words.py"),
|
||||||
os.path.join(HERE, "olt", "elevation.py"),
|
os.path.join(HERE, "olt", "elevation.py"),
|
||||||
os.path.join(HERE, "olt", "markup.py")]
|
os.path.join(HERE, "olt", "markup.py"),
|
||||||
|
# and what build.py itself imports (the pages step, olt.pages, is not trail code)
|
||||||
|
os.path.join(HERE, "olt", "fingerprint.py"),
|
||||||
|
os.path.join(HERE, "olt", "redirects.py"),
|
||||||
|
os.path.join(HERE, "olt", "site.py")]
|
||||||
# Incremental-build cache, deliberately outside the publish directory.
|
# Incremental-build cache, deliberately outside the publish directory.
|
||||||
CACHE_DIR = os.path.join(HERE, ".cache")
|
CACHE_DIR = os.path.join(HERE, ".cache")
|
||||||
# Files copied verbatim into the root of the publish directory (_headers, ...).
|
# Files copied verbatim into the root of the publish directory (_headers, ...).
|
||||||
|
|||||||
@@ -56,23 +56,49 @@ class SimplifyTogetherTest(unittest.TestCase):
|
|||||||
self.assertLess(geo.point_count(b), geo.point_count(a))
|
self.assertLess(geo.point_count(b), geo.point_count(a))
|
||||||
|
|
||||||
|
|
||||||
|
def olt_imports(path):
|
||||||
|
"""The olt modules a file imports, by any statement anywhere in it (inside a function too):
|
||||||
|
`from . import x`, `from .x import y`, `from olt import x`, `from olt.x import y`, `import olt.x`."""
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
tree = ast.parse(f.read())
|
||||||
|
found = set()
|
||||||
|
for node in ast.walk(tree):
|
||||||
|
if isinstance(node, ast.ImportFrom):
|
||||||
|
if node.level == 1:
|
||||||
|
found.update([node.module] if node.module else [a.name for a in node.names])
|
||||||
|
elif node.level == 0 and node.module == "olt":
|
||||||
|
found.update(a.name for a in node.names)
|
||||||
|
elif node.level == 0 and (node.module or "").startswith("olt."):
|
||||||
|
found.add(node.module.split(".")[1])
|
||||||
|
elif isinstance(node, ast.Import):
|
||||||
|
found.update(a.name.split(".")[1] for a in node.names if a.name.startswith("olt."))
|
||||||
|
return found
|
||||||
|
|
||||||
|
|
||||||
class CodeFilesTest(unittest.TestCase):
|
class CodeFilesTest(unittest.TestCase):
|
||||||
|
# Not part of a trail's derived files: pages renders the site's pages from the finished records.
|
||||||
|
NOT_TRAIL_CODE = {"pages"}
|
||||||
|
|
||||||
def test_the_files_a_trail_is_built_from_are_closed_under_their_imports(self):
|
def test_the_files_a_trail_is_built_from_are_closed_under_their_imports(self):
|
||||||
# The store's key is made of these files: one that is missing would let a change to it
|
# The store's key is made of these files: one that is missing would let a change to it
|
||||||
# leave stale files in the store.
|
# leave stale files in the store.
|
||||||
olt = os.path.join(build.HERE, "olt")
|
olt = os.path.join(build.HERE, "olt")
|
||||||
listed = {os.path.abspath(p) for p in build.CODE_FILES}
|
listed = {os.path.abspath(p) for p in build.CODE_FILES}
|
||||||
|
self.assertIn(os.path.abspath(build.__file__), listed)
|
||||||
for path in sorted(listed):
|
for path in sorted(listed):
|
||||||
if os.path.dirname(path) != olt:
|
if os.path.dirname(path) != olt and os.path.basename(path) != "build.py":
|
||||||
continue
|
continue
|
||||||
with open(path, encoding="utf-8") as f:
|
for name in sorted(olt_imports(path) - self.NOT_TRAIL_CODE):
|
||||||
tree = ast.parse(f.read())
|
self.assertIn(os.path.join(olt, name + ".py"), listed,
|
||||||
for node in ast.walk(tree):
|
f"{os.path.basename(path)} imports olt.{name}, which the build's key does not cover")
|
||||||
if isinstance(node, ast.ImportFrom) and node.level == 1:
|
|
||||||
names = [node.module] if node.module else [a.name for a in node.names]
|
def test_the_scan_sees_every_kind_of_import(self):
|
||||||
for name in names:
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
self.assertIn(os.path.join(olt, name + ".py"), listed,
|
path = os.path.join(tmp, "m.py")
|
||||||
f"{os.path.basename(path)} imports olt.{name}, which the build's key does not cover")
|
with open(path, "w", encoding="utf-8") as f:
|
||||||
|
f.write("from . import a\nfrom .b import x\nfrom olt import c, d\nfrom olt.e import y\n"
|
||||||
|
"import olt.f\nimport os\n\ndef g():\n from olt import h\n")
|
||||||
|
self.assertEqual(olt_imports(path), set("abcdefh"))
|
||||||
|
|
||||||
|
|
||||||
def tree_files(root):
|
def tree_files(root):
|
||||||
|
|||||||
Reference in new issue
Block a user