Skip to content

Commit

Permalink
Fix cloning of add-on store repository (#5701)
Browse files Browse the repository at this point in the history
* Fix cloning of add-on store repository

Since #5669, the add-on store reset no longer deletes the root
directory. However, if the root directory is not present, the current
code no longer invokes cloning, instead tries to load the git
repository directly.

With this change, the code clones whenever there is no .git directory,
which works for both cases.

* Fix pytest
  • Loading branch information
agners authored Mar 1, 2025
1 parent 86133f8 commit d42ec12
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion supervisor/store/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def branch(self) -> str:

async def load(self) -> None:
"""Init Git add-on repository."""
if not self.path.is_dir():
if not (self.path / ".git").is_dir():
await self.clone()
return

Expand Down
44 changes: 29 additions & 15 deletions tests/store/test_repository_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,30 @@ async def test_git_clone_error(

async def test_git_load(coresys: CoreSys, tmp_path: Path):
"""Test git load."""
repo = GitRepo(coresys, tmp_path, REPO_URL)
repo_dir = tmp_path / "repo"
repo = GitRepo(coresys, repo_dir, REPO_URL)
repo.clone = AsyncMock()

with (
patch("pathlib.Path.is_dir", return_value=True),
patch.object(
GitRepo, "sys_run_in_executor", new_callable=AsyncMock
) as run_in_executor,
):
await repo.load()
# Test with non-existing git repo root directory
await repo.load()
assert repo.clone.call_count == 1

repo.clone.reset_mock()

assert run_in_executor.call_count == 2
# Test with existing git repo root directory, but empty
repo_dir.mkdir()
await repo.load()
assert repo.clone.call_count == 1

repo.clone.reset_mock()

# Pretend we have a repo
(repo_dir / ".git").mkdir()

with patch("git.Repo") as mock_repo:
await repo.load()
assert repo.clone.call_count == 0
assert mock_repo.call_count == 1


@pytest.mark.parametrize(
Expand All @@ -87,21 +100,22 @@ async def test_git_load(coresys: CoreSys, tmp_path: Path):
NoSuchPathError(),
GitCommandError("init"),
UnicodeDecodeError("decode", b"", 0, 0, ""),
[AsyncMock(), GitCommandError("fsck")],
GitCommandError("fsck"),
],
)
async def test_git_load_error(coresys: CoreSys, tmp_path: Path, git_errors: Exception):
"""Test git load error."""
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
repo = GitRepo(coresys, tmp_path, REPO_URL)

# Pretend we have a repo
(tmp_path / ".git").mkdir()

with (
patch("pathlib.Path.is_dir", return_value=True),
patch.object(
GitRepo, "sys_run_in_executor", new_callable=AsyncMock
) as run_in_executor,
patch("git.Repo") as mock_repo,
pytest.raises(StoreGitError),
):
run_in_executor.side_effect = git_errors
mock_repo.side_effect = git_errors
await repo.load()

assert len(coresys.resolution.suggestions) == 0

0 comments on commit d42ec12

Please sign in to comment.