Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 1 | """Command line interface tests for llvmprojs.py |
| 2 | |
| 3 | Note that although this uses the unittest framework, it does *not* contain unit |
| 4 | tests. |
| 5 | |
| 6 | """ |
| 7 | |
| 8 | import shutil |
| 9 | import os |
| 10 | import subprocess |
| 11 | import unittest |
| 12 | |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 13 | from tempfile import mkdtemp |
| 14 | from uuid import uuid4 |
| 15 | |
| 16 | from linaropy.cd import cd |
| 17 | |
| 18 | |
| 19 | # TODO: move this somewhere more public (maybe linaropy?) |
| 20 | def debug(test): |
| 21 | """ |
| 22 | Decorator that dumps the output of any subprocess.CalledProcessError |
| 23 | exception. Use this to decorate a test function when you can't tell what the |
| 24 | problem is. |
| 25 | """ |
| 26 | def wrapper(*args, **kwargs): |
| 27 | # Catch any exceptions so we can dump all the output |
| 28 | try: |
| 29 | test(*args, **kwargs) |
| 30 | except subprocess.CalledProcessError as exc: |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 31 | print("Error in {}:".format(test.__name__)) |
| 32 | print("Command {} exited with error code {}:\n{}".format( |
| 33 | exc.cmd, exc.returncode, exc.output)) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 34 | return wrapper |
| 35 | |
| 36 | |
| 37 | class Testllvmprojs(unittest.TestCase): |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 38 | python = "python3" |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 39 | script = os.path.join("scripts", "llvm.py") |
| 40 | |
| 41 | @classmethod |
| 42 | def __create_dummy_commit(cls): |
| 43 | filename = "filethatshouldntexist" |
| 44 | cls.run_quietly(["touch", filename]) |
| 45 | cls.run_quietly(["git", "add", filename]) |
| 46 | cls.run_quietly(["git", "commit", "-m", "Dummy commit"]) |
| 47 | |
| 48 | @classmethod |
| 49 | def __create_dummy_repo(cls, repopath): |
| 50 | if not os.path.isdir(repopath): |
| 51 | os.makedirs(repopath) |
| 52 | |
| 53 | with cd(repopath): |
| 54 | cls.run_quietly(["git", "init"]) |
| 55 | cls.__create_dummy_commit() |
| 56 | |
| 57 | @classmethod |
| 58 | def __add_worktree(cls, repopath, worktreepath, branch): |
| 59 | with cd(repopath): |
| 60 | cls.run_quietly(["git", "worktree", "add", worktreepath, |
| 61 | "-b", branch]) |
| 62 | |
| 63 | @classmethod |
| 64 | def __get_subproj_repo(cls, subproj): |
| 65 | return os.path.join(cls.repos, subproj) |
| 66 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +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 | |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 83 | @classmethod |
| 84 | def setUpClass(cls): |
| 85 | """Create the file structure and environment that llvmprojs expects""" |
| 86 | cls.llvm_root = mkdtemp() |
| 87 | cls.repos = os.path.join(cls.llvm_root, "repos") |
| 88 | |
| 89 | cls.all_repos = ("llvm", "clang", "compiler-rt", "lld", "lldb", |
| 90 | "libcxx", "libcxxabi", "libunwind", "test-suite") |
| 91 | |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 92 | # Create dummy repos |
| 93 | for reponame in cls.all_repos: |
| 94 | cls.__create_dummy_repo(cls.__get_subproj_repo(reponame)) |
| 95 | |
| 96 | @classmethod |
| 97 | def tearDownClass(cls): |
| 98 | shutil.rmtree(cls.llvm_root) |
| 99 | |
| 100 | @classmethod |
| 101 | def setUp(cls): |
| 102 | cls.llvm_src = os.path.join(cls.llvm_root, "src" + str(uuid4())) |
| 103 | |
| 104 | # Create LLVM worktree |
| 105 | cls.branch = "br" + str(uuid4()) |
| 106 | cls.__add_worktree(cls.__get_subproj_repo("llvm"), cls.llvm_src, |
| 107 | cls.branch) |
| 108 | |
| 109 | # Set up the environment variables |
| 110 | os.environ["LLVM_ROOT"] = cls.llvm_root |
| 111 | os.environ["LLVM_SRC"] = cls.llvm_src |
| 112 | |
| 113 | @classmethod |
| 114 | def tearDown(cls): |
| 115 | # Clean up the directories where we might have added subprojects. |
| 116 | # This isn't 100% clean, because we don't clean up the repos between |
| 117 | # tests (so any branches will remain), but it's good enough for the |
| 118 | # current tests. |
| 119 | for subprojdir in (os.path.join(cls.llvm_src, "projects"), |
| 120 | os.path.join(cls.llvm_src, "tools")): |
| 121 | if os.path.isdir(subprojdir): |
| 122 | shutil.rmtree(subprojdir) |
| 123 | os.makedirs(subprojdir) |
| 124 | |
| 125 | # Run prune on the original repos, to remove any dangling worktrees. |
| 126 | for reponame in cls.all_repos: |
| 127 | repopath = cls.__get_subproj_repo(reponame) |
| 128 | with cd(repopath): |
| 129 | cls.run_quietly(["git", "worktree", "prune"]) |
| 130 | |
| 131 | def test_dump_empty_config(self): |
| 132 | """ |
| 133 | Test that we're correctly dumping an empty configuration (i.e. no |
| 134 | projects linked) when running llvmprojs without arguments. |
| 135 | """ |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 136 | output = self.run_with_output([self.python, self.script, "projects"]) |
| 137 | self.assertRegex(output, "Projects linked:.*\n.*none.*") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 138 | |
| 139 | def test_add_remove_subprojects(self): |
| 140 | """ |
| 141 | Test that we can add and remove one or several subprojects. |
| 142 | """ |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 143 | output = self.run_with_output([self.python, self.script, "projects", |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 144 | "--add", "clang"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 145 | self.assertRegex(output, "Projects linked:.*\n.*clang.*") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 146 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 147 | output = self.run_with_output([self.python, self.script, "projects", |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 148 | "--add", "libcxx", "lldb"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 149 | self.assertRegex( |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 150 | output, |
| 151 | "Projects linked:.*\n" + |
| 152 | ".*clang.*\n" + |
| 153 | ".*libcxx.*\n" + |
| 154 | ".*lldb.*") |
| 155 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 156 | output = self.run_with_output([self.python, self.script, "projects", |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 157 | "--remove", "libcxx"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 158 | self.assertRegex(output, |
| 159 | "Projects linked:.*\n" + |
| 160 | ".*clang.*\n" + |
| 161 | ".*lldb.*") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 162 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 163 | output = self.run_with_output([self.python, self.script, "projects", |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 164 | "--remove", "clang", "lldb"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 165 | self.assertRegex(output, |
| 166 | "Projects linked:.*\n" + |
| 167 | ".*none.*") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 168 | |
| 169 | def test_add_remove_invalid_subprojects(self): |
| 170 | """ |
| 171 | Test that we error out nicely when trying to add/remove invalid |
| 172 | subprojects. |
| 173 | """ |
| 174 | with self.assertRaises(subprocess.CalledProcessError) as context: |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 175 | self.run_with_output([self.python, self.script, "projects", |
| 176 | "--add", "inventedsubproject"]) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 177 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 178 | self.assertRegex( |
| 179 | str(context.exception.output), |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 180 | "(.*\n)*.*invalid choice:.*inventedsubproject(.*\n)*") |
| 181 | |
| 182 | with self.assertRaises(subprocess.CalledProcessError) as context: |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 183 | self.run_with_output([self.python, self.script, "projects", |
| 184 | "--remove", "inventedsubproject"]) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 185 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 186 | self.assertRegex( |
| 187 | str(context.exception.output), |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 188 | "(.*\n)*.*invalid choice:.*inventedsubproject(.*\n)*") |
| 189 | |
| 190 | def test_duplicate_add_remove(self): |
| 191 | """ |
| 192 | Test that we don't crash when trying to add / remove the same subproject |
| 193 | twice with the same command. |
| 194 | """ |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 195 | output = self.run_with_output([self.python, self.script, "projects", |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 196 | "--add", "clang", "lld", "clang"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 197 | self.assertRegex(output, |
| 198 | "Projects linked:.*\n" + |
| 199 | ".*clang.*\n" + |
| 200 | ".*lld.*") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 201 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 202 | output = self.run_with_output([self.python, self.script, "projects", |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 203 | "--remove", "lld", "lld", "clang"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 204 | self.assertRegex(output, |
| 205 | "Projects linked:.*\n" + |
| 206 | ".*none.*") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 207 | |
| 208 | def test_redundant_add_remove(self): |
| 209 | """ |
| 210 | Test that we can add a subproject that already exists (either because it |
| 211 | was added by our script or manually) or remove a subproject that doesn't |
| 212 | exist. |
| 213 | """ |
| 214 | self.__add_worktree(self.__get_subproj_repo("clang"), |
| 215 | os.path.join(self.llvm_src, "tools", "clang"), |
| 216 | self.branch) |
| 217 | self.__add_worktree( |
| 218 | self.__get_subproj_repo("compiler-rt"), |
| 219 | os.path.join(self.llvm_src, "projects", "compiler-rt"), |
| 220 | self.branch) |
| 221 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 222 | output = self.run_with_output([self.python, self.script, "projects", |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 223 | "--add", "clang"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 224 | self.assertRegex(output, |
| 225 | "Projects linked:.*\n" + |
| 226 | ".*clang.*") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 227 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 228 | output = self.run_with_output([self.python, self.script, "projects", |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 229 | "--add", "compiler-rt", "lld"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 230 | self.assertRegex( |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 231 | output, |
| 232 | "Projects linked:.*\n" + |
| 233 | ".*clang.*\n" + |
| 234 | ".*compiler-rt.*\n" + |
| 235 | ".*lld.*") |
| 236 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 237 | output = self.run_with_output([self.python, self.script, "projects", |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 238 | "--remove", "lldb", "libcxx", "lld"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 239 | self.assertRegex( |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 240 | output, |
| 241 | "Projects linked:.*\n" + |
| 242 | ".*clang.*\n" + |
| 243 | ".*compiler-rt.*") |
| 244 | |
| 245 | def test_simultaneous_add_remove(self): |
| 246 | """ |
| 247 | Test that we error out when someone is trying to add and remove the same |
| 248 | project with the same command. |
| 249 | """ |
| 250 | # Try the really basic case and make sure we're not touching anything |
| 251 | with self.assertRaises(subprocess.CalledProcessError) as context: |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 252 | self.run_with_output([self.python, self.script, "projects", |
| 253 | "--add", "clang", "--remove", "clang"]) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 254 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 255 | self.assertRegex( |
| 256 | str(context.exception.output), |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 257 | "(.*\n)*.*Can't add and remove clang at the same time(.*\n)*") |
| 258 | |
| 259 | self.assertFalse( |
| 260 | os.path.exists( |
| 261 | os.path.join(self.llvm_src, "tools", "clang"))) |
| 262 | |
| 263 | # Try something a bit more complicated and make sure we're not touching |
| 264 | # anything |
| 265 | self.__add_worktree( |
| 266 | self.__get_subproj_repo("lld"), |
| 267 | os.path.join(self.llvm_src, "tools", "lld"), |
| 268 | self.branch) |
| 269 | |
| 270 | with self.assertRaises(subprocess.CalledProcessError) as context: |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 271 | self.run_with_output([self.python, self.script, "projects", |
| 272 | "--add", "clang", "lld", "libcxx", |
| 273 | "--remove", "lld", "libcxx"]) |
| 274 | self.assertRegex( |
| 275 | str(context.exception.output), |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 276 | "(.*\n)*" + |
| 277 | ".*Can't add and remove (lld|libcxx) at the same time(.*\n)*") |
| 278 | |
| 279 | # Make sure we didn't touch anything |
| 280 | self.assertFalse( |
| 281 | os.path.exists( |
| 282 | os.path.join(self.llvm_src, "tools", "clang"))) |
| 283 | self.assertTrue( |
| 284 | os.path.exists( |
| 285 | os.path.join(self.llvm_src, "tools", "lld"))) |
| 286 | self.assertFalse( |
| 287 | os.path.exists( |
| 288 | os.path.join(self.llvm_src, "projects", "libcxx"))) |
| 289 | |
| 290 | def test_multiple_adds_removes(self): |
| 291 | """ |
| 292 | Test that we can have multiple --add and --remove options in the same |
| 293 | command and that only the last one of each kind matters. |
| 294 | """ |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 295 | output = self.run_with_output([self.python, self.script, "projects", |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 296 | "--add", "libcxxabi", |
| 297 | "--remove", "lld", "lldb", |
| 298 | "--add", "clang", "libcxx", |
| 299 | "--remove", "libunwind"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 300 | self.assertRegex(output, |
| 301 | "Projects linked:.*\n" + |
| 302 | ".*clang.*\n" + |
| 303 | ".*libcxx.*\n") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 304 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 305 | output = self.run_with_output([self.python, self.script, "projects", |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 306 | "--add", "libunwind", "libcxxabi", |
| 307 | "--remove", "clang", "libcxx", |
| 308 | "--add", "compiler-rt", |
| 309 | "--remove", "libcxxabi"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 310 | self.assertRegex(output, |
| 311 | "Projects linked:.*\n" + |
| 312 | ".*clang.*\n" + |
| 313 | ".*compiler-rt.*\n" + |
| 314 | ".*libcxx.*\n") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 315 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 316 | output = self.run_with_output([self.python, self.script, "projects", |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 317 | "--add", "libcxx", "--remove", "lld", |
| 318 | "--add", "lld", "--remove", "libcxx"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame^] | 319 | self.assertRegex(output, |
| 320 | "Projects linked:.*\n" + |
| 321 | ".*clang.*\n" + |
| 322 | ".*compiler-rt.*\n" + |
| 323 | ".*lld.*\n") |