Diana Picus | 3601bea | 2017-05-29 11:26:18 +0200 | [diff] [blame] | 1 | """Common TestCase used for testing llvm.py subcommands.""" |
| 2 | |
| 3 | import shutil |
| 4 | import os |
| 5 | import subprocess |
| 6 | import unittest |
| 7 | |
| 8 | from tempfile import mkdtemp |
| 9 | from uuid import uuid4 |
| 10 | |
| 11 | from linaropy.cd import cd |
| 12 | |
| 13 | |
| 14 | # TODO: move this somewhere more public (maybe linaropy?) |
| 15 | def 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 | |
| 32 | class LLVMTestCase(unittest.TestCase): |
| 33 | python = "python3" |
| 34 | script = os.path.join("scripts", "llvm.py") |
| 35 | |
| 36 | @classmethod |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 37 | def create_dummy_commit(cls, commitMessage="Dummy commit"): |
| 38 | filename = "filethatshouldntexist" + str(uuid4()) |
Diana Picus | 3601bea | 2017-05-29 11:26:18 +0200 | [diff] [blame] | 39 | cls.run_quietly(["touch", filename]) |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 40 | 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 Picus | 3601bea | 2017-05-29 11:26:18 +0200 | [diff] [blame] | 46 | |
| 47 | @classmethod |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 48 | 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 Picus | 3601bea | 2017-05-29 11:26:18 +0200 | [diff] [blame] | 57 | |
| 58 | with cd(repopath): |
Diana Picus | 3601bea | 2017-05-29 11:26:18 +0200 | [diff] [blame] | 59 | 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 Picus | 3601bea | 2017-05-29 11:26:18 +0200 | [diff] [blame] | 67 | @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): |
Diana Picus | 9f75686 | 2017-12-20 10:35:08 +0100 | [diff] [blame] | 85 | """Build a list representing an llvm subcommand with the given args.""" |
Diana Picus | 3601bea | 2017-05-29 11:26:18 +0200 | [diff] [blame] | 86 | command = [cls.python, cls.script] |
| 87 | |
Diana Picus | 3601bea | 2017-05-29 11:26:18 +0200 | [diff] [blame] | 88 | command.append(subcommand) |
| 89 | |
| 90 | if len(args): |
| 91 | command.extend(args) |
| 92 | |
| 93 | return command |