From cfe0b19d21aeb7e2179b8bba5590d3b45bbb7052 Mon Sep 17 00:00:00 2001 From: Yu-Ting Hsiung Date: Sun, 18 Jan 2026 18:20:54 +0800 Subject: [PATCH] test: extract fixtures to shorten tests --- tests/commands/conftest.py | 22 +- tests/commands/test_bump_command.py | 334 +++++++----------- tests/commands/test_changelog_command.py | 274 ++++++-------- ...aming_user_to_users_n_nfooter_content_.md} | 0 ...NGE__migrate_by_renaming_user_to_users_.md | 10 + ..._newline_separates_new_content_from_old.md | 7 + .../test_invalid_subject_is_skipped.md | 6 + tests/commands/test_init_command.py | 2 +- tests/conftest.py | 24 +- tests/test_bump_update_version_in_files.py | 18 +- tests/test_changelog.py | 15 +- 11 files changed, 295 insertions(+), 417 deletions(-) rename tests/commands/test_changelog_command/{test_breaking_change_content_v1_beta.md => test_breaking_change_content_v1_beta_feat_users___email_pattern_corrected_n_nBREAKING_CHANGE__migrate_by_renaming_user_to_users_n_nfooter_content_.md} (100%) create mode 100644 tests/commands/test_changelog_command/test_breaking_change_content_v1_beta_feat_users___email_pattern_corrected_n_nbody_content_n_nBREAKING_CHANGE__migrate_by_renaming_user_to_users_.md create mode 100644 tests/commands/test_changelog_command/test_changelog_incremental_newline_separates_new_content_from_old.md create mode 100644 tests/commands/test_changelog_command/test_invalid_subject_is_skipped.md diff --git a/tests/commands/conftest.py b/tests/commands/conftest.py index 88fc89b7c..c7d590450 100644 --- a/tests/commands/conftest.py +++ b/tests/commands/conftest.py @@ -1,22 +1,23 @@ import os +from pathlib import Path import pytest -from pytest_mock import MockerFixture +from pytest_mock import MockerFixture, MockType from commitizen import defaults from commitizen.config import BaseConfig from commitizen.config.json_config import JsonConfig -@pytest.fixture() -def config(): +@pytest.fixture +def config() -> BaseConfig: _config = BaseConfig() _config.settings.update({"name": defaults.DEFAULT_SETTINGS["name"]}) return _config -@pytest.fixture() -def config_customize(): +@pytest.fixture +def config_customize() -> JsonConfig: json_string = r"""{ "commitizen": { "name": "cz_customize", @@ -41,20 +42,19 @@ def config_customize(): } } }""" - _config = JsonConfig(data=json_string, path="not_exist.json") - return _config + return JsonConfig(data=json_string, path=Path("not_exist.json")) -@pytest.fixture() +@pytest.fixture def changelog_path() -> str: return os.path.join(os.getcwd(), "CHANGELOG.md") -@pytest.fixture() +@pytest.fixture def config_path() -> str: return os.path.join(os.getcwd(), "pyproject.toml") -@pytest.fixture() -def success_mock(mocker: MockerFixture): +@pytest.fixture +def success_mock(mocker: MockerFixture) -> MockType: return mocker.patch("commitizen.out.success") diff --git a/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py index d79a76b5a..a1c70b948 100644 --- a/tests/commands/test_bump_command.py +++ b/tests/commands/test_bump_command.py @@ -5,7 +5,7 @@ from pathlib import Path from textwrap import dedent from typing import TYPE_CHECKING -from unittest.mock import MagicMock, call +from unittest.mock import call import pytest @@ -53,8 +53,7 @@ def test_bump_patch_increment(commit_msg: str, util: UtilFixture): util.create_file_and_commit(commit_msg) util.run_cli("bump", "--yes") - tag_exists = git.tag_exist("0.1.1") - assert tag_exists is True + assert git.tag_exist("0.1.1") is True @pytest.mark.parametrize("commit_msg", ("feat: new file", "feat(user): new file")) @@ -62,9 +61,11 @@ def test_bump_patch_increment(commit_msg: str, util: UtilFixture): def test_bump_minor_increment(commit_msg: str, util: UtilFixture): util.create_file_and_commit(commit_msg) util.run_cli("bump", "--yes") - tag_exists = git.tag_exist("0.2.0") - cmd_res = cmd.run('git for-each-ref refs/tags --format "%(objecttype):%(refname)"') - assert tag_exists is True and "commit:refs/tags/0.2.0\n" in cmd_res.out + assert git.tag_exist("0.2.0") is True + assert ( + "commit:refs/tags/0.2.0" + in cmd.run('git for-each-ref refs/tags --format "%(objecttype):%(refname)"').out + ) @pytest.mark.parametrize("commit_msg", ("feat: new file", "feat(user): new file")) @@ -72,12 +73,13 @@ def test_bump_minor_increment(commit_msg: str, util: UtilFixture): def test_bump_minor_increment_annotated(commit_msg: str, util: UtilFixture): util.create_file_and_commit(commit_msg) util.run_cli("bump", "--yes", "--annotated-tag") - tag_exists = git.tag_exist("0.2.0") - cmd_res = cmd.run('git for-each-ref refs/tags --format "%(objecttype):%(refname)"') - assert tag_exists is True and "tag:refs/tags/0.2.0\n" in cmd_res.out + assert git.tag_exist("0.2.0") is True + assert ( + "tag:refs/tags/0.2.0" + in cmd.run('git for-each-ref refs/tags --format "%(objecttype):%(refname)"').out + ) - _is_signed = git.is_signed_tag("0.2.0") - assert _is_signed is False + assert git.is_signed_tag("0.2.0") is False @pytest.mark.parametrize("commit_msg", ("feat: new file", "feat(user): new file")) @@ -85,30 +87,29 @@ def test_bump_minor_increment_annotated(commit_msg: str, util: UtilFixture): def test_bump_minor_increment_signed(commit_msg: str, util: UtilFixture): util.create_file_and_commit(commit_msg) util.run_cli("bump", "--yes", "--gpg-sign") - tag_exists = git.tag_exist("0.2.0") - cmd_res = cmd.run('git for-each-ref refs/tags --format "%(objecttype):%(refname)"') - assert tag_exists is True and "tag:refs/tags/0.2.0\n" in cmd_res.out + assert git.tag_exist("0.2.0") is True + assert ( + "tag:refs/tags/0.2.0" + in cmd.run('git for-each-ref refs/tags --format "%(objecttype):%(refname)"').out + ) - _is_signed = git.is_signed_tag("0.2.0") - assert _is_signed is True + assert git.is_signed_tag("0.2.0") is True @pytest.mark.parametrize("commit_msg", ("feat: new file", "feat(user): new file")) def test_bump_minor_increment_annotated_config_file( - commit_msg: str, util: UtilFixture, tmp_commitizen_project + commit_msg: str, util: UtilFixture, pyproject: Path ): - tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") - tmp_commitizen_cfg_file.write( - f"{tmp_commitizen_cfg_file.read()}\nannotated_tag = 1" - ) + pyproject.write_text(pyproject.read_text() + "\nannotated_tag = 1") util.create_file_and_commit(commit_msg) util.run_cli("bump", "--yes") - tag_exists = git.tag_exist("0.2.0") - cmd_res = cmd.run('git for-each-ref refs/tags --format "%(objecttype):%(refname)"') - assert tag_exists is True and "tag:refs/tags/0.2.0\n" in cmd_res.out + assert git.tag_exist("0.2.0") is True + assert ( + "tag:refs/tags/0.2.0" + in cmd.run('git for-each-ref refs/tags --format "%(objecttype):%(refname)"').out + ) - _is_signed = git.is_signed_tag("0.2.0") - assert _is_signed is False + assert git.is_signed_tag("0.2.0") is False @pytest.mark.parametrize("commit_msg", ("feat: new file", "feat(user): new file")) @@ -119,12 +120,13 @@ def test_bump_minor_increment_signed_config_file( tmp_commitizen_cfg_file.write(f"{tmp_commitizen_cfg_file.read()}\ngpg_sign = 1") util.create_file_and_commit(commit_msg) util.run_cli("bump", "--yes") - tag_exists = git.tag_exist("0.2.0") - cmd_res = cmd.run('git for-each-ref refs/tags --format "%(objecttype):%(refname)"') - assert tag_exists is True and "tag:refs/tags/0.2.0\n" in cmd_res.out + assert git.tag_exist("0.2.0") is True + assert ( + "tag:refs/tags/0.2.0" + in cmd.run('git for-each-ref refs/tags --format "%(objecttype):%(refname)"').out + ) - _is_signed = git.is_signed_tag("0.2.0") - assert _is_signed is True + assert git.is_signed_tag("0.2.0") is True @pytest.mark.usefixtures("tmp_commitizen_project") @@ -143,11 +145,8 @@ def test_bump_minor_increment_signed_config_file( ) def test_bump_major_increment(commit_msg: str, util: UtilFixture): util.create_file_and_commit(commit_msg) - util.run_cli("bump", "--yes") - - tag_exists = git.tag_exist("1.0.0") - assert tag_exists is True + assert git.tag_exist("1.0.0") is True @pytest.mark.usefixtures("tmp_commitizen_project") @@ -166,11 +165,8 @@ def test_bump_major_increment(commit_msg: str, util: UtilFixture): ) def test_bump_major_increment_major_version_zero(commit_msg: str, util: UtilFixture): util.create_file_and_commit(commit_msg) - util.run_cli("bump", "--yes", "--major-version-zero") - - tag_exists = git.tag_exist("0.2.0") - assert tag_exists is True + assert git.tag_exist("0.2.0") is True @pytest.mark.usefixtures("tmp_commitizen_project") @@ -187,11 +183,8 @@ def test_bump_command_increment_option( commit_msg: str, increment: str, expected_tag: str, util: UtilFixture ): util.create_file_and_commit(commit_msg) - util.run_cli("bump", "--increment", increment, "--yes") - - tag_exists = git.tag_exist(expected_tag) - assert tag_exists is True + assert git.tag_exist(expected_tag) is True @pytest.mark.usefixtures("tmp_commitizen_project") @@ -200,109 +193,76 @@ def test_bump_command_prerelease(util: UtilFixture): # Create an alpha pre-release. util.run_cli("bump", "--prerelease", "alpha", "--yes") - - tag_exists = git.tag_exist("0.2.0a0") - assert tag_exists is True + assert git.tag_exist("0.2.0a0") is True # Create a beta pre-release. util.run_cli("bump", "--prerelease", "beta", "--yes") - - tag_exists = git.tag_exist("0.2.0b0") - assert tag_exists is True + assert git.tag_exist("0.2.0b0") is True # With a current beta pre-release, bumping alpha must bump beta # because we can't bump "backwards". util.run_cli("bump", "--prerelease", "alpha", "--yes") - - tag_exists = git.tag_exist("0.2.0a1") - assert tag_exists is False - tag_exists = git.tag_exist("0.2.0b1") - assert tag_exists is True + assert git.tag_exist("0.2.0a1") is False + assert git.tag_exist("0.2.0b1") is True # Create a rc pre-release. util.run_cli("bump", "--prerelease", "rc", "--yes") - - tag_exists = git.tag_exist("0.2.0rc0") - assert tag_exists is True + assert git.tag_exist("0.2.0rc0") is True # With a current rc pre-release, bumping alpha must bump rc. util.run_cli("bump", "--prerelease", "alpha", "--yes") - - tag_exists = git.tag_exist("0.2.0a1") - assert tag_exists is False - tag_exists = git.tag_exist("0.2.0b2") - assert tag_exists is False - tag_exists = git.tag_exist("0.2.0rc1") - assert tag_exists is True + assert git.tag_exist("0.2.0a1") is False + assert git.tag_exist("0.2.0b2") is False + assert git.tag_exist("0.2.0rc1") is True # With a current rc pre-release, bumping beta must bump rc. util.run_cli("bump", "--prerelease", "beta", "--yes") - - tag_exists = git.tag_exist("0.2.0a2") - assert tag_exists is False - tag_exists = git.tag_exist("0.2.0b2") - assert tag_exists is False - tag_exists = git.tag_exist("0.2.0rc2") - assert tag_exists is True + assert git.tag_exist("0.2.0a2") is False + assert git.tag_exist("0.2.0b2") is False + assert git.tag_exist("0.2.0rc2") is True # Create a final release from the current pre-release. util.run_cli("bump") - - tag_exists = git.tag_exist("0.2.0") - assert tag_exists is True + assert git.tag_exist("0.2.0") is True @pytest.mark.usefixtures("tmp_commitizen_project") def test_bump_command_prerelease_increment(util: UtilFixture): # FINAL RELEASE util.create_file_and_commit("fix: location") - util.run_cli("bump", "--yes") - assert git.tag_exist("0.1.1") + assert git.tag_exist("0.1.1") is True # PRERELEASE util.create_file_and_commit("fix: location") - util.run_cli("bump", "--prerelease", "alpha", "--yes") - - assert git.tag_exist("0.1.2a0") + assert git.tag_exist("0.1.2a0") is True util.create_file_and_commit("feat: location") - util.run_cli("bump", "--prerelease", "alpha", "--yes") - - assert git.tag_exist("0.2.0a0") + assert git.tag_exist("0.2.0a0") is True util.create_file_and_commit("feat!: breaking") - util.run_cli("bump", "--prerelease", "alpha", "--yes") - - assert git.tag_exist("1.0.0a0") + assert git.tag_exist("1.0.0a0") is True @pytest.mark.usefixtures("tmp_commitizen_project") def test_bump_command_prerelease_exact_mode(util: UtilFixture): # PRERELEASE util.create_file_and_commit("feat: location") - util.run_cli("bump", "--prerelease", "alpha", "--yes") - - tag_exists = git.tag_exist("0.2.0a0") - assert tag_exists is True + assert git.tag_exist("0.2.0a0") is True # PRERELEASE + PATCH BUMP util.run_cli("bump", "--prerelease", "alpha", "--yes", "--increment-mode=exact") - tag_exists = git.tag_exist("0.2.0a1") - assert tag_exists is True + assert git.tag_exist("0.2.0a1") is True # PRERELEASE + MINOR BUMP # --increment-mode allows the minor version to bump, and restart the prerelease util.create_file_and_commit("feat: location") - util.run_cli("bump", "--prerelease", "alpha", "--yes", "--increment-mode=exact") - - tag_exists = git.tag_exist("0.3.0a0") - assert tag_exists is True + assert git.tag_exist("0.3.0a0") is True # PRERELEASE + MAJOR BUMP # --increment-mode=exact allows the major version to bump, and restart the prerelease @@ -315,8 +275,7 @@ def test_bump_command_prerelease_exact_mode(util: UtilFixture): "--increment-mode=exact", ) - tag_exists = git.tag_exist("1.0.0a0") - assert tag_exists is True + assert git.tag_exist("1.0.0a0") is True @pytest.mark.usefixtures("tmp_commitizen_project") @@ -329,11 +288,8 @@ def test_bump_on_git_with_hooks_no_verify_disabled(util: UtilFixture): # MINOR util.create_file_and_commit("feat: new file") - util.run_cli("bump", "--yes") - - tag_exists = git.tag_exist("0.2.0") - assert tag_exists is True + assert git.tag_exist("0.2.0") is True @pytest.mark.usefixtures("tmp_commitizen_project") @@ -361,11 +317,8 @@ def test_bump_on_git_with_hooks_no_verify_enabled(util: UtilFixture): # MINOR util.create_file_and_commit("feat: new file") - util.run_cli("bump", "--yes", "--no-verify") - - tag_exists = git.tag_exist("0.2.0") - assert tag_exists is True + assert git.tag_exist("0.2.0") is True @pytest.mark.usefixtures("tmp_commitizen_project") @@ -376,7 +329,6 @@ def test_bump_when_bumping_is_not_support(util: UtilFixture): with pytest.raises(NoPatternMapError) as excinfo: util.run_cli("-n", "cz_jira", "bump", "--yes") - assert "'cz_jira' rule does not support bump" in str(excinfo.value) @@ -400,8 +352,7 @@ def test_bump_when_no_new_commit(util: UtilFixture): with pytest.raises(NoCommitsFoundError) as excinfo: util.run_cli("bump", "--yes") - expected_error_message = "[NO_COMMITS_FOUND]\nNo new commits found." - assert expected_error_message in str(excinfo.value) + assert "[NO_COMMITS_FOUND]\nNo new commits found." in str(excinfo.value) def test_bump_when_version_inconsistent_in_version_files( @@ -421,8 +372,7 @@ def test_bump_when_version_inconsistent_in_version_files( with pytest.raises(CurrentVersionNotFoundError) as excinfo: util.run_cli("bump", "--yes", "--check-consistency") - partial_expected_error_message = "Current version 0.1.0 is not found in" - assert partial_expected_error_message in str(excinfo.value) + assert "Current version 0.1.0 is not found in" in str(excinfo.value) def test_bump_major_version_zero_when_major_is_not_zero( @@ -447,10 +397,9 @@ def test_bump_major_version_zero_when_major_is_not_zero( with pytest.raises(NotAllowed) as excinfo: util.run_cli("bump", "--yes", "--major-version-zero") - expected_error_message = ( - "--major-version-zero is meaningless for current version 1.0.0" + assert "--major-version-zero is meaningless for current version 1.0.0" in str( + excinfo.value ) - assert expected_error_message in str(excinfo.value) def test_bump_files_only(tmp_commitizen_project, util: UtilFixture): @@ -465,15 +414,12 @@ def test_bump_files_only(tmp_commitizen_project, util: UtilFixture): util.create_file_and_commit("feat: new user interface") util.run_cli("bump", "--yes") - tag_exists = git.tag_exist("0.2.0") - assert tag_exists is True + assert git.tag_exist("0.2.0") is True util.create_file_and_commit("feat: another new feature") with pytest.raises(ExpectedExit): util.run_cli("bump", "--yes", "--files-only") - - tag_exists = git.tag_exist("0.3.0") - assert tag_exists is False + assert git.tag_exist("0.3.0") is False with open(tmp_version_file, encoding="utf-8") as f: assert "0.3.0" in f.read() @@ -495,15 +441,14 @@ def test_bump_local_version(tmp_commitizen_project, util: UtilFixture): util.create_file_and_commit("feat: new user interface") util.run_cli("bump", "--yes", "--local-version") - tag_exists = git.tag_exist("4.5.1+0.2.0") - assert tag_exists is True + assert git.tag_exist("4.5.1+0.2.0") is True with open(tmp_version_file, encoding="utf-8") as f: assert "4.5.1+0.2.0" in f.read() @pytest.mark.usefixtures("tmp_commitizen_project") -def test_bump_dry_run(util: UtilFixture, capsys): +def test_bump_dry_run(util: UtilFixture, capsys: pytest.CaptureFixture): util.create_file_and_commit("feat: new file") with pytest.raises(DryRunExit): @@ -511,12 +456,10 @@ def test_bump_dry_run(util: UtilFixture, capsys): out, _ = capsys.readouterr() assert "0.2.0" in out - - tag_exists = git.tag_exist("0.2.0") - assert tag_exists is False + assert git.tag_exist("0.2.0") is False -def test_bump_in_non_git_project(tmpdir, config, util: UtilFixture): +def test_bump_in_non_git_project(tmpdir, util: UtilFixture): with tmpdir.as_cwd(): with pytest.raises(NotAGitProjectError): with pytest.raises(ExpectedExit): @@ -541,31 +484,24 @@ def test_none_increment_exit_is_exception(): @pytest.mark.usefixtures("tmp_commitizen_project") def test_none_increment_should_not_call_git_tag_and_error_code_is_not_zero( - mocker: MockFixture, util: UtilFixture + mocker: MockFixture, + util: UtilFixture, ): util.create_file_and_commit("test(test_get_all_droplets): fix bad comparison test") - - # stash git.tag for later restore - stashed_git_tag = git.tag - dummy_value = git.tag("0.0.2") - git.tag = MagicMock(return_value=dummy_value) + git_tag_mock = mocker.patch("commitizen.git.tag") with pytest.raises(NoneIncrementExit) as e: util.run_cli("bump", "--yes") - git.tag.assert_not_called() + git_tag_mock.assert_not_called() assert e.value.exit_code == ExitCode.NO_INCREMENT - # restore pop stashed - git.tag = stashed_git_tag - @pytest.mark.usefixtures("tmp_commitizen_project") def test_bump_with_changelog_arg(util: UtilFixture, changelog_path): util.create_file_and_commit("feat(user): new file") util.run_cli("bump", "--yes", "--changelog") - tag_exists = git.tag_exist("0.2.0") - assert tag_exists is True + assert git.tag_exist("0.2.0") is True with open(changelog_path, encoding="utf-8") as f: out = f.read() @@ -580,8 +516,7 @@ def test_bump_with_changelog_config(util: UtilFixture, changelog_path, config_pa fp.write("update_changelog_on_bump = true\n") util.run_cli("bump", "--yes") - tag_exists = git.tag_exist("0.2.0") - assert tag_exists is True + assert git.tag_exist("0.2.0") is True with open(changelog_path, encoding="utf-8") as f: out = f.read() @@ -590,7 +525,9 @@ def test_bump_with_changelog_config(util: UtilFixture, changelog_path, config_pa @pytest.mark.usefixtures("tmp_commitizen_project") -def test_prevent_prerelease_when_no_increment_detected(util: UtilFixture, capsys): +def test_prevent_prerelease_when_no_increment_detected( + util: UtilFixture, capsys: pytest.CaptureFixture +): util.create_file_and_commit("feat: new file") util.run_cli("bump", "--yes") @@ -603,21 +540,21 @@ def test_prevent_prerelease_when_no_increment_detected(util: UtilFixture, capsys with pytest.raises(NoCommitsFoundError) as excinfo: util.run_cli("bump", "-pr", "beta") - expected_error_message = ( - "[NO_COMMITS_FOUND]\nNo commits found to generate a pre-release." + assert "[NO_COMMITS_FOUND]\nNo commits found to generate a pre-release." in str( + excinfo.value ) - assert expected_error_message in str(excinfo.value) @pytest.mark.usefixtures("tmp_commitizen_project") -def test_bump_with_changelog_to_stdout_arg(util: UtilFixture, capsys, changelog_path): +def test_bump_with_changelog_to_stdout_arg( + util: UtilFixture, capsys: pytest.CaptureFixture, changelog_path: Path +): util.create_file_and_commit("feat(user): this should appear in stdout") util.run_cli("bump", "--yes", "--changelog-to-stdout") out, _ = capsys.readouterr() assert "this should appear in stdout" in out - tag_exists = git.tag_exist("0.2.0") - assert tag_exists is True + assert git.tag_exist("0.2.0") is True with open(changelog_path, encoding="utf-8") as f: out = f.read() @@ -627,7 +564,7 @@ def test_bump_with_changelog_to_stdout_arg(util: UtilFixture, capsys, changelog_ @pytest.mark.usefixtures("tmp_commitizen_project") def test_bump_with_changelog_to_stdout_dry_run_arg( - util: UtilFixture, capsys, changelog_path + util: UtilFixture, capsys: pytest.CaptureFixture, changelog_path: Path ): util.create_file_and_commit( "feat(user): this should appear in stdout with dry-run enabled" @@ -636,15 +573,16 @@ def test_bump_with_changelog_to_stdout_dry_run_arg( util.run_cli("bump", "--yes", "--changelog-to-stdout", "--dry-run") out, _ = capsys.readouterr() - tag_exists = git.tag_exist("0.2.0") - assert tag_exists is False + assert git.tag_exist("0.2.0") is False assert out.startswith("#") assert "this should appear in stdout with dry-run enabled" in out assert "0.2.0" in out @pytest.mark.usefixtures("tmp_commitizen_project") -def test_bump_without_git_to_stdout_arg(util: UtilFixture, capsys, changelog_path): +def test_bump_without_git_to_stdout_arg( + util: UtilFixture, capsys: pytest.CaptureFixture +): util.create_file_and_commit("feat(user): this should appear in stdout") util.run_cli("bump", "--yes") out, _ = capsys.readouterr() @@ -656,7 +594,7 @@ def test_bump_without_git_to_stdout_arg(util: UtilFixture, capsys, changelog_pat @pytest.mark.usefixtures("tmp_commitizen_project") -def test_bump_with_git_to_stdout_arg(util: UtilFixture, capsys, changelog_path): +def test_bump_with_git_to_stdout_arg(util: UtilFixture, capsys: pytest.CaptureFixture): util.create_file_and_commit("feat(user): this should appear in stdout") util.run_cli("bump", "--yes", "--git-output-to-stderr") out, _ = capsys.readouterr() @@ -799,10 +737,10 @@ def test_bump_invalid_manual_version_raises_exception( with pytest.raises(InvalidManualVersion) as excinfo: util.run_cli("bump", "--yes", manual_version) - expected_error_message = ( + assert ( f"[INVALID_MANUAL_VERSION]\nInvalid manual version: '{manual_version}'" + in str(excinfo.value) ) - assert expected_error_message in str(excinfo.value) @pytest.mark.usefixtures("tmp_commitizen_project") @@ -823,23 +761,18 @@ def test_bump_manual_version(util: UtilFixture, manual_version): util.create_file_and_commit("feat: new file") util.run_cli("bump", "--yes", manual_version) - tag_exists = git.tag_exist(manual_version) - assert tag_exists is True + assert git.tag_exist(manual_version) is True @pytest.mark.usefixtures("tmp_commitizen_project") def test_bump_manual_version_disallows_major_version_zero(util: UtilFixture): util.create_file_and_commit("feat: new file") - - manual_version = "0.2.0" - with pytest.raises(NotAllowed) as excinfo: - util.run_cli("bump", "--yes", "--major-version-zero", manual_version) + util.run_cli("bump", "--yes", "--major-version-zero", "0.2.0") - expected_error_message = ( - "--major-version-zero cannot be combined with MANUAL_VERSION" + assert "--major-version-zero cannot be combined with MANUAL_VERSION" in str( + excinfo.value ) - assert expected_error_message in str(excinfo.value) @pytest.mark.parametrize("commit_msg", ("feat: new file", "feat(user): new file")) @@ -862,8 +795,7 @@ def test_bump_with_pre_bump_hooks( util.create_file_and_commit(commit_msg) util.run_cli("bump", "--yes") - tag_exists = git.tag_exist("0.2.0") - assert tag_exists is True + assert git.tag_exist("0.2.0") is True run_mock.assert_has_calls( [ @@ -914,8 +846,7 @@ def test_bump_with_hooks_and_increment( util.create_file_and_commit("test: some test") util.run_cli("bump", "--yes", "--increment", "MINOR") - tag_exists = git.tag_exist("0.2.0") - assert tag_exists is True + assert git.tag_exist("0.2.0") is True @pytest.mark.usefixtures("tmp_git_project") @@ -929,7 +860,7 @@ def test_bump_use_version_provider(mocker: MockFixture, util: UtilFixture): util.create_file_and_commit("fix: fake commit") util.run_cli("bump", "--yes", "--changelog") - assert git.tag_exist("0.0.1") + assert git.tag_exist("0.0.1") is True get_provider.assert_called_once() mock.get_version.assert_called_once() mock.set_version.assert_called_once_with("0.0.1") @@ -950,9 +881,7 @@ def test_bump_command_prerelease_scheme_via_cli( "--version-scheme", "semver", ) - - tag_exists = git.tag_exist("0.2.0-a0") - assert tag_exists is True + assert git.tag_exist("0.2.0-a0") is True for version_file in [tmp_version_file, tmp_commitizen_cfg_file]: with open(version_file) as f: @@ -960,9 +889,7 @@ def test_bump_command_prerelease_scheme_via_cli( # PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE util.run_cli("bump", "--yes") - - tag_exists = git.tag_exist("0.2.0") - assert tag_exists is True + assert git.tag_exist("0.2.0") is True for version_file in [tmp_version_file, tmp_commitizen_cfg_file]: with open(version_file) as f: @@ -979,18 +906,14 @@ def test_bump_command_prerelease_scheme_via_config( tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") util.run_cli("bump", "--prerelease", "alpha", "--yes") - - tag_exists = git.tag_exist("0.2.0-a0") - assert tag_exists is True + assert git.tag_exist("0.2.0-a0") is True for version_file in [tmp_version_file, tmp_commitizen_cfg_file]: with open(version_file) as f: assert "0.2.0-a0" in f.read() util.run_cli("bump", "--prerelease", "alpha", "--yes") - - tag_exists = git.tag_exist("0.2.0-a1") - assert tag_exists is True + assert git.tag_exist("0.2.0-a1") is True for version_file in [tmp_version_file, tmp_commitizen_cfg_file]: with open(version_file) as f: @@ -998,9 +921,7 @@ def test_bump_command_prerelease_scheme_via_config( # PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE util.run_cli("bump", "--yes") - - tag_exists = git.tag_exist("0.2.0") - assert tag_exists is True + assert git.tag_exist("0.2.0") is True for version_file in [tmp_version_file, tmp_commitizen_cfg_file]: with open(version_file) as f: @@ -1017,18 +938,14 @@ def test_bump_command_prerelease_scheme_check_old_tags( tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") util.run_cli("bump", "--prerelease", "alpha", "--yes") - - tag_exists = git.tag_exist("v0.2.0-a0") - assert tag_exists is True + assert git.tag_exist("v0.2.0-a0") is True for version_file in [tmp_version_file, tmp_commitizen_cfg_file]: with open(version_file) as f: assert "0.2.0-a0" in f.read() util.run_cli("bump", "--prerelease", "alpha") - - tag_exists = git.tag_exist("v0.2.0-a1") - assert tag_exists is True + assert git.tag_exist("v0.2.0-a1") is True for version_file in [tmp_version_file, tmp_commitizen_cfg_file]: with open(version_file) as f: @@ -1036,9 +953,7 @@ def test_bump_command_prerelease_scheme_check_old_tags( # PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE util.run_cli("bump") - - tag_exists = git.tag_exist("v0.2.0") - assert tag_exists is True + assert git.tag_exist("v0.2.0") is True for version_file in [tmp_version_file, tmp_commitizen_cfg_file]: with open(version_file) as f: @@ -1057,11 +972,8 @@ def test_bump_command_prerelease_scheme_check_old_tags( ) def test_bump_with_plugin(util: UtilFixture, message: str, expected_tag: str): util.create_file_and_commit(message) - util.run_cli("--name", "cz_semver", "bump", "--yes") - - tag_exists = git.tag_exist(expected_tag) - assert tag_exists is True + assert git.tag_exist(expected_tag) is True @pytest.mark.usefixtures("tmp_commitizen_project") @@ -1078,11 +990,8 @@ def test_bump_with_major_version_zero_with_plugin( util: UtilFixture, message: str, expected_tag: str ): util.create_file_and_commit(message) - util.run_cli("--name", "cz_semver", "bump", "--yes", "--major-version-zero") - - tag_exists = git.tag_exist(expected_tag) - assert tag_exists is True + assert git.tag_exist(expected_tag) is True @pytest.mark.usefixtures("tmp_commitizen_project") @@ -1099,7 +1008,7 @@ def test_bump_command_version_type_deprecation(util: UtilFixture): "semver", ) - assert git.tag_exist("0.2.0-a0") + assert git.tag_exist("0.2.0-a0") is True @pytest.mark.usefixtures("tmp_commitizen_project") @@ -1118,7 +1027,7 @@ def test_bump_command_version_scheme_priority_over_version_type(util: UtilFixtur "pep440", ) - assert git.tag_exist("0.2.0a0") + assert git.tag_exist("0.2.0a0") is True @pytest.mark.parametrize( @@ -1169,8 +1078,7 @@ def test_bump_template_option_precedence( args.append("0.1.1") util.run_cli(*args) - out = changelog.read_text() - assert out == expected + assert changelog.read_text() == expected def test_bump_template_extras_precedence( @@ -1244,7 +1152,7 @@ def test_bump_template_extra_quotes( def test_bump_changelog_contains_increment_only( - tmp_commitizen_project, util: UtilFixture, capsys + tmp_commitizen_project: Path, util: UtilFixture, capsys: pytest.CaptureFixture ): """Issue 1024""" # Initialize commitizen up to v1.0.0 @@ -1275,7 +1183,7 @@ def test_bump_changelog_contains_increment_only( @pytest.mark.usefixtures("tmp_commitizen_project") -def test_bump_get_next(util: UtilFixture, capsys): +def test_bump_get_next(util: UtilFixture, capsys: pytest.CaptureFixture): util.create_file_and_commit("feat: new file") with pytest.raises(DryRunExit): @@ -1283,13 +1191,13 @@ def test_bump_get_next(util: UtilFixture, capsys): out, _ = capsys.readouterr() assert "0.2.0" in out - - tag_exists = git.tag_exist("0.2.0") - assert tag_exists is False + assert git.tag_exist("0.2.0") is False @pytest.mark.usefixtures("tmp_commitizen_project") -def test_bump_get_next_update_changelog_on_bump(util: UtilFixture, capsys, config_path): +def test_bump_get_next_update_changelog_on_bump( + util: UtilFixture, capsys: pytest.CaptureFixture, config_path: Path +): util.create_file_and_commit("feat: new file") with open(config_path, "a", encoding="utf-8") as fp: fp.write("update_changelog_on_bump = true\n") @@ -1299,9 +1207,7 @@ def test_bump_get_next_update_changelog_on_bump(util: UtilFixture, capsys, confi out, _ = capsys.readouterr() assert "0.2.0" in out - - tag_exists = git.tag_exist("0.2.0") - assert tag_exists is False + assert git.tag_exist("0.2.0") is False @pytest.mark.usefixtures("tmp_commitizen_project") @@ -1416,7 +1322,7 @@ def test_bump_detect_legacy_tags_from_scm( util.run_cli("bump", "--increment", "patch", "--changelog") - assert git.tag_exist("v0.4.3") + assert git.tag_exist("v0.4.3") is True def test_bump_warn_but_dont_fail_on_invalid_tags( @@ -1446,7 +1352,7 @@ def test_bump_warn_but_dont_fail_on_invalid_tags( _, err = capsys.readouterr() assert err.count("Invalid version tag: '0.4.3.deadbeaf'") == 1 - assert git.tag_exist("0.4.3") + assert git.tag_exist("0.4.3") is True def test_is_initial_tag(mocker: MockFixture, tmp_commitizen_project, util: UtilFixture): diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index 08a2b6cf7..dc9785214 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -30,6 +30,25 @@ from tests.utils import UtilFixture +@pytest.fixture +def changelog_jinja_file(tmp_project_root: Path) -> Path: + return tmp_project_root / "changelog.jinja" + + +@pytest.fixture +def changelog_tpl( + tmp_project_root: Path, any_changelog_format: ChangelogFormat +) -> Path: + return tmp_project_root / any_changelog_format.template + + +@pytest.fixture +def changelog_file( + tmp_project_root: Path, any_changelog_format: ChangelogFormat +) -> Path: + return tmp_project_root / any_changelog_format.default_changelog_file + + @pytest.mark.usefixtures("tmp_commitizen_project") def test_changelog_from_version_zero_point_two( capsys: pytest.CaptureFixture, @@ -391,21 +410,14 @@ def test_changelog_multiple_incremental_do_not_add_new_lines( util: UtilFixture, ): """Test for bug https://github.com/commitizen-tools/commitizen/issues/192""" - util.create_file_and_commit("feat: add new output") - - util.run_cli("changelog", "--incremental") - - util.create_file_and_commit("fix: output glitch") - - util.run_cli("changelog", "--incremental") - - util.create_file_and_commit("fix: no more explosions") - - util.run_cli("changelog", "--incremental") - - util.create_file_and_commit("feat: add more stuff") - - util.run_cli("changelog", "--incremental") + for commit_message in [ + "feat: add new output", + "fix: output glitch", + "fix: no more explosions", + "feat: add more stuff", + ]: + util.create_file_and_commit(commit_message) + util.run_cli("changelog", "--incremental") with open(changelog_path, encoding="utf-8") as f: out = f.read() @@ -415,28 +427,24 @@ def test_changelog_multiple_incremental_do_not_add_new_lines( @pytest.mark.usefixtures("tmp_commitizen_project") def test_changelog_incremental_newline_separates_new_content_from_old( - changelog_path: str, util: UtilFixture + changelog_path: str, + util: UtilFixture, + file_regression: FileRegressionFixture, ): """Test for https://github.com/commitizen-tools/commitizen/issues/509""" with open(changelog_path, "w", encoding="utf-8") as f: f.write("Pre-existing content that should be kept\n") util.create_file_and_commit("feat: add more cat videos") - util.run_cli("changelog", "--incremental") - with open(changelog_path, encoding="utf-8") as f: out = f.read() - assert ( - out - == "Pre-existing content that should be kept\n\n## Unreleased\n\n### Feat\n\n- add more cat videos\n" - ) + file_regression.check(out, extension=".md") def test_changelog_without_revision(tmp_commitizen_project, util: UtilFixture): - changelog_file = tmp_commitizen_project.join("CHANGELOG.md") - changelog_file.write( + tmp_commitizen_project.join("CHANGELOG.md").write( """ # Unreleased @@ -444,6 +452,14 @@ def test_changelog_without_revision(tmp_commitizen_project, util: UtilFixture): """ ) + # No revision + with pytest.raises(NoRevisionError): + util.run_cli("changelog", "--incremental") + + util.create_file_and_commit("feat: new file") + util.create_tag("2.0.0") + + # With different tag name and changelog content with pytest.raises(NoRevisionError): util.run_cli("changelog", "--incremental") @@ -455,64 +471,34 @@ def test_changelog_incremental_with_revision(util: UtilFixture): util.run_cli("changelog", "--incremental", "0.2.0") -def test_changelog_with_different_tag_name_and_changelog_content( - tmp_commitizen_project, util: UtilFixture -): - changelog_file = tmp_commitizen_project.join("CHANGELOG.md") - changelog_file.write( - """ - # Unreleased - - ## v1.0.0 - """ - ) - util.create_file_and_commit("feat: new file") - util.create_tag("2.0.0") - - with pytest.raises(NoRevisionError): - util.run_cli("changelog", "--incremental") - - @pytest.mark.usefixtures("chdir") def test_changelog_in_non_git_project(util: UtilFixture): with pytest.raises(NotAGitProjectError): util.run_cli("changelog", "--incremental") -@pytest.mark.usefixtures("tmp_commitizen_project") -def test_breaking_change_content_v1_beta( - capsys: pytest.CaptureFixture, - file_regression: FileRegressionFixture, - util: UtilFixture, -): - commit_message = ( +@pytest.mark.parametrize( + "commit_message", + [ "feat(users): email pattern corrected\n\n" "BREAKING CHANGE: migrate by renaming user to users\n\n" - "footer content" - ) - util.create_file_and_commit(commit_message) - with pytest.raises(DryRunExit): - util.run_cli("changelog", "--dry-run") - out, _ = capsys.readouterr() - file_regression.check(out, extension=".md") - - + "footer content", + "feat(users): email pattern corrected\n\n" + "body content\n\n" + "BREAKING CHANGE: migrate by renaming user to users", + ], +) @pytest.mark.usefixtures("tmp_commitizen_project") -def test_breaking_change_content_v1( +def test_breaking_change_content_v1_beta( capsys: pytest.CaptureFixture, file_regression: FileRegressionFixture, util: UtilFixture, + commit_message: str, ): - commit_message = ( - "feat(users): email pattern corrected\n\n" - "body content\n\n" - "BREAKING CHANGE: migrate by renaming user to users" - ) util.create_file_and_commit(commit_message) with pytest.raises(DryRunExit): util.run_cli("changelog", "--dry-run") out, _ = capsys.readouterr() - file_regression.check(out, extension=".md") @@ -803,9 +789,7 @@ def test_changelog_incremental_with_merge_prerelease( @pytest.mark.usefixtures("tmp_commitizen_project") -def test_changelog_with_filename_as_empty_string( - changelog_path: str, config_path: str, util: UtilFixture -): +def test_changelog_with_filename_as_empty_string(config_path: str, util: UtilFixture): with open(config_path, "a", encoding="utf-8") as f: f.write("changelog_file = true\n") @@ -928,7 +912,7 @@ def test_changelog_multiple_matching_tags( @pytest.mark.usefixtures("tmp_commitizen_project") def test_changelog_from_rev_range_default_tag_format( - config_path: str, changelog_path: str, util: UtilFixture + changelog_path: str, util: UtilFixture ): """Checks that rev_range is calculated with the default (None) tag format""" # create commit and tag @@ -1013,7 +997,6 @@ def test_changelog_from_rev_version_range_with_legacy_tags( file_regression: FileRegressionFixture, util: UtilFixture, ): - changelog = Path(changelog_path) Path(config_path).write_text( "\n".join( [ @@ -1036,7 +1019,7 @@ def test_changelog_from_rev_version_range_with_legacy_tags( util.create_tag("legacy-0.4.0") util.run_cli("changelog", "0.2.0..0.4.0") - file_regression.check(changelog.read_text(), extension=".md") + file_regression.check(Path(changelog_path).read_text(), extension=".md") @pytest.mark.usefixtures("tmp_commitizen_project") @@ -1081,7 +1064,6 @@ def test_changelog_from_rev_version_with_big_range_from_arg( def test_changelog_from_rev_latest_version_dry_run( capsys: pytest.CaptureFixture, config_path: str, - changelog_path: str, file_regression: FileRegressionFixture, util: UtilFixture, ): @@ -1107,7 +1089,11 @@ def test_changelog_from_rev_latest_version_dry_run( @pytest.mark.usefixtures("tmp_commitizen_project") -def test_invalid_subject_is_skipped(capsys: pytest.CaptureFixture, util: UtilFixture): +def test_invalid_subject_is_skipped( + capsys: pytest.CaptureFixture, + file_regression: FileRegressionFixture, + util: UtilFixture, +): """Fix #510""" non_conformant_commit_title = ( "Merge pull request #487 from manang/master\n\n" @@ -1119,7 +1105,7 @@ def test_invalid_subject_is_skipped(capsys: pytest.CaptureFixture, util: UtilFix util.run_cli("changelog", "--dry-run") out, _ = capsys.readouterr() - assert out == ("## Unreleased\n\n### Feat\n\n- a new world\n\n") + file_regression.check(out, extension=".md") @pytest.mark.usefixtures("tmp_commitizen_project") @@ -1170,10 +1156,8 @@ def test_empty_commit_list(mocker: MockFixture, util: UtilFixture): @pytest.mark.usefixtures("tmp_commitizen_project") @pytest.mark.freeze_time("2022-02-13") def test_changelog_prerelease_rev_with_use_scheme_semver( - mocker: MockFixture, capsys: pytest.CaptureFixture, config_path: str, - changelog_path: str, file_regression: FileRegressionFixture, util: UtilFixture, ): @@ -1190,27 +1174,23 @@ def test_changelog_prerelease_rev_with_use_scheme_semver( util.run_cli("bump", "--yes", "--prerelease", "alpha") capsys.readouterr() - tag_exists = git.tag_exist("0.3.0-a0") - assert tag_exists is True + assert git.tag_exist("0.3.0-a0") with pytest.raises(DryRunExit): util.run_cli("changelog", "0.3.0-a0", "--dry-run") out, _ = capsys.readouterr() - file_regression.check(out, extension=".md") util.run_cli("bump", "--yes", "--prerelease", "alpha") capsys.readouterr() - tag_exists = git.tag_exist("0.3.0-a1") - assert tag_exists is True + assert git.tag_exist("0.3.0-a1") with pytest.raises(DryRunExit): util.run_cli("changelog", "0.3.0-a1", "--dry-run") out, _ = capsys.readouterr() - file_regression.check(out, extension=".second-prerelease.md") @@ -1269,26 +1249,24 @@ def test_changelog_from_current_version_tag_with_nonversion_tag( write_patch = mocker.patch("commitizen.commands.changelog.out.write") - changelog = Changelog( - config, - { - "dry_run": True, - "incremental": False, - "unreleased_version": None, - "rev_range": "2.0.0", - }, - ) - with pytest.raises(DryRunExit): - changelog() - - full_changelog = "\ + Changelog( + config, + { + "dry_run": True, + "incremental": False, + "unreleased_version": None, + "rev_range": "2.0.0", + }, + )() + + write_patch.assert_called_with( + "\ ## 2.0.0 (2022-02-13)\n\n\ ### Feat\n\n\ - commit 2\n\ - commit 1\n" - - write_patch.assert_called_with(full_changelog) + ) @pytest.mark.parametrize( @@ -1302,23 +1280,21 @@ def test_changelog_from_current_version_tag_with_nonversion_tag( ), ) def test_changelog_template_option_precedence( - mocker: MockFixture, tmp_commitizen_project: Path, - any_changelog_format: ChangelogFormat, arg: str, cfg: str, expected: str, util: UtilFixture, + changelog_file: Path, + changelog_tpl: Path, ): project_root = Path(tmp_commitizen_project) cfg_template = project_root / "changelog.cfg" cmd_template = project_root / "changelog.cmd" - default_template = project_root / any_changelog_format.template - changelog = project_root / any_changelog_format.default_changelog_file cfg_template.write_text("from config") cmd_template.write_text("from cmd") - default_template.write_text("default") + changelog_tpl.write_text("default") util.create_file_and_commit("feat: new file") @@ -1339,26 +1315,22 @@ def test_changelog_template_option_precedence( testargs.append(arg) util.run_cli(*testargs) - out = changelog.read_text() - assert out == expected + assert changelog_file.read_text() == expected def test_changelog_template_extras_precedence( - mocker: MockFixture, - tmp_commitizen_project: Path, + changelog_tpl: Path, mock_plugin: BaseCommitizen, - any_changelog_format: ChangelogFormat, + pyproject: Path, + changelog_file: Path, util: UtilFixture, ): - project_root = Path(tmp_commitizen_project) - changelog_tpl = project_root / any_changelog_format.template changelog_tpl.write_text("{{first}} - {{second}} - {{third}}") mock_plugin.template_extras = dict( first="from-plugin", second="from-plugin", third="from-plugin" ) - pyproject = project_root / "pyproject.toml" pyproject.write_text( dedent( """\ @@ -1372,17 +1344,14 @@ def test_changelog_template_extras_precedence( ) util.create_file_and_commit("feat: new file") - util.run_cli("changelog", "--extra", "first=from-command") - changelog = project_root / any_changelog_format.default_changelog_file - assert changelog.read_text() == "from-command - from-config - from-plugin" + assert changelog_file.read_text() == "from-command - from-config - from-plugin" @pytest.mark.usefixtures("tmp_commitizen_project") @pytest.mark.freeze_time("2021-06-11") def test_changelog_only_tag_matching_tag_format_included_prefix( - mocker: MockFixture, changelog_path: Path, config_path: Path, util: UtilFixture, @@ -1406,7 +1375,6 @@ def test_changelog_only_tag_matching_tag_format_included_prefix( @pytest.mark.usefixtures("tmp_commitizen_project") def test_changelog_only_tag_matching_tag_format_included_prefix_sep( - mocker: MockFixture, changelog_path: Path, config_path: Path, util: UtilFixture, @@ -1484,7 +1452,6 @@ def test_changelog_only_tag_matching_tag_format_included_suffix_sep( @pytest.mark.usefixtures("tmp_commitizen_project") def test_changelog_legacy_tags( - mocker: MockFixture, changelog_path: Path, config_path: Path, util: UtilFixture, @@ -1519,7 +1486,6 @@ def test_changelog_legacy_tags( @pytest.mark.usefixtures("tmp_commitizen_project") @pytest.mark.freeze_time("2024-11-18") def test_changelog_incremental_change_tag_format( - mocker: MockFixture, changelog_path: Path, config_path: Path, file_regression: FileRegressionFixture, @@ -1563,7 +1529,6 @@ def test_changelog_incremental_change_tag_format( @pytest.mark.usefixtures("tmp_commitizen_project") def test_changelog_ignored_tags( - mocker: MockFixture, changelog_path: Path, config_path: Path, capsys: pytest.CaptureFixture, @@ -1588,29 +1553,27 @@ def test_changelog_ignored_tags( util.create_file_and_commit("feat: another new file") util.create_tag("not-ignored") util.run_cli("bump", "--changelog", "--yes") - out = open(changelog_path).read() - _, err = capsys.readouterr() + with open(changelog_path) as f: + out = f.read() assert "## ignore-0.1.0" not in out - assert "Invalid version tag: 'ignore-0.1.0'" not in err assert "## ignored" not in out - assert "Invalid version tag: 'ignored'" not in err assert "## not-ignored" not in out - assert "Invalid version tag: 'not-ignored'" in err assert "## v0.3.0" in out + + _, err = capsys.readouterr() + assert "Invalid version tag: 'ignore-0.1.0'" not in err + assert "Invalid version tag: 'ignored'" not in err + assert "Invalid version tag: 'not-ignored'" in err assert "Invalid version tag: 'v0.3.0'" not in err def test_changelog_template_extra_quotes( - tmp_commitizen_project: Path, - any_changelog_format: ChangelogFormat, + changelog_tpl: Path, + changelog_file: Path, util: UtilFixture, ): - project_root = Path(tmp_commitizen_project) - changelog_tpl = project_root / any_changelog_format.template changelog_tpl.write_text("{{first}} - {{second}} - {{third}}") - util.create_file_and_commit("feat: new file") - util.run_cli( "changelog", "-e", @@ -1621,99 +1584,80 @@ def test_changelog_template_extra_quotes( 'third="double quotes"', ) - changelog = project_root / any_changelog_format.default_changelog_file - assert changelog.read_text() == "no-quote - single quotes - double quotes" + assert changelog_file.read_text() == "no-quote - single quotes - double quotes" @pytest.mark.parametrize( "extra, expected", ( pytest.param("key=value=", "value=", id="2-equals"), - pytest.param("key==value", "=value", id="2-consecutives-equals"), + pytest.param("key==value", "=value", id="2-consecutive-equals"), pytest.param("key==value==", "=value==", id="multiple-equals"), ), ) def test_changelog_template_extra_weird_but_valid( - tmp_commitizen_project: Path, - any_changelog_format: ChangelogFormat, + changelog_tpl: Path, + changelog_file: Path, extra: str, expected: str, util: UtilFixture, ): - project_root = Path(tmp_commitizen_project) - changelog_tpl = project_root / any_changelog_format.template changelog_tpl.write_text("{{key}}") - util.create_file_and_commit("feat: new file") - util.run_cli("changelog", "-e", extra) - changelog = project_root / any_changelog_format.default_changelog_file - assert changelog.read_text() == expected + assert changelog_file.read_text() == expected @pytest.mark.parametrize("extra", ("no-equal", "", "=no-key")) def test_changelog_template_extra_bad_format( - tmp_commitizen_project: Path, - any_changelog_format: ChangelogFormat, + changelog_tpl: Path, extra: str, util: UtilFixture, ): - project_root = Path(tmp_commitizen_project) - changelog_tpl = project_root / any_changelog_format.template changelog_tpl.write_text("") - util.create_file_and_commit("feat: new file") - with pytest.raises(InvalidCommandArgumentError): util.run_cli("changelog", "-e", extra) def test_export_changelog_template_from_default( - tmp_commitizen_project: Path, any_changelog_format: ChangelogFormat, util: UtilFixture, + changelog_jinja_file: Path, repo_root: Path, ): - project_root = Path(tmp_commitizen_project) - target = project_root / "changelog.jinja" src = repo_root / "commitizen" / "templates" / any_changelog_format.template - util.run_cli("changelog", "--export-template", str(target)) + util.run_cli("changelog", "--export-template", str(changelog_jinja_file)) - assert target.exists() - assert target.read_text() == src.read_text() + assert changelog_jinja_file.exists() + assert changelog_jinja_file.read_text() == src.read_text() def test_export_changelog_template_from_plugin( - mocker: MockFixture, - tmp_commitizen_project: Path, + changelog_jinja_file: Path, mock_plugin: BaseCommitizen, changelog_format: ChangelogFormat, tmp_path: Path, util: UtilFixture, ): - project_root = Path(tmp_commitizen_project) - target = project_root / "changelog.jinja" src = tmp_path / changelog_format.template tpl = "I am a custom template" src.write_text(tpl) mock_plugin.template_loader = FileSystemLoader(tmp_path) - util.run_cli("changelog", "--export-template", str(target)) + util.run_cli("changelog", "--export-template", str(changelog_jinja_file)) - assert target.exists() - assert target.read_text() == tpl + assert changelog_jinja_file.exists() + assert changelog_jinja_file.read_text() == tpl def test_export_changelog_template_fails_when_template_has_no_filename( mocker: MockFixture, - tmp_commitizen_project: Path, + changelog_jinja_file: Path, util: UtilFixture, ): - project_root = Path(tmp_commitizen_project) - target = project_root / "changelog.jinja" - # Mock a template object with no filename class FakeTemplate: filename = None @@ -1724,7 +1668,7 @@ class FakeTemplate: ) with pytest.raises(NotAllowed) as exc_info: - util.run_cli("changelog", "--export-template", str(target)) + util.run_cli("changelog", "--export-template", str(changelog_jinja_file)) - assert not target.exists() + assert not changelog_jinja_file.exists() assert "Template filename is not set" in str(exc_info.value) diff --git a/tests/commands/test_changelog_command/test_breaking_change_content_v1_beta.md b/tests/commands/test_changelog_command/test_breaking_change_content_v1_beta_feat_users___email_pattern_corrected_n_nBREAKING_CHANGE__migrate_by_renaming_user_to_users_n_nfooter_content_.md similarity index 100% rename from tests/commands/test_changelog_command/test_breaking_change_content_v1_beta.md rename to tests/commands/test_changelog_command/test_breaking_change_content_v1_beta_feat_users___email_pattern_corrected_n_nBREAKING_CHANGE__migrate_by_renaming_user_to_users_n_nfooter_content_.md diff --git a/tests/commands/test_changelog_command/test_breaking_change_content_v1_beta_feat_users___email_pattern_corrected_n_nbody_content_n_nBREAKING_CHANGE__migrate_by_renaming_user_to_users_.md b/tests/commands/test_changelog_command/test_breaking_change_content_v1_beta_feat_users___email_pattern_corrected_n_nbody_content_n_nBREAKING_CHANGE__migrate_by_renaming_user_to_users_.md new file mode 100644 index 000000000..c4809739a --- /dev/null +++ b/tests/commands/test_changelog_command/test_breaking_change_content_v1_beta_feat_users___email_pattern_corrected_n_nbody_content_n_nBREAKING_CHANGE__migrate_by_renaming_user_to_users_.md @@ -0,0 +1,10 @@ +## Unreleased + +### BREAKING CHANGE + +- migrate by renaming user to users + +### Feat + +- **users**: email pattern corrected + diff --git a/tests/commands/test_changelog_command/test_changelog_incremental_newline_separates_new_content_from_old.md b/tests/commands/test_changelog_command/test_changelog_incremental_newline_separates_new_content_from_old.md new file mode 100644 index 000000000..788105a3f --- /dev/null +++ b/tests/commands/test_changelog_command/test_changelog_incremental_newline_separates_new_content_from_old.md @@ -0,0 +1,7 @@ +Pre-existing content that should be kept + +## Unreleased + +### Feat + +- add more cat videos diff --git a/tests/commands/test_changelog_command/test_invalid_subject_is_skipped.md b/tests/commands/test_changelog_command/test_invalid_subject_is_skipped.md new file mode 100644 index 000000000..d24ea7ba7 --- /dev/null +++ b/tests/commands/test_changelog_command/test_invalid_subject_is_skipped.md @@ -0,0 +1,6 @@ +## Unreleased + +### Feat + +- a new world + diff --git a/tests/commands/test_init_command.py b/tests/commands/test_init_command.py index e76652413..8398e784b 100644 --- a/tests/commands/test_init_command.py +++ b/tests/commands/test_init_command.py @@ -119,7 +119,7 @@ def test_init_without_choosing_tag(config: BaseConfig, mocker: MockFixture, tmpd commands.Init(config)() -@pytest.fixture(scope="function") +@pytest.fixture def pre_commit_installed(mocker: MockFixture): # Assume the `pre-commit` is installed mocker.patch( diff --git a/tests/conftest.py b/tests/conftest.py index 9b4c033e5..bcafc0926 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -72,7 +72,7 @@ def chdir(tmp_path: Path) -> Iterator[Path]: os.chdir(cwd) -@pytest.fixture(scope="function") +@pytest.fixture def tmp_git_project(tmpdir): with tmpdir.as_cwd(): cmd.run("git init") @@ -80,7 +80,7 @@ def tmp_git_project(tmpdir): yield tmpdir -@pytest.fixture(scope="function") +@pytest.fixture def tmp_commitizen_project(tmp_git_project): tmp_commitizen_cfg_file = tmp_git_project.join("pyproject.toml") tmp_commitizen_cfg_file.write('[tool.commitizen]\nversion="0.1.0"\n') @@ -88,7 +88,17 @@ def tmp_commitizen_project(tmp_git_project): yield tmp_git_project -@pytest.fixture(scope="function") +@pytest.fixture +def tmp_project_root(tmp_commitizen_project) -> Path: + return Path(tmp_commitizen_project) + + +@pytest.fixture +def pyproject(tmp_project_root: Path) -> Path: + return tmp_project_root / "pyproject.toml" + + +@pytest.fixture def tmp_commitizen_project_initial(tmp_git_project, util: UtilFixture): def _initial( config_extra: str | None = None, @@ -124,7 +134,7 @@ def _get_gpg_keyid(signer_mail): return _m.group(1) if _m else None -@pytest.fixture(scope="function") +@pytest.fixture def tmp_commitizen_project_with_gpg(tmp_commitizen_project): # create a temporary GPGHOME to store a temporary keyring. # Home path must be less than 104 characters @@ -148,14 +158,14 @@ def tmp_commitizen_project_with_gpg(tmp_commitizen_project): yield tmp_commitizen_project -@pytest.fixture() +@pytest.fixture def config(): _config = BaseConfig() _config.settings.update({"name": defaults.DEFAULT_SETTINGS["name"]}) return _config -@pytest.fixture() +@pytest.fixture def config_path() -> str: return os.path.join(os.getcwd(), "pyproject.toml") @@ -241,7 +251,7 @@ def info(self) -> str: return "" -@pytest.fixture() +@pytest.fixture def use_cz_semver(mocker): new_cz = {**registry, "cz_semver": SemverCommitizen} mocker.patch.dict("commitizen.cz.registry", new_cz) diff --git a/tests/test_bump_update_version_in_files.py b/tests/test_bump_update_version_in_files.py index 5fb812f19..2cc464e8f 100644 --- a/tests/test_bump_update_version_in_files.py +++ b/tests/test_bump_update_version_in_files.py @@ -26,32 +26,32 @@ def fixture(source: str, destination: str) -> Path: return fixture -@pytest.fixture(scope="function") +@pytest.fixture def commitizen_config_file(sample_file: SampleFileFixture) -> Path: return sample_file("sample_pyproject.toml", "pyproject.toml") -@pytest.fixture(scope="function") +@pytest.fixture def python_version_file(sample_file: SampleFileFixture) -> Path: return sample_file("sample_version.py", "__version__.py") -@pytest.fixture(scope="function") +@pytest.fixture def inconsistent_python_version_file(sample_file: SampleFileFixture) -> Path: return sample_file("inconsistent_version.py", "__version__.py") -@pytest.fixture(scope="function") +@pytest.fixture def random_location_version_file(sample_file: SampleFileFixture) -> Path: return sample_file("sample_cargo.lock", "Cargo.lock") -@pytest.fixture(scope="function") +@pytest.fixture def version_repeated_file(sample_file: SampleFileFixture) -> Path: return sample_file("repeated_version_number.json", "package.json") -@pytest.fixture(scope="function") +@pytest.fixture def docker_compose_file(sample_file: SampleFileFixture) -> Path: return sample_file("sample_docker_compose.yaml", "docker-compose.yaml") @@ -70,21 +70,21 @@ def multiple_versions_to_update_poetry_lock( return sample_file(request.param, "pyproject.toml") -@pytest.fixture(scope="function") +@pytest.fixture def multiple_versions_increase_string(tmp_path: Path) -> str: tmp_file = tmp_path / "anyfile" tmp_file.write_text(MULTIPLE_VERSIONS_INCREASE_STRING) return str(tmp_file) -@pytest.fixture(scope="function") +@pytest.fixture def multiple_versions_reduce_string(tmp_path: Path) -> str: tmp_file = tmp_path / "anyfile" tmp_file.write_text(MULTIPLE_VERSIONS_REDUCE_STRING) return str(tmp_file) -@pytest.fixture(scope="function") +@pytest.fixture def version_files( commitizen_config_file: Path, python_version_file: Path, diff --git a/tests/test_changelog.py b/tests/test_changelog.py index bcf90b11d..0831bcc1f 100644 --- a/tests/test_changelog.py +++ b/tests/test_changelog.py @@ -587,27 +587,25 @@ def changelog_content(data_dir: Path) -> str: def test_get_commit_tag_is_a_version(gitcommits, tags): commit = gitcommits[0] tag = git.GitTag(*TAGS[0]) - current_key = changelog.get_commit_tag(commit, tags) - assert current_key == tag + assert changelog.get_commit_tag(commit, tags) == tag def test_get_commit_tag_is_None(gitcommits, tags): commit = gitcommits[1] - current_key = changelog.get_commit_tag(commit, tags) - assert current_key is None + assert changelog.get_commit_tag(commit, tags) is None @pytest.mark.parametrize("test_input", TAGS) def test_valid_tag_included_in_changelog(test_input): tag = git.GitTag(*test_input) rules = changelog.TagRules() - assert rules.include_in_changelog(tag) + assert rules.include_in_changelog(tag) is True def test_invalid_tag_included_in_changelog(): tag = git.GitTag("not_a_version", "rev", "date") rules = changelog.TagRules() - assert not rules.include_in_changelog(tag) + assert rules.include_in_changelog(tag) is False COMMITS_TREE = ( @@ -1188,12 +1186,9 @@ def test_generate_tree_from_commits(gitcommits, tags, merge_prereleases): def test_generate_tree_from_commits_with_no_commits(tags): - gitcommits = [] parser = ConventionalCommitsCz.commit_parser changelog_pattern = ConventionalCommitsCz.bump_pattern - tree = changelog.generate_tree_from_commits( - gitcommits, tags, parser, changelog_pattern - ) + tree = changelog.generate_tree_from_commits([], tags, parser, changelog_pattern) assert tuple(tree) == ({"changes": {}, "date": "", "version": "Unreleased"},)