Skip to content

Commit 18f12e5

Browse files
committed
Add better testing for initializing git repos
1 parent 6e7cf2a commit 18f12e5

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

infrahub_sdk/repository.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def initialize_repo(self) -> None:
2121

2222
if root_path.exists() and (root_path / ".git").is_dir():
2323
self.git = Repo(self.root_directory) # Open existing repo
24+
try:
25+
self.active_branch # noqa: B018
26+
except KeyError:
27+
raise ValueError("Git repository does not have an active branch.")
2428
else:
2529
self.git = Repo.init(self.root_directory, default_branch=self.branch.encode("utf-8"))
2630
self.create_initial_commit()

tests/unit/sdk/test_repository.py

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,77 @@
11
import tempfile
2+
from pathlib import Path
3+
4+
import pytest
5+
from dulwich.repo import Repo
26

37
from infrahub_sdk.repository import GitRepoManager
48

59

6-
def test_init_repository():
7-
temp_dir = tempfile.mkdtemp()
8-
repo = GitRepoManager(temp_dir)
9-
assert repo.active_branch == "main"
10+
@pytest.fixture
11+
def temp_dir():
12+
"""Fixture to create a temporary directory for testing."""
13+
with tempfile.TemporaryDirectory() as tmp_dir:
14+
yield tmp_dir
15+
16+
17+
def test_initialize_repo_creates_new_repo(temp_dir):
18+
"""Test that a new Git repository is created if none exists."""
19+
manager = GitRepoManager(root_directory=temp_dir, branch="main")
20+
21+
# Verify .git directory is created
22+
assert (Path(temp_dir) / ".git").is_dir()
23+
24+
# Verify the repository is initialized
25+
assert manager.git is not None
26+
assert isinstance(manager.git, Repo)
27+
28+
29+
def test_initialize_repo_uses_existing_repo(temp_dir):
30+
"""Test that the GitRepoManager uses an existing repository without an active branch."""
31+
# Manually initialize a repo
32+
Repo.init(temp_dir)
33+
34+
with pytest.raises(ValueError, match="Git repository does not have an active branch."):
35+
manager = GitRepoManager(temp_dir)
36+
assert manager.git is not None
37+
assert isinstance(manager.git, Repo)
38+
assert (Path(temp_dir) / ".git").is_dir()
39+
40+
41+
def test_create_initial_commit(temp_dir):
42+
"""Test that an initial commit is created."""
43+
manager = GitRepoManager(temp_dir)
44+
45+
# Verify there is at least one commit
46+
assert len(list(manager.git.get_walker())) == 1
47+
48+
49+
def test_active_branch_returns_correct_branch(temp_dir):
50+
"""Test that the active branch is correctly returned."""
51+
manager = GitRepoManager(temp_dir, branch="develop")
52+
53+
# Verify the active branch is "develop"
54+
assert manager.active_branch == "develop"
55+
56+
57+
def test_initialize_repo_raises_error_on_failure(monkeypatch, temp_dir):
58+
"""Test that an error is raised if the repository cannot be initialized."""
59+
60+
def mock_init(*args, **kwargs): # noqa: ANN002, ANN003
61+
return None # Simulate failure
62+
63+
monkeypatch.setattr(Repo, "init", mock_init)
64+
65+
with pytest.raises(ValueError, match="Git repository not initialized."):
66+
GitRepoManager(temp_dir)
67+
68+
69+
def test_active_branch_raises_error_if_repo_not_initialized(temp_dir):
70+
"""Test that accessing the active branch raises an error if the repo is not initialized."""
71+
manager = GitRepoManager(temp_dir)
72+
73+
# Manually unset the repository to simulate uninitialized state
74+
manager.git = None
75+
76+
with pytest.raises(ValueError, match="Repository is not initialized."):
77+
_ = manager.active_branch

0 commit comments

Comments
 (0)