diff --git a/swh/indexer/indexer.py b/swh/indexer/indexer.py --- a/swh/indexer/indexer.py +++ b/swh/indexer/indexer.py @@ -310,12 +310,11 @@ A summary Dict of the task's status """ - status = "uneventful" sha1s = [ hashutil.hash_to_bytes(id_) if isinstance(id_, str) else id_ for id_ in ids ] results = [] - summary: Dict = {} + summary: Dict = {"status": "uneventful"} try: for sha1 in sha1s: try: @@ -329,17 +328,15 @@ res = self.index(sha1, raw_content, **kwargs) if res: # If no results, skip it results.append(res) - status = "eventful" + summary["status"] = "eventful" summary = self.persist_index_computations(results, policy_update) self.results = results except Exception: if not self.catch_exceptions: raise self.log.exception("Problem when reading contents metadata.") - status = "failed" - finally: - summary["status"] = status - return summary + summary["status"] = "failed" + return summary class ContentPartitionIndexer(BaseIndexer): diff --git a/swh/indexer/tests/test_indexer.py b/swh/indexer/tests/test_indexer.py new file mode 100644 --- /dev/null +++ b/swh/indexer/tests/test_indexer.py @@ -0,0 +1,40 @@ +# Copyright (C) 2020 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +from typing import Any, Dict, Optional, Union + +import pytest + +from swh.indexer.indexer import ContentIndexer +from swh.model.model import Revision + +from .utils import BASE_TEST_CONFIG + + +class TestException(Exception): + pass + + +class CrashingIndexer(ContentIndexer): + USE_TOOLS = False + + def index( + self, id: Union[bytes, Dict, Revision], data: Optional[bytes] = None, **kwargs + ) -> Dict[str, Any]: + pass + + def persist_index_computations(self, results, policy_update) -> Dict[str, int]: + raise TestException() + + +def test_catch_exceptions(): + indexer = CrashingIndexer(config=BASE_TEST_CONFIG) + + assert indexer.run([b"foo"], policy_update=True) == {"status": "failed"} + + indexer.catch_exceptions = False + + with pytest.raises(TestException): + indexer.run([b"foo"], policy_update=True)