diff --git a/tests/lib/test_cloner.py b/tests/lib/test_cloner.py index 1e961c5..7691856 100644 --- a/tests/lib/test_cloner.py +++ b/tests/lib/test_cloner.py @@ -1,5 +1,7 @@ import git import pytest + +import repo_cloner.lib.cloner from repo_cloner.lib import gen_repo_hashed_name, DirNotFoundError, Cloner from cloner_test_fixtures import * from pathlib import Path @@ -282,6 +284,70 @@ def test_sync_submodules_failed(cloner_dir_with_config, caplog): assert caplog.records[1].message == "Repo fetch failed for Mocked Project" +def my_check_submodules_repo( + repo_tool, cache_file: str, + submodule_list: list, + scan_depth = None): + if "namespace_whatever_" in cache_file: + submodule_list.append("https://git.hosting:namespace/submodule1.git") + submodule_list.append("https://git.hosting:namespace/submodule2.git") + if "/namespace_submodule1_" in cache_file: + submodule_list.append("https://git.hosting:namespace/submodule3.git") + return submodule_list + + +def test_submodules_sync_succeed(cloner_dir_with_config, caplog): + cloner_dir_with_config.joinpath("cache", "submodules").mkdir(parents = True) + cloner_dir_with_config.joinpath("cache", "submodules", "submodules.cache") \ + .write_text("https://git.hosting:previous/submodule.git") + ds = MockDirStruct(cloner_dir_with_config) + ds.config.cloner_repo_url = "https://git.hosting:namespace/whatever.git" + ds.config.cloner_submodules = True + + # mock almost everything + mocks = { + 'initialized': PropertyMock(side_effect = [True, False, True, True, True]), + 'fetch': MagicMock(return_value = True), + 'clone': MagicMock(return_value = True), + } + + with patch.multiple("repo_cloner.lib.cloner.RepoTool", **mocks): + Cloner.check_submodules_repo = my_check_submodules_repo + cl = Cloner(ds) + assert cl.sync() + + assert repo_cloner.lib.cloner.RepoTool.fetch.call_count == 4 + assert repo_cloner.lib.cloner.RepoTool.clone.call_count == 1 + + +def test_submodules_sync_one_fail(cloner_dir_with_config, caplog): + cloner_dir_with_config.joinpath("cache", "submodules").mkdir(parents = True) + cloner_dir_with_config.joinpath("cache", "submodules", "submodules.cache") \ + .write_text("https://git.hosting:previous/submodule.git") + ds = MockDirStruct(cloner_dir_with_config) + ds.config.cloner_repo_url = "https://git.hosting:namespace/whatever.git" + ds.config.cloner_submodules = True + # just to make coverage nicer + ds.config.cloner_submodule_depth = 0 + + # mock almost everything + mocks = { + 'initialized': PropertyMock(return_value = True), + 'fetch': MagicMock(side_effect = [True, True, True, False, True]), + 'clone': MagicMock(return_value = True), + } + + with patch.multiple("repo_cloner.lib.cloner.RepoTool", **mocks): + Cloner.check_submodules_repo = my_check_submodules_repo + cl = Cloner(ds) + assert not cl.sync() + + assert repo_cloner.lib.cloner.RepoTool.fetch.call_count == 5 + assert repo_cloner.lib.cloner.RepoTool.clone.call_count == 0 + + assert "Clone/fetch of submodule: https://git.hosting:namespace/submodule2.git failed" in caplog.text + + def test_perform_check(cloner_dir_with_config, monkeypatch, caplog): call_counter: int = 0