aboutsummaryrefslogtreecommitdiff
path: root/tests/cli/testllvmprojects.py
blob: c33ddb592ac1534b9b9ef8f400791d8158db161d (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
"""Command line interface tests for llvmprojs.py

Note that although this uses the unittest framework, it does *not* contain unit
tests.

"""

import shutil
import os
import subprocess
import unittest

from functools import partial
from tempfile import mkdtemp
from uuid import uuid4

from linaropy.cd import cd


# TODO: move this somewhere more public (maybe linaropy?)
def debug(test):
    """
    Decorator that dumps the output of any subprocess.CalledProcessError
    exception. Use this to decorate a test function when you can't tell what the
    problem is.
    """
    def wrapper(*args, **kwargs):
        # Catch any exceptions so we can dump all the output
        try:
            test(*args, **kwargs)
        except subprocess.CalledProcessError as exc:
            print "Error in {}:".format(test.__name__)
            print "Command {} exited with error code {}:\n{}".format(
                exc.cmd, exc.returncode, exc.output)
    return wrapper


class Testllvmprojs(unittest.TestCase):
    script = os.path.join("scripts", "llvm.py")

    @classmethod
    def __create_dummy_commit(cls):
        filename = "filethatshouldntexist"
        cls.run_quietly(["touch", filename])
        cls.run_quietly(["git", "add", filename])
        cls.run_quietly(["git", "commit", "-m", "Dummy commit"])

    @classmethod
    def __create_dummy_repo(cls, repopath):
        if not os.path.isdir(repopath):
            os.makedirs(repopath)

        with cd(repopath):
            cls.run_quietly(["git", "init"])
            cls.__create_dummy_commit()

    @classmethod
    def __add_worktree(cls, repopath, worktreepath, branch):
        with cd(repopath):
            cls.run_quietly(["git", "worktree", "add", worktreepath,
                             "-b", branch])

    @classmethod
    def __get_subproj_repo(cls, subproj):
        return os.path.join(cls.repos, subproj)

    @classmethod
    def setUpClass(cls):
        """Create the file structure and environment that llvmprojs expects"""
        cls.llvm_root = mkdtemp()
        cls.repos = os.path.join(cls.llvm_root, "repos")

        cls.all_repos = ("llvm", "clang", "compiler-rt", "lld", "lldb",
                         "libcxx", "libcxxabi", "libunwind", "test-suite")

        # Set up helper functions for running commands (this needs to be done
        # before creating the repos)
        # FIXME: In newer versions of Python (3.3+) we should be able to bind
        # the stdout to DEVNULL, as below:
        # cls.run_quietly = partial(subprocess.call, stdout=subprocess.DEVNULL)
        # This doesn't work in Python 2, so we'll just have to ignore the output
        # for now...
        cls.run_with_output = partial(subprocess.check_output,
                                      stderr=subprocess.STDOUT)
        cls.run_quietly = cls.run_with_output

        # Create dummy repos
        for reponame in cls.all_repos:
            cls.__create_dummy_repo(cls.__get_subproj_repo(reponame))

    @classmethod
    def tearDownClass(cls):
        shutil.rmtree(cls.llvm_root)

    @classmethod
    def setUp(cls):
        cls.llvm_src = os.path.join(cls.llvm_root, "src" + str(uuid4()))

        # Create LLVM worktree
        cls.branch = "br" + str(uuid4())
        cls.__add_worktree(cls.__get_subproj_repo("llvm"), cls.llvm_src,
                           cls.branch)

        # Set up the environment variables
        os.environ["LLVM_ROOT"] = cls.llvm_root
        os.environ["LLVM_SRC"] = cls.llvm_src

    @classmethod
    def tearDown(cls):
        # Clean up the directories where we might have added subprojects.
        # This isn't 100% clean, because we don't clean up the repos between
        # tests (so any branches will remain), but it's good enough for the
        # current tests.
        for subprojdir in (os.path.join(cls.llvm_src, "projects"),
                           os.path.join(cls.llvm_src, "tools")):
            if os.path.isdir(subprojdir):
                shutil.rmtree(subprojdir)
                os.makedirs(subprojdir)

        # Run prune on the original repos, to remove any dangling worktrees.
        for reponame in cls.all_repos:
            repopath = cls.__get_subproj_repo(reponame)
            with cd(repopath):
                cls.run_quietly(["git", "worktree", "prune"])

    def test_dump_empty_config(self):
        """
        Test that we're correctly dumping an empty configuration (i.e. no
        projects linked) when running llvmprojs without arguments.
        """
        output = self.run_with_output(["python", self.script, "projects"])
        self.assertRegexpMatches(output, "Projects linked:.*\n.*none.*")

    def test_add_remove_subprojects(self):
        """
        Test that we can add and remove one or several subprojects.
        """
        output = self.run_with_output(["python", self.script, "projects",
                                       "--add", "clang"])
        self.assertRegexpMatches(output, "Projects linked:.*\n.*clang.*")

        output = self.run_with_output(["python", self.script, "projects",
                                       "--add", "libcxx", "lldb"])
        self.assertRegexpMatches(
            output,
            "Projects linked:.*\n" +
            ".*clang.*\n" +
            ".*libcxx.*\n" +
            ".*lldb.*")

        output = self.run_with_output(["python", self.script, "projects",
                                       "--remove", "libcxx"])
        self.assertRegexpMatches(output,
                                 "Projects linked:.*\n" +
                                 ".*clang.*\n" +
                                 ".*lldb.*")

        output = self.run_with_output(["python", self.script, "projects",
                                       "--remove", "clang", "lldb"])
        self.assertRegexpMatches(output,
                                 "Projects linked:.*\n" +
                                 ".*none.*")

    def test_add_remove_invalid_subprojects(self):
        """
        Test that we error out nicely when trying to add/remove invalid
        subprojects.
        """
        with self.assertRaises(subprocess.CalledProcessError) as context:
            self.run_quietly(["python", self.script, "projects",
                              "--add", "inventedsubproject"])

        self.assertRegexpMatches(
            context.exception.output,
            "(.*\n)*.*invalid choice:.*inventedsubproject(.*\n)*")

        with self.assertRaises(subprocess.CalledProcessError) as context:
            self.run_quietly(["python", self.script, "projects",
                              "--remove", "inventedsubproject"])

        self.assertRegexpMatches(
            context.exception.output,
            "(.*\n)*.*invalid choice:.*inventedsubproject(.*\n)*")

    def test_duplicate_add_remove(self):
        """
        Test that we don't crash when trying to add / remove the same subproject
        twice with the same command.
        """
        output = self.run_with_output(["python", self.script, "projects",
                                       "--add", "clang", "lld", "clang"])
        self.assertRegexpMatches(output,
                                 "Projects linked:.*\n" +
                                 ".*clang.*\n" +
                                 ".*lld.*")

        output = self.run_with_output(["python", self.script, "projects",
                                       "--remove", "lld", "lld", "clang"])
        self.assertRegexpMatches(output,
                                 "Projects linked:.*\n" +
                                 ".*none.*")

    def test_redundant_add_remove(self):
        """
        Test that we can add a subproject that already exists (either because it
        was added by our script or manually) or remove a subproject that doesn't
        exist.
        """
        self.__add_worktree(self.__get_subproj_repo("clang"),
                            os.path.join(self.llvm_src, "tools", "clang"),
                            self.branch)
        self.__add_worktree(
            self.__get_subproj_repo("compiler-rt"),
            os.path.join(self.llvm_src, "projects", "compiler-rt"),
            self.branch)

        output = self.run_with_output(["python", self.script, "projects",
                                       "--add", "clang"])
        self.assertRegexpMatches(output,
                                 "Projects linked:.*\n" +
                                 ".*clang.*")

        output = self.run_with_output(["python", self.script, "projects",
                                       "--add", "compiler-rt", "lld"])
        self.assertRegexpMatches(
            output,
            "Projects linked:.*\n" +
            ".*clang.*\n" +
            ".*compiler-rt.*\n" +
            ".*lld.*")

        output = self.run_with_output(["python", self.script, "projects",
                                       "--remove", "lldb", "libcxx", "lld"])
        self.assertRegexpMatches(
            output,
            "Projects linked:.*\n" +
            ".*clang.*\n" +
            ".*compiler-rt.*")

    def test_simultaneous_add_remove(self):
        """
        Test that we error out when someone is trying to add and remove the same
        project with the same command.
        """
        # Try the really basic case and make sure we're not touching anything
        with self.assertRaises(subprocess.CalledProcessError) as context:
            self.run_quietly(["python", self.script, "projects",
                              "--add", "clang", "--remove", "clang"])

        self.assertRegexpMatches(
            context.exception.output,
            "(.*\n)*.*Can't add and remove clang at the same time(.*\n)*")

        self.assertFalse(
            os.path.exists(
                os.path.join(self.llvm_src, "tools", "clang")))

        # Try something a bit more complicated and make sure we're not touching
        # anything
        self.__add_worktree(
            self.__get_subproj_repo("lld"),
            os.path.join(self.llvm_src, "tools", "lld"),
            self.branch)

        with self.assertRaises(subprocess.CalledProcessError) as context:
            self.run_quietly(["python", self.script, "projects",
                              "--add", "clang", "lld", "libcxx",
                              "--remove", "lld", "libcxx"])
        self.assertRegexpMatches(
            context.exception.output,
            "(.*\n)*" +
            ".*Can't add and remove (lld|libcxx) at the same time(.*\n)*")

        # Make sure we didn't touch anything
        self.assertFalse(
            os.path.exists(
                os.path.join(self.llvm_src, "tools", "clang")))
        self.assertTrue(
            os.path.exists(
                os.path.join(self.llvm_src, "tools", "lld")))
        self.assertFalse(
            os.path.exists(
                os.path.join(self.llvm_src, "projects", "libcxx")))

    def test_multiple_adds_removes(self):
        """
        Test that we can have multiple --add and --remove options in the same
        command and that only the last one of each kind matters.
        """
        output = self.run_with_output(["python", self.script, "projects",
                                       "--add", "libcxxabi",
                                       "--remove", "lld", "lldb",
                                       "--add", "clang", "libcxx",
                                       "--remove", "libunwind"])
        self.assertRegexpMatches(output,
                                 "Projects linked:.*\n" +
                                 ".*clang.*\n" +
                                 ".*libcxx.*\n")

        output = self.run_with_output(["python", self.script, "projects",
                                       "--add", "libunwind", "libcxxabi",
                                       "--remove", "clang", "libcxx",
                                       "--add", "compiler-rt",
                                       "--remove", "libcxxabi"])
        self.assertRegexpMatches(output,
                                 "Projects linked:.*\n" +
                                 ".*clang.*\n" +
                                 ".*compiler-rt.*\n" +
                                 ".*libcxx.*\n")

        output = self.run_with_output(["python", self.script, "projects",
                                       "--add", "libcxx", "--remove", "lld",
                                       "--add", "lld", "--remove", "libcxx"])
        self.assertRegexpMatches(output,
                                 "Projects linked:.*\n" +
                                 ".*clang.*\n" +
                                 ".*compiler-rt.*\n" +
                                 ".*lld.*\n")