mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 20:08:37 +00:00
tests: cover tools/stale_build.py (TEST-STALE-BUILD)
stale_build.py is imported by make_game.py and first_light.py but no test covered it. The new test builds small source trees in a temporary folder with explicit mtimes: include following, missing headers, include cycles, duplicate includes, empty sources, missing exe and the equal-mtime case.
This commit is contained in:
143
tests/test_stale_build.py
Normal file
143
tests/test_stale_build.py
Normal file
@ -0,0 +1,143 @@
|
||||
"""tools/stale_build.py: which sources of a runtime/test program are newer than its built copy.
|
||||
|
||||
python3 -I tests/test_stale_build.py
|
||||
|
||||
Standard library only. Every case builds a small source tree in a temporary folder and sets the modification times
|
||||
explicitly, so the results do not depend on how fast the files were written. No game data.
|
||||
"""
|
||||
import os
|
||||
import pathlib
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = pathlib.Path(__file__).resolve().parent.parent
|
||||
TOOLS = ROOT / "tools"
|
||||
sys.path.insert(0, str(TOOLS))
|
||||
import stale_build # noqa: E402
|
||||
|
||||
BASE = 1_700_000_000
|
||||
OLD = BASE + 100 # the built copy
|
||||
NEW = BASE + 1000 # the sources
|
||||
|
||||
|
||||
def write(root, rel, text=""):
|
||||
path = os.path.join(root, *rel.split("/"))
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
with open(path, "w", encoding="utf-8", newline="\n") as f:
|
||||
f.write(text)
|
||||
return path
|
||||
|
||||
|
||||
def set_mtime(path, when):
|
||||
os.utime(path, (when, when))
|
||||
|
||||
|
||||
def rel(root, paths):
|
||||
return sorted(os.path.relpath(p, root).replace(os.sep, "/") for p in paths)
|
||||
|
||||
|
||||
class StaleBuildTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.tmp.cleanup)
|
||||
self.root = self.tmp.name
|
||||
# prog.cpp: a header beside it, a header in runtime/src through "../src", and one that does not exist.
|
||||
self.cpp = write(self.root, "runtime/test/prog.cpp",
|
||||
'#include "helper.h"\n#include "../src/x.h"\n#include "missing.h"\nint main() {}\n')
|
||||
self.helper = write(self.root, "runtime/test/helper.h", "#pragma once\n")
|
||||
self.x = write(self.root, "runtime/src/x.h", "#pragma once\n")
|
||||
self.exe = os.path.join(self.root, "build", "prog")
|
||||
os.makedirs(os.path.dirname(self.exe))
|
||||
write(self.root, "build/prog", "binary")
|
||||
for path in (self.cpp, self.helper, self.x):
|
||||
set_mtime(path, NEW)
|
||||
|
||||
def build(self, when):
|
||||
set_mtime(self.exe, when)
|
||||
|
||||
def test_program_sources_follows_includes_and_skips_missing(self):
|
||||
found = stale_build.program_sources(self.root, "prog")
|
||||
self.assertEqual(rel(self.root, found),
|
||||
["runtime/src/x.h", "runtime/test/helper.h", "runtime/test/prog.cpp"])
|
||||
|
||||
def test_stale_exe_lists_the_newer_sources(self):
|
||||
self.build(OLD)
|
||||
self.assertEqual(sorted(stale_build.newer_sources(self.exe, self.root, "prog")),
|
||||
["runtime/src/x.h", "runtime/test/helper.h", "runtime/test/prog.cpp"])
|
||||
|
||||
def test_only_the_touched_header_is_reported(self):
|
||||
self.build(BASE + 500)
|
||||
set_mtime(self.helper, BASE + 600)
|
||||
set_mtime(self.cpp, BASE + 400)
|
||||
set_mtime(self.x, BASE + 400)
|
||||
self.assertEqual(stale_build.newer_sources(self.exe, self.root, "prog"), ["runtime/test/helper.h"])
|
||||
|
||||
def test_fresh_exe_lists_nothing(self):
|
||||
self.build(BASE + 2000)
|
||||
self.assertEqual(stale_build.newer_sources(self.exe, self.root, "prog"), [])
|
||||
|
||||
def test_missing_exe_lists_nothing(self):
|
||||
self.assertEqual(stale_build.newer_sources(os.path.join(self.root, "build", "absent"),
|
||||
self.root, "prog"), [])
|
||||
|
||||
def test_missing_exe_path_outside_any_tree(self):
|
||||
self.assertEqual(stale_build.newer_sources("/nonexistent/exe", self.root, "prog"), [])
|
||||
|
||||
def test_program_without_source_lists_nothing(self):
|
||||
self.build(OLD)
|
||||
self.assertEqual(stale_build.program_sources(self.root, "no_such_program"), [])
|
||||
self.assertEqual(stale_build.newer_sources(self.exe, self.root, "no_such_program"), [])
|
||||
|
||||
def test_no_runtime_folder_at_all(self):
|
||||
empty = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(empty.cleanup)
|
||||
self.assertEqual(stale_build.program_sources(empty.name, "prog"), [])
|
||||
|
||||
def test_empty_source_file_is_a_source(self):
|
||||
empty = write(self.root, "runtime/test/empty.cpp", "")
|
||||
set_mtime(empty, NEW)
|
||||
self.build(OLD)
|
||||
self.assertEqual(rel(self.root, stale_build.program_sources(self.root, "empty")),
|
||||
["runtime/test/empty.cpp"])
|
||||
self.assertEqual(stale_build.newer_sources(self.exe, self.root, "empty"), ["runtime/test/empty.cpp"])
|
||||
|
||||
def test_include_cycle_terminates_and_lists_each_file_once(self):
|
||||
write(self.root, "runtime/test/cyc.cpp", '#include "a.h"\n')
|
||||
write(self.root, "runtime/test/a.h", '#include "b.h"\n')
|
||||
write(self.root, "runtime/test/b.h", '#include "a.h"\n')
|
||||
found = stale_build.program_sources(self.root, "cyc")
|
||||
self.assertEqual(rel(self.root, found), ["runtime/test/a.h", "runtime/test/b.h", "runtime/test/cyc.cpp"])
|
||||
|
||||
def test_header_included_twice_is_listed_once(self):
|
||||
write(self.root, "runtime/test/dup.cpp",
|
||||
'#include "helper.h"\n#include "helper.h"\n#include "../test/helper.h"\n')
|
||||
found = stale_build.program_sources(self.root, "dup")
|
||||
self.assertEqual(rel(self.root, found), ["runtime/test/dup.cpp", "runtime/test/helper.h"])
|
||||
|
||||
def test_directory_named_like_an_include_is_skipped(self):
|
||||
os.makedirs(os.path.join(self.root, "runtime", "test", "isdir.h"))
|
||||
write(self.root, "runtime/test/dirinc.cpp", '#include "isdir.h"\n')
|
||||
found = stale_build.program_sources(self.root, "dirinc")
|
||||
self.assertEqual(rel(self.root, found), ["runtime/test/dirinc.cpp"])
|
||||
|
||||
def test_exe_mtime_equal_to_source_is_not_stale(self):
|
||||
# newer_sources compares with ">", so a source whose mtime equals the exe's is not reported. This pins that
|
||||
# behaviour: a same-second build of an edited source is taken as current.
|
||||
self.build(NEW)
|
||||
self.assertEqual(stale_build.newer_sources(self.exe, self.root, "prog"), [])
|
||||
|
||||
def test_exe_one_second_older_than_one_source_is_stale(self):
|
||||
self.build(NEW - 1)
|
||||
self.assertEqual(sorted(stale_build.newer_sources(self.exe, self.root, "prog")),
|
||||
["runtime/src/x.h", "runtime/test/helper.h", "runtime/test/prog.cpp"])
|
||||
|
||||
def test_commented_include_is_not_followed(self):
|
||||
write(self.root, "runtime/test/cmt.cpp", '// #include "hidden.h"\n#include "helper.h"\n')
|
||||
write(self.root, "runtime/test/hidden.h", "")
|
||||
found = stale_build.program_sources(self.root, "cmt")
|
||||
self.assertEqual(rel(self.root, found), ["runtime/test/cmt.cpp", "runtime/test/helper.h"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user