blob: 0baf7e306c63b860eb5593e4ca14dc6c4847804c [file] [log] [blame]
Diana Picus3b2ef822016-10-13 16:53:18 +03001import os
2import unittest
3import uuid
4
5from sh import git
Diana Picusefc7bda2017-06-09 19:14:08 +02006from unittest.mock import MagicMock, call
Diana Picus3b2ef822016-10-13 16:53:18 +03007
8from linaropy.cd import cd
9from linaropy.proj import Proj
10from linaropy.git.clone import Clone
11from linaropy.git.worktree import Worktree
12
13from modules.llvm import LLVMSourceConfig, LLVMSubproject
14
15
16class TestLLVMSourceConfig(unittest.TestCase):
17 testdirprefix = "SourceConfigUT"
18
19 def __create_dummy_commit(self):
20 filename = "file" + str(uuid.uuid4())
21 open(filename, "a").close()
22 git("add", filename)
23 git("commit", "-m", "Branches without commits confuse git")
24
25 def __create_dummy_repo(self, path):
26 if not os.path.exists(path):
27 os.makedirs(path)
28
29 with cd(path):
30 git("init")
31 self.__create_dummy_commit()
32
33 def __get_subproj_repo_path(self, subproj):
34 return os.path.join(self.originalLLVM.repodir, "..", subproj + "-repo")
35
36 def __get_subproj_repo(self, subproj):
37 return Clone(self.proj, self.__get_subproj_repo_path(subproj))
38
39 def setUp(self):
40 # We're going to create a hierarchy with [llvm|clang|whatever]-repo
41 # containing dummy repos, and llvm-copy containing a worktree of
42 # llvm-repo
43 self.proj = Proj(prefix=TestLLVMSourceConfig.testdirprefix)
44 path = os.path.join(self.proj.projdir, "llvm-repo")
45 self.__create_dummy_repo(path)
46 self.originalLLVM = Clone(self.proj, path)
47
48 subprojs = LLVMSubproject.get_all_subprojects()
Diana Picusb4307602017-04-05 19:48:39 +020049 for subproj in list(subprojs.keys()):
Diana Picus3b2ef822016-10-13 16:53:18 +030050 repo = self.__get_subproj_repo_path(subproj)
51 self.__create_dummy_repo(repo)
52
53 self.temporaryLLVMbranch = "a-branch"
54 self.temporaryLLVM = Worktree.create(
55 self.proj, self.originalLLVM, os.path.join(
56 self.proj.projdir, "llvm-copy"), self.temporaryLLVMbranch)
57
58 def tearDown(self):
59 self.proj.cleanup()
60
Diana Picus052b7d32017-11-24 16:19:41 +010061 def test_get_path(self):
62 sourcePath = self.temporaryLLVM.repodir
63 config = LLVMSourceConfig(self.proj, sourcePath)
64 self.assertEqual(config.get_path(), sourcePath)
65
Diana Picus3b2ef822016-10-13 16:53:18 +030066 def test_detect_enabled_all(self):
67 subprojs = LLVMSubproject.get_all_subprojects()
68 sourcePath = self.temporaryLLVM.repodir
69
70 for subproj in subprojs:
71 path = subprojs[subproj].get_cmake_path(sourcePath)
72 worktree = Worktree.create(
73 self.proj,
74 self.__get_subproj_repo(subproj),
75 path,
76 self.temporaryLLVMbranch)
77 self.assertTrue(os.path.isdir(path), "Failed to create worktree")
78
79 config = LLVMSourceConfig(self.proj, sourcePath)
80 enabled = config.get_enabled_subprojects()
81
82 self.assertEqual(set(subprojs), set(enabled),
83 "Expected %s but detected only %s" %
84 (str(set(subprojs)), str(enabled)))
85
86 def test_detect_enabled_none(self):
87 sourcePath = self.temporaryLLVM.repodir
88
89 path = os.path.join(sourcePath, "unrelated")
90 os.makedirs(path)
91
92 path = os.path.join(sourcePath, "wrong", "place", "for", "lld")
93 os.makedirs(path)
94
95 path = os.path.join(sourcePath, "projects", "clang")
96 os.makedirs(path)
97
98 path = os.path.join(sourcePath, "tools", "libcxx")
99 os.makedirs(path)
100
101 config = LLVMSourceConfig(self.proj, sourcePath)
102 enabled = config.get_enabled_subprojects()
103
104 self.assertEqual(
105 enabled,
106 [],
107 "Detected unexpected projects %s" % str(enabled)
108 )
109
110 def test_detect_enabled_some(self):
111 sourcePath = self.temporaryLLVM.repodir
112 subprojs = LLVMSubproject.get_all_subprojects()
113
114 for subproj in ["lld", "libcxxabi", "clang"]:
115 path = subprojs[subproj].get_cmake_path(sourcePath)
116 worktree = Worktree.create(
117 self.proj,
118 self.__get_subproj_repo(subproj),
119 path,
120 self.temporaryLLVMbranch)
121 self.assertTrue(os.path.isdir(path), "Failed to create worktree")
122
123 config = LLVMSourceConfig(self.proj, sourcePath)
124 enabled = config.get_enabled_subprojects()
125
126 self.assertTrue("lld" in enabled, "Failed to detect lld")
127 self.assertTrue("clang" in enabled, "Failed to detect clang")
128 self.assertTrue("libcxxabi" in enabled,
129 "Failed to detect libcxxabi")
130
131 def test_detect_enabled_not_worktree(self):
132 sourcePath = self.temporaryLLVM.repodir
133 subprojs = LLVMSubproject.get_all_subprojects()
134
135 path = subprojs["compiler-rt"].get_cmake_path(sourcePath)
136 os.makedirs(path)
137
138 config = LLVMSourceConfig(self.proj, sourcePath)
Diana Picusefc7bda2017-06-09 19:14:08 +0200139 with self.assertRaises(EnvironmentError) as context:
140 config.get_enabled_subprojects()
Diana Picus3b2ef822016-10-13 16:53:18 +0300141
Diana Picusefc7bda2017-06-09 19:14:08 +0200142 self.assertRegex(
143 str(context.exception),
144 ".*compiler-rt is not a worktree.*"
Diana Picus3b2ef822016-10-13 16:53:18 +0300145 )
146
147 def test_detect_enabled_wrong_branch(self):
148 sourcePath = self.temporaryLLVM.repodir
149 subprojs = LLVMSubproject.get_all_subprojects()
150
151 path = subprojs["compiler-rt"].get_cmake_path(sourcePath)
152 branch = "different-than-" + self.temporaryLLVMbranch
153 worktree = Worktree.create(
154 self.proj,
155 self.__get_subproj_repo("compiler-rt"),
156 path,
157 branch)
158 self.assertTrue(os.path.isdir(path), "Failed to create worktree")
159
160 config = LLVMSourceConfig(self.proj, sourcePath)
Diana Picusefc7bda2017-06-09 19:14:08 +0200161 with self.assertRaises(EnvironmentError) as context:
162 config.get_enabled_subprojects()
Diana Picus3b2ef822016-10-13 16:53:18 +0300163
Diana Picusefc7bda2017-06-09 19:14:08 +0200164 self.assertRegex(str(context.exception),
165 "compiler-rt is on branch {}, but should be on {}".format(branch,
166 self.temporaryLLVMbranch))
Diana Picus3b2ef822016-10-13 16:53:18 +0300167
168 def test_add_invalid_subproject(self):
169 config = LLVMSourceConfig(self.proj, self.temporaryLLVM.repodir)
170 subproj = "not-an-llvm-subproject"
171 subprojPath = self.originalLLVM.repodir # Dummy path
172
173 with self.assertRaises(ValueError) as context:
174 config.update({subproj : Clone(self.proj, subprojPath)})
175
Diana Picusb4307602017-04-05 19:48:39 +0200176 self.assertRegex(str(context.exception),
177 "Unknown llvm subproject %s" % subproj)
Diana Picus3b2ef822016-10-13 16:53:18 +0300178
179 def test_add_each_subproject(self):
180 sourcePath = self.temporaryLLVM.repodir
181
182 config = LLVMSourceConfig(self.proj, sourcePath)
183
184 subprojs = LLVMSubproject.get_all_subprojects()
185 for subproj in subprojs:
186 expectedPath = subprojs[subproj].get_cmake_path(sourcePath)
187 config.update({subproj : self.__get_subproj_repo(subproj)})
188 self.assertTrue(os.path.isdir(expectedPath),
189 "Failed to add subproject %s" % subproj)
190
191 def test_add_all_subprojects(self):
192 sourcePath = self.temporaryLLVM.repodir
193
194 config = LLVMSourceConfig(self.proj, sourcePath)
195
196 to_add = {}
197 subprojs = LLVMSubproject.get_all_subprojects()
198
199 for subproj in subprojs:
200 to_add[subproj] = self.__get_subproj_repo(subproj)
201
202 config.update(to_add)
203
204 for subproj in subprojs:
205 expectedPath = subprojs[subproj].get_cmake_path(sourcePath)
206 self.assertTrue(os.path.isdir(expectedPath),
207 "Failed to add subproject %s" % subproj)
208
209 def test_add_some_subprojects(self):
210 sourcePath = self.temporaryLLVM.repodir
211
212 config = LLVMSourceConfig(self.proj, sourcePath)
213
214 to_add = {}
215 to_add["clang"] = self.__get_subproj_repo("clang")
216 to_add["compiler-rt"] = self.__get_subproj_repo("compiler-rt")
217
218 config.update(to_add)
219
220 subprojs = LLVMSubproject.get_all_subprojects()
221 self.assertTrue(
222 os.path.isdir(subprojs["clang"].get_cmake_path(sourcePath)),
223 "Failed to add subproject clang")
224 self.assertTrue(
225 os.path.isdir(subprojs["compiler-rt"].get_cmake_path(sourcePath)),
226 "Failed to add subproject compiler-rt")
227
228 def test_add_existing_subprojects(self):
229 sourcePath = self.temporaryLLVM.repodir
230
231 config = LLVMSourceConfig(self.proj, sourcePath)
232
233 subprojs = LLVMSubproject.get_all_subprojects()
234 existingPath = subprojs["lldb"].get_cmake_path(sourcePath)
235 Worktree.create(
236 self.proj,
237 self.__get_subproj_repo("lldb"),
238 existingPath,
239 self.temporaryLLVMbranch)
240
241 config.update({ "lldb" : self.__get_subproj_repo("lldb")})
242
243 # If we got this far, we're probably ok, but let's be pedantic and check
244 # that the subproject is still there
245 self.assertTrue(os.path.isdir(existingPath),
246 "Existing subproject vanished")
247
248 def test_add_subproject_existing_branch(self):
249 """
250 Test that we can add a subproject that already has the branch that LLVM
251 is on. This can happen for instance if we have added and then removed
252 the subproject and now we're trying to add it again.
253 """
254 sourcePath = self.temporaryLLVM.repodir
255
256 config = LLVMSourceConfig(self.proj, sourcePath)
257
258 clangRepo = self.__get_subproj_repo("clang")
259 with cd(clangRepo.repodir):
260 # Make sure that the branch that LLVM is on already exists in the
261 # clang repo as well.
262 git("checkout", "-b", self.temporaryLLVMbranch)
263 self.__create_dummy_commit()
264 git("checkout", "master")
265
266 config.update( { "clang" : clangRepo })
267
268 subprojs = LLVMSubproject.get_all_subprojects()
269 path = subprojs["clang"].get_cmake_path(sourcePath)
270 self.assertTrue(os.path.isdir(path), "Failed to add subproject")
271
272 def test_add_subproject_not_a_worktree(self):
273 """
274 Test that we can't update a config to include a subproject that exists
275 but is not a worktree.
276 """
277 sourcePath = self.temporaryLLVM.repodir
278 branch = "different-than-" + self.temporaryLLVMbranch
279
280 config = LLVMSourceConfig(self.proj, sourcePath)
281
282 subprojs = LLVMSubproject.get_all_subprojects()
283 existingPath = subprojs["lldb"].get_cmake_path(sourcePath)
284 os.makedirs(existingPath)
285
286 with self.assertRaises(EnvironmentError) as context:
287 config.update({ "lldb" : self.__get_subproj_repo("lldb")})
288
Diana Picusb4307602017-04-05 19:48:39 +0200289 self.assertRegex(str(context.exception),
Diana Picusefc7bda2017-06-09 19:14:08 +0200290 "{} is not a worktree.*"
Diana Picusb4307602017-04-05 19:48:39 +0200291 .format(existingPath))
Diana Picus3b2ef822016-10-13 16:53:18 +0300292
293 # If we got this far, we're probably ok, but let's be pedantic and check
294 # that the subproject is still there
295 self.assertTrue(os.path.isdir(existingPath),
296 "Existing subproject vanished")
297
298 def test_add_subproject_wrong_branch(self):
299 """
300 Test that we can't update a config to include a subproject that exists
301 but is on the wrong branch.
302 """
303 sourcePath = self.temporaryLLVM.repodir
304 branch = "different-than-" + self.temporaryLLVMbranch
305
306 config = LLVMSourceConfig(self.proj, sourcePath)
307
308 subprojs = LLVMSubproject.get_all_subprojects()
309 existingPath = subprojs["lldb"].get_cmake_path(sourcePath)
310 Worktree.create(
311 self.proj,
312 self.__get_subproj_repo("lldb"),
313 existingPath,
314 branch)
315
316 with self.assertRaises(EnvironmentError) as context:
317 config.update({ "lldb" : self.__get_subproj_repo("lldb")})
318
Diana Picusb4307602017-04-05 19:48:39 +0200319 self.assertRegex(str(context.exception),
Diana Picusefc7bda2017-06-09 19:14:08 +0200320 "lldb is on branch {}, but should be on {}.*"
321 .format(branch, self.temporaryLLVMbranch))
Diana Picus3b2ef822016-10-13 16:53:18 +0300322
323 # If we got this far, we're probably ok, but let's be pedantic and check
324 # that the subproject is still there
325 self.assertTrue(os.path.isdir(existingPath),
326 "Existing subproject vanished")
327
328 def test_remove_subproject(self):
329 sourcePath = self.temporaryLLVM.repodir
330
331 subprojs = LLVMSubproject.get_all_subprojects()
332
333 lldPath = subprojs["lld"].get_cmake_path(sourcePath)
334 lldWorktree = Worktree.create(
335 self.proj,
336 self.__get_subproj_repo("lld"),
337 lldPath,
338 self.temporaryLLVMbranch)
339
340 clangPath = subprojs["clang"].get_cmake_path(sourcePath)
341 clangWorktree = Worktree.create(
342 self.proj,
343 self.__get_subproj_repo("clang"),
344 clangPath,
345 self.temporaryLLVMbranch)
346
347 config = LLVMSourceConfig(self.proj, sourcePath)
348 config.update(remove=["lld"])
349
350 self.assertFalse(os.path.isdir(lldPath), "Failed to remove subproject")
351 self.assertTrue(os.path.isdir(clangPath), "Removed sibling subproject")
352
353 def test_remove_some_subprojects(self):
354 sourcePath = self.temporaryLLVM.repodir
355
356 subprojs = LLVMSubproject.get_all_subprojects()
357
358 for subproj in ["clang", "compiler-rt", "lld", "lldb", "libunwind"]:
359 path = subprojs[subproj].get_cmake_path(sourcePath)
360 worktree = Worktree.create(
361 self.proj,
362 self.__get_subproj_repo(subproj),
363 path,
364 self.temporaryLLVMbranch)
365
366 config = LLVMSourceConfig(self.proj, sourcePath)
367 config.update(remove=["compiler-rt", "lld"])
368
369 for subproj in ["compiler-rt", "lld"]:
370 path = subprojs[subproj].get_cmake_path(sourcePath)
371 self.assertFalse(
372 os.path.isdir(path),
373 "Failed to remove subproject")
374
375 for subproj in ["clang", "lldb", "libunwind"]:
376 path = subprojs[subproj].get_cmake_path(sourcePath)
377 self.assertTrue(os.path.isdir(path), "Removed sibling subproject")
378
379 def test_remove_all_subprojects(self):
380 sourcePath = self.temporaryLLVM.repodir
381
382 subprojs = LLVMSubproject.get_all_subprojects()
383
Diana Picusb4307602017-04-05 19:48:39 +0200384 for subproj in list(subprojs.keys()):
Diana Picus3b2ef822016-10-13 16:53:18 +0300385 path = subprojs[subproj].get_cmake_path(sourcePath)
386 worktree = Worktree.create(
387 self.proj,
388 self.__get_subproj_repo(subproj),
389 path,
390 self.temporaryLLVMbranch)
391 self.assertTrue(os.path.isdir(path), "Failed to create worktree")
392
393 config = LLVMSourceConfig(self.proj, sourcePath)
Diana Picusb4307602017-04-05 19:48:39 +0200394 config.update(remove=list(subprojs.keys()))
Diana Picus3b2ef822016-10-13 16:53:18 +0300395
Diana Picusb4307602017-04-05 19:48:39 +0200396 for subproj in list(subprojs.keys()):
Diana Picus3b2ef822016-10-13 16:53:18 +0300397 path = subprojs[subproj].get_cmake_path(sourcePath)
398 self.assertFalse(
399 os.path.isdir(path),
400 "Failed to remove subproject")
401
402 def test_remove_each_subproject(self):
403 sourcePath = self.temporaryLLVM.repodir
404
405 subprojs = LLVMSubproject.get_all_subprojects()
406
Diana Picusb4307602017-04-05 19:48:39 +0200407 for subproj in list(subprojs.keys()):
Diana Picus3b2ef822016-10-13 16:53:18 +0300408 path = subprojs[subproj].get_cmake_path(sourcePath)
409 worktree = Worktree.create(
410 self.proj,
411 self.__get_subproj_repo(subproj),
412 path,
413 self.temporaryLLVMbranch)
414 self.assertTrue(os.path.isdir(path), "Failed to create worktree")
415
416 config = LLVMSourceConfig(self.proj, sourcePath)
417
Diana Picusb4307602017-04-05 19:48:39 +0200418 for subproj in list(subprojs.keys()):
Diana Picus3b2ef822016-10-13 16:53:18 +0300419 config.update(remove=[subproj])
420
Diana Picusb4307602017-04-05 19:48:39 +0200421 for subproj in list(subprojs.keys()):
Diana Picus3b2ef822016-10-13 16:53:18 +0300422 path = subprojs[subproj].get_cmake_path(sourcePath)
423 self.assertFalse(
424 os.path.isdir(path),
425 "Failed to remove subproject")
426
427 def test_remove_duplicates(self):
428 sourcePath = self.temporaryLLVM.repodir
429
430 subprojs = LLVMSubproject.get_all_subprojects()
431
432 for subproj in ["clang", "compiler-rt", "lld", "lldb", "libunwind"]:
433 path = subprojs[subproj].get_cmake_path(sourcePath)
434 worktree = Worktree.create(
435 self.proj,
436 self.__get_subproj_repo(subproj),
437 path,
438 self.temporaryLLVMbranch)
439
440 config = LLVMSourceConfig(self.proj, sourcePath)
441 config.update(remove=["compiler-rt", "lld", "lld", "compiler-rt"])
442
443 for subproj in ["compiler-rt", "lld"]:
444 path = subprojs[subproj].get_cmake_path(sourcePath)
445 self.assertFalse(
446 os.path.isdir(path),
447 "Failed to remove subproject")
448
449 for subproj in ["clang", "lldb", "libunwind"]:
450 path = subprojs[subproj].get_cmake_path(sourcePath)
451 self.assertTrue(os.path.isdir(path), "Removed sibling subproject")
452
453 def test_remove_invalid_subproject(self):
454 sourcePath = self.temporaryLLVM.repodir
455 config = LLVMSourceConfig(self.proj, sourcePath)
456
457 subproj = "not-an-llvm-subproject"
458
459 with self.assertRaises(ValueError) as context:
460 config.update(remove=[subproj])
461
Diana Picusb4307602017-04-05 19:48:39 +0200462 self.assertRegex(str(context.exception),
463 "Unknown llvm subproject %s" % subproj)
Diana Picus3b2ef822016-10-13 16:53:18 +0300464
465 def test_remove_inexistent_subproject(self):
466 sourcePath = self.temporaryLLVM.repodir
467
468 subprojs = LLVMSubproject.get_all_subprojects()
469
470 lldPath = subprojs["lld"].get_cmake_path(sourcePath)
471 lldWorktree = Worktree.create(
472 self.proj,
473 self.__get_subproj_repo("lld"),
474 lldPath,
475 self.temporaryLLVMbranch)
476
477 clangPath = subprojs["clang"].get_cmake_path(sourcePath)
478
479 config = LLVMSourceConfig(self.proj, sourcePath)
480 config.update(remove=["clang"])
481
482 self.assertFalse(
483 os.path.isdir(clangPath),
484 "Failed to remove subproject")
485 self.assertTrue(os.path.isdir(lldPath), "Removed sibling subproject")
486
487 def test_add_after_remove(self):
488 sourcePath = self.temporaryLLVM.repodir
489
490 subprojs = LLVMSubproject.get_all_subprojects()
491 lldbRepo = self.__get_subproj_repo("lldb")
492
493 config = LLVMSourceConfig(self.proj, sourcePath)
494
495 config.update({"lldb" : lldbRepo})
496 self.assertTrue(
497 os.path.isdir(subprojs["lldb"].get_cmake_path(sourcePath)),
498 "Failed to add lldb")
499
500 config.update(remove=["lldb"])
501 self.assertFalse(
502 os.path.isdir(subprojs["lldb"].get_cmake_path(sourcePath)),
503 "Failed to remove lldb")
504
505 config.update({"lldb" : lldbRepo })
506 self.assertTrue(
507 os.path.isdir(subprojs["lldb"].get_cmake_path(sourcePath)),
508 "Failed to add lldb")
509
510 def test_mixed_adds_removes(self):
511 sourcePath = self.temporaryLLVM.repodir
512
513 subprojs = LLVMSubproject.get_all_subprojects()
514
515 for subproj in ["clang", "compiler-rt", "lld", "lldb", "libunwind"]:
516 path = subprojs[subproj].get_cmake_path(sourcePath)
517 worktree = Worktree.create(
518 self.proj,
519 self.__get_subproj_repo(subproj),
520 path,
521 self.temporaryLLVMbranch)
522
523 config = LLVMSourceConfig(self.proj, sourcePath)
524 config.update({
525 "libcxx" : self.__get_subproj_repo("libcxx"),
526 "libcxxabi" : self.__get_subproj_repo("libcxxabi")},
527 ["compiler-rt", "lld"])
528
529 for subproj in ["libcxx", "libcxxabi"]:
530 path = subprojs[subproj].get_cmake_path(sourcePath)
531 self.assertTrue(os.path.isdir(path), "Failed to add subproject")
532
533 for subproj in ["compiler-rt", "lld"]:
534 path = subprojs[subproj].get_cmake_path(sourcePath)
535 self.assertFalse(
536 os.path.isdir(path),
537 "Failed to remove subproject")
538
539 for subproj in ["clang", "lldb", "libunwind"]:
540 path = subprojs[subproj].get_cmake_path(sourcePath)
541 self.assertTrue(os.path.isdir(path), "Removed sibling subproject")
542
543 def test_simultaneous_add_remove(self):
544 sourcePath = self.temporaryLLVM.repodir
545 subprojs = LLVMSubproject.get_all_subprojects()
546
547 clangRepo = self.__get_subproj_repo("clang")
548 lldRepo = self.__get_subproj_repo("lld")
549 libunwindRepo = self.__get_subproj_repo("libunwind")
550
551 config = LLVMSourceConfig(self.proj, sourcePath)
552 with self.assertRaises(ValueError) as context:
553 config.update(
554 { "clang" : clangRepo, "lld" : lldRepo, "libunwind" :
555 libunwindRepo}, ["libcxx", "lld", "libcxxabi"])
556
557 self.assertEqual(str(context.exception),
558 "Can't add and remove lld at the same time")
559
560 # Make sure we didn't add any of the others either
561 for subproj in ["clang", "libunwind"]:
562 path = subprojs[subproj].get_cmake_path(sourcePath)
563 self.assertFalse(
564 os.path.isdir(path),
565 "Incorrectly added subproject")
566
567 # TODO: test with a different dictionary than the default one (not
568 # necessarily containing subprojects - it can contain "potato", "banana" and
569 # "gazpacho" for all we care); in fact, it would probably be best to move the
570 # existing tests to that...
571
572 # TODO: test that CMake gets our layout
573
Diana Picusefc7bda2017-06-09 19:14:08 +0200574 def test_for_each_enabled(self):
575 sourcePath = self.temporaryLLVM.repodir
576
577 config = LLVMSourceConfig(self.proj, sourcePath)
578
579 logPath = unittest.mock.MagicMock()
580 config.for_each_enabled(logPath)
581
582 logPath.assert_called_with(self.temporaryLLVM.repodir)
583
Diana Picus5fec8b72017-11-27 16:09:58 +0100584 subprojs = config.subprojs
Diana Picusefc7bda2017-06-09 19:14:08 +0200585 enabled = ["clang", "compiler-rt", "lld", "lldb", "libunwind"]
586 calls = []
587
588 for subproj in enabled:
589 path = subprojs[subproj].get_cmake_path(sourcePath)
590 worktree = Worktree.create(
591 self.proj,
592 self.__get_subproj_repo(subproj),
593 path,
594 self.temporaryLLVMbranch)
595 calls.append(call(path)) # Expect our mock to be called with path
596
597 logPath = unittest.mock.MagicMock()
598 config.for_each_enabled(logPath)
599
600 logPath.assert_has_calls(calls, any_order=True)
601
Diana Picusefc7bda2017-06-09 19:14:08 +0200602 def test_for_each_enabled_error(self):
603 """Test that we rethrow exceptions correctly."""
604 sourcePath = self.temporaryLLVM.repodir
605
606 config = LLVMSourceConfig(self.proj, sourcePath)
Diana Picus5fec8b72017-11-27 16:09:58 +0100607 subprojs = config.subprojs
Diana Picusefc7bda2017-06-09 19:14:08 +0200608 enabled = ["clang", "compiler-rt", "lld", "lldb", "libunwind"]
609
610 for subproj in enabled:
611 path = subprojs[subproj].get_cmake_path(sourcePath)
612 worktree = Worktree.create(
613 self.proj,
614 self.__get_subproj_repo(subproj),
615 path,
616 self.temporaryLLVMbranch)
617
Diana Picus5fec8b72017-11-27 16:09:58 +0100618 def throw(projPath):
619 if "lld" in projPath:
620 raise ValueError("An error has been!!1")
621
Diana Picusefc7bda2017-06-09 19:14:08 +0200622 with self.assertRaises(RuntimeError) as context:
Diana Picus5fec8b72017-11-27 16:09:58 +0100623 config.for_each_enabled(throw)
Diana Picusefc7bda2017-06-09 19:14:08 +0200624 self.assertRegex(str(context.exception),
Diana Picus5fec8b72017-11-27 16:09:58 +0100625 "Error while processing lld(.*\n)*")
Diana Picusefc7bda2017-06-09 19:14:08 +0200626 self.assertRegex(str(context.exception.__cause__),
Diana Picus5fec8b72017-11-27 16:09:58 +0100627 "An error has been!!1(.*\n)*")
628
629 def test_for_each_subproj(self):
630 sourcePath = self.temporaryLLVM.repodir
631
632 config = LLVMSourceConfig(self.proj, sourcePath)
633
634 subprojs = config.subprojs
635 enabled = ["clang", "compiler-rt", "lld", "lldb", "libunwind"]
636 calls = []
637
638 for subproj in enabled:
639 # Make sure subproj looks enabled
640 path = subprojs[subproj].get_cmake_path(sourcePath)
641 worktree = Worktree.create(
642 self.proj,
643 self.__get_subproj_repo(subproj),
644 path,
645 self.temporaryLLVMbranch)
646 # Expect our mock to be called with the proper subproj and
647 # enabled == True
648 calls.append(call(subprojs[subproj], True))
649
650 for subproj in set(subprojs.keys()) - set(enabled):
651 # Expect our mock to be called with the proper subproj and
652 # enabled == False
653 calls.append(call(subprojs[subproj], False))
654
655 try:
656 action = MagicMock()
657 config.for_each_subproj(action)
658 except:
659 print("Exception during test?! ")
660
661 action.assert_has_calls(calls, any_order=True)
662
663 def test_for_each_subproj_error(self):
664 """Test that we rethrow exceptions correctly."""
665 sourcePath = self.temporaryLLVM.repodir
666
667 config = LLVMSourceConfig(self.proj, sourcePath)
668 subprojs = config.subprojs
669 enabled = ["clang", "compiler-rt", "lld", "lldb", "libunwind"]
670
671 for subproj in enabled:
672 path = subprojs[subproj].get_cmake_path(sourcePath)
673 worktree = Worktree.create(
674 self.proj,
675 self.__get_subproj_repo(subproj),
676 path,
677 self.temporaryLLVMbranch)
678
679 def throw_enabled(subproj, enabled):
680 # Throw for one of the enabled projects (e.g. lld)
681 if "lld" in subproj.cmake_path:
682 raise ValueError("An error has been!!1")
683
684 def throw_disabled(subproj, enabled):
685 # Throw for one of the disabled projects (e.g. libcxx)
686 if "libcxx" in subproj.cmake_path:
687 raise ValueError("An error has been!!1")
688
689 with self.assertRaises(RuntimeError) as context:
690 config.for_each_subproj(throw_enabled)
691 self.assertRegex(str(context.exception),
692 "Error while processing lld(.*\n)*")
693 self.assertRegex(str(context.exception.__cause__),
694 "An error has been!!1(.*\n)*")
695
696 with self.assertRaises(RuntimeError) as context:
697 config.for_each_subproj(throw_disabled)
698 self.assertRegex(str(context.exception),
699 "Error while processing libcxx(.*\n)*")
700 self.assertRegex(str(context.exception.__cause__),
701 "An error has been!!1(.*\n)*")
702
Diana Picusefc7bda2017-06-09 19:14:08 +0200703
Diana Picus3b2ef822016-10-13 16:53:18 +0300704if __name__ == "__main__":
705 unittest.main()