aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiana Picus <diana.picus@linaro.org>2017-11-27 16:09:58 +0100
committerDiana Picus <diana.picus@linaro.org>2017-12-15 12:21:47 +0100
commit5fec8b75ddf572779af5dc2a639c8eb15c6bfc9f (patch)
tree7e382dd57cd716a504a48303c8a2410352ed228a
parentadb07c441e2a96996c42142859b97b1baf7033ed (diff)
downloadlinaro-scripts-5fec8b75ddf572779af5dc2a639c8eb15c6bfc9f.tar.gz
Add for_each_subproj in LLVMSourceConfig
Add a method that runs an action on every subproject known by the source config. The action will be passed the subproject and a boolean indicating whether or not it is enabled. Change-Id: Ief7b67364143e40de103880fe1fefb4bdb4e93cf
-rw-r--r--modules/llvm.py16
-rw-r--r--tests/unittests/testllvmsourceconfig.py93
2 files changed, 100 insertions, 9 deletions
diff --git a/modules/llvm.py b/modules/llvm.py
index 2ce4994..8d549fd 100644
--- a/modules/llvm.py
+++ b/modules/llvm.py
@@ -165,6 +165,22 @@ class LLVMSourceConfig(object):
# Visit LLVM last, in case getting the enabled subprojects errors out.
action(self.llvmSourceTree.repodir)
+ def for_each_subproj(self, action):
+ """Perform the given action for each subproject excluding LLVM.
+
+ The action must be a callable object receiving an LLVMSubproject
+ parameter and a boolean representing whether the subproject is enabled
+ in the current configuration or not.
+
+ If the action throws an exception, it will be rethrown as a
+ RuntimeError. Note that this does not have transactional behaviour.
+ """
+ for subprojName, subproj in self.subprojs.items():
+ try:
+ action(subproj, self.__is_enabled(subprojName))
+ except Exception as exc:
+ raise RuntimeError("Error while processing {}".format(subprojName)) from exc
+
def __get_subproj_object(self, subprojName):
"""Get the LLVMSubproject object corresponding to subprojName."""
if not subprojName in list(self.subprojs.keys()):
diff --git a/tests/unittests/testllvmsourceconfig.py b/tests/unittests/testllvmsourceconfig.py
index 8a60065..2c69fa3 100644
--- a/tests/unittests/testllvmsourceconfig.py
+++ b/tests/unittests/testllvmsourceconfig.py
@@ -576,7 +576,7 @@ class TestLLVMSourceConfig(unittest.TestCase):
logPath.assert_called_with(self.temporaryLLVM.repodir)
- subprojs = LLVMSubproject.get_all_subprojects()
+ subprojs = config.subprojs
enabled = ["clang", "compiler-rt", "lld", "lldb", "libunwind"]
calls = []
@@ -594,16 +594,12 @@ class TestLLVMSourceConfig(unittest.TestCase):
logPath.assert_has_calls(calls, any_order=True)
- def _throw(self, projPath):
- if "lld" in projPath:
- raise ValueError("An error has been!!1")
-
def test_for_each_enabled_error(self):
"""Test that we rethrow exceptions correctly."""
sourcePath = self.temporaryLLVM.repodir
config = LLVMSourceConfig(self.proj, sourcePath)
- subprojs = LLVMSubproject.get_all_subprojects()
+ subprojs = config.subprojs
enabled = ["clang", "compiler-rt", "lld", "lldb", "libunwind"]
for subproj in enabled:
@@ -614,12 +610,91 @@ class TestLLVMSourceConfig(unittest.TestCase):
path,
self.temporaryLLVMbranch)
+ def throw(projPath):
+ if "lld" in projPath:
+ raise ValueError("An error has been!!1")
+
with self.assertRaises(RuntimeError) as context:
- config.for_each_enabled(self._throw)
+ config.for_each_enabled(throw)
self.assertRegex(str(context.exception),
- "(.*\n?)*Error while processing lld(.*\n)*")
+ "Error while processing lld(.*\n)*")
self.assertRegex(str(context.exception.__cause__),
- "(.*\n?)*An error has been!!1(.*\n)*")
+ "An error has been!!1(.*\n)*")
+
+ def test_for_each_subproj(self):
+ sourcePath = self.temporaryLLVM.repodir
+
+ config = LLVMSourceConfig(self.proj, sourcePath)
+
+ subprojs = config.subprojs
+ enabled = ["clang", "compiler-rt", "lld", "lldb", "libunwind"]
+ calls = []
+
+ for subproj in enabled:
+ # Make sure subproj looks enabled
+ path = subprojs[subproj].get_cmake_path(sourcePath)
+ worktree = Worktree.create(
+ self.proj,
+ self.__get_subproj_repo(subproj),
+ path,
+ self.temporaryLLVMbranch)
+ # Expect our mock to be called with the proper subproj and
+ # enabled == True
+ calls.append(call(subprojs[subproj], True))
+
+ for subproj in set(subprojs.keys()) - set(enabled):
+ # Expect our mock to be called with the proper subproj and
+ # enabled == False
+ calls.append(call(subprojs[subproj], False))
+
+ try:
+ action = MagicMock()
+ config.for_each_subproj(action)
+ except:
+ print("Exception during test?! ")
+
+ action.assert_has_calls(calls, any_order=True)
+
+ def test_for_each_subproj_error(self):
+ """Test that we rethrow exceptions correctly."""
+ sourcePath = self.temporaryLLVM.repodir
+
+ config = LLVMSourceConfig(self.proj, sourcePath)
+ subprojs = config.subprojs
+ enabled = ["clang", "compiler-rt", "lld", "lldb", "libunwind"]
+
+ for subproj in enabled:
+ path = subprojs[subproj].get_cmake_path(sourcePath)
+ worktree = Worktree.create(
+ self.proj,
+ self.__get_subproj_repo(subproj),
+ path,
+ self.temporaryLLVMbranch)
+
+ def throw_enabled(subproj, enabled):
+ # Throw for one of the enabled projects (e.g. lld)
+ if "lld" in subproj.cmake_path:
+ raise ValueError("An error has been!!1")
+
+ def throw_disabled(subproj, enabled):
+ # Throw for one of the disabled projects (e.g. libcxx)
+ if "libcxx" in subproj.cmake_path:
+ raise ValueError("An error has been!!1")
+
+ with self.assertRaises(RuntimeError) as context:
+ config.for_each_subproj(throw_enabled)
+ self.assertRegex(str(context.exception),
+ "Error while processing lld(.*\n)*")
+ self.assertRegex(str(context.exception.__cause__),
+ "An error has been!!1(.*\n)*")
+
+ with self.assertRaises(RuntimeError) as context:
+ config.for_each_subproj(throw_disabled)
+ self.assertRegex(str(context.exception),
+ "Error while processing libcxx(.*\n)*")
+ self.assertRegex(str(context.exception.__cause__),
+ "An error has been!!1(.*\n)*")
+
if __name__ == "__main__":
unittest.main()