diff --git a/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py index 94b40e94e..955213e7c 100644 --- a/tests/commands/test_bump_command.py +++ b/tests/commands/test_bump_command.py @@ -2,7 +2,6 @@ import inspect import re -from pathlib import Path from textwrap import dedent from typing import TYPE_CHECKING from unittest.mock import call @@ -29,7 +28,8 @@ ) if TYPE_CHECKING: - import py + from pathlib import Path + from pytest_mock import MockFixture from pytest_regressions.file_regression import FileRegressionFixture @@ -117,8 +117,8 @@ def test_bump_minor_increment_annotated_config_file( def test_bump_minor_increment_signed_config_file( commit_msg: str, util: UtilFixture, tmp_commitizen_project_with_gpg ): - tmp_commitizen_cfg_file = tmp_commitizen_project_with_gpg.join("pyproject.toml") - with Path(tmp_commitizen_cfg_file).open("a", encoding="utf-8") as f: + tmp_commitizen_cfg_file = tmp_commitizen_project_with_gpg / "pyproject.toml" + with tmp_commitizen_cfg_file.open("a", encoding="utf-8") as f: f.write("\ngpg_sign = 1") util.create_file_and_commit(commit_msg) util.run_cli("bump", "--yes") @@ -360,11 +360,11 @@ def test_bump_when_no_new_commit(util: UtilFixture): def test_bump_when_version_inconsistent_in_version_files( tmp_commitizen_project, util: UtilFixture ): - tmp_version_file = tmp_commitizen_project.join("__version__.py") - tmp_version_file.write("100.999.10000") - tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") + tmp_version_file = tmp_commitizen_project / "__version__.py" + tmp_version_file.write_text("100.999.10000") + tmp_commitizen_cfg_file = tmp_commitizen_project / "pyproject.toml" tmp_version_file_string = str(tmp_version_file).replace("\\", "/") - with Path(tmp_commitizen_cfg_file).open("a", encoding="utf-8") as f: + with tmp_commitizen_cfg_file.open("a", encoding="utf-8") as f: f.write(f'\nversion_files = ["{tmp_version_file_string}"]') util.create_file_and_commit("feat: new file") @@ -378,17 +378,17 @@ def test_bump_when_version_inconsistent_in_version_files( def test_bump_major_version_zero_when_major_is_not_zero( tmp_commitizen_project, util: UtilFixture ): - tmp_version_file = tmp_commitizen_project.join("__version__.py") - tmp_version_file.write("1.0.0") + tmp_version_file = tmp_commitizen_project / "__version__.py" + tmp_version_file.write_text("1.0.0") tmp_version_file_string = str(tmp_version_file).replace("\\", "/") - tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") - tmp_commitizen_cfg_file.write( + tmp_commitizen_cfg_file = tmp_commitizen_project / "pyproject.toml" + tmp_commitizen_cfg_file.write_text( f"[tool.commitizen]\n" 'version="1.0.0"\n' f'version_files = ["{str(tmp_version_file_string)}"]' ) - tmp_changelog_file = tmp_commitizen_project.join("CHANGELOG.md") - tmp_changelog_file.write("## v1.0.0") + tmp_changelog_file = tmp_commitizen_project / "CHANGELOG.md" + tmp_changelog_file.write_text("## v1.0.0") util.create_file_and_commit("feat(user): new file") util.create_tag("v1.0.0") @@ -403,11 +403,11 @@ def test_bump_major_version_zero_when_major_is_not_zero( def test_bump_files_only(tmp_commitizen_project, util: UtilFixture): - tmp_version_file = tmp_commitizen_project.join("__version__.py") - tmp_version_file.write("0.1.0") - tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") + tmp_version_file = tmp_commitizen_project / "__version__.py" + tmp_version_file.write_text("0.1.0") + tmp_commitizen_cfg_file = tmp_commitizen_project / "pyproject.toml" tmp_version_file_string = str(tmp_version_file).replace("\\", "/") - with Path(tmp_commitizen_cfg_file).open("a", encoding="utf-8") as f: + with tmp_commitizen_cfg_file.open("a", encoding="utf-8") as f: f.write(f'\nversion_files = ["{tmp_version_file_string}"]') util.create_file_and_commit("feat: new user interface") @@ -420,17 +420,17 @@ def test_bump_files_only(tmp_commitizen_project, util: UtilFixture): assert git.tag_exist("0.3.0") is False - assert "0.3.0" in Path(tmp_version_file).read_text(encoding="utf-8") + assert "0.3.0" in tmp_version_file.read_text(encoding="utf-8") - assert "0.3.0" in Path(tmp_commitizen_cfg_file).read_text(encoding="utf-8") + assert "0.3.0" in tmp_commitizen_cfg_file.read_text(encoding="utf-8") def test_bump_local_version(tmp_commitizen_project, util: UtilFixture): - tmp_version_file = tmp_commitizen_project.join("__version__.py") - tmp_version_file.write("4.5.1+0.1.0") - tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") + tmp_version_file = tmp_commitizen_project / "__version__.py" + tmp_version_file.write_text("4.5.1+0.1.0") + tmp_commitizen_cfg_file = tmp_commitizen_project / "pyproject.toml" tmp_version_file_string = str(tmp_version_file).replace("\\", "/") - tmp_commitizen_cfg_file.write( + tmp_commitizen_cfg_file.write_text( f"[tool.commitizen]\n" 'version="4.5.1+0.1.0"\n' f'version_files = ["{tmp_version_file_string}"]' @@ -440,7 +440,7 @@ def test_bump_local_version(tmp_commitizen_project, util: UtilFixture): util.run_cli("bump", "--yes", "--local-version") assert git.tag_exist("4.5.1+0.2.0") is True - assert "4.5.1+0.2.0" in Path(tmp_version_file).read_text(encoding="utf-8") + assert "4.5.1+0.2.0" in tmp_version_file.read_text(encoding="utf-8") @pytest.mark.usefixtures("tmp_commitizen_project") @@ -455,11 +455,11 @@ def test_bump_dry_run(util: UtilFixture, capsys: pytest.CaptureFixture): assert git.tag_exist("0.2.0") is False -def test_bump_in_non_git_project(tmpdir, util: UtilFixture): - with tmpdir.as_cwd(): - with pytest.raises(NotAGitProjectError): - with pytest.raises(ExpectedExit): - util.run_cli("bump", "--yes") +def test_bump_in_non_git_project(tmp_path, monkeypatch, util: UtilFixture): + monkeypatch.chdir(tmp_path) + with pytest.raises(NotAGitProjectError): + with pytest.raises(ExpectedExit): + util.run_cli("bump", "--yes") def test_none_increment_exit_should_be_a_class(): @@ -676,13 +676,13 @@ def test_bump_changelog_command_commits_untracked_changelog_and_version_files( - Call commitizen main cli and assert that the `CHANGELOG.md` and the version file were committed. """ - with tmp_commitizen_project.join("pyproject.toml").open( + with (tmp_commitizen_project / "pyproject.toml").open( mode="a", encoding="utf-8", ) as commitizen_config: commitizen_config.write(f"version_files = [\n'{version_regex}'\n]") - with tmp_commitizen_project.join(version_filepath).open( + with (tmp_commitizen_project / version_filepath).open( mode="a+", encoding="utf-8" ) as version_file: version_file.write(version_file_content) @@ -789,12 +789,10 @@ def test_bump_version_with_less_components_in_config( assert git.tag_exist(expected_version_after_bump) is True for version_file in [ - tmp_commitizen_project.join("__version__.py"), - tmp_commitizen_project.join("pyproject.toml"), + tmp_commitizen_project / "__version__.py", + tmp_commitizen_project / "pyproject.toml", ]: - assert expected_version_after_bump in Path(version_file).read_text( - encoding="utf-8" - ) + assert expected_version_after_bump in version_file.read_text(encoding="utf-8") @pytest.mark.parametrize("commit_msg", ["feat: new file", "feat(user): new file"]) @@ -804,8 +802,8 @@ def test_bump_with_pre_bump_hooks( pre_bump_hook = "scripts/pre_bump_hook.sh" post_bump_hook = "scripts/post_bump_hook.sh" - tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") - with Path(tmp_commitizen_cfg_file).open("a", encoding="utf-8") as f: + tmp_commitizen_cfg_file = tmp_commitizen_project / "pyproject.toml" + with tmp_commitizen_cfg_file.open("a", encoding="utf-8") as f: f.write( f'\npre_bump_hooks = ["{pre_bump_hook}"]\n' f'post_bump_hooks = ["{post_bump_hook}"]\n' @@ -855,8 +853,8 @@ def test_bump_with_hooks_and_increment( pre_bump_hook = "scripts/pre_bump_hook.sh" post_bump_hook = "scripts/post_bump_hook.sh" - tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") - with Path(tmp_commitizen_cfg_file).open("a", encoding="utf-8") as f: + tmp_commitizen_cfg_file = tmp_commitizen_project / "pyproject.toml" + with tmp_commitizen_cfg_file.open("a", encoding="utf-8") as f: f.write( f'\npre_bump_hooks = ["{pre_bump_hook}"]\n' f'post_bump_hooks = ["{post_bump_hook}"]\n' @@ -892,8 +890,8 @@ def test_bump_command_prerelease_scheme_via_cli( tmp_commitizen_project_initial, util: UtilFixture ): tmp_commitizen_project = tmp_commitizen_project_initial() - tmp_version_file = tmp_commitizen_project.join("__version__.py") - tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") + tmp_version_file = tmp_commitizen_project / "__version__.py" + tmp_commitizen_cfg_file = tmp_commitizen_project / "pyproject.toml" util.run_cli( "bump", @@ -906,14 +904,14 @@ def test_bump_command_prerelease_scheme_via_cli( assert git.tag_exist("0.2.0-a0") is True for version_file in [tmp_version_file, tmp_commitizen_cfg_file]: - assert "0.2.0-a0" in Path(version_file).read_text(encoding="utf-8") + assert "0.2.0-a0" in version_file.read_text(encoding="utf-8") # PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE util.run_cli("bump", "--yes") assert git.tag_exist("0.2.0") is True for version_file in [tmp_version_file, tmp_commitizen_cfg_file]: - assert "0.2.0" in Path(version_file).read_text(encoding="utf-8") + assert "0.2.0" in version_file.read_text(encoding="utf-8") def test_bump_command_prerelease_scheme_via_config( @@ -922,27 +920,27 @@ def test_bump_command_prerelease_scheme_via_config( tmp_commitizen_project = tmp_commitizen_project_initial( config_extra='version_scheme = "semver"\n', ) - tmp_version_file = tmp_commitizen_project.join("__version__.py") - tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") + tmp_version_file = tmp_commitizen_project / "__version__.py" + tmp_commitizen_cfg_file = tmp_commitizen_project / "pyproject.toml" util.run_cli("bump", "--prerelease", "alpha", "--yes") assert git.tag_exist("0.2.0-a0") is True for version_file in [tmp_version_file, tmp_commitizen_cfg_file]: - assert "0.2.0-a0" in Path(version_file).read_text(encoding="utf-8") + assert "0.2.0-a0" in version_file.read_text(encoding="utf-8") util.run_cli("bump", "--prerelease", "alpha", "--yes") assert git.tag_exist("0.2.0-a1") is True for version_file in [tmp_version_file, tmp_commitizen_cfg_file]: - assert "0.2.0-a1" in Path(version_file).read_text(encoding="utf-8") + assert "0.2.0-a1" in version_file.read_text(encoding="utf-8") # PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE util.run_cli("bump", "--yes") assert git.tag_exist("0.2.0") is True for version_file in [tmp_version_file, tmp_commitizen_cfg_file]: - assert "0.2.0" in Path(version_file).read_text(encoding="utf-8") + assert "0.2.0" in version_file.read_text(encoding="utf-8") def test_bump_command_prerelease_scheme_check_old_tags( @@ -951,27 +949,27 @@ def test_bump_command_prerelease_scheme_check_old_tags( tmp_commitizen_project = tmp_commitizen_project_initial( config_extra=('tag_format = "v$version"\nversion_scheme = "semver"\n'), ) - tmp_version_file = tmp_commitizen_project.join("__version__.py") - tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") + tmp_version_file = tmp_commitizen_project / "__version__.py" + tmp_commitizen_cfg_file = tmp_commitizen_project / "pyproject.toml" util.run_cli("bump", "--prerelease", "alpha", "--yes") assert git.tag_exist("v0.2.0-a0") is True for version_file in [tmp_version_file, tmp_commitizen_cfg_file]: - assert "0.2.0-a0" in Path(version_file).read_text(encoding="utf-8") + assert "0.2.0-a0" in version_file.read_text(encoding="utf-8") util.run_cli("bump", "--prerelease", "alpha") assert git.tag_exist("v0.2.0-a1") is True for version_file in [tmp_version_file, tmp_commitizen_cfg_file]: - assert "0.2.0-a1" in Path(version_file).read_text(encoding="utf-8") + assert "0.2.0-a1" in version_file.read_text(encoding="utf-8") # PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE util.run_cli("bump") assert git.tag_exist("v0.2.0") is True for version_file in [tmp_version_file, tmp_commitizen_cfg_file]: - assert "0.2.0" in Path(version_file).read_text(encoding="utf-8") + assert "0.2.0" in version_file.read_text(encoding="utf-8") @pytest.mark.usefixtures("tmp_commitizen_project") @@ -1062,7 +1060,7 @@ def test_bump_template_option_precedence( cfg: str, expected: str, ): - project_root = Path(tmp_commitizen_project) + project_root = tmp_commitizen_project cfg_template = project_root / "changelog.cfg" cmd_template = project_root / "changelog.cmd" default_template = project_root / any_changelog_format.template @@ -1101,7 +1099,7 @@ def test_bump_template_extras_precedence( any_changelog_format: ChangelogFormat, mock_plugin: BaseCommitizen, ): - project_root = Path(tmp_commitizen_project) + project_root = tmp_commitizen_project changelog_tpl = project_root / any_changelog_format.template changelog_tpl.write_text("{{first}} - {{second}} - {{third}}") @@ -1142,7 +1140,7 @@ def test_bump_template_extra_quotes( util: UtilFixture, any_changelog_format: ChangelogFormat, ): - project_root = Path(tmp_commitizen_project) + project_root = tmp_commitizen_project changelog_tpl = project_root / any_changelog_format.template changelog_tpl.write_text("{{first}} - {{second}} - {{third}}") @@ -1170,7 +1168,7 @@ def test_bump_changelog_contains_increment_only( ): """Issue 1024""" # Initialize commitizen up to v1.0.0 - project_root = Path(tmp_commitizen_project) + project_root = tmp_commitizen_project tmp_commitizen_cfg_file = project_root / "pyproject.toml" tmp_commitizen_cfg_file.write_text( '[tool.commitizen]\nversion="1.0.0"\nupdate_changelog_on_bump = true\n' @@ -1233,90 +1231,90 @@ def test_bump_get_next__no_eligible_commits_raises(util: UtilFixture): def test_bump_allow_no_commit_with_no_commit( - tmp_commitizen_project, util: UtilFixture, capsys + tmp_commitizen_project, monkeypatch, util: UtilFixture, capsys ): - with tmp_commitizen_project.as_cwd(): - # Create the first commit and bump to 1.0.0 - util.create_file_and_commit("feat(user)!: new file") - util.run_cli("bump", "--yes") + monkeypatch.chdir(tmp_commitizen_project) + # Create the first commit and bump to 1.0.0 + util.create_file_and_commit("feat(user)!: new file") + util.run_cli("bump", "--yes") - # Verify NoCommitsFoundError should be raised - # when there's no new commit and "--allow-no-commit" is not set - with pytest.raises(NoCommitsFoundError): - util.run_cli("bump") + # Verify NoCommitsFoundError should be raised + # when there's no new commit and "--allow-no-commit" is not set + with pytest.raises(NoCommitsFoundError): + util.run_cli("bump") - # bump to 1.0.1 with new commit when "--allow-no-commit" is set - util.run_cli("bump", "--allow-no-commit") - out, _ = capsys.readouterr() - assert "bump: version 1.0.0 → 1.0.1" in out + # bump to 1.0.1 with new commit when "--allow-no-commit" is set + util.run_cli("bump", "--allow-no-commit") + out, _ = capsys.readouterr() + assert "bump: version 1.0.0 → 1.0.1" in out def test_bump_allow_no_commit_with_no_eligible_commit( - tmp_commitizen_project, util: UtilFixture, capsys + tmp_commitizen_project, monkeypatch, util: UtilFixture, capsys ): - with tmp_commitizen_project.as_cwd(): - # Create the first commit and bump to 1.0.0 - util.create_file_and_commit("feat(user)!: new file") - util.run_cli("bump", "--yes") + monkeypatch.chdir(tmp_commitizen_project) + # Create the first commit and bump to 1.0.0 + util.create_file_and_commit("feat(user)!: new file") + util.run_cli("bump", "--yes") - # Create a commit that is ineligible to bump - util.create_file_and_commit("docs(bump): add description for allow no commit") + # Create a commit that is ineligible to bump + util.create_file_and_commit("docs(bump): add description for allow no commit") - # Verify NoneIncrementExit should be raised - # when there's no eligible bumping commit and "--allow-no-commit" is not set - with pytest.raises(NoneIncrementExit): - util.run_cli("bump", "--yes") + # Verify NoneIncrementExit should be raised + # when there's no eligible bumping commit and "--allow-no-commit" is not set + with pytest.raises(NoneIncrementExit): + util.run_cli("bump", "--yes") - # bump to 1.0.1 with ineligible commit when "--allow-no-commit" is set - util.run_cli("bump", "--allow-no-commit") - out, _ = capsys.readouterr() - assert "bump: version 1.0.0 → 1.0.1" in out + # bump to 1.0.1 with ineligible commit when "--allow-no-commit" is set + util.run_cli("bump", "--allow-no-commit") + out, _ = capsys.readouterr() + assert "bump: version 1.0.0 → 1.0.1" in out def test_bump_allow_no_commit_with_increment( - tmp_commitizen_project, util: UtilFixture, capsys + tmp_commitizen_project, monkeypatch, util: UtilFixture, capsys ): - with tmp_commitizen_project.as_cwd(): - # # Create the first commit and bump to 1.0.0 - util.create_file_and_commit("feat(user)!: new file") - util.run_cli("bump", "--yes") + monkeypatch.chdir(tmp_commitizen_project) + # # Create the first commit and bump to 1.0.0 + util.create_file_and_commit("feat(user)!: new file") + util.run_cli("bump", "--yes") - # Verify NoCommitsFoundError should be raised - # when there's no new commit and "--allow-no-commit" is not set - with pytest.raises(NoCommitsFoundError): - util.run_cli("bump", "--yes") + # Verify NoCommitsFoundError should be raised + # when there's no new commit and "--allow-no-commit" is not set + with pytest.raises(NoCommitsFoundError): + util.run_cli("bump", "--yes") - # bump to 1.1.0 with no new commit when "--allow-no-commit" is set - # and increment is specified - util.run_cli("bump", "--yes", "--allow-no-commit", "--increment", "MINOR") - out, _ = capsys.readouterr() - assert "bump: version 1.0.0 → 1.1.0" in out + # bump to 1.1.0 with no new commit when "--allow-no-commit" is set + # and increment is specified + util.run_cli("bump", "--yes", "--allow-no-commit", "--increment", "MINOR") + out, _ = capsys.readouterr() + assert "bump: version 1.0.0 → 1.1.0" in out def test_bump_allow_no_commit_with_manual_version( - tmp_commitizen_project, util: UtilFixture, capsys + tmp_commitizen_project, monkeypatch, util: UtilFixture, capsys ): - with tmp_commitizen_project.as_cwd(): - # # Create the first commit and bump to 1.0.0 - util.create_file_and_commit("feat(user)!: new file") - util.run_cli("bump", "--yes") + monkeypatch.chdir(tmp_commitizen_project) + # # Create the first commit and bump to 1.0.0 + util.create_file_and_commit("feat(user)!: new file") + util.run_cli("bump", "--yes") - # Verify NoCommitsFoundError should be raised - # when there's no new commit and "--allow-no-commit" is not set - with pytest.raises(NoCommitsFoundError): - util.run_cli("bump", "--yes") + # Verify NoCommitsFoundError should be raised + # when there's no new commit and "--allow-no-commit" is not set + with pytest.raises(NoCommitsFoundError): + util.run_cli("bump", "--yes") - # bump to 1.1.0 with no new commit when "--allow-no-commit" is set - # and increment is specified - util.run_cli("bump", "--yes", "--allow-no-commit", "2.0.0") - out, _ = capsys.readouterr() - assert "bump: version 1.0.0 → 2.0.0" in out + # bump to 1.1.0 with no new commit when "--allow-no-commit" is set + # and increment is specified + util.run_cli("bump", "--yes", "--allow-no-commit", "2.0.0") + out, _ = capsys.readouterr() + assert "bump: version 1.0.0 → 2.0.0" in out def test_bump_detect_legacy_tags_from_scm( - tmp_commitizen_project: py.path.local, util: UtilFixture + tmp_commitizen_project: Path, util: UtilFixture ): - project_root = Path(tmp_commitizen_project) + project_root = tmp_commitizen_project tmp_commitizen_cfg_file = project_root / "pyproject.toml" tmp_commitizen_cfg_file.write_text( "\n".join( @@ -1340,11 +1338,11 @@ def test_bump_detect_legacy_tags_from_scm( def test_bump_warn_but_dont_fail_on_invalid_tags( - tmp_commitizen_project: py.path.local, + tmp_commitizen_project: Path, util: UtilFixture, capsys: pytest.CaptureFixture, ): - project_root = Path(tmp_commitizen_project) + project_root = tmp_commitizen_project tmp_commitizen_cfg_file = project_root / "pyproject.toml" tmp_commitizen_cfg_file.write_text( "\n".join( diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index 6fe2289b0..b2d024ac7 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -31,22 +31,22 @@ @pytest.fixture -def changelog_jinja_file(tmp_project_root: Path) -> Path: - return tmp_project_root / "changelog.jinja" +def changelog_jinja_file(tmp_commitizen_project: Path) -> Path: + return tmp_commitizen_project / "changelog.jinja" @pytest.fixture def changelog_tpl( - tmp_project_root: Path, any_changelog_format: ChangelogFormat + tmp_commitizen_project: Path, any_changelog_format: ChangelogFormat ) -> Path: - return tmp_project_root / any_changelog_format.template + return tmp_commitizen_project / any_changelog_format.template @pytest.fixture def changelog_file( - tmp_project_root: Path, any_changelog_format: ChangelogFormat + tmp_commitizen_project: Path, any_changelog_format: ChangelogFormat ) -> Path: - return tmp_project_root / any_changelog_format.default_changelog_file + return tmp_commitizen_project / any_changelog_format.default_changelog_file @pytest.mark.usefixtures("tmp_commitizen_project") @@ -437,7 +437,7 @@ def test_changelog_incremental_newline_separates_new_content_from_old( def test_changelog_without_revision(tmp_commitizen_project, util: UtilFixture): - tmp_commitizen_project.join("CHANGELOG.md").write( + (tmp_commitizen_project / "CHANGELOG.md").write_text( """ # Unreleased @@ -1266,7 +1266,7 @@ def test_changelog_template_option_precedence( changelog_file: Path, changelog_tpl: Path, ): - project_root = Path(tmp_commitizen_project) + project_root = tmp_commitizen_project cfg_template = project_root / "changelog.cfg" cmd_template = project_root / "changelog.cmd" @@ -1655,7 +1655,7 @@ def test_changelog_template_incremental_variable( Test that the changelog template is not rendered when the incremental flag is not set. Reference: https://github.com/commitizen-tools/commitizen/discussions/1620 """ - project_root = Path(tmp_commitizen_project) + project_root = tmp_commitizen_project changelog_tpl = project_root / any_changelog_format.template changelog_tpl.write_text( dedent(""" diff --git a/tests/commands/test_check_command.py b/tests/commands/test_check_command.py index 0ea5062ec..f3f860313 100644 --- a/tests/commands/test_check_command.py +++ b/tests/commands/test_check_command.py @@ -119,9 +119,9 @@ def test_check_conventional_commit_succeeds( ), ], ) -def test_check_no_conventional_commit(commit_msg, config, tmpdir): - tempfile = tmpdir.join("temp_commit_file") - tempfile.write(commit_msg) +def test_check_no_conventional_commit(commit_msg, config, tmp_path): + tempfile = tmp_path / "temp_commit_file" + tempfile.write_text(commit_msg) with pytest.raises(InvalidCommitMessageError): commands.Check(config=config, arguments={"commit_msg_file": tempfile})() @@ -136,9 +136,11 @@ def test_check_no_conventional_commit(commit_msg, config, tmpdir): "bump: 0.0.1 -> 1.0.0", ], ) -def test_check_conventional_commit(commit_msg, config, success_mock: MockType, tmpdir): - tempfile = tmpdir.join("temp_commit_file") - tempfile.write(commit_msg) +def test_check_conventional_commit( + commit_msg, config, success_mock: MockType, tmp_path +): + tempfile = tmp_path / "temp_commit_file" + tempfile.write_text(commit_msg) commands.Check(config=config, arguments={"commit_msg_file": tempfile})() success_mock.assert_called_once() diff --git a/tests/commands/test_commit_command.py b/tests/commands/test_commit_command.py index 7e6c04d53..2322cb3cb 100644 --- a/tests/commands/test_commit_command.py +++ b/tests/commands/test_commit_command.py @@ -51,10 +51,9 @@ def staging_is_clean(mocker: MockFixture, tmp_git_project): @pytest.fixture def backup_file(tmp_git_project, monkeypatch): """Write backup message so Commit finds it when run from tmp_git_project.""" - with tmp_git_project.as_cwd(): - path = get_backup_file_path() - path.write_text("backup commit", encoding="utf-8") monkeypatch.chdir(tmp_git_project) + path = get_backup_file_path() + path.write_text("backup commit", encoding="utf-8") @pytest.mark.usefixtures("staging_is_clean", "commit_mock", "prompt_mock_feat") @@ -263,10 +262,10 @@ def test_commit_when_no_user_answer(config, mocker: MockFixture): commands.Commit(config, {})() -def test_commit_in_non_git_project(tmpdir, config): - with tmpdir.as_cwd(): - with pytest.raises(NotAGitProjectError): - commands.Commit(config, {}) +def test_commit_in_non_git_project(tmp_path, monkeypatch, config): + monkeypatch.chdir(tmp_path) + with pytest.raises(NotAGitProjectError): + commands.Commit(config, {}) @pytest.mark.usefixtures("staging_is_clean", "commit_mock", "prompt_mock_feat") diff --git a/tests/commands/test_init_command.py b/tests/commands/test_init_command.py index 7d1f2f2be..db47fd064 100644 --- a/tests/commands/test_init_command.py +++ b/tests/commands/test_init_command.py @@ -61,7 +61,7 @@ def unsafe_ask(self): def test_init_without_setup_pre_commit_hook( - tmpdir, mocker: MockFixture, config: BaseConfig + tmp_path, monkeypatch, mocker: MockFixture, config: BaseConfig ): mocker.patch( "questionary.select", @@ -76,13 +76,13 @@ def test_init_without_setup_pre_commit_hook( mocker.patch("questionary.text", return_value=FakeQuestion("$version")) # Return None to skip hook installation mocker.patch("questionary.checkbox", return_value=FakeQuestion(None)) - with tmpdir.as_cwd(): - commands.Init(config)() + monkeypatch.chdir(tmp_path) + commands.Init(config)() - config_data = Path("pyproject.toml").read_text(encoding="utf-8") - assert config_data == expected_config + config_data = Path("pyproject.toml").read_text(encoding="utf-8") + assert config_data == expected_config - assert not Path(pre_commit_config_filename).exists() + assert not Path(pre_commit_config_filename).exists() def test_init_when_config_already_exists(config: BaseConfig, capsys): @@ -95,7 +95,9 @@ def test_init_when_config_already_exists(config: BaseConfig, capsys): assert captured.out == f"Config file {path} already exists\n" -def test_init_without_choosing_tag(config: BaseConfig, mocker: MockFixture, tmpdir): +def test_init_without_choosing_tag( + config: BaseConfig, mocker: MockFixture, tmp_path, monkeypatch +): mocker.patch( "commitizen.commands.init.get_tag_names", return_value=["0.0.2", "0.0.1"] ) @@ -112,9 +114,9 @@ def test_init_without_choosing_tag(config: BaseConfig, mocker: MockFixture, tmpd mocker.patch("questionary.confirm", return_value=FakeQuestion(False)) mocker.patch("questionary.text", return_value=FakeQuestion("y")) - with tmpdir.as_cwd(): - with pytest.raises(NoAnswersError): - commands.Init(config)() + monkeypatch.chdir(tmp_path) + with pytest.raises(NoAnswersError): + commands.Init(config)() @pytest.fixture @@ -177,26 +179,26 @@ def check_pre_commit_config(expected: list[dict[str, Any]]): @pytest.mark.usefixtures("pre_commit_installed") class TestPreCommitCases: def test_no_existing_pre_commit_config( - self, default_choice: str, tmpdir, config: BaseConfig + self, default_choice: str, tmp_path, monkeypatch, config: BaseConfig ): - with tmpdir.as_cwd(): - commands.Init(config)() - check_cz_config(default_choice) - check_pre_commit_config([cz_hook_config]) + monkeypatch.chdir(tmp_path) + commands.Init(config)() + check_cz_config(default_choice) + check_pre_commit_config([cz_hook_config]) def test_empty_pre_commit_config( - self, default_choice: str, tmpdir, config: BaseConfig + self, default_choice: str, tmp_path, monkeypatch, config: BaseConfig ): - with tmpdir.as_cwd(): - p = tmpdir.join(pre_commit_config_filename) - p.write("") + monkeypatch.chdir(tmp_path) + p = tmp_path / pre_commit_config_filename + p.write_text("") - commands.Init(config)() - check_cz_config(default_choice) - check_pre_commit_config([cz_hook_config]) + commands.Init(config)() + check_cz_config(default_choice) + check_pre_commit_config([cz_hook_config]) def test_pre_commit_config_without_cz_hook( - self, default_choice: str, tmpdir, config: BaseConfig + self, default_choice: str, tmp_path, monkeypatch, config: BaseConfig ): existing_hook_config = { "repo": "https://github.com/pre-commit/pre-commit-hooks", @@ -204,40 +206,40 @@ def test_pre_commit_config_without_cz_hook( "hooks": [{"id", "trailing-whitespace"}], } - with tmpdir.as_cwd(): - p = tmpdir.join(pre_commit_config_filename) - p.write(yaml.safe_dump({"repos": [existing_hook_config]})) + monkeypatch.chdir(tmp_path) + p = tmp_path / pre_commit_config_filename + p.write_text(yaml.safe_dump({"repos": [existing_hook_config]})) - commands.Init(config)() - check_cz_config(default_choice) - check_pre_commit_config([existing_hook_config, cz_hook_config]) + commands.Init(config)() + check_cz_config(default_choice) + check_pre_commit_config([existing_hook_config, cz_hook_config]) def test_cz_hook_exists_in_pre_commit_config( - self, default_choice: str, tmpdir, config: BaseConfig + self, default_choice: str, tmp_path, monkeypatch, config: BaseConfig ): - with tmpdir.as_cwd(): - p = tmpdir.join(pre_commit_config_filename) - p.write(yaml.safe_dump({"repos": [cz_hook_config]})) + monkeypatch.chdir(tmp_path) + p = tmp_path / pre_commit_config_filename + p.write_text(yaml.safe_dump({"repos": [cz_hook_config]})) - commands.Init(config)() - check_cz_config(default_choice) - # check that config is not duplicated - check_pre_commit_config([cz_hook_config]) + commands.Init(config)() + check_cz_config(default_choice) + # check that config is not duplicated + check_pre_commit_config([cz_hook_config]) class TestNoPreCommitInstalled: @pytest.mark.usefixtures("default_choice") def test_pre_commit_not_installed( - self, mocker: MockFixture, config: BaseConfig, tmpdir + self, mocker: MockFixture, config: BaseConfig, tmp_path, monkeypatch ): # Assume `pre-commit` is not installed mocker.patch( "commitizen.project_info.is_pre_commit_installed", return_value=False, ) - with tmpdir.as_cwd(): - with pytest.raises(InitFailedError): - commands.Init(config)() + monkeypatch.chdir(tmp_path) + with pytest.raises(InitFailedError): + commands.Init(config)() class TestAskTagFormat: @@ -273,7 +275,7 @@ def test_empty_input_returns_default(self, mocker: MockFixture, config: BaseConf def test_init_with_confirmed_tag_format( - config: BaseConfig, mocker: MockFixture, tmpdir + config: BaseConfig, mocker: MockFixture, tmp_path, monkeypatch ): mocker.patch( "commitizen.commands.init.get_tag_names", return_value=["v0.0.2", "v0.0.1"] @@ -292,14 +294,16 @@ def test_init_with_confirmed_tag_format( mocker.patch("questionary.text", return_value=FakeQuestion("$version")) mocker.patch("questionary.checkbox", return_value=FakeQuestion(None)) - with tmpdir.as_cwd(): - commands.Init(config)() - assert 'tag_format = "v$version"' in Path("pyproject.toml").read_text( - encoding="utf-8" - ) + monkeypatch.chdir(tmp_path) + commands.Init(config)() + assert 'tag_format = "v$version"' in Path("pyproject.toml").read_text( + encoding="utf-8" + ) -def test_init_with_no_existing_tags(config: BaseConfig, mocker: MockFixture, tmpdir): +def test_init_with_no_existing_tags( + config: BaseConfig, mocker: MockFixture, tmp_path, monkeypatch +): mocker.patch("commitizen.commands.init.get_tag_names", return_value=[]) mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value="v1.0.0") mocker.patch( @@ -315,13 +319,13 @@ def test_init_with_no_existing_tags(config: BaseConfig, mocker: MockFixture, tmp mocker.patch("questionary.text", return_value=FakeQuestion("$version")) mocker.patch("questionary.checkbox", return_value=FakeQuestion(None)) - with tmpdir.as_cwd(): - commands.Init(config)() - assert 'version = "0.0.1"' in Path("pyproject.toml").read_text(encoding="utf-8") + monkeypatch.chdir(tmp_path) + commands.Init(config)() + assert 'version = "0.0.1"' in Path("pyproject.toml").read_text(encoding="utf-8") def test_init_with_no_existing_latest_tag( - config: BaseConfig, mocker: MockFixture, tmpdir + config: BaseConfig, mocker: MockFixture, tmp_path, monkeypatch ): mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value=None) mocker.patch( @@ -337,12 +341,14 @@ def test_init_with_no_existing_latest_tag( mocker.patch("questionary.text", return_value=FakeQuestion("$version")) mocker.patch("questionary.checkbox", return_value=FakeQuestion(None)) - with tmpdir.as_cwd(): - commands.Init(config)() - assert 'version = "0.0.1"' in Path("pyproject.toml").read_text(encoding="utf-8") + monkeypatch.chdir(tmp_path) + commands.Init(config)() + assert 'version = "0.0.1"' in Path("pyproject.toml").read_text(encoding="utf-8") -def test_init_with_existing_tags(config: BaseConfig, mocker: MockFixture, tmpdir): +def test_init_with_existing_tags( + config: BaseConfig, mocker: MockFixture, tmp_path, monkeypatch +): expected_tags = ["v1.0.0", "v0.9.0", "v0.8.0"] mocker.patch("commitizen.commands.init.get_tag_names", return_value=expected_tags) mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value="v1.0.0") @@ -360,12 +366,14 @@ def test_init_with_existing_tags(config: BaseConfig, mocker: MockFixture, tmpdir mocker.patch("questionary.text", return_value=FakeQuestion("$version")) mocker.patch("questionary.checkbox", return_value=FakeQuestion(None)) - with tmpdir.as_cwd(): - commands.Init(config)() - assert 'version = "1.0.0"' in Path("pyproject.toml").read_text(encoding="utf-8") + monkeypatch.chdir(tmp_path) + commands.Init(config)() + assert 'version = "1.0.0"' in Path("pyproject.toml").read_text(encoding="utf-8") -def test_init_with_valid_tag_selection(config: BaseConfig, mocker: MockFixture, tmpdir): +def test_init_with_valid_tag_selection( + config: BaseConfig, mocker: MockFixture, tmp_path, monkeypatch +): expected_tags = ["v1.0.0", "v0.9.0", "v0.8.0"] mocker.patch("commitizen.commands.init.get_tag_names", return_value=expected_tags) mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value="v1.0.0") @@ -388,14 +396,16 @@ def test_init_with_valid_tag_selection(config: BaseConfig, mocker: MockFixture, mocker.patch("questionary.text", return_value=FakeQuestion("$version")) mocker.patch("questionary.checkbox", return_value=FakeQuestion(None)) - with tmpdir.as_cwd(): - commands.Init(config)() - content = Path("pyproject.toml").read_text(encoding="utf-8") - assert 'version = "0.9.0"' in content - assert 'version_scheme = "semver"' in content + monkeypatch.chdir(tmp_path) + commands.Init(config)() + content = Path("pyproject.toml").read_text(encoding="utf-8") + assert 'version = "0.9.0"' in content + assert 'version_scheme = "semver"' in content -def test_init_configuration_settings(tmpdir, mocker: MockFixture, config: BaseConfig): +def test_init_configuration_settings( + tmp_path, monkeypatch, mocker: MockFixture, config: BaseConfig +): """Test that all configuration settings are properly initialized.""" mocker.patch( "questionary.select", @@ -410,22 +420,22 @@ def test_init_configuration_settings(tmpdir, mocker: MockFixture, config: BaseCo mocker.patch("questionary.text", return_value=FakeQuestion("$version")) mocker.patch("questionary.checkbox", return_value=FakeQuestion(None)) - with tmpdir.as_cwd(): - commands.Init(config)() + monkeypatch.chdir(tmp_path) + commands.Init(config)() - config_data = Path("pyproject.toml").read_text(encoding="utf-8") + config_data = Path("pyproject.toml").read_text(encoding="utf-8") - # Verify all expected settings are present - assert 'name = "cz_conventional_commits"' in config_data - assert 'tag_format = "$version"' in config_data - assert 'version_scheme = "semver"' in config_data - assert 'version = "0.0.1"' in config_data - assert "update_changelog_on_bump = true" in config_data - assert "major_version_zero = true" in config_data + # Verify all expected settings are present + assert 'name = "cz_conventional_commits"' in config_data + assert 'tag_format = "$version"' in config_data + assert 'version_scheme = "semver"' in config_data + assert 'version = "0.0.1"' in config_data + assert "update_changelog_on_bump = true" in config_data + assert "major_version_zero = true" in config_data def test_init_configuration_with_version_provider( - tmpdir, mocker: MockFixture, config: BaseConfig + tmp_path, monkeypatch, mocker: MockFixture, config: BaseConfig ): """Test configuration initialization with a different version provider.""" mocker.patch( @@ -441,21 +451,21 @@ def test_init_configuration_with_version_provider( mocker.patch("questionary.text", return_value=FakeQuestion("$version")) mocker.patch("questionary.checkbox", return_value=FakeQuestion(None)) - with tmpdir.as_cwd(): - commands.Init(config)() + monkeypatch.chdir(tmp_path) + commands.Init(config)() - config_data = Path("pyproject.toml").read_text(encoding="utf-8") - - # Verify version provider is set instead of version - assert 'name = "cz_conventional_commits"' in config_data - assert 'tag_format = "$version"' in config_data - assert 'version_scheme = "semver"' in config_data - assert 'version_provider = "pep621"' in config_data - assert "update_changelog_on_bump = true" in config_data - assert "major_version_zero = true" in config_data - assert ( - "version = " not in config_data - ) # Version should not be set when using version_provider + config_data = Path("pyproject.toml").read_text(encoding="utf-8") + + # Verify version provider is set instead of version + assert 'name = "cz_conventional_commits"' in config_data + assert 'tag_format = "$version"' in config_data + assert 'version_scheme = "semver"' in config_data + assert 'version_provider = "pep621"' in config_data + assert "update_changelog_on_bump = true" in config_data + assert "major_version_zero = true" in config_data + assert ( + "version = " not in config_data + ) # Version should not be set when using version_provider def test_construct_name_choice_from_registry(config: BaseConfig): diff --git a/tests/conftest.py b/tests/conftest.py index 5c1342316..178b92200 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -74,52 +74,50 @@ def chdir(tmp_path: Path) -> Iterator[Path]: @pytest.fixture -def tmp_git_project(tmpdir): - with tmpdir.as_cwd(): - cmd.run("git init") +def tmp_git_project(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): + monkeypatch.chdir(tmp_path) + cmd.run("git init") - yield tmpdir + return tmp_path @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') +def tmp_commitizen_project(tmp_git_project: Path): + (tmp_git_project / "pyproject.toml").write_text( + '[tool.commitizen]\nversion="0.1.0"\n' + ) return tmp_git_project @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" +def pyproject(tmp_commitizen_project: Path) -> Path: + return tmp_commitizen_project / "pyproject.toml" @pytest.fixture -def tmp_commitizen_project_initial(tmp_git_project, util: UtilFixture): +def tmp_commitizen_project_initial( + tmp_git_project: Path, util: UtilFixture, monkeypatch: pytest.MonkeyPatch +): def _initial( config_extra: str | None = None, version="0.1.0", initial_commit="feat: new user interface", ): - with tmp_git_project.as_cwd(): - tmp_commitizen_cfg_file = tmp_git_project.join("pyproject.toml") - tmp_commitizen_cfg_file.write(f'[tool.commitizen]\nversion="{version}"\n') - tmp_version_file = tmp_git_project.join("__version__.py") - tmp_version_file.write(version) - tmp_commitizen_cfg_file = tmp_git_project.join("pyproject.toml") - tmp_version_file_string = str(tmp_version_file).replace("\\", "/") - with open(tmp_commitizen_cfg_file, "a", encoding="utf-8") as f: - f.write(f'\nversion_files = ["{tmp_version_file_string}"]\n') - if config_extra: - tmp_commitizen_cfg_file.write(config_extra, mode="a") - util.create_file_and_commit(initial_commit) - - return tmp_git_project + monkeypatch.chdir(tmp_git_project) + tmp_commitizen_cfg_file = tmp_git_project / "pyproject.toml" + tmp_commitizen_cfg_file.write_text(f'[tool.commitizen]\nversion="{version}"\n') + tmp_version_file = tmp_git_project / "__version__.py" + tmp_version_file.write_text(version) + tmp_version_file_string = str(tmp_version_file).replace("\\", "/") + with tmp_commitizen_cfg_file.open("a", encoding="utf-8") as f: + f.write(f'\nversion_files = ["{tmp_version_file_string}"]\n') + if config_extra: + with tmp_commitizen_cfg_file.open("a", encoding="utf-8") as f: + f.write(config_extra) + util.create_file_and_commit(initial_commit) + + return tmp_git_project return _initial diff --git a/tests/providers/test_uv_provider.py b/tests/providers/test_uv_provider.py index 3dc55a91b..e0da97916 100644 --- a/tests/providers/test_uv_provider.py +++ b/tests/providers/test_uv_provider.py @@ -87,34 +87,35 @@ ) def test_uv_provider( config: BaseConfig, - tmpdir, + tmp_path, + monkeypatch, file_regression: FileRegressionFixture, pyproject_content: str, ): - with tmpdir.as_cwd(): - pyproject_toml_file = tmpdir / UvProvider.filename - pyproject_toml_file.write_text(pyproject_content, encoding="utf-8") + monkeypatch.chdir(tmp_path) + pyproject_toml_file = tmp_path / UvProvider.filename + pyproject_toml_file.write_text(pyproject_content, encoding="utf-8") - uv_lock_file = tmpdir / UvProvider.lock_filename - uv_lock_file.write_text(UV_LOCK_SIMPLIFIED, encoding="utf-8") + uv_lock_file = tmp_path / UvProvider.lock_filename + uv_lock_file.write_text(UV_LOCK_SIMPLIFIED, encoding="utf-8") - config.settings["version_provider"] = "uv" + config.settings["version_provider"] = "uv" - provider = get_provider(config) - assert isinstance(provider, UvProvider) - assert provider.get_version() == "4.2.1" + provider = get_provider(config) + assert isinstance(provider, UvProvider) + assert provider.get_version() == "4.2.1" - provider.set_version("100.100.100") - assert provider.get_version() == "100.100.100" + provider.set_version("100.100.100") + assert provider.get_version() == "100.100.100" - updated_pyproject_toml_content = pyproject_toml_file.read_text(encoding="utf-8") - updated_uv_lock_content = uv_lock_file.read_text(encoding="utf-8") + updated_pyproject_toml_content = pyproject_toml_file.read_text(encoding="utf-8") + updated_uv_lock_content = uv_lock_file.read_text(encoding="utf-8") - for content in (updated_pyproject_toml_content, updated_uv_lock_content): - # updated project version - assert "100.100.100" in content - # commitizen version which was the same as project version and should not be affected - assert "4.2.1" in content + for content in (updated_pyproject_toml_content, updated_uv_lock_content): + # updated project version + assert "100.100.100" in content + # commitizen version which was the same as project version and should not be affected + assert "4.2.1" in content - file_regression.check(updated_pyproject_toml_content, extension=".toml") - file_regression.check(updated_uv_lock_content, extension=".lock") + file_regression.check(updated_pyproject_toml_content, extension=".toml") + file_regression.check(updated_uv_lock_content, extension=".lock") diff --git a/tests/test_conf.py b/tests/test_conf.py index 3b7937603..b56fa0464 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -156,25 +156,27 @@ @pytest.fixture -def config_files_manager(request, tmpdir): - with tmpdir.as_cwd(): - filename = request.param - with open(filename, "w", encoding="utf-8") as f: - if "toml" in filename: - f.write(PYPROJECT) - elif "json" in filename: - json.dump(DICT_CONFIG, f) - elif "yaml" in filename: - yaml.dump(DICT_CONFIG, f) - yield +def config_files_manager(request, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + filename = request.param + path = tmp_path / filename + path.parent.mkdir(parents=True, exist_ok=True) + with path.open("w", encoding="utf-8") as f: + if "toml" in filename: + f.write(PYPROJECT) + elif "json" in filename: + json.dump(DICT_CONFIG, f) + elif "yaml" in filename: + yaml.dump(DICT_CONFIG, f) + return @pytest.mark.usefixtures("in_repo_root") -def test_find_git_project_root(tmpdir): +def test_find_git_project_root(tmp_path, monkeypatch): assert git.find_git_project_root() == Path(os.getcwd()) - with tmpdir.as_cwd() as _: - assert git.find_git_project_root() is None + monkeypatch.chdir(tmp_path) + assert git.find_git_project_root() is None @pytest.mark.parametrize("config_files_manager", defaults.CONFIG_FILES, indirect=True) @@ -193,54 +195,60 @@ def test_load_conf(self, config_files_manager): cfg = config.read_cfg() assert cfg.settings == _settings - def test_conf_returns_default_when_no_files(self, tmpdir): - with tmpdir.as_cwd(): - cfg = config.read_cfg() - assert cfg.settings == defaults.DEFAULT_SETTINGS + def test_conf_returns_default_when_no_files(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + cfg = config.read_cfg() + assert cfg.settings == defaults.DEFAULT_SETTINGS - def test_load_empty_pyproject_toml_and_cz_toml_with_config(self, tmpdir): - with tmpdir.as_cwd(): - p = tmpdir.join("pyproject.toml") - p.write("") - p = tmpdir.join(".cz.toml") - p.write(TOML_STR) + def test_load_empty_pyproject_toml_and_cz_toml_with_config( + self, tmp_path, monkeypatch + ): + monkeypatch.chdir(tmp_path) + (tmp_path / "pyproject.toml").write_text("") + (tmp_path / ".cz.toml").write_text(TOML_STR) - cfg = config.read_cfg() - assert cfg.settings == _settings + cfg = config.read_cfg() + assert cfg.settings == _settings - def test_load_pyproject_toml_from_config_argument(self, tmpdir): - with tmpdir.as_cwd(): - _not_root_path = tmpdir.mkdir("not_in_root").join("pyproject.toml") - _not_root_path.write(PYPROJECT) + def test_load_pyproject_toml_from_config_argument(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + _not_root_path = tmp_path / "not_in_root" / "pyproject.toml" + _not_root_path.parent.mkdir(parents=True, exist_ok=True) + _not_root_path.write_text(PYPROJECT) - cfg = config.read_cfg(filepath="./not_in_root/pyproject.toml") - assert cfg.settings == _settings + cfg = config.read_cfg(_not_root_path) + assert cfg.settings == _settings - def test_load_cz_json_not_from_config_argument(self, tmpdir): - with tmpdir.as_cwd(): - _not_root_path = tmpdir.mkdir("not_in_root").join(".cz.json") - _not_root_path.write(JSON_STR) + def test_load_cz_json_not_from_config_argument(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + _not_root_path = tmp_path / "not_in_root" / ".cz.json" + _not_root_path.parent.mkdir(parents=True, exist_ok=True) + _not_root_path.write_text(JSON_STR) - cfg = config.read_cfg(filepath="./not_in_root/.cz.json") - json_cfg_by_class = JsonConfig(data=JSON_STR, path=_not_root_path) - assert cfg.settings == json_cfg_by_class.settings + cfg = config.read_cfg(_not_root_path) + json_cfg_by_class = JsonConfig(data=JSON_STR, path=_not_root_path) + assert cfg.settings == json_cfg_by_class.settings - def test_load_cz_yaml_not_from_config_argument(self, tmpdir): - with tmpdir.as_cwd(): - _not_root_path = tmpdir.mkdir("not_in_root").join(".cz.yaml") - _not_root_path.write(YAML_STR) + def test_load_cz_yaml_not_from_config_argument(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + _not_root_path = tmp_path / "not_in_root" / ".cz.yaml" + _not_root_path.parent.mkdir(parents=True, exist_ok=True) + _not_root_path.write_text(YAML_STR) - cfg = config.read_cfg(filepath="./not_in_root/.cz.yaml") - yaml_cfg_by_class = YAMLConfig(data=YAML_STR, path=_not_root_path) - assert cfg.settings == yaml_cfg_by_class._settings + cfg = config.read_cfg(_not_root_path) + yaml_cfg_by_class = YAMLConfig(data=YAML_STR, path=_not_root_path) + assert cfg.settings == yaml_cfg_by_class._settings - def test_load_empty_pyproject_toml_from_config_argument(self, tmpdir): - with tmpdir.as_cwd(): - _not_root_path = tmpdir.mkdir("not_in_root").join("pyproject.toml") - _not_root_path.write("") + def test_load_empty_pyproject_toml_from_config_argument( + self, tmp_path, monkeypatch + ): + monkeypatch.chdir(tmp_path) + _not_root_path = tmp_path / "not_in_root" / "pyproject.toml" + _not_root_path.parent.mkdir(parents=True, exist_ok=True) + _not_root_path.write_text("") - with pytest.raises(ConfigFileIsEmpty): - config.read_cfg(filepath="./not_in_root/pyproject.toml") + with pytest.raises(ConfigFileIsEmpty): + config.read_cfg(_not_root_path) class TestWarnMultipleConfigFiles: @@ -263,22 +271,22 @@ class TestWarnMultipleConfigFiles: ], ) def test_warn_multiple_config_files_same_dir( - self, tmpdir, capsys, files, expected_path + self, tmp_path, monkeypatch, capsys, files, expected_path ): """Test warning when multiple config files exist in same directory.""" - with tmpdir.as_cwd(): - for filename, content in files: - tmpdir.join(filename).write(content) + monkeypatch.chdir(tmp_path) + for filename, content in files: + (tmp_path / filename).write_text(content) - cfg = config.read_cfg() - captured = capsys.readouterr() + cfg = config.read_cfg() + captured = capsys.readouterr() - assert "Multiple config files detected" in captured.err - for filename, _ in files: - assert filename in captured.err - assert f"Using config file: '{expected_path}'" in captured.err + assert "Multiple config files detected" in captured.err + for filename, _ in files: + assert filename in captured.err + assert f"Using config file: '{expected_path}'" in captured.err - assert cfg.path == Path(expected_path) + assert cfg.path == Path(expected_path) @pytest.mark.parametrize( ("config_file", "content"), @@ -292,44 +300,45 @@ def test_warn_multiple_config_files_same_dir( ], ) def test_warn_same_filename_different_directories_with_git( - self, tmpdir, capsys, config_file, content + self, tmp_path, monkeypatch, capsys, config_file, content ): """Test warning when same config filename exists in the current directory and in the git root.""" - with tmpdir.as_cwd(): - cmd.run("git init") + monkeypatch.chdir(tmp_path) + cmd.run("git init") - # Create config in git root - tmpdir.join(config_file).write(content) + # Create config in git root + (tmp_path / config_file).write_text(content) - # Create same filename in subdirectory - subdir = tmpdir.mkdir("subdir") - subdir.join(config_file).write(content) + # Create same filename in subdirectory + subdir = tmp_path / "subdir" + subdir.mkdir() + (subdir / config_file).write_text(content) - with subdir.as_cwd(): - cfg = config.read_cfg() - captured = capsys.readouterr() + monkeypatch.chdir(subdir) + cfg = config.read_cfg() + captured = capsys.readouterr() - assert "Multiple config files detected" in captured.err - assert f"Using config file: '{config_file}'" in captured.err - assert cfg.path == Path(config_file) + assert "Multiple config files detected" in captured.err + assert f"Using config file: '{config_file}'" in captured.err + assert cfg.path == Path(config_file) - def test_no_warn_with_explicit_config_path(self, tmpdir, capsys): + def test_no_warn_with_explicit_config_path(self, tmp_path, monkeypatch, capsys): """Test that no warning is issued when user explicitly specifies config.""" - with tmpdir.as_cwd(): - # Create multiple config files - tmpdir.join(".cz.toml").write(PYPROJECT) - tmpdir.join(".cz.json").write(JSON_STR) + monkeypatch.chdir(tmp_path) + # Create multiple config files + (tmp_path / ".cz.toml").write_text(PYPROJECT) + (tmp_path / ".cz.json").write_text(JSON_STR) - # Read config with explicit path - cfg = config.read_cfg(filepath=".cz.json") + # Read config with explicit path + cfg = config.read_cfg(Path(".cz.json")) - # No warning should be issued - captured = capsys.readouterr() - assert "Multiple config files detected" not in captured.err + # No warning should be issued + captured = capsys.readouterr() + assert "Multiple config files detected" not in captured.err - # Verify the explicitly specified config is loaded (compare to expected JSON config) - json_cfg_expected = JsonConfig(data=JSON_STR, path=Path(".cz.json")) - assert cfg.settings == json_cfg_expected.settings + # Verify the explicitly specified config is loaded (compare to expected JSON config) + json_cfg_expected = JsonConfig(data=JSON_STR, path=Path(".cz.json")) + assert cfg.settings == json_cfg_expected.settings @pytest.mark.parametrize( ("config_file", "content", "with_git"), @@ -348,33 +357,33 @@ def test_no_warn_with_explicit_config_path(self, tmpdir, capsys): ], ) def test_no_warn_with_single_config_file( - self, tmpdir, capsys, config_file, content, with_git + self, tmp_path, monkeypatch, capsys, config_file, content, with_git ): """Test that no warning is issued when user explicitly specifies config.""" - with tmpdir.as_cwd(): - if with_git: - cmd.run("git init") + monkeypatch.chdir(tmp_path) + if with_git: + cmd.run("git init") - tmpdir.join(config_file).write(content) + (tmp_path / config_file).write_text(content) - cfg = config.read_cfg() - captured = capsys.readouterr() + cfg = config.read_cfg() + captured = capsys.readouterr() - # No warning should be issued - assert "Multiple config files detected" not in captured.err - assert cfg.path == Path(config_file) + # No warning should be issued + assert "Multiple config files detected" not in captured.err + assert cfg.path == Path(config_file) def test_no_warn_with_no_commitizen_section_in_pyproject_toml_and_cz_toml( - self, tmpdir, capsys + self, tmp_path, monkeypatch, capsys ): - with tmpdir.as_cwd(): - tmpdir.join("pyproject.toml").write("[tool.foo]\nbar = 'baz'") - tmpdir.join(".cz.toml").write(TOML_STR) + monkeypatch.chdir(tmp_path) + (tmp_path / "pyproject.toml").write_text("[tool.foo]\nbar = 'baz'") + (tmp_path / ".cz.toml").write_text(TOML_STR) - cfg = config.read_cfg() - captured = capsys.readouterr() - assert "Multiple config files detected" not in captured.err - assert cfg.path == Path(".cz.toml") + cfg = config.read_cfg() + captured = capsys.readouterr() + assert "Multiple config files detected" not in captured.err + assert cfg.path == Path(".cz.toml") @pytest.mark.parametrize( @@ -386,29 +395,34 @@ def test_no_warn_with_no_commitizen_section_in_pyproject_toml_and_cz_toml( ], ) class TestTomlConfig: - def test_init_empty_config_content(self, tmpdir, config_file): - path = tmpdir.mkdir("commitizen").join(config_file) + def test_init_empty_config_content(self, tmp_path, config_file): + path = tmp_path / "commitizen" / config_file + path.parent.mkdir(parents=True, exist_ok=True) toml_config = TomlConfig(data="", path=path) toml_config.init_empty_config_content() - assert Path(path).read_text(encoding="utf-8") == "[tool.commitizen]\n" + assert path.read_text(encoding="utf-8") == "[tool.commitizen]\n" - def test_init_empty_config_content_with_existing_content(self, tmpdir, config_file): + def test_init_empty_config_content_with_existing_content( + self, tmp_path, config_file + ): existing_content = "[tool.black]\nline-length = 88\n" - path = tmpdir.mkdir("commitizen").join(config_file) - path.write(existing_content) + path = tmp_path / "commitizen" / config_file + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(existing_content) toml_config = TomlConfig(data="", path=path) toml_config.init_empty_config_content() assert ( - Path(path).read_text(encoding="utf-8") + path.read_text(encoding="utf-8") == existing_content + "\n[tool.commitizen]\n" ) - def test_init_with_invalid_config_content(self, tmpdir, config_file): + def test_init_with_invalid_config_content(self, tmp_path, config_file): existing_content = "invalid toml content" - path = tmpdir.mkdir("commitizen").join(config_file) + path = tmp_path / "commitizen" / config_file + path.parent.mkdir(parents=True, exist_ok=True) with pytest.raises(InvalidConfigurationError) as excinfo: TomlConfig(data=existing_content, path=path) @@ -423,17 +437,19 @@ def test_init_with_invalid_config_content(self, tmpdir, config_file): ], ) class TestJsonConfig: - def test_init_empty_config_content(self, tmpdir, config_file): - path = tmpdir.mkdir("commitizen").join(config_file) + def test_init_empty_config_content(self, tmp_path, config_file): + path = tmp_path / "commitizen" / config_file + path.parent.mkdir(parents=True, exist_ok=True) json_config = JsonConfig(data="{}", path=path) json_config.init_empty_config_content() with path.open(encoding="utf-8") as json_file: assert json.load(json_file) == {"commitizen": {}} - def test_init_with_invalid_config_content(self, tmpdir, config_file): + def test_init_with_invalid_config_content(self, tmp_path, config_file): existing_content = "invalid json content" - path = tmpdir.mkdir("commitizen").join(config_file) + path = tmp_path / "commitizen" / config_file + path.parent.mkdir(parents=True, exist_ok=True) with pytest.raises(InvalidConfigurationError) as excinfo: JsonConfig(data=existing_content, path=path) @@ -448,17 +464,19 @@ def test_init_with_invalid_config_content(self, tmpdir, config_file): ], ) class TestYamlConfig: - def test_init_empty_config_content(self, tmpdir, config_file): - path = tmpdir.mkdir("commitizen").join(config_file) + def test_init_empty_config_content(self, tmp_path, config_file): + path = tmp_path / "commitizen" / config_file + path.parent.mkdir(parents=True, exist_ok=True) yaml_config = YAMLConfig(data="{}", path=path) yaml_config.init_empty_config_content() with path.open() as yaml_file: assert yaml.safe_load(yaml_file) == {"commitizen": {}} - def test_init_with_invalid_content(self, tmpdir, config_file): + def test_init_with_invalid_content(self, tmp_path, config_file): existing_content = "invalid: .cz.yaml: content: maybe?" - path = tmpdir.mkdir("commitizen").join(config_file) + path = tmp_path / "commitizen" / config_file + path.parent.mkdir(parents=True, exist_ok=True) with pytest.raises(InvalidConfigurationError) as excinfo: YAMLConfig(data=existing_content, path=path) diff --git a/tests/test_cz_customize.py b/tests/test_cz_customize.py index 22a3e80f3..311eea19a 100644 --- a/tests/test_cz_customize.py +++ b/tests/test_cz_customize.py @@ -564,13 +564,13 @@ def test_info_unicode(config_with_unicode): assert "This is a customized cz with emojis 🎉!" in cz.info() -def test_info_with_info_path(tmpdir, config_info): - with tmpdir.as_cwd(): - tmpfile = tmpdir.join("info.txt") - tmpfile.write("Test info") +def test_info_with_info_path(tmp_path, monkeypatch, config_info): + monkeypatch.chdir(tmp_path) + tmpfile = tmp_path / "info.txt" + tmpfile.write_text("Test info") - cz = CustomizeCommitsCz(config_info) - assert "Test info" in cz.info() + cz = CustomizeCommitsCz(config_info) + assert "Test info" in cz.info() def test_info_without_info(config_without_info):