Skip to content

Commit f4ee17f

Browse files
committed
Add integration test for dulwich
1 parent d39c85c commit f4ee17f

File tree

5 files changed

+54
-30
lines changed

5 files changed

+54
-30
lines changed

infrahub_sdk/repository.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,11 @@ def initialize_repo(self) -> Repo:
2222
else:
2323
repo = Repo.init(self.root_directory, default_branch=self.branch.encode("utf-8"))
2424

25-
self.create_initial_commit()
26-
2725
if not repo:
2826
raise ValueError("Failed to initialize or open a repository.")
2927

3028
return repo
3129

32-
def create_initial_commit(self) -> None:
33-
committer = b"Initial Commit <[email protected]>"
34-
commit_msg = b"Initial commit"
35-
porcelain.commit(self.root_directory, message=commit_msg, author=committer)
36-
3730
@property
3831
def active_branch(self) -> str | None:
3932
active_branch = porcelain.active_branch(self.root_directory).decode("utf-8")

infrahub_sdk/testing/repository.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from pathlib import Path
88
from typing import TYPE_CHECKING
99

10+
from dulwich import porcelain
11+
1012
from infrahub_sdk.graphql import Mutation
1113
from infrahub_sdk.protocols import CoreGenericRepository
1214
from infrahub_sdk.repository import GitRepoManager
@@ -64,6 +66,15 @@ def init(self) -> None:
6466

6567
self._repo = GitRepoManager(str(Path(self.dst_directory / self.name)), branch=self.initial_branch)
6668

69+
files = list(
70+
porcelain.get_untracked_paths(self._repo.git.path, self._repo.git.path, self._repo.git.open_index())
71+
)
72+
files_to_add = [str(Path(self._repo.git.path) / t) for t in files]
73+
if files_to_add:
74+
porcelain.add(repo=self._repo.git.path, paths=files_to_add)
75+
porcelain.commit(repo=self._repo.git.path, message="First commit")
76+
porcelain.checkout_branch(self._repo.git, self.initial_branch.encode("utf-8"))
77+
6778
async def add_to_infrahub(self, client: InfrahubClient, branch: str | None = None) -> dict:
6879
input_data = {
6980
"data": {

tests/fixtures/integration/repo/blank_schema.yml

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
import pytest
6+
7+
from infrahub_sdk.testing.docker import TestInfrahubDockerClient
8+
from infrahub_sdk.testing.repository import GitRepo
9+
from infrahub_sdk.utils import get_fixtures_dir
10+
11+
if TYPE_CHECKING:
12+
from infrahub_sdk import InfrahubClient
13+
14+
15+
class TestInfrahubRepository(TestInfrahubDockerClient):
16+
@pytest.fixture(scope="class")
17+
def infrahub_version(self) -> str:
18+
return "1.1.0"
19+
20+
async def test_add_repository(self, client: InfrahubClient, remote_repos_dir):
21+
src_directory = get_fixtures_dir() / "integration/repo"
22+
repo = GitRepo(name="test", src_directory=src_directory, dst_directory=remote_repos_dir)
23+
commit = repo._repo.git[repo._repo.git.head()]
24+
response = await repo.add_to_infrahub(client=client)
25+
repos = await client.all(kind=repo.type)
26+
27+
assert len(list(repo._repo.git.get_walker())) == 1
28+
assert commit.message.decode("utf-8") == "First commit"
29+
assert response.get(f"{repo.type.value}Create", {}).get("ok")
30+
assert len(repos) == 1

tests/unit/sdk/test_repository.py

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from dulwich.repo import Repo
66

77
from infrahub_sdk.repository import GitRepoManager
8+
from infrahub_sdk.testing.repository import GitRepo
9+
from infrahub_sdk.utils import get_fixtures_dir
810

911

1012
@pytest.fixture
@@ -29,21 +31,12 @@ def test_initialize_repo_creates_new_repo(temp_dir):
2931
def test_initialize_repo_uses_existing_repo(temp_dir):
3032
"""Test that the GitRepoManager uses an existing repository without an active branch."""
3133
# Manually initialize a repo
32-
Repo.init(temp_dir)
34+
Repo.init(temp_dir, default_branch=b"main")
3335

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."""
4336
manager = GitRepoManager(temp_dir)
44-
45-
# Verify there is at least one commit
46-
assert len(list(manager.git.get_walker())) == 1
37+
assert manager.git is not None
38+
assert isinstance(manager.git, Repo)
39+
assert (Path(temp_dir) / ".git").is_dir()
4740

4841

4942
def test_active_branch_returns_correct_branch(temp_dir):
@@ -62,16 +55,13 @@ def mock_init(*args, **kwargs): # noqa: ANN002, ANN003
6255

6356
monkeypatch.setattr(Repo, "init", mock_init)
6457

65-
with pytest.raises(ValueError, match="Git repository not initialized."):
58+
with pytest.raises(ValueError, match="Failed to initialize or open a repository."):
6659
GitRepoManager(temp_dir)
6760

6861

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
62+
def test_gitrepo_init(temp_dir):
63+
src_directory = get_fixtures_dir() / "integration/repo"
64+
repo = GitRepo(name="test", src_directory=src_directory, dst_directory=Path(temp_dir))
65+
assert len(list(repo._repo.git.get_walker())) == 1
66+
commit = repo._repo.git[repo._repo.git.head()]
67+
assert commit.message.decode("utf-8") == "First commit"

0 commit comments

Comments
 (0)