Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 1 | import os |
| 2 | import unittest |
| 3 | import uuid |
| 4 | |
| 5 | from sh import git |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 6 | from unittest.mock import MagicMock, call |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 7 | |
| 8 | from linaropy.cd import cd |
| 9 | from linaropy.proj import Proj |
| 10 | from linaropy.git.clone import Clone |
| 11 | from linaropy.git.worktree import Worktree |
| 12 | |
| 13 | from modules.llvm import LLVMSourceConfig, LLVMSubproject |
| 14 | |
| 15 | |
| 16 | class TestLLVMSourceConfig(unittest.TestCase): |
| 17 | testdirprefix = "SourceConfigUT" |
| 18 | |
| 19 | def __create_dummy_commit(self): |
| 20 | filename = "file" + str(uuid.uuid4()) |
| 21 | open(filename, "a").close() |
| 22 | git("add", filename) |
| 23 | git("commit", "-m", "Branches without commits confuse git") |
| 24 | |
| 25 | def __create_dummy_repo(self, path): |
| 26 | if not os.path.exists(path): |
| 27 | os.makedirs(path) |
| 28 | |
| 29 | with cd(path): |
| 30 | git("init") |
| 31 | self.__create_dummy_commit() |
| 32 | |
| 33 | def __get_subproj_repo_path(self, subproj): |
| 34 | return os.path.join(self.originalLLVM.repodir, "..", subproj + "-repo") |
| 35 | |
| 36 | def __get_subproj_repo(self, subproj): |
| 37 | return Clone(self.proj, self.__get_subproj_repo_path(subproj)) |
| 38 | |
| 39 | def setUp(self): |
| 40 | # We're going to create a hierarchy with [llvm|clang|whatever]-repo |
| 41 | # containing dummy repos, and llvm-copy containing a worktree of |
| 42 | # llvm-repo |
| 43 | self.proj = Proj(prefix=TestLLVMSourceConfig.testdirprefix) |
| 44 | path = os.path.join(self.proj.projdir, "llvm-repo") |
| 45 | self.__create_dummy_repo(path) |
| 46 | self.originalLLVM = Clone(self.proj, path) |
| 47 | |
| 48 | subprojs = LLVMSubproject.get_all_subprojects() |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 49 | for subproj in list(subprojs.keys()): |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 50 | repo = self.__get_subproj_repo_path(subproj) |
| 51 | self.__create_dummy_repo(repo) |
| 52 | |
| 53 | self.temporaryLLVMbranch = "a-branch" |
| 54 | self.temporaryLLVM = Worktree.create( |
| 55 | self.proj, self.originalLLVM, os.path.join( |
| 56 | self.proj.projdir, "llvm-copy"), self.temporaryLLVMbranch) |
| 57 | |
| 58 | def tearDown(self): |
| 59 | self.proj.cleanup() |
| 60 | |
| 61 | def test_detect_enabled_all(self): |
| 62 | subprojs = LLVMSubproject.get_all_subprojects() |
| 63 | sourcePath = self.temporaryLLVM.repodir |
| 64 | |
| 65 | for subproj in subprojs: |
| 66 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 67 | worktree = Worktree.create( |
| 68 | self.proj, |
| 69 | self.__get_subproj_repo(subproj), |
| 70 | path, |
| 71 | self.temporaryLLVMbranch) |
| 72 | self.assertTrue(os.path.isdir(path), "Failed to create worktree") |
| 73 | |
| 74 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 75 | enabled = config.get_enabled_subprojects() |
| 76 | |
| 77 | self.assertEqual(set(subprojs), set(enabled), |
| 78 | "Expected %s but detected only %s" % |
| 79 | (str(set(subprojs)), str(enabled))) |
| 80 | |
| 81 | def test_detect_enabled_none(self): |
| 82 | sourcePath = self.temporaryLLVM.repodir |
| 83 | |
| 84 | path = os.path.join(sourcePath, "unrelated") |
| 85 | os.makedirs(path) |
| 86 | |
| 87 | path = os.path.join(sourcePath, "wrong", "place", "for", "lld") |
| 88 | os.makedirs(path) |
| 89 | |
| 90 | path = os.path.join(sourcePath, "projects", "clang") |
| 91 | os.makedirs(path) |
| 92 | |
| 93 | path = os.path.join(sourcePath, "tools", "libcxx") |
| 94 | os.makedirs(path) |
| 95 | |
| 96 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 97 | enabled = config.get_enabled_subprojects() |
| 98 | |
| 99 | self.assertEqual( |
| 100 | enabled, |
| 101 | [], |
| 102 | "Detected unexpected projects %s" % str(enabled) |
| 103 | ) |
| 104 | |
| 105 | def test_detect_enabled_some(self): |
| 106 | sourcePath = self.temporaryLLVM.repodir |
| 107 | subprojs = LLVMSubproject.get_all_subprojects() |
| 108 | |
| 109 | for subproj in ["lld", "libcxxabi", "clang"]: |
| 110 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 111 | worktree = Worktree.create( |
| 112 | self.proj, |
| 113 | self.__get_subproj_repo(subproj), |
| 114 | path, |
| 115 | self.temporaryLLVMbranch) |
| 116 | self.assertTrue(os.path.isdir(path), "Failed to create worktree") |
| 117 | |
| 118 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 119 | enabled = config.get_enabled_subprojects() |
| 120 | |
| 121 | self.assertTrue("lld" in enabled, "Failed to detect lld") |
| 122 | self.assertTrue("clang" in enabled, "Failed to detect clang") |
| 123 | self.assertTrue("libcxxabi" in enabled, |
| 124 | "Failed to detect libcxxabi") |
| 125 | |
| 126 | def test_detect_enabled_not_worktree(self): |
| 127 | sourcePath = self.temporaryLLVM.repodir |
| 128 | subprojs = LLVMSubproject.get_all_subprojects() |
| 129 | |
| 130 | path = subprojs["compiler-rt"].get_cmake_path(sourcePath) |
| 131 | os.makedirs(path) |
| 132 | |
| 133 | config = LLVMSourceConfig(self.proj, sourcePath) |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 134 | with self.assertRaises(EnvironmentError) as context: |
| 135 | config.get_enabled_subprojects() |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 136 | |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 137 | self.assertRegex( |
| 138 | str(context.exception), |
| 139 | ".*compiler-rt is not a worktree.*" |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 140 | ) |
| 141 | |
| 142 | def test_detect_enabled_wrong_branch(self): |
| 143 | sourcePath = self.temporaryLLVM.repodir |
| 144 | subprojs = LLVMSubproject.get_all_subprojects() |
| 145 | |
| 146 | path = subprojs["compiler-rt"].get_cmake_path(sourcePath) |
| 147 | branch = "different-than-" + self.temporaryLLVMbranch |
| 148 | worktree = Worktree.create( |
| 149 | self.proj, |
| 150 | self.__get_subproj_repo("compiler-rt"), |
| 151 | path, |
| 152 | branch) |
| 153 | self.assertTrue(os.path.isdir(path), "Failed to create worktree") |
| 154 | |
| 155 | config = LLVMSourceConfig(self.proj, sourcePath) |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 156 | with self.assertRaises(EnvironmentError) as context: |
| 157 | config.get_enabled_subprojects() |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 158 | |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 159 | self.assertRegex(str(context.exception), |
| 160 | "compiler-rt is on branch {}, but should be on {}".format(branch, |
| 161 | self.temporaryLLVMbranch)) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 162 | |
| 163 | def test_add_invalid_subproject(self): |
| 164 | config = LLVMSourceConfig(self.proj, self.temporaryLLVM.repodir) |
| 165 | subproj = "not-an-llvm-subproject" |
| 166 | subprojPath = self.originalLLVM.repodir # Dummy path |
| 167 | |
| 168 | with self.assertRaises(ValueError) as context: |
| 169 | config.update({subproj : Clone(self.proj, subprojPath)}) |
| 170 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 171 | self.assertRegex(str(context.exception), |
| 172 | "Unknown llvm subproject %s" % subproj) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 173 | |
| 174 | def test_add_each_subproject(self): |
| 175 | sourcePath = self.temporaryLLVM.repodir |
| 176 | |
| 177 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 178 | |
| 179 | subprojs = LLVMSubproject.get_all_subprojects() |
| 180 | for subproj in subprojs: |
| 181 | expectedPath = subprojs[subproj].get_cmake_path(sourcePath) |
| 182 | config.update({subproj : self.__get_subproj_repo(subproj)}) |
| 183 | self.assertTrue(os.path.isdir(expectedPath), |
| 184 | "Failed to add subproject %s" % subproj) |
| 185 | |
| 186 | def test_add_all_subprojects(self): |
| 187 | sourcePath = self.temporaryLLVM.repodir |
| 188 | |
| 189 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 190 | |
| 191 | to_add = {} |
| 192 | subprojs = LLVMSubproject.get_all_subprojects() |
| 193 | |
| 194 | for subproj in subprojs: |
| 195 | to_add[subproj] = self.__get_subproj_repo(subproj) |
| 196 | |
| 197 | config.update(to_add) |
| 198 | |
| 199 | for subproj in subprojs: |
| 200 | expectedPath = subprojs[subproj].get_cmake_path(sourcePath) |
| 201 | self.assertTrue(os.path.isdir(expectedPath), |
| 202 | "Failed to add subproject %s" % subproj) |
| 203 | |
| 204 | def test_add_some_subprojects(self): |
| 205 | sourcePath = self.temporaryLLVM.repodir |
| 206 | |
| 207 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 208 | |
| 209 | to_add = {} |
| 210 | to_add["clang"] = self.__get_subproj_repo("clang") |
| 211 | to_add["compiler-rt"] = self.__get_subproj_repo("compiler-rt") |
| 212 | |
| 213 | config.update(to_add) |
| 214 | |
| 215 | subprojs = LLVMSubproject.get_all_subprojects() |
| 216 | self.assertTrue( |
| 217 | os.path.isdir(subprojs["clang"].get_cmake_path(sourcePath)), |
| 218 | "Failed to add subproject clang") |
| 219 | self.assertTrue( |
| 220 | os.path.isdir(subprojs["compiler-rt"].get_cmake_path(sourcePath)), |
| 221 | "Failed to add subproject compiler-rt") |
| 222 | |
| 223 | def test_add_existing_subprojects(self): |
| 224 | sourcePath = self.temporaryLLVM.repodir |
| 225 | |
| 226 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 227 | |
| 228 | subprojs = LLVMSubproject.get_all_subprojects() |
| 229 | existingPath = subprojs["lldb"].get_cmake_path(sourcePath) |
| 230 | Worktree.create( |
| 231 | self.proj, |
| 232 | self.__get_subproj_repo("lldb"), |
| 233 | existingPath, |
| 234 | self.temporaryLLVMbranch) |
| 235 | |
| 236 | config.update({ "lldb" : self.__get_subproj_repo("lldb")}) |
| 237 | |
| 238 | # If we got this far, we're probably ok, but let's be pedantic and check |
| 239 | # that the subproject is still there |
| 240 | self.assertTrue(os.path.isdir(existingPath), |
| 241 | "Existing subproject vanished") |
| 242 | |
| 243 | def test_add_subproject_existing_branch(self): |
| 244 | """ |
| 245 | Test that we can add a subproject that already has the branch that LLVM |
| 246 | is on. This can happen for instance if we have added and then removed |
| 247 | the subproject and now we're trying to add it again. |
| 248 | """ |
| 249 | sourcePath = self.temporaryLLVM.repodir |
| 250 | |
| 251 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 252 | |
| 253 | clangRepo = self.__get_subproj_repo("clang") |
| 254 | with cd(clangRepo.repodir): |
| 255 | # Make sure that the branch that LLVM is on already exists in the |
| 256 | # clang repo as well. |
| 257 | git("checkout", "-b", self.temporaryLLVMbranch) |
| 258 | self.__create_dummy_commit() |
| 259 | git("checkout", "master") |
| 260 | |
| 261 | config.update( { "clang" : clangRepo }) |
| 262 | |
| 263 | subprojs = LLVMSubproject.get_all_subprojects() |
| 264 | path = subprojs["clang"].get_cmake_path(sourcePath) |
| 265 | self.assertTrue(os.path.isdir(path), "Failed to add subproject") |
| 266 | |
| 267 | def test_add_subproject_not_a_worktree(self): |
| 268 | """ |
| 269 | Test that we can't update a config to include a subproject that exists |
| 270 | but is not a worktree. |
| 271 | """ |
| 272 | sourcePath = self.temporaryLLVM.repodir |
| 273 | branch = "different-than-" + self.temporaryLLVMbranch |
| 274 | |
| 275 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 276 | |
| 277 | subprojs = LLVMSubproject.get_all_subprojects() |
| 278 | existingPath = subprojs["lldb"].get_cmake_path(sourcePath) |
| 279 | os.makedirs(existingPath) |
| 280 | |
| 281 | with self.assertRaises(EnvironmentError) as context: |
| 282 | config.update({ "lldb" : self.__get_subproj_repo("lldb")}) |
| 283 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 284 | self.assertRegex(str(context.exception), |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 285 | "{} is not a worktree.*" |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 286 | .format(existingPath)) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 287 | |
| 288 | # If we got this far, we're probably ok, but let's be pedantic and check |
| 289 | # that the subproject is still there |
| 290 | self.assertTrue(os.path.isdir(existingPath), |
| 291 | "Existing subproject vanished") |
| 292 | |
| 293 | def test_add_subproject_wrong_branch(self): |
| 294 | """ |
| 295 | Test that we can't update a config to include a subproject that exists |
| 296 | but is on the wrong branch. |
| 297 | """ |
| 298 | sourcePath = self.temporaryLLVM.repodir |
| 299 | branch = "different-than-" + self.temporaryLLVMbranch |
| 300 | |
| 301 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 302 | |
| 303 | subprojs = LLVMSubproject.get_all_subprojects() |
| 304 | existingPath = subprojs["lldb"].get_cmake_path(sourcePath) |
| 305 | Worktree.create( |
| 306 | self.proj, |
| 307 | self.__get_subproj_repo("lldb"), |
| 308 | existingPath, |
| 309 | branch) |
| 310 | |
| 311 | with self.assertRaises(EnvironmentError) as context: |
| 312 | config.update({ "lldb" : self.__get_subproj_repo("lldb")}) |
| 313 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 314 | self.assertRegex(str(context.exception), |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 315 | "lldb is on branch {}, but should be on {}.*" |
| 316 | .format(branch, self.temporaryLLVMbranch)) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 317 | |
| 318 | # If we got this far, we're probably ok, but let's be pedantic and check |
| 319 | # that the subproject is still there |
| 320 | self.assertTrue(os.path.isdir(existingPath), |
| 321 | "Existing subproject vanished") |
| 322 | |
| 323 | def test_remove_subproject(self): |
| 324 | sourcePath = self.temporaryLLVM.repodir |
| 325 | |
| 326 | subprojs = LLVMSubproject.get_all_subprojects() |
| 327 | |
| 328 | lldPath = subprojs["lld"].get_cmake_path(sourcePath) |
| 329 | lldWorktree = Worktree.create( |
| 330 | self.proj, |
| 331 | self.__get_subproj_repo("lld"), |
| 332 | lldPath, |
| 333 | self.temporaryLLVMbranch) |
| 334 | |
| 335 | clangPath = subprojs["clang"].get_cmake_path(sourcePath) |
| 336 | clangWorktree = Worktree.create( |
| 337 | self.proj, |
| 338 | self.__get_subproj_repo("clang"), |
| 339 | clangPath, |
| 340 | self.temporaryLLVMbranch) |
| 341 | |
| 342 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 343 | config.update(remove=["lld"]) |
| 344 | |
| 345 | self.assertFalse(os.path.isdir(lldPath), "Failed to remove subproject") |
| 346 | self.assertTrue(os.path.isdir(clangPath), "Removed sibling subproject") |
| 347 | |
| 348 | def test_remove_some_subprojects(self): |
| 349 | sourcePath = self.temporaryLLVM.repodir |
| 350 | |
| 351 | subprojs = LLVMSubproject.get_all_subprojects() |
| 352 | |
| 353 | for subproj in ["clang", "compiler-rt", "lld", "lldb", "libunwind"]: |
| 354 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 355 | worktree = Worktree.create( |
| 356 | self.proj, |
| 357 | self.__get_subproj_repo(subproj), |
| 358 | path, |
| 359 | self.temporaryLLVMbranch) |
| 360 | |
| 361 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 362 | config.update(remove=["compiler-rt", "lld"]) |
| 363 | |
| 364 | for subproj in ["compiler-rt", "lld"]: |
| 365 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 366 | self.assertFalse( |
| 367 | os.path.isdir(path), |
| 368 | "Failed to remove subproject") |
| 369 | |
| 370 | for subproj in ["clang", "lldb", "libunwind"]: |
| 371 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 372 | self.assertTrue(os.path.isdir(path), "Removed sibling subproject") |
| 373 | |
| 374 | def test_remove_all_subprojects(self): |
| 375 | sourcePath = self.temporaryLLVM.repodir |
| 376 | |
| 377 | subprojs = LLVMSubproject.get_all_subprojects() |
| 378 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 379 | for subproj in list(subprojs.keys()): |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 380 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 381 | worktree = Worktree.create( |
| 382 | self.proj, |
| 383 | self.__get_subproj_repo(subproj), |
| 384 | path, |
| 385 | self.temporaryLLVMbranch) |
| 386 | self.assertTrue(os.path.isdir(path), "Failed to create worktree") |
| 387 | |
| 388 | config = LLVMSourceConfig(self.proj, sourcePath) |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 389 | config.update(remove=list(subprojs.keys())) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 390 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 391 | for subproj in list(subprojs.keys()): |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 392 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 393 | self.assertFalse( |
| 394 | os.path.isdir(path), |
| 395 | "Failed to remove subproject") |
| 396 | |
| 397 | def test_remove_each_subproject(self): |
| 398 | sourcePath = self.temporaryLLVM.repodir |
| 399 | |
| 400 | subprojs = LLVMSubproject.get_all_subprojects() |
| 401 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 402 | for subproj in list(subprojs.keys()): |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 403 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 404 | worktree = Worktree.create( |
| 405 | self.proj, |
| 406 | self.__get_subproj_repo(subproj), |
| 407 | path, |
| 408 | self.temporaryLLVMbranch) |
| 409 | self.assertTrue(os.path.isdir(path), "Failed to create worktree") |
| 410 | |
| 411 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 412 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 413 | for subproj in list(subprojs.keys()): |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 414 | config.update(remove=[subproj]) |
| 415 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 416 | for subproj in list(subprojs.keys()): |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 417 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 418 | self.assertFalse( |
| 419 | os.path.isdir(path), |
| 420 | "Failed to remove subproject") |
| 421 | |
| 422 | def test_remove_duplicates(self): |
| 423 | sourcePath = self.temporaryLLVM.repodir |
| 424 | |
| 425 | subprojs = LLVMSubproject.get_all_subprojects() |
| 426 | |
| 427 | for subproj in ["clang", "compiler-rt", "lld", "lldb", "libunwind"]: |
| 428 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 429 | worktree = Worktree.create( |
| 430 | self.proj, |
| 431 | self.__get_subproj_repo(subproj), |
| 432 | path, |
| 433 | self.temporaryLLVMbranch) |
| 434 | |
| 435 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 436 | config.update(remove=["compiler-rt", "lld", "lld", "compiler-rt"]) |
| 437 | |
| 438 | for subproj in ["compiler-rt", "lld"]: |
| 439 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 440 | self.assertFalse( |
| 441 | os.path.isdir(path), |
| 442 | "Failed to remove subproject") |
| 443 | |
| 444 | for subproj in ["clang", "lldb", "libunwind"]: |
| 445 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 446 | self.assertTrue(os.path.isdir(path), "Removed sibling subproject") |
| 447 | |
| 448 | def test_remove_invalid_subproject(self): |
| 449 | sourcePath = self.temporaryLLVM.repodir |
| 450 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 451 | |
| 452 | subproj = "not-an-llvm-subproject" |
| 453 | |
| 454 | with self.assertRaises(ValueError) as context: |
| 455 | config.update(remove=[subproj]) |
| 456 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 457 | self.assertRegex(str(context.exception), |
| 458 | "Unknown llvm subproject %s" % subproj) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 459 | |
| 460 | def test_remove_inexistent_subproject(self): |
| 461 | sourcePath = self.temporaryLLVM.repodir |
| 462 | |
| 463 | subprojs = LLVMSubproject.get_all_subprojects() |
| 464 | |
| 465 | lldPath = subprojs["lld"].get_cmake_path(sourcePath) |
| 466 | lldWorktree = Worktree.create( |
| 467 | self.proj, |
| 468 | self.__get_subproj_repo("lld"), |
| 469 | lldPath, |
| 470 | self.temporaryLLVMbranch) |
| 471 | |
| 472 | clangPath = subprojs["clang"].get_cmake_path(sourcePath) |
| 473 | |
| 474 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 475 | config.update(remove=["clang"]) |
| 476 | |
| 477 | self.assertFalse( |
| 478 | os.path.isdir(clangPath), |
| 479 | "Failed to remove subproject") |
| 480 | self.assertTrue(os.path.isdir(lldPath), "Removed sibling subproject") |
| 481 | |
| 482 | def test_add_after_remove(self): |
| 483 | sourcePath = self.temporaryLLVM.repodir |
| 484 | |
| 485 | subprojs = LLVMSubproject.get_all_subprojects() |
| 486 | lldbRepo = self.__get_subproj_repo("lldb") |
| 487 | |
| 488 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 489 | |
| 490 | config.update({"lldb" : lldbRepo}) |
| 491 | self.assertTrue( |
| 492 | os.path.isdir(subprojs["lldb"].get_cmake_path(sourcePath)), |
| 493 | "Failed to add lldb") |
| 494 | |
| 495 | config.update(remove=["lldb"]) |
| 496 | self.assertFalse( |
| 497 | os.path.isdir(subprojs["lldb"].get_cmake_path(sourcePath)), |
| 498 | "Failed to remove lldb") |
| 499 | |
| 500 | config.update({"lldb" : lldbRepo }) |
| 501 | self.assertTrue( |
| 502 | os.path.isdir(subprojs["lldb"].get_cmake_path(sourcePath)), |
| 503 | "Failed to add lldb") |
| 504 | |
| 505 | def test_mixed_adds_removes(self): |
| 506 | sourcePath = self.temporaryLLVM.repodir |
| 507 | |
| 508 | subprojs = LLVMSubproject.get_all_subprojects() |
| 509 | |
| 510 | for subproj in ["clang", "compiler-rt", "lld", "lldb", "libunwind"]: |
| 511 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 512 | worktree = Worktree.create( |
| 513 | self.proj, |
| 514 | self.__get_subproj_repo(subproj), |
| 515 | path, |
| 516 | self.temporaryLLVMbranch) |
| 517 | |
| 518 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 519 | config.update({ |
| 520 | "libcxx" : self.__get_subproj_repo("libcxx"), |
| 521 | "libcxxabi" : self.__get_subproj_repo("libcxxabi")}, |
| 522 | ["compiler-rt", "lld"]) |
| 523 | |
| 524 | for subproj in ["libcxx", "libcxxabi"]: |
| 525 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 526 | self.assertTrue(os.path.isdir(path), "Failed to add subproject") |
| 527 | |
| 528 | for subproj in ["compiler-rt", "lld"]: |
| 529 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 530 | self.assertFalse( |
| 531 | os.path.isdir(path), |
| 532 | "Failed to remove subproject") |
| 533 | |
| 534 | for subproj in ["clang", "lldb", "libunwind"]: |
| 535 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 536 | self.assertTrue(os.path.isdir(path), "Removed sibling subproject") |
| 537 | |
| 538 | def test_simultaneous_add_remove(self): |
| 539 | sourcePath = self.temporaryLLVM.repodir |
| 540 | subprojs = LLVMSubproject.get_all_subprojects() |
| 541 | |
| 542 | clangRepo = self.__get_subproj_repo("clang") |
| 543 | lldRepo = self.__get_subproj_repo("lld") |
| 544 | libunwindRepo = self.__get_subproj_repo("libunwind") |
| 545 | |
| 546 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 547 | with self.assertRaises(ValueError) as context: |
| 548 | config.update( |
| 549 | { "clang" : clangRepo, "lld" : lldRepo, "libunwind" : |
| 550 | libunwindRepo}, ["libcxx", "lld", "libcxxabi"]) |
| 551 | |
| 552 | self.assertEqual(str(context.exception), |
| 553 | "Can't add and remove lld at the same time") |
| 554 | |
| 555 | # Make sure we didn't add any of the others either |
| 556 | for subproj in ["clang", "libunwind"]: |
| 557 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 558 | self.assertFalse( |
| 559 | os.path.isdir(path), |
| 560 | "Incorrectly added subproject") |
| 561 | |
| 562 | # TODO: test with a different dictionary than the default one (not |
| 563 | # necessarily containing subprojects - it can contain "potato", "banana" and |
| 564 | # "gazpacho" for all we care); in fact, it would probably be best to move the |
| 565 | # existing tests to that... |
| 566 | |
| 567 | # TODO: test that CMake gets our layout |
| 568 | |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 569 | def test_for_each_enabled(self): |
| 570 | sourcePath = self.temporaryLLVM.repodir |
| 571 | |
| 572 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 573 | |
| 574 | logPath = unittest.mock.MagicMock() |
| 575 | config.for_each_enabled(logPath) |
| 576 | |
| 577 | logPath.assert_called_with(self.temporaryLLVM.repodir) |
| 578 | |
Diana Picus | 5fec8b7 | 2017-11-27 16:09:58 +0100 | [diff] [blame^] | 579 | subprojs = config.subprojs |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 580 | enabled = ["clang", "compiler-rt", "lld", "lldb", "libunwind"] |
| 581 | calls = [] |
| 582 | |
| 583 | for subproj in enabled: |
| 584 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 585 | worktree = Worktree.create( |
| 586 | self.proj, |
| 587 | self.__get_subproj_repo(subproj), |
| 588 | path, |
| 589 | self.temporaryLLVMbranch) |
| 590 | calls.append(call(path)) # Expect our mock to be called with path |
| 591 | |
| 592 | logPath = unittest.mock.MagicMock() |
| 593 | config.for_each_enabled(logPath) |
| 594 | |
| 595 | logPath.assert_has_calls(calls, any_order=True) |
| 596 | |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 597 | def test_for_each_enabled_error(self): |
| 598 | """Test that we rethrow exceptions correctly.""" |
| 599 | sourcePath = self.temporaryLLVM.repodir |
| 600 | |
| 601 | config = LLVMSourceConfig(self.proj, sourcePath) |
Diana Picus | 5fec8b7 | 2017-11-27 16:09:58 +0100 | [diff] [blame^] | 602 | subprojs = config.subprojs |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 603 | enabled = ["clang", "compiler-rt", "lld", "lldb", "libunwind"] |
| 604 | |
| 605 | for subproj in enabled: |
| 606 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 607 | worktree = Worktree.create( |
| 608 | self.proj, |
| 609 | self.__get_subproj_repo(subproj), |
| 610 | path, |
| 611 | self.temporaryLLVMbranch) |
| 612 | |
Diana Picus | 5fec8b7 | 2017-11-27 16:09:58 +0100 | [diff] [blame^] | 613 | def throw(projPath): |
| 614 | if "lld" in projPath: |
| 615 | raise ValueError("An error has been!!1") |
| 616 | |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 617 | with self.assertRaises(RuntimeError) as context: |
Diana Picus | 5fec8b7 | 2017-11-27 16:09:58 +0100 | [diff] [blame^] | 618 | config.for_each_enabled(throw) |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 619 | self.assertRegex(str(context.exception), |
Diana Picus | 5fec8b7 | 2017-11-27 16:09:58 +0100 | [diff] [blame^] | 620 | "Error while processing lld(.*\n)*") |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 621 | self.assertRegex(str(context.exception.__cause__), |
Diana Picus | 5fec8b7 | 2017-11-27 16:09:58 +0100 | [diff] [blame^] | 622 | "An error has been!!1(.*\n)*") |
| 623 | |
| 624 | def test_for_each_subproj(self): |
| 625 | sourcePath = self.temporaryLLVM.repodir |
| 626 | |
| 627 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 628 | |
| 629 | subprojs = config.subprojs |
| 630 | enabled = ["clang", "compiler-rt", "lld", "lldb", "libunwind"] |
| 631 | calls = [] |
| 632 | |
| 633 | for subproj in enabled: |
| 634 | # Make sure subproj looks enabled |
| 635 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 636 | worktree = Worktree.create( |
| 637 | self.proj, |
| 638 | self.__get_subproj_repo(subproj), |
| 639 | path, |
| 640 | self.temporaryLLVMbranch) |
| 641 | # Expect our mock to be called with the proper subproj and |
| 642 | # enabled == True |
| 643 | calls.append(call(subprojs[subproj], True)) |
| 644 | |
| 645 | for subproj in set(subprojs.keys()) - set(enabled): |
| 646 | # Expect our mock to be called with the proper subproj and |
| 647 | # enabled == False |
| 648 | calls.append(call(subprojs[subproj], False)) |
| 649 | |
| 650 | try: |
| 651 | action = MagicMock() |
| 652 | config.for_each_subproj(action) |
| 653 | except: |
| 654 | print("Exception during test?! ") |
| 655 | |
| 656 | action.assert_has_calls(calls, any_order=True) |
| 657 | |
| 658 | def test_for_each_subproj_error(self): |
| 659 | """Test that we rethrow exceptions correctly.""" |
| 660 | sourcePath = self.temporaryLLVM.repodir |
| 661 | |
| 662 | config = LLVMSourceConfig(self.proj, sourcePath) |
| 663 | subprojs = config.subprojs |
| 664 | enabled = ["clang", "compiler-rt", "lld", "lldb", "libunwind"] |
| 665 | |
| 666 | for subproj in enabled: |
| 667 | path = subprojs[subproj].get_cmake_path(sourcePath) |
| 668 | worktree = Worktree.create( |
| 669 | self.proj, |
| 670 | self.__get_subproj_repo(subproj), |
| 671 | path, |
| 672 | self.temporaryLLVMbranch) |
| 673 | |
| 674 | def throw_enabled(subproj, enabled): |
| 675 | # Throw for one of the enabled projects (e.g. lld) |
| 676 | if "lld" in subproj.cmake_path: |
| 677 | raise ValueError("An error has been!!1") |
| 678 | |
| 679 | def throw_disabled(subproj, enabled): |
| 680 | # Throw for one of the disabled projects (e.g. libcxx) |
| 681 | if "libcxx" in subproj.cmake_path: |
| 682 | raise ValueError("An error has been!!1") |
| 683 | |
| 684 | with self.assertRaises(RuntimeError) as context: |
| 685 | config.for_each_subproj(throw_enabled) |
| 686 | self.assertRegex(str(context.exception), |
| 687 | "Error while processing lld(.*\n)*") |
| 688 | self.assertRegex(str(context.exception.__cause__), |
| 689 | "An error has been!!1(.*\n)*") |
| 690 | |
| 691 | with self.assertRaises(RuntimeError) as context: |
| 692 | config.for_each_subproj(throw_disabled) |
| 693 | self.assertRegex(str(context.exception), |
| 694 | "Error while processing libcxx(.*\n)*") |
| 695 | self.assertRegex(str(context.exception.__cause__), |
| 696 | "An error has been!!1(.*\n)*") |
| 697 | |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 698 | |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 699 | if __name__ == "__main__": |
| 700 | unittest.main() |