From 5d86a42ad1d0048465ed448d06b5959ff0a3ea01 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Fri, 20 Nov 2026 16:33:34 -0700 Subject: [PATCH] Build the shared test site into a directory that is made new, with a test, and give the zip archives fixed member times so equal files make equal bytes --- web/build.py | 16 ++++++++++++--- web/tests/builtsite.py | 5 ++++- web/tests/test_builtsite.py | 41 +++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 web/tests/test_builtsite.py diff --git a/web/build.py b/web/build.py index c62b8c7..e974741 100644 --- a/web/build.py +++ b/web/build.py @@ -277,7 +277,7 @@ def copy_sources(tid, files, tdir, stats): # own directory) decide them, so a change to any of them makes a new key. Kept by content, not by # modification time, so a store restored from a CI cache or made in another checkout is as good as one # made here. -STORE_VERSION = "1" +STORE_VERSION = "2" # 2: archives have fixed member times _code_digest = None @@ -569,6 +569,9 @@ def archive_readme(name): f"{resolution}") +ARCHIVE_TIME = (1980, 1, 1, 0, 0, 0) # the earliest time a zip can hold + + def build_archive(name, recs, out_dir, stats): """Write /data/ (zip, deflate) unless it already holds exactly these files and is newer than every one of them and than this script.""" @@ -606,12 +609,19 @@ def build_archive(name, recs, out_dir, stats): stats.skipped += 1 return with zipfile.ZipFile(tmp, "w", compression=zipfile.ZIP_DEFLATED) as zf: - info = zipfile.ZipInfo("README.txt", date_time=time.localtime(mtime(__file__))[:6]) + # Every member gets the same fixed time and mode, so the same files always make the + # same bytes, whenever and wherever the archive is made (and so the store can hand + # one made elsewhere to a build here). + info = zipfile.ZipInfo("README.txt", date_time=ARCHIVE_TIME) info.compress_type = zipfile.ZIP_DEFLATED info.external_attr = 0o644 << 16 zf.writestr(info, archive_readme(name)) for arc, src in members: - zf.write(src, arc) + info = zipfile.ZipInfo(arc, date_time=ARCHIVE_TIME) + info.compress_type = zipfile.ZIP_DEFLATED + info.external_attr = 0o644 << 16 + with open(src, "rb") as source, zf.open(info, "w") as member: + shutil.copyfileobj(source, member, 1 << 20) try: os.makedirs(os.path.dirname(kept), exist_ok=True) for old in os.listdir(os.path.dirname(kept)): diff --git a/web/tests/builtsite.py b/web/tests/builtsite.py index cd57de3..9a2da45 100644 --- a/web/tests/builtsite.py +++ b/web/tests/builtsite.py @@ -32,7 +32,10 @@ _built = bool(PREBUILT) def build(out, *extra, env=None): - """Run the site build into out (a directory that need not exist). Raises when it fails.""" + """Run the site build into out, a directory that is made new: whatever was there is removed first, + so nothing in it can be a page an earlier build (of another branch, say) wrote and this one + does not. Raises when the build fails.""" + shutil.rmtree(out, ignore_errors=True) done = subprocess.run([sys.executable, os.path.join(WEB, "build.py"), "--out", out, *extra], stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True, env=env) if done.returncode: diff --git a/web/tests/test_builtsite.py b/web/tests/test_builtsite.py new file mode 100644 index 0000000..3871de4 --- /dev/null +++ b/web/tests/test_builtsite.py @@ -0,0 +1,41 @@ +"""The shared test build is always a fresh one: nothing in the directory tests read can be left over +from an earlier build, such as a page that another branch wrote and this one does not.""" +import os +import subprocess +import sys +import tempfile +import unittest +from unittest import mock + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) + +import builtsite # noqa: E402 + + +class FreshBuildTest(unittest.TestCase): + def test_a_page_left_by_an_earlier_build_is_not_there_when_the_next_build_starts(self): + with tempfile.TemporaryDirectory() as tmp: + out = os.path.join(tmp, "site") + os.makedirs(os.path.join(out, "regions", "gone")) + for rel in (os.path.join("regions", "gone", "index.html"), "old.json"): + with open(os.path.join(out, rel), "w") as f: + f.write("left over") + seen = [] + + def fake_build(cmd, **kwargs): + seen.append(sorted(os.listdir(out)) if os.path.isdir(out) else None) + os.makedirs(out, exist_ok=True) + return subprocess.CompletedProcess(cmd, 0, "", "") + + with mock.patch.object(builtsite.subprocess, "run", fake_build): + builtsite.build(out) + self.assertIn(seen[0], (None, [])) + self.assertEqual(os.listdir(out), []) + + def test_the_shared_site_is_in_a_directory_named_for_this_process(self): + if not builtsite.PREBUILT: + self.assertTrue(builtsite.OUT.endswith(f"test-site-{os.getpid()}")) + + +if __name__ == "__main__": + unittest.main()