aboutsummaryrefslogtreecommitdiff
path: root/tests/cli/llvmtestcase.py
blob: b2fc765e28552d1f2dd4195bddc40f606a4815b8 (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
"""Common TestCase used for testing llvm.py subcommands."""

import shutil
import os
import subprocess
import unittest

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 LLVMTestCase(unittest.TestCase):
    python = "python3"
    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)

    @staticmethod
    def run_with_output(*args, **kwargs):
        """Helper for running a command and capturing stdout and stderr"""
        kwargs["stderr"] = subprocess.STDOUT
        return str(subprocess.check_output(*args, **kwargs), 'utf-8')

    @staticmethod
    def run_quietly(*args, **kwargs):
        """
        Helper for running a command and ignoring stdout and stderr. Exceptions
        are still thrown if something goes wrong
        """
        kwargs["stdout"] = subprocess.DEVNULL
        kwargs["stderr"] = subprocess.DEVNULL
        return subprocess.check_call(*args, **kwargs)

    @classmethod
    def command_with_defaults(cls, subcommand, *args, **kwargs):
        """
        Build a list representing a llvm subcommand with the given
        args. Unless otherwise specified in kwargs, this uses the values for
        repos and env that it finds in cls.
        """
        command = [cls.python, cls.script]

        repos = cls.repos
        if "repos" in kwargs:
            repos = kwargs["repos"]
        if repos:
            command.append("--repos")
            command.append(repos)

        env = cls.env
        if "env" in kwargs:
            env = kwargs["env"]
        if env:
            command.append("--env")
            command.append(env)

        command.append(subcommand)

        if len(args):
            command.extend(args)

        return command