|
15 | 15 | _trigger_release, |
16 | 16 | ) |
17 | 17 | from exasol.toolbox.release import ( |
| 18 | + ReleaseTypes, |
18 | 19 | Version, |
19 | 20 | extract_release_notes, |
20 | 21 | new_changelog, |
| 22 | + poetry_command, |
21 | 23 | ) |
22 | 24 |
|
23 | 25 |
|
@@ -56,7 +58,7 @@ def set_poetry_version(version): |
56 | 58 | return subprocess.CompletedProcess( |
57 | 59 | args=["poetry", "version", "--no-ansi", "--short"], |
58 | 60 | returncode=0, |
59 | | - stdout=version.encode("utf8"), |
| 61 | + stdout=version, |
60 | 62 | stderr="", |
61 | 63 | ) |
62 | 64 |
|
@@ -154,53 +156,113 @@ def test_extract_release_notes(unreleased_md): |
154 | 156 | assert expected == actual |
155 | 157 |
|
156 | 158 |
|
157 | | -@pytest.mark.parametrize( |
158 | | - "error_cmd", |
159 | | - [ |
160 | | - ("git", "remote", "show", "origin"), |
161 | | - ("git", "checkout", "main"), |
162 | | - ("git", "pull"), |
163 | | - ("git", "tag", "--list"), |
164 | | - ("gh", "release", "list"), |
165 | | - ("git", "tag", "0.3.0"), |
166 | | - ("git", "push", "origin", "0.3.0"), |
167 | | - ], |
168 | | -) |
169 | | -@patch("exasol.toolbox.nox._release.Version.from_poetry", return_value="0.3.0") |
170 | | -def test_trigger_release(mock, error_cmd): |
171 | | - def simulate_fail(args, **kwargs): |
172 | | - print("_______________") |
173 | | - print(args) |
174 | | - if args == error_cmd: |
175 | | - raise CalledProcessError(returncode=1, cmd=error_cmd) |
176 | | - result = "" |
| 159 | +@pytest.fixture(scope="class") |
| 160 | +def mock_from_poetry(): |
| 161 | + with patch( |
| 162 | + "exasol.toolbox.nox._release.Version.from_poetry", return_value="0.3.0" |
| 163 | + ) as mock_obj: |
| 164 | + yield mock_obj |
| 165 | + |
| 166 | + |
| 167 | +class TestTriggerReleaseWithMocking: |
| 168 | + @staticmethod |
| 169 | + def _get_mock_string(args) -> str: |
177 | 170 | if args == ("git", "remote", "show", "origin"): |
178 | | - result = "test\nHEAD branch: main\ntest" |
179 | | - return MagicMock(returncode=0, stdout=result) |
| 171 | + return "test\nHEAD branch: main\ntest" |
| 172 | + if args in [("git", "tag", "--list"), ("gh", "release", "list")]: |
| 173 | + return "0.1.0\n0.2.0" |
| 174 | + return "" |
180 | 175 |
|
181 | | - with patch("subprocess.run", side_effect=simulate_fail): |
182 | | - with pytest.raises(ReleaseError) as ex: |
183 | | - _trigger_release() |
184 | | - print(ex.value) |
| 176 | + def _get_subprocess_run_mock(self, args) -> str: |
| 177 | + return MagicMock(returncode=0, stdout=self._get_mock_string(args)) |
185 | 178 |
|
| 179 | + def test_works_as_expected(self, mock_from_poetry): |
| 180 | + def simulate_pass(args, **kwargs): |
| 181 | + return self._get_subprocess_run_mock(args) |
186 | 182 |
|
187 | | -@pytest.mark.parametrize( |
188 | | - "error_cmd, result", |
189 | | - [ |
190 | | - ( |
| 183 | + with patch("subprocess.run", side_effect=simulate_pass): |
| 184 | + result = _trigger_release() |
| 185 | + assert result == mock_from_poetry.return_value |
| 186 | + |
| 187 | + @pytest.mark.parametrize( |
| 188 | + "error_cmd", |
| 189 | + [ |
191 | 190 | ("git", "remote", "show", "origin"), |
192 | | - "test\nHEAD: \ntest", |
193 | | - ), |
194 | | - ( |
| 191 | + ("git", "checkout", "main"), |
| 192 | + ("git", "pull"), |
195 | 193 | ("git", "tag", "--list"), |
196 | | - "0.1.0\n0.2.0\n0.3.0", |
197 | | - ), |
198 | | - ( |
199 | 194 | ("gh", "release", "list"), |
200 | | - "0.1.0\n0.2.0\n0.3.0", |
201 | | - ), |
202 | | - ], |
203 | | -) |
204 | | -@patch("exasol.toolbox.nox._release.Version.from_poetry", return_value="0.3.0") |
205 | | -def test_trigger_release_bad_return(mock, error_cmd, result): |
206 | | - assert True |
| 195 | + ("git", "tag", "0.3.0"), |
| 196 | + ("git", "push", "origin", "0.3.0"), |
| 197 | + ], |
| 198 | + ) |
| 199 | + def test_caught_called_process_error_raises_release_error( |
| 200 | + self, mock_from_poetry, error_cmd |
| 201 | + ): |
| 202 | + def simulate_fail(args, **kwargs): |
| 203 | + if args == error_cmd: |
| 204 | + raise CalledProcessError(returncode=1, cmd=error_cmd) |
| 205 | + return self._get_subprocess_run_mock(args) |
| 206 | + |
| 207 | + with patch("subprocess.run", side_effect=simulate_fail): |
| 208 | + with pytest.raises(ReleaseError) as ex: |
| 209 | + _trigger_release() |
| 210 | + assert str(error_cmd) in str(ex) |
| 211 | + |
| 212 | + def test_default_branch_could_not_be_found(self, mock_from_poetry): |
| 213 | + def simulate_fail(args, **kwargs): |
| 214 | + if args == ("git", "remote", "show", "origin"): |
| 215 | + return MagicMock(returncode=0, stdout="DUMMY TEXT") |
| 216 | + return self._get_subprocess_run_mock(args) |
| 217 | + |
| 218 | + with patch("subprocess.run", side_effect=simulate_fail): |
| 219 | + with pytest.raises(ReleaseError) as ex: |
| 220 | + _trigger_release() |
| 221 | + assert "default branch could not be found" in str(ex) |
| 222 | + |
| 223 | + def test_tag_already_exists(self, mock_from_poetry): |
| 224 | + version = mock_from_poetry.return_value |
| 225 | + |
| 226 | + def simulate_fail(args, **kwargs): |
| 227 | + if args == ("git", "tag", "--list"): |
| 228 | + return MagicMock(returncode=0, stdout=f"0.1.0\n0.2.0\n{version}") |
| 229 | + return self._get_subprocess_run_mock(args) |
| 230 | + |
| 231 | + with patch("subprocess.run", side_effect=simulate_fail): |
| 232 | + with pytest.raises(ReleaseError) as ex: |
| 233 | + _trigger_release() |
| 234 | + assert f"tag {version} already exists" in str(ex) |
| 235 | + |
| 236 | + def test_release_already_exists(self, mock_from_poetry): |
| 237 | + version = mock_from_poetry.return_value |
| 238 | + |
| 239 | + def simulate_fail(args, **kwargs): |
| 240 | + if args == ("gh", "release", "list"): |
| 241 | + return MagicMock(returncode=0, stdout=f"0.1.0\n0.2.0\n{version}") |
| 242 | + return self._get_subprocess_run_mock(args) |
| 243 | + |
| 244 | + with patch("subprocess.run", side_effect=simulate_fail): |
| 245 | + with pytest.raises(ReleaseError) as ex: |
| 246 | + _trigger_release() |
| 247 | + assert f"release {version} already exists" in str(ex) |
| 248 | + |
| 249 | + |
| 250 | +@patch("exasol.toolbox.release.which", return_value=None) |
| 251 | +def test_poetry_decorator_no_poetry_executable(mock): |
| 252 | + @poetry_command |
| 253 | + def test(): |
| 254 | + pass |
| 255 | + |
| 256 | + with pytest.raises(ToolboxError): |
| 257 | + test() |
| 258 | + |
| 259 | + |
| 260 | +@patch("exasol.toolbox.release.which", return_value="test/path") |
| 261 | +def test_poetry_decorator_subprocess(mock): |
| 262 | + @poetry_command |
| 263 | + def test(): |
| 264 | + raise subprocess.CalledProcessError(returncode=1, cmd=["test"]) |
| 265 | + pass |
| 266 | + |
| 267 | + with pytest.raises(ToolboxError): |
| 268 | + test() |
0 commit comments