|
7 | 7 | from commitizen import commands |
8 | 8 | from commitizen.__version__ import __version__ |
9 | 9 | from commitizen.config.base_config import BaseConfig |
| 10 | +from commitizen.cz.base import BaseCommitizen |
| 11 | +from commitizen.exceptions import NoCommitsFoundError, NoPatternMapError |
| 12 | +from tests.utils import UtilFixture |
10 | 13 |
|
11 | 14 |
|
12 | 15 | def test_version_for_showing_project_version_error(config, capsys): |
@@ -282,14 +285,172 @@ def test_version_unknown_scheme(config, capsys): |
282 | 285 | assert "Unknown version scheme." in captured.err |
283 | 286 |
|
284 | 287 |
|
285 | | -def test_version_use_git_commits_not_implemented(config, capsys): |
| 288 | +@pytest.mark.parametrize( |
| 289 | + ("commit_message", "expected_version"), |
| 290 | + [ |
| 291 | + ("feat: new feature", "1.1.0"), |
| 292 | + ("fix: a bug", "1.0.1"), |
| 293 | + ("feat!: breaking change", "2.0.0"), |
| 294 | + ("docs: update readme", "1.0.0"), |
| 295 | + ], |
| 296 | +) |
| 297 | +@pytest.mark.usefixtures("tmp_git_project") |
| 298 | +def test_version_next_use_git_commits( |
| 299 | + config: BaseConfig, |
| 300 | + capsys: pytest.CaptureFixture, |
| 301 | + util: UtilFixture, |
| 302 | + commit_message: str, |
| 303 | + expected_version: str, |
| 304 | +): |
| 305 | + """USE_GIT_COMMITS derives the next version from commits since the last tag.""" |
286 | 306 | config.settings["version"] = "1.0.0" |
| 307 | + util.create_file_and_commit("feat: initial commit") |
| 308 | + util.create_tag("1.0.0") |
| 309 | + util.create_file_and_commit(commit_message) |
| 310 | + |
287 | 311 | commands.Version( |
288 | 312 | config, |
289 | 313 | {"project": True, "next": "USE_GIT_COMMITS"}, |
290 | 314 | )() |
| 315 | + |
291 | 316 | captured = capsys.readouterr() |
292 | | - assert "USE_GIT_COMMITS is not implemented" in captured.err |
| 317 | + assert captured.out == f"{expected_version}\n" |
| 318 | + |
| 319 | + |
| 320 | +@pytest.mark.usefixtures("tmp_git_project") |
| 321 | +def test_version_next_use_git_commits_manual_version( |
| 322 | + config: BaseConfig, capsys: pytest.CaptureFixture, util: UtilFixture |
| 323 | +): |
| 324 | + """USE_GIT_COMMITS also works with an explicit MANUAL_VERSION.""" |
| 325 | + util.create_file_and_commit("feat: initial commit") |
| 326 | + util.create_tag("1.0.0") |
| 327 | + util.create_file_and_commit("feat: new feature") |
| 328 | + |
| 329 | + commands.Version( |
| 330 | + config, |
| 331 | + {"manual_version": "1.0.0", "next": "USE_GIT_COMMITS"}, |
| 332 | + )() |
| 333 | + |
| 334 | + captured = capsys.readouterr() |
| 335 | + assert captured.out == "1.1.0\n" |
| 336 | + |
| 337 | + |
| 338 | +@pytest.mark.usefixtures("tmp_git_project") |
| 339 | +def test_version_next_use_git_commits_without_matching_tag( |
| 340 | + config: BaseConfig, capsys: pytest.CaptureFixture, util: UtilFixture |
| 341 | +): |
| 342 | + """When no tag matches the current version, all commits are considered.""" |
| 343 | + config.settings["version"] = "2.0.0" |
| 344 | + util.create_file_and_commit("feat: initial commit") |
| 345 | + util.create_file_and_commit("feat: new feature") |
| 346 | + |
| 347 | + commands.Version( |
| 348 | + config, |
| 349 | + {"project": True, "next": "USE_GIT_COMMITS"}, |
| 350 | + )() |
| 351 | + |
| 352 | + captured = capsys.readouterr() |
| 353 | + assert captured.out == "2.1.0\n" |
| 354 | + |
| 355 | + |
| 356 | +@pytest.mark.usefixtures("tmp_git_project") |
| 357 | +def test_version_next_use_git_commits_major_version_zero( |
| 358 | + config: BaseConfig, capsys: pytest.CaptureFixture, util: UtilFixture |
| 359 | +): |
| 360 | + """major_version_zero uses the zero bump map so no major bump is emitted.""" |
| 361 | + config.settings["version"] = "0.1.0" |
| 362 | + config.settings["major_version_zero"] = True |
| 363 | + util.create_file_and_commit("feat: initial commit") |
| 364 | + util.create_tag("0.1.0") |
| 365 | + util.create_file_and_commit("feat!: breaking change") |
| 366 | + |
| 367 | + commands.Version( |
| 368 | + config, |
| 369 | + {"project": True, "next": "USE_GIT_COMMITS"}, |
| 370 | + )() |
| 371 | + |
| 372 | + captured = capsys.readouterr() |
| 373 | + assert captured.out == "0.2.0\n" |
| 374 | + |
| 375 | + |
| 376 | +@pytest.mark.usefixtures("tmp_git_project") |
| 377 | +def test_version_next_use_git_commits_prerelease_without_commits( |
| 378 | + config: BaseConfig, capsys: pytest.CaptureFixture, util: UtilFixture |
| 379 | +): |
| 380 | + """A prerelease with no new commits finalizes into its release version.""" |
| 381 | + config.settings["version"] = "1.0.0rc1" |
| 382 | + util.create_file_and_commit("feat: initial commit") |
| 383 | + util.create_tag("1.0.0rc1") |
| 384 | + |
| 385 | + commands.Version( |
| 386 | + config, |
| 387 | + {"project": True, "next": "USE_GIT_COMMITS"}, |
| 388 | + )() |
| 389 | + |
| 390 | + captured = capsys.readouterr() |
| 391 | + assert captured.out == "1.0.0\n" |
| 392 | + |
| 393 | + |
| 394 | +@pytest.mark.usefixtures("tmp_git_project") |
| 395 | +def test_version_next_use_git_commits_no_commits_raises( |
| 396 | + config: BaseConfig, util: UtilFixture |
| 397 | +): |
| 398 | + """No new commits since the last (non-prerelease) tag raises an error.""" |
| 399 | + config.settings["version"] = "1.0.0" |
| 400 | + util.create_file_and_commit("feat: initial commit") |
| 401 | + util.create_tag("1.0.0") |
| 402 | + |
| 403 | + with pytest.raises(NoCommitsFoundError): |
| 404 | + commands.Version( |
| 405 | + config, |
| 406 | + {"project": True, "next": "USE_GIT_COMMITS"}, |
| 407 | + )() |
| 408 | + |
| 409 | + |
| 410 | +class _NoBumpRulesCz(BaseCommitizen): |
| 411 | + """A commitizen rule without bump pattern or map to trigger NoPatternMapError.""" |
| 412 | + |
| 413 | + bump_pattern = None |
| 414 | + bump_map = None |
| 415 | + |
| 416 | + def questions(self): |
| 417 | + return [] |
| 418 | + |
| 419 | + def message(self, answers): |
| 420 | + return "" |
| 421 | + |
| 422 | + def example(self) -> str: |
| 423 | + return "" |
| 424 | + |
| 425 | + def schema(self) -> str: |
| 426 | + return "" |
| 427 | + |
| 428 | + def schema_pattern(self) -> str: |
| 429 | + return "" |
| 430 | + |
| 431 | + def info(self) -> str: |
| 432 | + return "" |
| 433 | + |
| 434 | + |
| 435 | +@pytest.mark.usefixtures("tmp_git_project") |
| 436 | +def test_version_next_use_git_commits_no_pattern_map_raises( |
| 437 | + config: BaseConfig, util: UtilFixture, mocker: MockerFixture |
| 438 | +): |
| 439 | + """A rule that does not support bumping raises NoPatternMapError.""" |
| 440 | + config.settings["version"] = "1.0.0" |
| 441 | + mocker.patch( |
| 442 | + "commitizen.factory.committer_factory", |
| 443 | + return_value=_NoBumpRulesCz(config), |
| 444 | + ) |
| 445 | + util.create_file_and_commit("feat: initial commit") |
| 446 | + util.create_tag("1.0.0") |
| 447 | + util.create_file_and_commit("feat: new feature") |
| 448 | + |
| 449 | + with pytest.raises(NoPatternMapError): |
| 450 | + commands.Version( |
| 451 | + config, |
| 452 | + {"project": True, "next": "USE_GIT_COMMITS"}, |
| 453 | + )() |
293 | 454 |
|
294 | 455 |
|
295 | 456 | def test_version_no_arguments_shows_commitizen_version(config, capsys): |
|
0 commit comments