Skip to content

Commit

Permalink
test: improve git isolation and fixes test missing a repository
Browse files Browse the repository at this point in the history
  • Loading branch information
noirbizarre authored and Lee-W committed Jan 23, 2023
1 parent 12571fe commit 190e20e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 52 deletions.
14 changes: 8 additions & 6 deletions tests/commands/test_bump_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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)

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 @@ -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"]
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 @@ -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")
Expand Down Expand Up @@ -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
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
75 changes: 35 additions & 40 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 @@ -29,20 +28,19 @@ 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:
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 @@ -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:
Expand All @@ -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()
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

0 comments on commit 190e20e

Please sign in to comment.