blob: b2fc765e28552d1f2dd4195bddc40f606a4815b8 [file] [log] [blame]
Diana Picus3601bea2017-05-29 11:26:18 +02001"""Common TestCase used for testing llvm.py subcommands."""
2
3import shutil
4import os
5import subprocess
6import unittest
7
8from tempfile import mkdtemp
9from uuid import uuid4
10
11from linaropy.cd import cd
12
13
14# TODO: move this somewhere more public (maybe linaropy?)
15def debug(test):
16 """
17 Decorator that dumps the output of any subprocess.CalledProcessError
18 exception. Use this to decorate a test function when you can't tell what the
19 problem is.
20 """
21 def wrapper(*args, **kwargs):
22 # Catch any exceptions so we can dump all the output
23 try:
24 test(*args, **kwargs)
25 except subprocess.CalledProcessError as exc:
26 print("Error in {}:".format(test.__name__))
27 print("Command {} exited with error code {}:\n{}".format(
28 exc.cmd, exc.returncode, exc.output))
29 return wrapper
30
31
32class LLVMTestCase(unittest.TestCase):
33 python = "python3"
34 script = os.path.join("scripts", "llvm.py")
35
36 @classmethod
37 def create_dummy_commit(cls):
38 filename = "filethatshouldntexist"
39 cls.run_quietly(["touch", filename])
40 cls.run_quietly(["git", "add", filename])
41 cls.run_quietly(["git", "commit", "-m", "Dummy commit"])
42
43 @classmethod
44 def create_dummy_repo(cls, repopath):
45 if not os.path.isdir(repopath):
46 os.makedirs(repopath)
47
48 with cd(repopath):
49 cls.run_quietly(["git", "init"])
50 cls.create_dummy_commit()
51
52 @classmethod
53 def add_worktree(cls, repopath, worktreepath, branch):
54 with cd(repopath):
55 cls.run_quietly(["git", "worktree", "add", worktreepath,
56 "-b", branch])
57
58 @classmethod
59 def get_subproj_repo(cls, subproj):
60 return os.path.join(cls.repos, subproj)
61
62 @staticmethod
63 def run_with_output(*args, **kwargs):
64 """Helper for running a command and capturing stdout and stderr"""
65 kwargs["stderr"] = subprocess.STDOUT
66 return str(subprocess.check_output(*args, **kwargs), 'utf-8')
67
68 @staticmethod
69 def run_quietly(*args, **kwargs):
70 """
71 Helper for running a command and ignoring stdout and stderr. Exceptions
72 are still thrown if something goes wrong
73 """
74 kwargs["stdout"] = subprocess.DEVNULL
75 kwargs["stderr"] = subprocess.DEVNULL
76 return subprocess.check_call(*args, **kwargs)
77
78 @classmethod
79 def command_with_defaults(cls, subcommand, *args, **kwargs):
80 """
81 Build a list representing a llvm subcommand with the given
82 args. Unless otherwise specified in kwargs, this uses the values for
83 repos and env that it finds in cls.
84 """
85 command = [cls.python, cls.script]
86
87 repos = cls.repos
88 if "repos" in kwargs:
89 repos = kwargs["repos"]
90 if repos:
91 command.append("--repos")
92 command.append(repos)
93
94 env = cls.env
95 if "env" in kwargs:
96 env = kwargs["env"]
97 if env:
98 command.append("--env")
99 command.append(env)
100
101 command.append(subcommand)
102
103 if len(args):
104 command.extend(args)
105
106 return command