|
1 | 1 | import tempfile |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pytest |
| 5 | +from dulwich.repo import Repo |
2 | 6 |
|
3 | 7 | from infrahub_sdk.repository import GitRepoManager |
4 | 8 |
|
5 | 9 |
|
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