-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
312 lines (267 loc) · 8.56 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
from collections import deque
import unittest
import cbuild
import tempfile
import os
class CBuildTests(unittest.TestCase):
def setUp(self):
self.tmpdir = tempfile.TemporaryDirectory()
self.runs = deque()
old_run = cbuild.run
def new_run(cmd):
self._record_runs(cmd)
old_run(cmd)
cbuild.run = new_run
def tearDown(self):
self.tmpdir.cleanup()
def _setup_files(self, files):
for file, val in files.items():
file = os.path.join(self.tmpdir.name, file)
dir = os.path.dirname(file)
os.makedirs(dir, exist_ok=True)
if isinstance(val, str):
with open(file, "w") as f:
f.write(val)
elif isinstance(val, tuple):
typ, val = val
if typ == "symlink":
target = os.path.join(self.tmpdir.name, val)
os.symlink(target, file)
else:
raise ValueError(f"Unknown type: {typ}")
else:
raise ValueError(f"Unknown entry: ({file}, {val})")
def _assert_file_exists(self, *path):
path = os.path.join(self.tmpdir.name, *path)
self.assertTrue(os.path.exists(path), f"Path doesn't exist: {path}")
self.assertTrue(os.path.isfile(path))
def _assert_dir_exists(self, *path):
path = os.path.join(self.tmpdir.name, *path)
self.assertTrue(os.path.exists(path))
self.assertTrue(os.path.isdir(path))
def _record_runs(self, cmd):
self.runs.append(cmd)
def _assert_ran(self, cmd_components, msg=None):
try:
top = self.runs.popleft()
except IndexError:
if msg:
msg = "Reason: " + msg
self.fail(msg)
top_exec = top.split()[0]
self.assertEqual(
top_exec,
cmd_components[0],
"executable is not the same. " + (msg or ""),
)
for comp in cmd_components[1:]:
self.assertIn(
comp,
top,
f"'{comp}' is missing in {top}. " + (msg or ""),
)
def _assert_nothing_ran(self):
self.assertEqual(
len(self.runs),
0,
"Expected runs to be empty. Got [{}] instead".format(", ".join(self.runs)),
)
def _touch(self, file):
file = os.path.join(self.tmpdir.name, file)
os.utime(file)
def test_gcc_works(self):
self._setup_files(
{
"src/bar/baz.c": """
int main() {
return 0;
}
"""
}
)
config = cbuild.Config(
project_root=self.tmpdir.name,
cc="gcc",
build_dir="build",
binary="main",
)
cbuild.build(config)
self._assert_file_exists("build", "src", "bar", "baz.o")
self._assert_file_exists("build", "main")
def test_include_dirs(self):
self._setup_files(
{
"src/foo.c": """
#include <liba.h>
#include <libb/lib.h>
#include "libc/lib.h"
#include "bar.h"
#include "../baz.h"
int main() {}
""",
"src/bar.h": "",
"includes/liba.h": "",
"baz.h": "",
"vendor/libb/lib.h": "",
"includes/libb": ("symlink", "vendor/libb"),
"vendor/libc/lib.h": "",
"src/libc": ("symlink", "vendor/libc"),
}
)
config = cbuild.Config(
project_root=self.tmpdir.name,
cc="gcc",
build_dir="build",
binary="main",
cflags="-Iincludes",
)
cbuild.build(config)
self._assert_ran(["gcc", "src/foo.c"])
self._assert_ran(["gcc", "build/main"])
cbuild.build(config)
self._assert_nothing_ran()
for file in [
"src/bar.h",
"includes/liba.h",
"baz.h",
"vendor/libb/lib.h",
"vendor/libc/lib.h",
]:
self._touch(file)
cbuild.build(config)
self._assert_ran(["gcc", "src/foo.c"], file)
self._assert_ran(["gcc", "build/main"], file)
def test_unused_include(self):
self._setup_files(
{
"src/foo.c": """
#include "bar.h"
int main() {}
""",
"src/bar.h": "",
"src/unused.h": "",
}
)
config = cbuild.Config(
project_root=self.tmpdir.name,
cc="gcc",
build_dir="build",
binary="main",
)
cbuild.build(config)
self._assert_ran(["gcc", "src/foo.c"])
self._assert_ran(["gcc", "build/main"])
cbuild.build(config)
self._assert_nothing_ran()
self._touch("src/bar.h")
cbuild.build(config)
self._assert_ran(["gcc", "src/foo.c"])
self._assert_ran(["gcc", "build/main"])
self._touch("src/unused.h")
cbuild.build(config)
self._assert_nothing_ran()
def test_transitive_deps(self):
self._setup_files(
{
"src/foo.c": """
#include "bar.h"
int main() {}
""",
"src/bar.h": """
#include "baz.h"
""",
"src/baz.h": "",
}
)
config = cbuild.Config(
project_root=self.tmpdir.name,
cc="gcc",
build_dir="build",
binary="main",
)
cbuild.build(config)
self._assert_ran(["gcc", "src/foo.c"])
self._assert_ran(["gcc", "build/main"])
cbuild.build(config)
self._assert_nothing_ran()
self._touch("src/baz.h")
cbuild.build(config)
self._assert_ran(["gcc", "src/foo.c"])
self._assert_ran(["gcc", "build/main"])
def test_multiple_c_files(self):
self._setup_files(
{
"src/foo.c": """
int main() {}
""",
"src/bar.c": """
int foo() {}
""",
}
)
config = cbuild.Config(
project_root=self.tmpdir.name,
cc="gcc",
build_dir="build",
binary="main",
)
cbuild.build(config)
self._assert_ran(["gcc"])
self._assert_ran(["gcc"])
self._assert_ran(["gcc", "build/src/foo.o", "build/src/bar.o", "build/main"])
cbuild.build(config)
self._assert_nothing_ran()
self._touch("src/foo.c")
cbuild.build(config)
self._assert_ran(["gcc", "src/foo.c"])
self._assert_ran(["gcc", "build/src/foo.o", "build/src/bar.o", "build/main"])
self._touch("src/bar.c")
cbuild.build(config)
self._assert_ran(["gcc", "src/bar.c"])
self._assert_ran(["gcc", "build/src/foo.o", "build/src/bar.o", "build/main"])
def test_quoted_include_not_in_same_dir(self):
self._setup_files(
{
"src/foo.c": """
#include "lib.h"
int main() {}
""",
"includes/lib.h": "",
}
)
config = cbuild.Config(
project_root=self.tmpdir.name,
cc="gcc",
cflags="-Iincludes",
build_dir="build",
binary="main",
)
cbuild.build(config)
self._assert_ran(["gcc", "src/foo.c"])
self._assert_ran(["gcc", "build/src/foo.o", "build/main"])
cbuild.build(config)
self._assert_nothing_ran()
self._touch("includes/lib.h")
cbuild.build(config)
self._assert_ran(["gcc", "src/foo.c"])
self._assert_ran(["gcc", "build/src/foo.o", "build/main"])
def test_ignore_dirs_work(self):
self._setup_files(
{
"ignored/foo.c": """
int main() {}
""",
"src/lib.h": "",
}
)
config = cbuild.Config(
project_root=self.tmpdir.name,
cc="gcc",
ignore_dirs=["ignored"],
build_dir="build",
binary="main",
)
cbuild.build(config)
self._assert_nothing_ran()
if __name__ == "__main__":
unittest.main()