Skip to content

test: improve git isolation and fixes test missing a repository #647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions tests/commands/test_bump_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ def test_bump_on_git_with_hooks_no_verify_enabled(mocker):
assert tag_exists is True


def test_bump_when_bumpping_is_not_support(mocker, tmp_commitizen_project):
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_when_bumpping_is_not_support(mocker):
create_file_and_commit(
"feat: new user interface\n\nBREAKING CHANGE: age is no longer supported"
)
Expand Down Expand Up @@ -429,7 +430,8 @@ def test_bump_local_version(mocker, tmp_commitizen_project):
assert "4.5.1+0.2.0" in f.read()


def test_bump_dry_run(mocker, capsys, tmp_commitizen_project):
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_dry_run(mocker, capsys):
create_file_and_commit("feat: new file")

testargs = ["cz", "bump", "--yes", "--dry-run"]
Expand Down Expand Up @@ -471,9 +473,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, tmp_commitizen_project
):
def test_none_increment_should_not_call_git_tag_and_error_code_is_not_zero(mocker):
create_file_and_commit("test(test_get_all_droplets): fix bad comparison test")
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)
Expand Down Expand Up @@ -528,9 +528,8 @@ def test_bump_with_changelog_config(mocker, changelog_path, config_path):
assert "0.2.0" in out


def test_prevent_prerelease_when_no_increment_detected(
mocker, capsys, tmp_commitizen_project
):
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_prevent_prerelease_when_no_increment_detected(mocker, capsys):
create_file_and_commit("feat: new file")

testargs = ["cz", "bump", "--yes"]
Expand Down Expand Up @@ -685,6 +684,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)

Expand Down
1 change: 1 addition & 0 deletions tests/commands/test_changelog_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ def test_changelog_without_revision(mocker, 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"]
Expand Down
4 changes: 3 additions & 1 deletion tests/commands/test_commit_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@


@pytest.fixture
def staging_is_clean(mocker):
def staging_is_clean(mocker, 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")
Expand Down Expand Up @@ -127,6 +128,7 @@ def test_commit_command_with_signoff_option(config, mocker):
success_mock.assert_called_once()


@pytest.mark.usefixtures("tmp_git_project")
def test_commit_when_nothing_to_commit(config, mocker):
is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean")
is_staging_clean_mock.return_value = True
Expand Down
13 changes: 9 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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):
Expand Down
73 changes: 35 additions & 38 deletions tests/test_bump_create_commit_message.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import sys
from pathlib import Path
from textwrap import dedent
Expand Down Expand Up @@ -28,18 +27,19 @@ def test_create_tag(test_input, expected):


@pytest.mark.parametrize("retry", (True, False))
def test_bump_pre_commit_changelog(tmp_commitizen_project, mocker, freezer, retry):
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_pre_commit_changelog(mocker, freezer, retry):
freezer.move_to("2022-04-01")
testargs = ["cz", "bump", "--changelog", "--yes"]
if retry:
testargs.append("--retry")
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
Expand All @@ -48,44 +48,43 @@ def test_bump_pre_commit_changelog(tmp_commitizen_project, mocker, freezer, retr
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, freezer, retry
):
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_pre_commit_changelog_fails_always(mocker, 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:
Expand All @@ -96,11 +95,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()
2 changes: 1 addition & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down