From 190e20e3ab871c66ae88e4af08ee6fbf40006532 Mon Sep 17 00:00:00 2001 From: Axel H Date: Wed, 28 Dec 2022 19:59:14 +0100 Subject: [PATCH] test: improve git isolation and fixes test missing a repository --- tests/commands/test_bump_command.py | 14 +++-- tests/commands/test_changelog_command.py | 1 + tests/commands/test_commit_command.py | 4 +- tests/conftest.py | 13 ++-- tests/test_bump_create_commit_message.py | 75 +++++++++++------------- tests/utils.py | 2 +- 6 files changed, 57 insertions(+), 52 deletions(-) diff --git a/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py index dcdca163b2..6b5102978f 100644 --- a/tests/commands/test_bump_command.py +++ b/tests/commands/test_bump_command.py @@ -278,7 +278,8 @@ def test_bump_on_git_with_hooks_no_verify_enabled(mocker: MockFixture): assert tag_exists is True -def test_bump_when_bumpping_is_not_support(mocker: MockFixture, tmp_commitizen_project): +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_bump_when_bumpping_is_not_support(mocker: MockFixture): create_file_and_commit( "feat: new user interface\n\nBREAKING CHANGE: age is no longer supported" ) @@ -432,7 +433,8 @@ def test_bump_local_version(mocker: MockFixture, tmp_commitizen_project): assert "4.5.1+0.2.0" in f.read() -def test_bump_dry_run(mocker: MockFixture, capsys, tmp_commitizen_project): +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_bump_dry_run(mocker: MockFixture, capsys): create_file_and_commit("feat: new file") testargs = ["cz", "bump", "--yes", "--dry-run"] @@ -475,7 +477,7 @@ 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, tmp_commitizen_project + mocker: MockFixture, ): create_file_and_commit("test(test_get_all_droplets): fix bad comparison test") testargs = ["cz", "bump", "--yes"] @@ -531,9 +533,8 @@ def test_bump_with_changelog_config(mocker: MockFixture, changelog_path, config_ assert "0.2.0" in out -def test_prevent_prerelease_when_no_increment_detected( - mocker: MockFixture, capsys, tmp_commitizen_project -): +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_prevent_prerelease_when_no_increment_detected(mocker: MockFixture, capsys): create_file_and_commit("feat: new file") testargs = ["cz", "bump", "--yes"] @@ -690,6 +691,7 @@ def test_bump_changelog_command_commits_untracked_changelog_and_version_files( ["cz", "bump", "--increment", "PATCH", "1.2.3"], ], ) +@pytest.mark.usefixtures("tmp_commitizen_project") def test_bump_invalid_manual_args_raises_exception(mocker, testargs): mocker.patch.object(sys, "argv", testargs) diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index f37915baa2..b98ec6e503 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -345,6 +345,7 @@ def test_changelog_without_revision(mocker: MockFixture, tmp_commitizen_project) cli.main() +@pytest.mark.usefixtures("tmp_commitizen_project") def test_changelog_incremental_with_revision(mocker): """combining incremental with a revision doesn't make sense""" testargs = ["cz", "changelog", "--incremental", "0.2.0"] diff --git a/tests/commands/test_commit_command.py b/tests/commands/test_commit_command.py index 05e72cfabd..dd62fafe85 100644 --- a/tests/commands/test_commit_command.py +++ b/tests/commands/test_commit_command.py @@ -17,9 +17,10 @@ @pytest.fixture -def staging_is_clean(mocker: MockFixture): +def staging_is_clean(mocker: MockFixture, tmp_git_project): is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean") is_staging_clean_mock.return_value = False + return tmp_git_project @pytest.mark.usefixtures("staging_is_clean") @@ -128,6 +129,7 @@ def test_commit_command_with_signoff_option(config, mocker: MockFixture): success_mock.assert_called_once() +@pytest.mark.usefixtures("tmp_git_project") def test_commit_when_nothing_to_commit(config, mocker: MockFixture): is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean") is_staging_clean_mock.return_value = True diff --git a/tests/conftest.py b/tests/conftest.py index 27539e9dba..42a4606e1b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -15,6 +15,12 @@ @pytest.fixture(autouse=True) def git_sandbox(monkeypatch: pytest.MonkeyPatch, tmp_path: Path): """Ensure git commands are executed without the current user settings""" + # Clear any GIT_ prefixed environment variable + for var in os.environ: + if var.startswith("GIT_"): + monkeypatch.delenv(var) + + # Define a dedicated temporary git config monkeypatch.setenv("GIT_CONFIG_GLOBAL", str(tmp_path / "gitconfig")) cmd.run(f"git config --global user.name {SIGNER}") cmd.run(f"git config --global user.email {SIGNER_MAIL}") @@ -30,11 +36,10 @@ def tmp_git_project(tmpdir): @pytest.fixture(scope="function") def tmp_commitizen_project(tmp_git_project): - with tmp_git_project.as_cwd(): - tmp_commitizen_cfg_file = tmp_git_project.join("pyproject.toml") - tmp_commitizen_cfg_file.write("[tool.commitizen]\n" 'version="0.1.0"\n') + tmp_commitizen_cfg_file = tmp_git_project.join("pyproject.toml") + tmp_commitizen_cfg_file.write("[tool.commitizen]\n" 'version="0.1.0"\n') - yield tmp_git_project + yield tmp_git_project def _get_gpg_keyid(signer_mail): diff --git a/tests/test_bump_create_commit_message.py b/tests/test_bump_create_commit_message.py index 6fc31778d7..0609e1a236 100644 --- a/tests/test_bump_create_commit_message.py +++ b/tests/test_bump_create_commit_message.py @@ -1,4 +1,3 @@ -import os import sys from pathlib import Path from textwrap import dedent @@ -29,9 +28,8 @@ def test_create_tag(test_input, expected): @pytest.mark.parametrize("retry", (True, False)) -def test_bump_pre_commit_changelog( - tmp_commitizen_project, mocker: MockFixture, freezer, retry -): +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_bump_pre_commit_changelog(mocker: MockFixture, freezer, retry): freezer.move_to("2022-04-01") testargs = ["cz", "bump", "--changelog", "--yes"] if retry: @@ -39,10 +37,10 @@ def test_bump_pre_commit_changelog( else: pytest.xfail("it will fail because pre-commit will reformat CHANGELOG.md") mocker.patch.object(sys, "argv", testargs) - with tmp_commitizen_project.as_cwd(): - # Configure prettier as a pre-commit hook - Path(".pre-commit-config.yaml").write_text( - """ + # Configure prettier as a pre-commit hook + Path(".pre-commit-config.yaml").write_text( + dedent( + """\ repos: - repo: https://github.com/pre-commit/mirrors-prettier rev: v2.6.2 @@ -51,44 +49,43 @@ def test_bump_pre_commit_changelog( stages: [commit] """ ) - # Prettier inherits editorconfig - Path(".editorconfig").write_text( - """ + ) + # Prettier inherits editorconfig + Path(".editorconfig").write_text( + dedent( + """\ [*] indent_size = 4 """ ) - cmd.run("git add -A") - if os.name == "nt": - cmd.run('git commit -m "fix: _test"') - else: - cmd.run("git commit -m 'fix: _test'") - cmd.run("pre-commit install") - cli.main() - # Pre-commit fixed last line adding extra indent and "\" char - assert Path("CHANGELOG.md").read_text() == dedent( - """\ - ## 0.1.1 (2022-04-01) + ) + cmd.run("git add -A") + cmd.run('git commit -m "fix: _test"') + cmd.run("pre-commit install") + cli.main() + # Pre-commit fixed last line adding extra indent and "\" char + assert Path("CHANGELOG.md").read_text() == dedent( + """\ + ## 0.1.1 (2022-04-01) - ### Fix + ### Fix - - \\_test - """ - ) + - \\_test + """ + ) @pytest.mark.parametrize("retry", (True, False)) -def test_bump_pre_commit_changelog_fails_always( - tmp_commitizen_project, mocker: MockFixture, freezer, retry -): +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_bump_pre_commit_changelog_fails_always(mocker: MockFixture, freezer, retry): freezer.move_to("2022-04-01") testargs = ["cz", "bump", "--changelog", "--yes"] if retry: testargs.append("--retry") mocker.patch.object(sys, "argv", testargs) - with tmp_commitizen_project.as_cwd(): - Path(".pre-commit-config.yaml").write_text( - """ + Path(".pre-commit-config.yaml").write_text( + dedent( + """\ repos: - repo: local hooks: @@ -99,11 +96,9 @@ def test_bump_pre_commit_changelog_fails_always( files: CHANGELOG.md """ ) - cmd.run("git add -A") - if os.name == "nt": - cmd.run('git commit -m "feat: forbid changelogs"') - else: - cmd.run("git commit -m 'feat: forbid changelogs'") - cmd.run("pre-commit install") - with pytest.raises(exceptions.BumpCommitFailedError): - cli.main() + ) + cmd.run("git add -A") + cmd.run('git commit -m "feat: forbid changelogs"') + cmd.run("pre-commit install") + with pytest.raises(exceptions.BumpCommitFailedError): + cli.main() diff --git a/tests/utils.py b/tests/utils.py index 5dcf6722e9..a1e14ad88c 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -17,7 +17,7 @@ def create_file_and_commit(message: str, filename: Optional[str] = None): if not filename: filename = str(uuid.uuid4()) - Path(f"./{filename}").touch() + Path(filename).touch() c = cmd.run("git add .") if c.return_code != 0: raise exceptions.CommitError(c.err)