From 12c59f9b2339c9e34846ab0a0c547b5731b018a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Val=C3=AD=C4=8Dek?= Date: Thu, 4 Aug 2022 13:03:01 +0200 Subject: [PATCH] cloner.detector_run MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Václav Valíček --- repo_cloner/lib/cloner.py | 9 ++++++--- tests/lib/test_cloner.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/repo_cloner/lib/cloner.py b/repo_cloner/lib/cloner.py index aec4ad5..b9d2786 100644 --- a/repo_cloner/lib/cloner.py +++ b/repo_cloner/lib/cloner.py @@ -212,8 +212,11 @@ class Cloner: @property def detector_enabled(self) -> bool: + log.debug(f"Querying detector config file") return os.path.exists(os.path.join(self._dirs.conf_dir, self.__detector_cfg)) - def run_detector(self, callback: Callable[[DetectedCommit], None]): - detector = Detector(self.__main_repo_path, self._dirs.cache_dir, self._config.cloner_project_name) - detector.run(callback) + def detector_run(self, callback: Callable[[DetectedCommit], None]): + detector = Detector(Path(self.__main_repo_path), Path(self._dirs.cache_dir), self._config.cloner_project_name) + if detector.check_fingerprint(): + log.debug(f"Starting detector discovery") + detector.run(callback) diff --git a/tests/lib/test_cloner.py b/tests/lib/test_cloner.py index a2f6740..a53ffe1 100644 --- a/tests/lib/test_cloner.py +++ b/tests/lib/test_cloner.py @@ -495,3 +495,31 @@ def test_detector_enabled(cloner_dir_with_config): assert not cl.detector_enabled Path(cloner_dir_with_config).joinpath("config", "detector.cfg").touch() assert cl.detector_enabled + + +def test_detector_run(cloner_dir_with_config): + called = False + + def callback_test(commit): + nonlocal called + called = True + + def run_mock(callback): + callback("Anything") + + ds = MockDirStruct(cloner_dir_with_config) + ds.config.cloner_repo_url = "https://mock" + + mocks = { + 'check_fingerprint': MagicMock(side_effect = [False, True]), + 'run': MagicMock(side_effect = run_mock), + } + + with patch.multiple('repo_cloner.lib.cloner.Detector', **mocks): + cl = Cloner(ds) + cl.detector_run(callback_test) + assert not repo_cloner.lib.cloner.Detector.run.called + assert not called + cl.detector_run(callback_test) + assert repo_cloner.lib.cloner.Detector.run.called + assert called