|
| 1 | +"""Tests for bumpversion.scm.git.""" |
| 2 | + |
1 | 3 | import subprocess
|
2 | 4 | from pathlib import Path
|
3 | 5 | from unittest.mock import MagicMock, patch
|
4 | 6 |
|
5 | 7 | import pytest
|
6 | 8 | from pytest import param
|
7 | 9 |
|
8 |
| -from bumpversion.exceptions import DirtyWorkingDirectoryError |
| 10 | +from bumpversion.exceptions import BumpVersionError, DirtyWorkingDirectoryError |
9 | 11 | from bumpversion.scm.git import Git, assert_nondirty, commit_info, moveable_tag, revision_info, tag
|
10 | 12 | from bumpversion.scm.models import LatestTagInfo, SCMConfig
|
11 | 13 | from bumpversion.utils import run_command
|
@@ -70,6 +72,73 @@ def test_returns_correct_commit_and_tag_info(self, git_repo: Path, scm_config: S
|
70 | 72 | assert tag_info.repository_root == git_repo
|
71 | 73 | assert tag_info.dirty is False
|
72 | 74 |
|
| 75 | + class TestGitAddPath: |
| 76 | + """Tests for the add_path method in the Git class.""" |
| 77 | + |
| 78 | + @patch("bumpversion.scm.git.run_command") |
| 79 | + @patch("bumpversion.scm.git.Git.latest_tag_info") |
| 80 | + @patch("bumpversion.scm.git.is_subpath") |
| 81 | + def test_valid_subpath_is_added( |
| 82 | + self, mock_is_subpath, mock_latest_tag_info, mock_run_command, scm_config: SCMConfig, tmp_path: Path |
| 83 | + ): |
| 84 | + """A path that is a subpath of the repository root should be added.""" |
| 85 | + # Arrange |
| 86 | + repository_root = tmp_path / "repository" |
| 87 | + repository_root.mkdir() |
| 88 | + path_to_add = repository_root / "file.txt" |
| 89 | + mock_latest_tag_info.return_value.repository_root = repository_root |
| 90 | + mock_is_subpath.return_value = True |
| 91 | + mock_run_command.return_value = None |
| 92 | + git_instance = Git(scm_config) |
| 93 | + |
| 94 | + # Act |
| 95 | + with inside_dir(repository_root): |
| 96 | + git_instance.add_path(path_to_add) |
| 97 | + |
| 98 | + # Assert |
| 99 | + mock_run_command.assert_called_once_with(["git", "add", "--update", "file.txt"]) |
| 100 | + |
| 101 | + @patch("bumpversion.scm.git.run_command") |
| 102 | + @patch("bumpversion.scm.git.Git.latest_tag_info") |
| 103 | + @patch("bumpversion.scm.git.is_subpath") |
| 104 | + def test_invalid_subpath_is_not_added( |
| 105 | + self, mock_is_subpath, mock_latest_tag_info, mock_run_command, scm_config, tmp_path: Path |
| 106 | + ): |
| 107 | + """A path that is not a subpath of the repository root should not be added.""" |
| 108 | + # Arrange |
| 109 | + repository_root = tmp_path / "repository" |
| 110 | + repository_root.mkdir() |
| 111 | + path_to_add = repository_root / "file.txt" |
| 112 | + mock_latest_tag_info.return_value.repository_root = repository_root |
| 113 | + mock_is_subpath.return_value = False |
| 114 | + git_instance = Git(scm_config) |
| 115 | + |
| 116 | + # Act |
| 117 | + git_instance.add_path(path_to_add) |
| 118 | + |
| 119 | + # Assert |
| 120 | + mock_run_command.assert_not_called() |
| 121 | + |
| 122 | + @patch("bumpversion.scm.git.run_command") |
| 123 | + @patch("bumpversion.scm.git.Git.latest_tag_info") |
| 124 | + @patch("bumpversion.scm.git.is_subpath") |
| 125 | + def test_raises_error_on_command_failure( |
| 126 | + self, mock_is_subpath, mock_latest_tag_info, mock_run_command, scm_config: SCMConfig, tmp_path: Path |
| 127 | + ): |
| 128 | + """If the git command fails, a BumpVersionError should be raised.""" |
| 129 | + # Arrange |
| 130 | + repository_root = tmp_path / "repository" |
| 131 | + repository_root.mkdir() |
| 132 | + path_to_add = repository_root / "file.txt" |
| 133 | + mock_latest_tag_info.return_value.repository_root = repository_root |
| 134 | + mock_is_subpath.return_value = True |
| 135 | + mock_run_command.side_effect = subprocess.CalledProcessError(returncode=1, cmd="git add") |
| 136 | + git_instance = Git(scm_config) |
| 137 | + |
| 138 | + # Act / Assert |
| 139 | + with pytest.raises(BumpVersionError): |
| 140 | + git_instance.add_path(path_to_add) |
| 141 | + |
73 | 142 |
|
74 | 143 | class TestRevisionInfo:
|
75 | 144 | """Tests for the revision_info function."""
|
|
0 commit comments