aboutsummaryrefslogtreecommitdiff
path: root/tests/unittests/testllvmsourceconfig.py
blob: 3ad93b19f59d9f4d16bd53e2d52d23967a91d50b (plain)
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import os
import unittest
import uuid

from sh import git

from linaropy.cd import cd
from linaropy.proj import Proj
from linaropy.git.clone import Clone
from linaropy.git.worktree import Worktree

from modules.llvm import LLVMSourceConfig, LLVMSubproject


class TestLLVMSourceConfig(unittest.TestCase):
    testdirprefix = "SourceConfigUT"

    def __create_dummy_commit(self):
        filename = "file" + str(uuid.uuid4())
        open(filename, "a").close()
        git("add", filename)
        git("commit", "-m", "Branches without commits confuse git")

    def __create_dummy_repo(self, path):
        if not os.path.exists(path):
            os.makedirs(path)

        with cd(path):
            git("init")
            self.__create_dummy_commit()

    def __get_subproj_repo_path(self, subproj):
        return os.path.join(self.originalLLVM.repodir, "..", subproj + "-repo")

    def __get_subproj_repo(self, subproj):
        return Clone(self.proj, self.__get_subproj_repo_path(subproj))

    def setUp(self):
        # We're going to create a hierarchy with [llvm|clang|whatever]-repo
        # containing dummy repos, and llvm-copy containing a worktree of
        # llvm-repo
        self.proj = Proj(prefix=TestLLVMSourceConfig.testdirprefix)
        path = os.path.join(self.proj.projdir, "llvm-repo")
        self.__create_dummy_repo(path)
        self.originalLLVM = Clone(self.proj, path)

        subprojs = LLVMSubproject.get_all_subprojects()
        for subproj in list(subprojs.keys()):
            repo = self.__get_subproj_repo_path(subproj)
            self.__create_dummy_repo(repo)

        self.temporaryLLVMbranch = "a-branch"
        self.temporaryLLVM = Worktree.create(
            self.proj, self.originalLLVM, os.path.join(
                self.proj.projdir, "llvm-copy"), self.temporaryLLVMbranch)

    def tearDown(self):
        self.proj.cleanup()

    def test_detect_enabled_all(self):
        subprojs = LLVMSubproject.get_all_subprojects()
        sourcePath = self.temporaryLLVM.repodir

        for subproj in subprojs:
            path = subprojs[subproj].get_cmake_path(sourcePath)
            worktree = Worktree.create(
                self.proj,
                self.__get_subproj_repo(subproj),
                path,
                self.temporaryLLVMbranch)
            self.assertTrue(os.path.isdir(path), "Failed to create worktree")

        config = LLVMSourceConfig(self.proj, sourcePath)
        enabled = config.get_enabled_subprojects()

        self.assertEqual(set(subprojs), set(enabled),
                         "Expected %s but detected only %s" %
                         (str(set(subprojs)), str(enabled)))

    def test_detect_enabled_none(self):
        sourcePath = self.temporaryLLVM.repodir

        path = os.path.join(sourcePath, "unrelated")
        os.makedirs(path)

        path = os.path.join(sourcePath, "wrong", "place", "for", "lld")
        os.makedirs(path)

        path = os.path.join(sourcePath, "projects", "clang")
        os.makedirs(path)

        path = os.path.join(sourcePath, "tools", "libcxx")
        os.makedirs(path)

        config = LLVMSourceConfig(self.proj, sourcePath)
        enabled = config.get_enabled_subprojects()

        self.assertEqual(
            enabled,
            [],
            "Detected unexpected projects %s" % str(enabled)
        )

    def test_detect_enabled_some(self):
        sourcePath = self.temporaryLLVM.repodir
        subprojs = LLVMSubproject.get_all_subprojects()

        for subproj in ["lld", "libcxxabi", "clang"]:
            path = subprojs[subproj].get_cmake_path(sourcePath)
            worktree = Worktree.create(
                self.proj,
                self.__get_subproj_repo(subproj),
                path,
                self.temporaryLLVMbranch)
            self.assertTrue(os.path.isdir(path), "Failed to create worktree")

        config = LLVMSourceConfig(self.proj, sourcePath)
        enabled = config.get_enabled_subprojects()

        self.assertTrue("lld" in enabled, "Failed to detect lld")
        self.assertTrue("clang" in enabled, "Failed to detect clang")
        self.assertTrue("libcxxabi" in enabled,
                        "Failed to detect libcxxabi")

    def test_detect_enabled_not_worktree(self):
        sourcePath = self.temporaryLLVM.repodir
        subprojs = LLVMSubproject.get_all_subprojects()

        path = subprojs["compiler-rt"].get_cmake_path(sourcePath)
        os.makedirs(path)

        config = LLVMSourceConfig(self.proj, sourcePath)
        enabled = config.get_enabled_subprojects()

        # Check that if it's not a worktree, it's not enabled.
        self.assertEqual(
            enabled,
            [],
            "Detected unexpected projects %s" % str(enabled)
        )

    def test_detect_enabled_wrong_branch(self):
        sourcePath = self.temporaryLLVM.repodir
        subprojs = LLVMSubproject.get_all_subprojects()

        path = subprojs["compiler-rt"].get_cmake_path(sourcePath)
        branch = "different-than-" + self.temporaryLLVMbranch
        worktree = Worktree.create(
            self.proj,
            self.__get_subproj_repo("compiler-rt"),
            path,
            branch)
        self.assertTrue(os.path.isdir(path), "Failed to create worktree")

        config = LLVMSourceConfig(self.proj, sourcePath)
        enabled = config.get_enabled_subprojects()

        # Check that if it's a worktree on the wrong branch, it's not enabled.
        self.assertEqual(
            enabled,
            [],
            "Detected unexpected projects %s" % str(enabled)
        )

    def test_add_invalid_subproject(self):
        config = LLVMSourceConfig(self.proj, self.temporaryLLVM.repodir)
        subproj = "not-an-llvm-subproject"
        subprojPath = self.originalLLVM.repodir  # Dummy path

        with self.assertRaises(ValueError) as context:
            config.update({subproj : Clone(self.proj, subprojPath)})

        self.assertRegex(str(context.exception),
                         "Unknown llvm subproject %s" % subproj)

    def test_add_each_subproject(self):
        sourcePath = self.temporaryLLVM.repodir

        config = LLVMSourceConfig(self.proj, sourcePath)

        subprojs = LLVMSubproject.get_all_subprojects()
        for subproj in subprojs:
            expectedPath = subprojs[subproj].get_cmake_path(sourcePath)
            config.update({subproj : self.__get_subproj_repo(subproj)})
            self.assertTrue(os.path.isdir(expectedPath),
                            "Failed to add subproject %s" % subproj)

    def test_add_all_subprojects(self):
        sourcePath = self.temporaryLLVM.repodir

        config = LLVMSourceConfig(self.proj, sourcePath)

        to_add = {}
        subprojs = LLVMSubproject.get_all_subprojects()

        for subproj in subprojs:
            to_add[subproj] = self.__get_subproj_repo(subproj)

        config.update(to_add)

        for subproj in subprojs:
            expectedPath = subprojs[subproj].get_cmake_path(sourcePath)
            self.assertTrue(os.path.isdir(expectedPath),
                            "Failed to add subproject %s" % subproj)

    def test_add_some_subprojects(self):
        sourcePath = self.temporaryLLVM.repodir

        config = LLVMSourceConfig(self.proj, sourcePath)

        to_add = {}
        to_add["clang"] = self.__get_subproj_repo("clang")
        to_add["compiler-rt"] = self.__get_subproj_repo("compiler-rt")

        config.update(to_add)

        subprojs = LLVMSubproject.get_all_subprojects()
        self.assertTrue(
            os.path.isdir(subprojs["clang"].get_cmake_path(sourcePath)),
            "Failed to add subproject clang")
        self.assertTrue(
            os.path.isdir(subprojs["compiler-rt"].get_cmake_path(sourcePath)),
            "Failed to add subproject compiler-rt")

    def test_add_existing_subprojects(self):
        sourcePath = self.temporaryLLVM.repodir

        config = LLVMSourceConfig(self.proj, sourcePath)

        subprojs = LLVMSubproject.get_all_subprojects()
        existingPath = subprojs["lldb"].get_cmake_path(sourcePath)
        Worktree.create(
            self.proj,
            self.__get_subproj_repo("lldb"),
            existingPath,
            self.temporaryLLVMbranch)

        config.update({ "lldb" : self.__get_subproj_repo("lldb")})

        # If we got this far, we're probably ok, but let's be pedantic and check
        # that the subproject is still there
        self.assertTrue(os.path.isdir(existingPath),
                        "Existing subproject vanished")

    def test_add_subproject_existing_branch(self):
        """
        Test that we can add a subproject that already has the branch that LLVM
        is on. This can happen for instance if we have added and then removed
        the subproject and now we're trying to add it again.
        """
        sourcePath = self.temporaryLLVM.repodir

        config = LLVMSourceConfig(self.proj, sourcePath)

        clangRepo = self.__get_subproj_repo("clang")
        with cd(clangRepo.repodir):
            # Make sure that the branch that LLVM is on already exists in the
            # clang repo as well.
            git("checkout", "-b", self.temporaryLLVMbranch)
            self.__create_dummy_commit()
            git("checkout", "master")

        config.update( { "clang" : clangRepo })

        subprojs = LLVMSubproject.get_all_subprojects()
        path = subprojs["clang"].get_cmake_path(sourcePath)
        self.assertTrue(os.path.isdir(path), "Failed to add subproject")

    def test_add_subproject_not_a_worktree(self):
        """
        Test that we can't update a config to include a subproject that exists
        but is not a worktree.
        """
        sourcePath = self.temporaryLLVM.repodir
        branch = "different-than-" + self.temporaryLLVMbranch

        config = LLVMSourceConfig(self.proj, sourcePath)

        subprojs = LLVMSubproject.get_all_subprojects()
        existingPath = subprojs["lldb"].get_cmake_path(sourcePath)
        os.makedirs(existingPath)

        with self.assertRaises(EnvironmentError) as context:
            config.update({ "lldb" : self.__get_subproj_repo("lldb")})

        self.assertRegex(str(context.exception),
                         "{} already exists but is not a valid subproject directory.*"
                         .format(existingPath))

        # If we got this far, we're probably ok, but let's be pedantic and check
        # that the subproject is still there
        self.assertTrue(os.path.isdir(existingPath),
                        "Existing subproject vanished")

    def test_add_subproject_wrong_branch(self):
        """
        Test that we can't update a config to include a subproject that exists
        but is on the wrong branch.
        """
        sourcePath = self.temporaryLLVM.repodir
        branch = "different-than-" + self.temporaryLLVMbranch

        config = LLVMSourceConfig(self.proj, sourcePath)

        subprojs = LLVMSubproject.get_all_subprojects()
        existingPath = subprojs["lldb"].get_cmake_path(sourcePath)
        Worktree.create(
            self.proj,
            self.__get_subproj_repo("lldb"),
            existingPath,
            branch)

        with self.assertRaises(EnvironmentError) as context:
            config.update({ "lldb" : self.__get_subproj_repo("lldb")})

        self.assertRegex(str(context.exception),
                         "{} already exists but is not a valid subproject directory.*"
                         .format(existingPath))

        # If we got this far, we're probably ok, but let's be pedantic and check
        # that the subproject is still there
        self.assertTrue(os.path.isdir(existingPath),
                        "Existing subproject vanished")

    def test_remove_subproject(self):
        sourcePath = self.temporaryLLVM.repodir

        subprojs = LLVMSubproject.get_all_subprojects()

        lldPath = subprojs["lld"].get_cmake_path(sourcePath)
        lldWorktree = Worktree.create(
            self.proj,
            self.__get_subproj_repo("lld"),
            lldPath,
            self.temporaryLLVMbranch)

        clangPath = subprojs["clang"].get_cmake_path(sourcePath)
        clangWorktree = Worktree.create(
            self.proj,
            self.__get_subproj_repo("clang"),
            clangPath,
            self.temporaryLLVMbranch)

        config = LLVMSourceConfig(self.proj, sourcePath)
        config.update(remove=["lld"])

        self.assertFalse(os.path.isdir(lldPath), "Failed to remove subproject")
        self.assertTrue(os.path.isdir(clangPath), "Removed sibling subproject")

    def test_remove_some_subprojects(self):
        sourcePath = self.temporaryLLVM.repodir

        subprojs = LLVMSubproject.get_all_subprojects()

        for subproj in ["clang", "compiler-rt", "lld", "lldb", "libunwind"]:
            path = subprojs[subproj].get_cmake_path(sourcePath)
            worktree = Worktree.create(
                self.proj,
                self.__get_subproj_repo(subproj),
                path,
                self.temporaryLLVMbranch)

        config = LLVMSourceConfig(self.proj, sourcePath)
        config.update(remove=["compiler-rt", "lld"])

        for subproj in ["compiler-rt", "lld"]:
            path = subprojs[subproj].get_cmake_path(sourcePath)
            self.assertFalse(
                os.path.isdir(path),
                "Failed to remove subproject")

        for subproj in ["clang", "lldb", "libunwind"]:
            path = subprojs[subproj].get_cmake_path(sourcePath)
            self.assertTrue(os.path.isdir(path), "Removed sibling subproject")

    def test_remove_all_subprojects(self):
        sourcePath = self.temporaryLLVM.repodir

        subprojs = LLVMSubproject.get_all_subprojects()

        for subproj in list(subprojs.keys()):
            path = subprojs[subproj].get_cmake_path(sourcePath)
            worktree = Worktree.create(
                self.proj,
                self.__get_subproj_repo(subproj),
                path,
                self.temporaryLLVMbranch)
            self.assertTrue(os.path.isdir(path), "Failed to create worktree")

        config = LLVMSourceConfig(self.proj, sourcePath)
        config.update(remove=list(subprojs.keys()))

        for subproj in list(subprojs.keys()):
            path = subprojs[subproj].get_cmake_path(sourcePath)
            self.assertFalse(
                os.path.isdir(path),
                "Failed to remove subproject")

    def test_remove_each_subproject(self):
        sourcePath = self.temporaryLLVM.repodir

        subprojs = LLVMSubproject.get_all_subprojects()

        for subproj in list(subprojs.keys()):
            path = subprojs[subproj].get_cmake_path(sourcePath)
            worktree = Worktree.create(
                self.proj,
                self.__get_subproj_repo(subproj),
                path,
                self.temporaryLLVMbranch)
            self.assertTrue(os.path.isdir(path), "Failed to create worktree")

        config = LLVMSourceConfig(self.proj, sourcePath)

        for subproj in list(subprojs.keys()):
            config.update(remove=[subproj])

        for subproj in list(subprojs.keys()):
            path = subprojs[subproj].get_cmake_path(sourcePath)
            self.assertFalse(
                os.path.isdir(path),
                "Failed to remove subproject")

    def test_remove_duplicates(self):
        sourcePath = self.temporaryLLVM.repodir

        subprojs = LLVMSubproject.get_all_subprojects()

        for subproj in ["clang", "compiler-rt", "lld", "lldb", "libunwind"]:
            path = subprojs[subproj].get_cmake_path(sourcePath)
            worktree = Worktree.create(
                self.proj,
                self.__get_subproj_repo(subproj),
                path,
                self.temporaryLLVMbranch)

        config = LLVMSourceConfig(self.proj, sourcePath)
        config.update(remove=["compiler-rt", "lld", "lld", "compiler-rt"])

        for subproj in ["compiler-rt", "lld"]:
            path = subprojs[subproj].get_cmake_path(sourcePath)
            self.assertFalse(
                os.path.isdir(path),
                "Failed to remove subproject")

        for subproj in ["clang", "lldb", "libunwind"]:
            path = subprojs[subproj].get_cmake_path(sourcePath)
            self.assertTrue(os.path.isdir(path), "Removed sibling subproject")

    def test_remove_invalid_subproject(self):
        sourcePath = self.temporaryLLVM.repodir
        config = LLVMSourceConfig(self.proj, sourcePath)

        subproj = "not-an-llvm-subproject"

        with self.assertRaises(ValueError) as context:
            config.update(remove=[subproj])

        self.assertRegex(str(context.exception),
                         "Unknown llvm subproject %s" % subproj)

    def test_remove_inexistent_subproject(self):
        sourcePath = self.temporaryLLVM.repodir

        subprojs = LLVMSubproject.get_all_subprojects()

        lldPath = subprojs["lld"].get_cmake_path(sourcePath)
        lldWorktree = Worktree.create(
            self.proj,
            self.__get_subproj_repo("lld"),
            lldPath,
            self.temporaryLLVMbranch)

        clangPath = subprojs["clang"].get_cmake_path(sourcePath)

        config = LLVMSourceConfig(self.proj, sourcePath)
        config.update(remove=["clang"])

        self.assertFalse(
            os.path.isdir(clangPath),
            "Failed to remove subproject")
        self.assertTrue(os.path.isdir(lldPath), "Removed sibling subproject")

    def test_add_after_remove(self):
        sourcePath = self.temporaryLLVM.repodir

        subprojs = LLVMSubproject.get_all_subprojects()
        lldbRepo = self.__get_subproj_repo("lldb")

        config = LLVMSourceConfig(self.proj, sourcePath)

        config.update({"lldb" : lldbRepo})
        self.assertTrue(
            os.path.isdir(subprojs["lldb"].get_cmake_path(sourcePath)),
            "Failed to add lldb")

        config.update(remove=["lldb"])
        self.assertFalse(
            os.path.isdir(subprojs["lldb"].get_cmake_path(sourcePath)),
            "Failed to remove lldb")

        config.update({"lldb" : lldbRepo })
        self.assertTrue(
            os.path.isdir(subprojs["lldb"].get_cmake_path(sourcePath)),
            "Failed to add lldb")

    def test_mixed_adds_removes(self):
        sourcePath = self.temporaryLLVM.repodir

        subprojs = LLVMSubproject.get_all_subprojects()

        for subproj in ["clang", "compiler-rt", "lld", "lldb", "libunwind"]:
            path = subprojs[subproj].get_cmake_path(sourcePath)
            worktree = Worktree.create(
                self.proj,
                self.__get_subproj_repo(subproj),
                path,
                self.temporaryLLVMbranch)

        config = LLVMSourceConfig(self.proj, sourcePath)
        config.update({
                "libcxx" : self.__get_subproj_repo("libcxx"),
                "libcxxabi" : self.__get_subproj_repo("libcxxabi")},
                ["compiler-rt", "lld"])

        for subproj in ["libcxx", "libcxxabi"]:
            path = subprojs[subproj].get_cmake_path(sourcePath)
            self.assertTrue(os.path.isdir(path), "Failed to add subproject")

        for subproj in ["compiler-rt", "lld"]:
            path = subprojs[subproj].get_cmake_path(sourcePath)
            self.assertFalse(
                os.path.isdir(path),
                "Failed to remove subproject")

        for subproj in ["clang", "lldb", "libunwind"]:
            path = subprojs[subproj].get_cmake_path(sourcePath)
            self.assertTrue(os.path.isdir(path), "Removed sibling subproject")

    def test_simultaneous_add_remove(self):
        sourcePath = self.temporaryLLVM.repodir
        subprojs = LLVMSubproject.get_all_subprojects()

        clangRepo = self.__get_subproj_repo("clang")
        lldRepo = self.__get_subproj_repo("lld")
        libunwindRepo = self.__get_subproj_repo("libunwind")

        config = LLVMSourceConfig(self.proj, sourcePath)
        with self.assertRaises(ValueError) as context:
            config.update(
                    { "clang" : clangRepo, "lld" : lldRepo, "libunwind" :
                        libunwindRepo}, ["libcxx", "lld", "libcxxabi"])

        self.assertEqual(str(context.exception),
                         "Can't add and remove lld at the same time")

        # Make sure we didn't add any of the others either
        for subproj in ["clang", "libunwind"]:
            path = subprojs[subproj].get_cmake_path(sourcePath)
            self.assertFalse(
                os.path.isdir(path),
                "Incorrectly added subproject")

    # TODO: test with a different dictionary than the default one (not
    # necessarily containing subprojects - it can contain "potato", "banana" and
    # "gazpacho" for all we care); in fact, it would probably be best to move the
    # existing tests to that...

    # TODO: test that CMake gets our layout

if __name__ == "__main__":
    unittest.main()