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