upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRomain PINSOLLE <romain.pins@gmail.com>2026-11-24 10:53:37 +0100
committerRomain PINSOLLE <romain.pins@gmail.com>2026-11-24 10:53:37 +0100
commite8a4ae043066af233ffef99b344aec79baa480d7 (patch)
treeb70d9d31d29b0688a466242575dc6fa02c003109
parent21625bca8c675c62f9dbffc318ce6a59d57faf31 (diff)
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.
-rw-r--r--tests/test_stale_build.py143
1 files changed, 143 insertions, 0 deletions
diff --git a/tests/test_stale_build.py b/tests/test_stale_build.py
new file mode 100644
index 000000000..0d30b13e6
--- /dev/null
+++ b/tests/test_stale_build.py
@@ -0,0 +1,143 @@
1"""tools/stale_build.py: which sources of a runtime/test program are newer than its built copy.
2
3 python3 -I tests/test_stale_build.py
4
5Standard library only. Every case builds a small source tree in a temporary folder and sets the modification times
6explicitly, so the results do not depend on how fast the files were written. No game data.
7"""
8import os
9import pathlib
10import sys
11import tempfile
12import unittest
13
14ROOT = pathlib.Path(__file__).resolve().parent.parent
15TOOLS = ROOT / "tools"
16sys.path.insert(0, str(TOOLS))
17import stale_build # noqa: E402
18
19BASE = 1_700_000_000
20OLD = BASE + 100 # the built copy
21NEW = BASE + 1000 # the sources
22
23
24def write(root, rel, text=""):
25 path = os.path.join(root, *rel.split("/"))
26 os.makedirs(os.path.dirname(path), exist_ok=True)
27 with open(path, "w", encoding="utf-8", newline="\n") as f:
28 f.write(text)
29 return path
30
31
32def set_mtime(path, when):
33 os.utime(path, (when, when))
34
35
36def rel(root, paths):
37 return sorted(os.path.relpath(p, root).replace(os.sep, "/") for p in paths)
38
39
40class StaleBuildTests(unittest.TestCase):
41 def setUp(self):
42 self.tmp = tempfile.TemporaryDirectory()
43 self.addCleanup(self.tmp.cleanup)
44 self.root = self.tmp.name
45 # prog.cpp: a header beside it, a header in runtime/src through "../src", and one that does not exist.
46 self.cpp = write(self.root, "runtime/test/prog.cpp",
47 '#include "helper.h"\n#include "../src/x.h"\n#include "missing.h"\nint main() {}\n')
48 self.helper = write(self.root, "runtime/test/helper.h", "#pragma once\n")
49 self.x = write(self.root, "runtime/src/x.h", "#pragma once\n")
50 self.exe = os.path.join(self.root, "build", "prog")
51 os.makedirs(os.path.dirname(self.exe))
52 write(self.root, "build/prog", "binary")
53 for path in (self.cpp, self.helper, self.x):
54 set_mtime(path, NEW)
55
56 def build(self, when):
57 set_mtime(self.exe, when)
58
59 def test_program_sources_follows_includes_and_skips_missing(self):
60 found = stale_build.program_sources(self.root, "prog")
61 self.assertEqual(rel(self.root, found),
62 ["runtime/src/x.h", "runtime/test/helper.h", "runtime/test/prog.cpp"])
63
64 def test_stale_exe_lists_the_newer_sources(self):
65 self.build(OLD)
66 self.assertEqual(sorted(stale_build.newer_sources(self.exe, self.root, "prog")),
67 ["runtime/src/x.h", "runtime/test/helper.h", "runtime/test/prog.cpp"])
68
69 def test_only_the_touched_header_is_reported(self):
70 self.build(BASE + 500)
71 set_mtime(self.helper, BASE + 600)
72 set_mtime(self.cpp, BASE + 400)
73 set_mtime(self.x, BASE + 400)
74 self.assertEqual(stale_build.newer_sources(self.exe, self.root, "prog"), ["runtime/test/helper.h"])
75
76 def test_fresh_exe_lists_nothing(self):
77 self.build(BASE + 2000)
78 self.assertEqual(stale_build.newer_sources(self.exe, self.root, "prog"), [])
79
80 def test_missing_exe_lists_nothing(self):
81 self.assertEqual(stale_build.newer_sources(os.path.join(self.root, "build", "absent"),
82 self.root, "prog"), [])
83
84 def test_missing_exe_path_outside_any_tree(self):
85 self.assertEqual(stale_build.newer_sources("/nonexistent/exe", self.root, "prog"), [])
86
87 def test_program_without_source_lists_nothing(self):
88 self.build(OLD)
89 self.assertEqual(stale_build.program_sources(self.root, "no_such_program"), [])
90 self.assertEqual(stale_build.newer_sources(self.exe, self.root, "no_such_program"), [])
91
92 def test_no_runtime_folder_at_all(self):
93 empty = tempfile.TemporaryDirectory()
94 self.addCleanup(empty.cleanup)
95 self.assertEqual(stale_build.program_sources(empty.name, "prog"), [])
96
97 def test_empty_source_file_is_a_source(self):
98 empty = write(self.root, "runtime/test/empty.cpp", "")
99 set_mtime(empty, NEW)
100 self.build(OLD)
101 self.assertEqual(rel(self.root, stale_build.program_sources(self.root, "empty")),
102 ["runtime/test/empty.cpp"])
103 self.assertEqual(stale_build.newer_sources(self.exe, self.root, "empty"), ["runtime/test/empty.cpp"])
104
105 def test_include_cycle_terminates_and_lists_each_file_once(self):
106 write(self.root, "runtime/test/cyc.cpp", '#include "a.h"\n')
107 write(self.root, "runtime/test/a.h", '#include "b.h"\n')
108 write(self.root, "runtime/test/b.h", '#include "a.h"\n')
109 found = stale_build.program_sources(self.root, "cyc")
110 self.assertEqual(rel(self.root, found), ["runtime/test/a.h", "runtime/test/b.h", "runtime/test/cyc.cpp"])
111
112 def test_header_included_twice_is_listed_once(self):
113 write(self.root, "runtime/test/dup.cpp",
114 '#include "helper.h"\n#include "helper.h"\n#include "../test/helper.h"\n')
115 found = stale_build.program_sources(self.root, "dup")
116 self.assertEqual(rel(self.root, found), ["runtime/test/dup.cpp", "runtime/test/helper.h"])
117
118 def test_directory_named_like_an_include_is_skipped(self):
119 os.makedirs(os.path.join(self.root, "runtime", "test", "isdir.h"))
120 write(self.root, "runtime/test/dirinc.cpp", '#include "isdir.h"\n')
121 found = stale_build.program_sources(self.root, "dirinc")
122 self.assertEqual(rel(self.root, found), ["runtime/test/dirinc.cpp"])
123
124 def test_exe_mtime_equal_to_source_is_not_stale(self):
125 # newer_sources compares with ">", so a source whose mtime equals the exe's is not reported. This pins that
126 # behaviour: a same-second build of an edited source is taken as current.
127 self.build(NEW)
128 self.assertEqual(stale_build.newer_sources(self.exe, self.root, "prog"), [])
129
130 def test_exe_one_second_older_than_one_source_is_stale(self):
131 self.build(NEW - 1)
132 self.assertEqual(sorted(stale_build.newer_sources(self.exe, self.root, "prog")),
133 ["runtime/src/x.h", "runtime/test/helper.h", "runtime/test/prog.cpp"])
134
135 def test_commented_include_is_not_followed(self):
136 write(self.root, "runtime/test/cmt.cpp", '// #include "hidden.h"\n#include "helper.h"\n')
137 write(self.root, "runtime/test/hidden.h", "")
138 found = stale_build.program_sources(self.root, "cmt")
139 self.assertEqual(rel(self.root, found), ["runtime/test/cmt.cpp", "runtime/test/helper.h"])
140
141
142if __name__ == "__main__":
143 unittest.main()