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): |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 102 | cls.env = "src" + str(uuid4()) |
| 103 | cls.llvm_src = os.path.join(cls.llvm_root, cls.env, "llvm") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 104 | |
| 105 | # Create LLVM worktree |
| 106 | cls.branch = "br" + str(uuid4()) |
| 107 | cls.__add_worktree(cls.__get_subproj_repo("llvm"), cls.llvm_src, |
| 108 | cls.branch) |
| 109 | |
| 110 | # Set up the environment variables |
| 111 | os.environ["LLVM_ROOT"] = cls.llvm_root |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 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 | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 136 | output = self.run_with_output( |
| 137 | [self.python, self.script, self.env, "projects"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 138 | self.assertRegex(output, "Projects linked:.*\n.*none.*") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 139 | |
| 140 | def test_add_remove_subprojects(self): |
| 141 | """ |
| 142 | Test that we can add and remove one or several subprojects. |
| 143 | """ |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 144 | output = self.run_with_output( |
| 145 | [self.python, self.script, self.env, "projects", "--add", "clang"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 146 | self.assertRegex(output, "Projects linked:.*\n.*clang.*") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 147 | |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 148 | output = self.run_with_output([self.python, self.script, self.env, "projects", |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 149 | "--add", "libcxx", "lldb"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 150 | self.assertRegex( |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 151 | output, |
| 152 | "Projects linked:.*\n" + |
| 153 | ".*clang.*\n" + |
| 154 | ".*libcxx.*\n" + |
| 155 | ".*lldb.*") |
| 156 | |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 157 | output = self.run_with_output( |
| 158 | [self.python, self.script, self.env, "projects", "--remove", "libcxx"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 159 | self.assertRegex(output, |
| 160 | "Projects linked:.*\n" + |
| 161 | ".*clang.*\n" + |
| 162 | ".*lldb.*") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 163 | |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 164 | output = self.run_with_output([self.python, self.script, self.env, "projects", |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 165 | "--remove", "clang", "lldb"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 166 | self.assertRegex(output, |
| 167 | "Projects linked:.*\n" + |
| 168 | ".*none.*") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 169 | |
| 170 | def test_add_remove_invalid_subprojects(self): |
| 171 | """ |
| 172 | Test that we error out nicely when trying to add/remove invalid |
| 173 | subprojects. |
| 174 | """ |
| 175 | with self.assertRaises(subprocess.CalledProcessError) as context: |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 176 | self.run_with_output([self.python, self.script, self.env, "projects", |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 177 | "--add", "inventedsubproject"]) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 178 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 179 | self.assertRegex( |
| 180 | str(context.exception.output), |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 181 | "(.*\n)*.*invalid choice:.*inventedsubproject(.*\n)*") |
| 182 | |
| 183 | with self.assertRaises(subprocess.CalledProcessError) as context: |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 184 | self.run_with_output([self.python, |
| 185 | self.script, |
| 186 | self.env, |
| 187 | "projects", |
| 188 | "--remove", |
| 189 | "inventedsubproject"]) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 190 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 191 | self.assertRegex( |
| 192 | str(context.exception.output), |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 193 | "(.*\n)*.*invalid choice:.*inventedsubproject(.*\n)*") |
| 194 | |
| 195 | def test_duplicate_add_remove(self): |
| 196 | """ |
| 197 | Test that we don't crash when trying to add / remove the same subproject |
| 198 | twice with the same command. |
| 199 | """ |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 200 | output = self.run_with_output([self.python, self.script, self.env, "projects", |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 201 | "--add", "clang", "lld", "clang"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 202 | self.assertRegex(output, |
| 203 | "Projects linked:.*\n" + |
| 204 | ".*clang.*\n" + |
| 205 | ".*lld.*") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 206 | |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 207 | output = self.run_with_output([self.python, self.script, self.env, "projects", |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 208 | "--remove", "lld", "lld", "clang"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 209 | self.assertRegex(output, |
| 210 | "Projects linked:.*\n" + |
| 211 | ".*none.*") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 212 | |
| 213 | def test_redundant_add_remove(self): |
| 214 | """ |
| 215 | Test that we can add a subproject that already exists (either because it |
| 216 | was added by our script or manually) or remove a subproject that doesn't |
| 217 | exist. |
| 218 | """ |
| 219 | self.__add_worktree(self.__get_subproj_repo("clang"), |
| 220 | os.path.join(self.llvm_src, "tools", "clang"), |
| 221 | self.branch) |
| 222 | self.__add_worktree( |
| 223 | self.__get_subproj_repo("compiler-rt"), |
| 224 | os.path.join(self.llvm_src, "projects", "compiler-rt"), |
| 225 | self.branch) |
| 226 | |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 227 | output = self.run_with_output( |
| 228 | [self.python, self.script, self.env, "projects", "--add", "clang"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 229 | self.assertRegex(output, |
| 230 | "Projects linked:.*\n" + |
| 231 | ".*clang.*") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 232 | |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 233 | output = self.run_with_output([self.python, self.script, self.env, "projects", |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 234 | "--add", "compiler-rt", "lld"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 235 | self.assertRegex( |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 236 | output, |
| 237 | "Projects linked:.*\n" + |
| 238 | ".*clang.*\n" + |
| 239 | ".*compiler-rt.*\n" + |
| 240 | ".*lld.*") |
| 241 | |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 242 | output = self.run_with_output([self.python, |
| 243 | self.script, |
| 244 | self.env, |
| 245 | "projects", |
| 246 | "--remove", |
| 247 | "lldb", |
| 248 | "libcxx", |
| 249 | "lld"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 250 | self.assertRegex( |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 251 | output, |
| 252 | "Projects linked:.*\n" + |
| 253 | ".*clang.*\n" + |
| 254 | ".*compiler-rt.*") |
| 255 | |
| 256 | def test_simultaneous_add_remove(self): |
| 257 | """ |
| 258 | Test that we error out when someone is trying to add and remove the same |
| 259 | project with the same command. |
| 260 | """ |
| 261 | # Try the really basic case and make sure we're not touching anything |
| 262 | with self.assertRaises(subprocess.CalledProcessError) as context: |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 263 | self.run_with_output([self.python, |
| 264 | self.script, |
| 265 | self.env, |
| 266 | "projects", |
| 267 | "--add", |
| 268 | "clang", |
| 269 | "--remove", |
| 270 | "clang"]) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 271 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 272 | self.assertRegex( |
| 273 | str(context.exception.output), |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 274 | "(.*\n)*.*Can't add and remove clang at the same time(.*\n)*") |
| 275 | |
| 276 | self.assertFalse( |
| 277 | os.path.exists( |
| 278 | os.path.join(self.llvm_src, "tools", "clang"))) |
| 279 | |
| 280 | # Try something a bit more complicated and make sure we're not touching |
| 281 | # anything |
| 282 | self.__add_worktree( |
| 283 | self.__get_subproj_repo("lld"), |
| 284 | os.path.join(self.llvm_src, "tools", "lld"), |
| 285 | self.branch) |
| 286 | |
| 287 | with self.assertRaises(subprocess.CalledProcessError) as context: |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 288 | self.run_with_output([self.python, |
| 289 | self.script, |
| 290 | self.env, |
| 291 | "projects", |
| 292 | "--add", |
| 293 | "clang", |
| 294 | "lld", |
| 295 | "libcxx", |
| 296 | "--remove", |
| 297 | "lld", |
| 298 | "libcxx"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 299 | self.assertRegex( |
| 300 | str(context.exception.output), |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 301 | "(.*\n)*" + |
| 302 | ".*Can't add and remove (lld|libcxx) at the same time(.*\n)*") |
| 303 | |
| 304 | # Make sure we didn't touch anything |
| 305 | self.assertFalse( |
| 306 | os.path.exists( |
| 307 | os.path.join(self.llvm_src, "tools", "clang"))) |
| 308 | self.assertTrue( |
| 309 | os.path.exists( |
| 310 | os.path.join(self.llvm_src, "tools", "lld"))) |
| 311 | self.assertFalse( |
| 312 | os.path.exists( |
| 313 | os.path.join(self.llvm_src, "projects", "libcxx"))) |
| 314 | |
| 315 | def test_multiple_adds_removes(self): |
| 316 | """ |
| 317 | Test that we can have multiple --add and --remove options in the same |
| 318 | command and that only the last one of each kind matters. |
| 319 | """ |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 320 | output = self.run_with_output([self.python, |
| 321 | self.script, |
| 322 | self.env, |
| 323 | "projects", |
| 324 | "--add", |
| 325 | "libcxxabi", |
| 326 | "--remove", |
| 327 | "lld", |
| 328 | "lldb", |
| 329 | "--add", |
| 330 | "clang", |
| 331 | "libcxx", |
| 332 | "--remove", |
| 333 | "libunwind"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 334 | self.assertRegex(output, |
| 335 | "Projects linked:.*\n" + |
| 336 | ".*clang.*\n" + |
| 337 | ".*libcxx.*\n") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 338 | |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 339 | output = self.run_with_output([self.python, |
| 340 | self.script, |
| 341 | self.env, |
| 342 | "projects", |
| 343 | "--add", |
| 344 | "libunwind", |
| 345 | "libcxxabi", |
| 346 | "--remove", |
| 347 | "clang", |
| 348 | "libcxx", |
| 349 | "--add", |
| 350 | "compiler-rt", |
| 351 | "--remove", |
| 352 | "libcxxabi"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 353 | self.assertRegex(output, |
| 354 | "Projects linked:.*\n" + |
| 355 | ".*clang.*\n" + |
| 356 | ".*compiler-rt.*\n" + |
| 357 | ".*libcxx.*\n") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 358 | |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 359 | output = self.run_with_output([self.python, |
| 360 | self.script, |
| 361 | self.env, |
| 362 | "projects", |
| 363 | "--add", |
| 364 | "libcxx", |
| 365 | "--remove", |
| 366 | "lld", |
| 367 | "--add", |
| 368 | "lld", |
| 369 | "--remove", |
| 370 | "libcxx"]) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 371 | self.assertRegex(output, |
| 372 | "Projects linked:.*\n" + |
| 373 | ".*clang.*\n" + |
| 374 | ".*compiler-rt.*\n" + |
| 375 | ".*lld.*\n") |