blob: a6f234da433d4808a29d2c6f903ba067d0b4c44f [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
Diana Picusefc7bda2017-06-09 19:14:08 +020037 def create_dummy_commit(cls, commitMessage="Dummy commit"):
38 filename = "filethatshouldntexist" + str(uuid4())
Diana Picus3601bea2017-05-29 11:26:18 +020039 cls.run_quietly(["touch", filename])
Diana Picusefc7bda2017-06-09 19:14:08 +020040 try:
41 cls.run_quietly(["git", "add", filename])
42 cls.run_quietly(["git", "commit", "-m", commitMessage])
43 except subprocess.CalledProcessError as exc:
44 print("Command {} exited with error code {}:\n{}".format(
45 exc.cmd, exc.returncode, exc.output))
Diana Picus3601bea2017-05-29 11:26:18 +020046
47 @classmethod
Diana Picusefc7bda2017-06-09 19:14:08 +020048 def create_dummy_repo(cls, repopath, originpath=None):
49 if originpath is not None:
50 cls.run_quietly(["git", "clone", originpath, repopath])
51 else:
52 if not os.path.isdir(repopath):
53 os.makedirs(repopath)
54
55 with cd(repopath):
56 cls.run_quietly(["git", "init"])
Diana Picus3601bea2017-05-29 11:26:18 +020057
58 with cd(repopath):
Diana Picus3601bea2017-05-29 11:26:18 +020059 cls.create_dummy_commit()
60
61 @classmethod
62 def add_worktree(cls, repopath, worktreepath, branch):
63 with cd(repopath):
64 cls.run_quietly(["git", "worktree", "add", worktreepath,
65 "-b", branch])
66
Diana Picus3601bea2017-05-29 11:26:18 +020067 @staticmethod
68 def run_with_output(*args, **kwargs):
69 """Helper for running a command and capturing stdout and stderr"""
70 kwargs["stderr"] = subprocess.STDOUT
71 return str(subprocess.check_output(*args, **kwargs), 'utf-8')
72
73 @staticmethod
74 def run_quietly(*args, **kwargs):
75 """
76 Helper for running a command and ignoring stdout and stderr. Exceptions
77 are still thrown if something goes wrong
78 """
79 kwargs["stdout"] = subprocess.DEVNULL
80 kwargs["stderr"] = subprocess.DEVNULL
81 return subprocess.check_call(*args, **kwargs)
82
83 @classmethod
84 def command_with_defaults(cls, subcommand, *args, **kwargs):
85 """
86 Build a list representing a llvm subcommand with the given
Diana Picusadb07c42017-11-22 16:12:57 +010087 args. Unless otherwise specified in kwargs, this uses the value for
88 env that it finds in cls.
Diana Picus3601bea2017-05-29 11:26:18 +020089 """
90 command = [cls.python, cls.script]
91
Diana Picus3601bea2017-05-29 11:26:18 +020092 env = cls.env
93 if "env" in kwargs:
94 env = kwargs["env"]
95 if env:
96 command.append("--env")
97 command.append(env)
98
99 command.append(subcommand)
100
101 if len(args):
102 command.extend(args)
103
104 return command