aboutsummaryrefslogtreecommitdiff
path: root/tests/cli/testllvmconfigure.py
blob: cfd95d8778d7ac6a90a390a87eaccc38be277db8 (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
"""Command line interface tests for llvm.py configure.

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

"""
from os import path
from subprocess import CalledProcessError
from tempfile import mkdtemp
from uuid import uuid4

from llvmtestcase import LLVMTestCase, debug

class Testllvmconfigure(LLVMTestCase):

    @classmethod
    def llvm_configure(cls, *args, **kwargs):
        return cls.command_with_defaults("configure", *args, **kwargs)

    @classmethod
    def setUpClass(cls):
        cls.env = mkdtemp()

        cls.llvm_repo = path.join(cls.env, "llvm-repo")
        cls.create_dummy_repo(cls.llvm_repo)

        cls.llvm_worktree = path.join(cls.env, "llvm")
        cls.add_worktree(cls.llvm_repo, cls.llvm_worktree, "br")

    def test_source_dir_is_compulsory(self):
        """Test that we get an error if we don't pass the source dir."""
        with self.assertRaises(CalledProcessError) as context:
            self.run_with_output(
                    self.llvm_configure("--build-dir", "anywhere"))

        self.assertRegex(
            str(context.exception.output),
            "(.*\n)*the following arguments are required: --source-dir(.*\n)*")

    def test_build_dir_is_compulsory(self):
        """Test that we get an error if we don't pass the build dir."""
        with self.assertRaises(CalledProcessError) as context:
            self.run_with_output(
                    self.llvm_configure("--source-dir", self.llvm_worktree))

        self.assertRegex(
            str(context.exception.output),
            "(.*\n)*the following arguments are required: --build-dir(.*\n)*")

    def test_default_args(self):
        """Test that we can get a simple configure command."""
        output = self.run_with_output(self.llvm_configure("--dry-run",
                                                          "--source-dir",
                                                          self.llvm_worktree,
                                                          "--build-dir",
                                                          "anywhere"))
        self.assertRegex(output,
                         "anywhere\$ cmake -G Ninja .* {}".format(self.llvm_worktree))

    def test_generator(self):
        """Test that we can specify a custom generator."""
        output = self.run_with_output(self.llvm_configure("--dry-run",
                                                          "--source-dir",
                                                          self.llvm_worktree,
                                                          "--build-dir",
                                                          "anywhere",
                                                          "--cmake-generator",
                                                          "\"Unix Makefiles\""))
        self.assertRegex(output,
            "anywhere\$ cmake -G \"Unix Makefiles\" .* {}".format(self.llvm_worktree))

    def test_cmake_vars(self):
        """Test that we can specify custom cmake variables."""
        output = self.run_with_output(self.llvm_configure(
            "--dry-run",
            "--source-dir", self.llvm_worktree,
            "--build-dir", "anywhere",
            "--cmake-def", "VAR1=VAL1",
            "--cmake-def", "VAR2=\"-not --so simple\""))

        self.assertRegex(output,
            "anywhere\$ cmake -G Ninja .* -DVAR1=VAL1 -DVAR2=\"-not --so simple\" {}".format(self.llvm_worktree))

    def test_create_build_dir(self):
        """
        Test that we create the build directory if it doesn't already exist (but
        not in dry-run).
        """
        buildDir = path.join(self.env, "brand-new-build-dir")
        self.assertFalse(path.isdir(buildDir))

        self.run_with_output(self.llvm_configure("--dry-run",
            "--source-dir", self.llvm_worktree,
            "--build-dir", buildDir))
        self.assertFalse(path.isdir(buildDir))

        # When not running in dry run mode, we're going to try to execute a
        # CMake command that's going to fail because we don't have a valid LLVM
        # source directory. This is fine, since the purpose of this test isn't
        # to check the integration with CMake.
        with self.assertRaises(Exception) as context:
            print(self.run_with_output(self.llvm_configure(
                "--source-dir", self.llvm_worktree,
                "--build-dir", buildDir)))

        self.assertTrue(path.isdir(buildDir))