Tests: Cloner.check_submodules_repo

Signed-off-by: Václav Valíček <valicek1994@gmail.com>
This commit is contained in:
Václav Valíček 2022-08-02 23:13:40 +02:00
parent a2bf978fde
commit 1f06d38f49
Signed by: valicek
GPG Key ID: FF05BDCA0C73BB31
2 changed files with 84 additions and 5 deletions

View File

@ -7,7 +7,6 @@
<mapping directory="$PROJECT_DIR$/tests/_support_data/test-repo-different-tags" vcs="Git" /> <mapping directory="$PROJECT_DIR$/tests/_support_data/test-repo-different-tags" vcs="Git" />
<mapping directory="$PROJECT_DIR$/tests/_support_data/test-repo-new-commits" vcs="Git" /> <mapping directory="$PROJECT_DIR$/tests/_support_data/test-repo-new-commits" vcs="Git" />
<mapping directory="$PROJECT_DIR$/tests/_support_data/test-repo-reduced" vcs="Git" /> <mapping directory="$PROJECT_DIR$/tests/_support_data/test-repo-reduced" vcs="Git" />
<mapping directory="$PROJECT_DIR$/tests/_support_data/test-repo-striped" vcs="Git" />
<mapping directory="$PROJECT_DIR$/tests/_support_data/test-repo-submodules" vcs="Git" /> <mapping directory="$PROJECT_DIR$/tests/_support_data/test-repo-submodules" vcs="Git" />
<mapping directory="$PROJECT_DIR$/tests/_support_data/test-repo-submodules-long" vcs="Git" /> <mapping directory="$PROJECT_DIR$/tests/_support_data/test-repo-submodules-long" vcs="Git" />
<mapping directory="$PROJECT_DIR$/tests/_support_data/test-repo-submodules-multilevel" vcs="Git" /> <mapping directory="$PROJECT_DIR$/tests/_support_data/test-repo-submodules-multilevel" vcs="Git" />
@ -19,12 +18,9 @@
<mapping directory="$PROJECT_DIR$/tests/_support_data/test-submodules-adhoc/submodules-level-two/test-repo-base" vcs="Git" /> <mapping directory="$PROJECT_DIR$/tests/_support_data/test-submodules-adhoc/submodules-level-two/test-repo-base" vcs="Git" />
<mapping directory="$PROJECT_DIR$/tests/_support_data/test-submodules-adhoc/submodules-level-two/test-repo-reduced" vcs="Git" /> <mapping directory="$PROJECT_DIR$/tests/_support_data/test-submodules-adhoc/submodules-level-two/test-repo-reduced" vcs="Git" />
<mapping directory="$PROJECT_DIR$/tests/_support_data/test-submodules-adhoc/submodules-root" vcs="Git" /> <mapping directory="$PROJECT_DIR$/tests/_support_data/test-submodules-adhoc/submodules-root" vcs="Git" />
<mapping directory="$PROJECT_DIR$/tests/_support_data/test-submodules-adhoc/submodules-root/submodule-failed" vcs="Git" />
<mapping directory="$PROJECT_DIR$/tests/_support_data/test-submodules-adhoc/submodules-root/submodule-failed-cfg" vcs="Git" />
<mapping directory="$PROJECT_DIR$/tests/_support_data/test-submodules-adhoc/submodules-root/submodules-level-two" vcs="Git" /> <mapping directory="$PROJECT_DIR$/tests/_support_data/test-submodules-adhoc/submodules-root/submodules-level-two" vcs="Git" />
<mapping directory="$PROJECT_DIR$/tests/_support_data/test-submodules-adhoc/submodules-root/test-repo-base" vcs="Git" /> <mapping directory="$PROJECT_DIR$/tests/_support_data/test-submodules-adhoc/submodules-root/test-repo-base" vcs="Git" />
<mapping directory="$PROJECT_DIR$/tests/_support_data/test-submodules-adhoc/submodules-root/test-repo-different-tags" vcs="Git" /> <mapping directory="$PROJECT_DIR$/tests/_support_data/test-submodules-adhoc/submodules-root/test-repo-different-tags" vcs="Git" />
<mapping directory="$PROJECT_DIR$/tests/_support_data/test-submodules-adhoc/submodules-root/test-repo-new-commits" vcs="Git" /> <mapping directory="$PROJECT_DIR$/tests/_support_data/test-submodules-adhoc/submodules-root/test-repo-new-commits" vcs="Git" />
<mapping directory="$PROJECT_DIR$/tests/_support_data/tool/non-bare-init.git" vcs="Git" />
</component> </component>
</project> </project>

View File

@ -4,7 +4,40 @@ from repo_cloner.lib import gen_repo_hashed_name, DirNotFoundError, Cloner
from cloner_test_fixtures import * from cloner_test_fixtures import *
from pathlib import Path from pathlib import Path
import logging import logging
from unittest.mock import patch, PropertyMock from unittest.mock import patch, PropertyMock, MagicMock, call
some_commits = [
"d766d9c302463257695a4d53d857a2cecd024414",
"a2e308bb9c5e59e7a0c319dca0adf3966f3f3a60",
"36021088abb6ebcb2202897f3f27b26b21c25068",
"e811211f0895e9e792f569b1b38f8452e0efe42f",
"6854c793fd2cbd848650a94b9fed4924e5e428b2",
"2f08263b5aff30ce776bb0350503be69d45647da",
"fc53c9d8bc9e01c63632376df51abb67ca344a89",
"c95a862af3d13623da3d7a6b73ff79d68ff2d7ad",
"b21c1e9e329bcdef1980462cf62c09a9e807dafd",
"dec7ff27d658412f0088c4c3220990f1f1dc98f1",
"ad66f820c9dfa06c3767f81cb42b1398fa61ff05",
"b9af12b9f80b372c645b5f64c5a8f8b6a148ccfc",
"1fda6025ed303ecf03b9afa7cce882aaddc058f9",
"d33585f0a72d9de6c481891787e4d956b769ccb5",
"7f685afca71731cb9276f937fc8cceca6b47b1f2",
"af112317aa2459e88db9cb10533ea6f29818afee",
"ddc24a34983ff43b61e1bdf27b7ec2357932e7e0",
"7ccca22ba367433eedb226310373fd250a2af725",
"e4843c6d2391d0b45beea81e0dd34078484b474f",
"fe942d46c33fee0e68d2e0ed64dfa6515dbf89c4",
"1d80638229e0a7ef62b3b9cdab9ad9f63acf6d38",
"f968ea00a3007034ddf196285d7b60fec5e7fcf0",
"490859dbebe02af6bd643a89b6897961674f90fd",
"066de679c906a78f7f817ee9a48d3021fd8b6b7c",
"2a8277687fb6dee742e6e0193ea95fcd2264fbc4",
"8808c649a8b0a5279a2d9a46b730254cad649205",
"ad5db175fab144a8851b712195733c4a9c00d699",
"36d45a4c1e5ad8c47a14881c5427045c3de095d0",
"6b0b9affedb3e3daa4df00cd54678d78ea1c1d94",
"aa5056610ff57f73bae9633a985c6a8e41f3bc23",
]
def mock_time() -> float: def mock_time() -> float:
@ -142,6 +175,56 @@ def test__main_repo_path(cloner_dir_struct: Path, path_repo_base: Path):
assert x == ds.repos_dir.joinpath(hashed).as_posix() assert x == ds.repos_dir.joinpath(hashed).as_posix()
def test_check_submodules_repo(tmp_path):
from collections import namedtuple
from repo_cloner.lib import DiskStoredList
def submodule_scanner_mock(commit = None):
if commit.hexsha in ["f968ea00a3007034ddf196285d7b60fec5e7fcf0", "36d45a4c1e5ad8c47a14881c5427045c3de095d0"]:
return ["https://git.hosting:namespace/repo2.git"]
if commit.hexsha in ["6b0b9affedb3e3daa4df00cd54678d78ea1c1d94", "aa5056610ff57f73bae9633a985c6a8e41f3bc23"]:
return ["https://git.hosting:namespace/repo2.git", "https://git.hosting:namespace/repo3.git"]
return False
repo_path = tmp_path.joinpath("repo.git").as_posix()
cache_path = tmp_path.joinpath("cache.list")
submodule_cache = tmp_path.joinpath("submodules").as_posix()
submodules = DiskStoredList(submodule_cache)
submodules.append("https://git.hosting:namespace/repo1.git")
Commit = namedtuple("Commit", ["hexsha"])
# 30 commits
mocked_commits = [Commit(commit) for commit in some_commits]
# write cache file with collected commits
with open(cache_path.as_posix(), "w") as f:
f.writelines([f"{commit}\n" for commit in some_commits[0:20]])
mocks = {
'initialized': PropertyMock(return_value = True),
"path": PropertyMock(return_value = repo_path),
"list_commits": MagicMock(return_value = mocked_commits),
"list_submodules": MagicMock(side_effect = submodule_scanner_mock),
}
# prepare mocks for RepoTool
with patch.multiple("repo_cloner.lib.repo_tool.RepoTool", **mocks):
import repo_cloner.lib.repo_tool
rt = repo_cloner.lib.repo_tool.RepoTool(tmp_path.as_posix())
Cloner.check_submodules_repo(rt, cache_path.as_posix(), submodules, 100)
assert rt.list_commits.call_args == call(100)
assert rt.list_submodules.call_count == 10
returned_submodules = [x for x in submodules]
assert returned_submodules == [
'https://git.hosting:namespace/repo1.git', 'https://git.hosting:namespace/repo2.git',
'https://git.hosting:namespace/repo3.git'
]
def test_sync(cloner_dir_struct, tmp_path, monkeypatch): def test_sync(cloner_dir_struct, tmp_path, monkeypatch):
called: bool = False called: bool = False