Skip to content

Commit 5a83c5d

Browse files
committed
test: improve git isolation and fixes test missing a repository
1 parent ffd5707 commit 5a83c5d

6 files changed

+57
-52
lines changed

tests/commands/test_bump_command.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ def test_bump_on_git_with_hooks_no_verify_enabled(mocker):
276276
assert tag_exists is True
277277

278278

279-
def test_bump_when_bumpping_is_not_support(mocker, tmp_commitizen_project):
279+
@pytest.mark.usefixtures("tmp_commitizen_project")
280+
def test_bump_when_bumpping_is_not_support(mocker):
280281
create_file_and_commit(
281282
"feat: new user interface\n\nBREAKING CHANGE: age is no longer supported"
282283
)
@@ -430,7 +431,8 @@ def test_bump_local_version(mocker, tmp_commitizen_project):
430431
assert "4.5.1+0.2.0" in f.read()
431432

432433

433-
def test_bump_dry_run(mocker, capsys, tmp_commitizen_project):
434+
@pytest.mark.usefixtures("tmp_commitizen_project")
435+
def test_bump_dry_run(mocker, capsys):
434436
create_file_and_commit("feat: new file")
435437

436438
testargs = ["cz", "bump", "--yes", "--dry-run"]
@@ -472,9 +474,7 @@ def test_none_increment_exit_is_exception():
472474

473475

474476
@pytest.mark.usefixtures("tmp_commitizen_project")
475-
def test_none_increment_should_not_call_git_tag_and_error_code_is_not_zero(
476-
mocker, tmp_commitizen_project
477-
):
477+
def test_none_increment_should_not_call_git_tag_and_error_code_is_not_zero(mocker):
478478
create_file_and_commit("test(test_get_all_droplets): fix bad comparison test")
479479
testargs = ["cz", "bump", "--yes"]
480480
mocker.patch.object(sys, "argv", testargs)
@@ -529,9 +529,8 @@ def test_bump_with_changelog_config(mocker, changelog_path, config_path):
529529
assert "0.2.0" in out
530530

531531

532-
def test_prevent_prerelease_when_no_increment_detected(
533-
mocker, capsys, tmp_commitizen_project
534-
):
532+
@pytest.mark.usefixtures("tmp_commitizen_project")
533+
def test_prevent_prerelease_when_no_increment_detected(mocker, capsys):
535534
create_file_and_commit("feat: new file")
536535

537536
testargs = ["cz", "bump", "--yes"]
@@ -686,6 +685,7 @@ def test_bump_changelog_command_commits_untracked_changelog_and_version_files(
686685
["cz", "bump", "--increment", "PATCH", "1.2.3"],
687686
],
688687
)
688+
@pytest.mark.usefixtures("tmp_commitizen_project")
689689
def test_bump_invalid_manual_args_raises_exception(mocker, testargs):
690690
mocker.patch.object(sys, "argv", testargs)
691691

tests/commands/test_changelog_command.py

+1
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ def test_changelog_without_revision(mocker, tmp_commitizen_project):
340340
cli.main()
341341

342342

343+
@pytest.mark.usefixtures("tmp_commitizen_project")
343344
def test_changelog_incremental_with_revision(mocker):
344345
"""combining incremental with a revision doesn't make sense"""
345346
testargs = ["cz", "changelog", "--incremental", "0.2.0"]

tests/commands/test_commit_command.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616

1717

1818
@pytest.fixture
19-
def staging_is_clean(mocker):
19+
def staging_is_clean(mocker, tmp_git_project):
2020
is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean")
2121
is_staging_clean_mock.return_value = False
22+
return tmp_git_project
2223

2324

2425
@pytest.mark.usefixtures("staging_is_clean")
@@ -127,6 +128,7 @@ def test_commit_command_with_signoff_option(config, mocker):
127128
success_mock.assert_called_once()
128129

129130

131+
@pytest.mark.usefixtures("tmp_git_project")
130132
def test_commit_when_nothing_to_commit(config, mocker):
131133
is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean")
132134
is_staging_clean_mock.return_value = True

tests/conftest.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
@pytest.fixture(autouse=True)
1616
def git_sandbox(monkeypatch: pytest.MonkeyPatch, tmp_path: Path):
1717
"""Ensure git commands are executed without the current user settings"""
18+
# Clear any GIT_ prefixed environment variable
19+
for var in os.environ:
20+
if var.startswith("GIT_"):
21+
monkeypatch.delenv(var)
22+
23+
# Define a dedicated temporary git config
1824
monkeypatch.setenv("GIT_CONFIG_GLOBAL", str(tmp_path / "gitconfig"))
1925
cmd.run(f"git config --global user.name {SIGNER}")
2026
cmd.run(f"git config --global user.email {SIGNER_MAIL}")
@@ -30,11 +36,10 @@ def tmp_git_project(tmpdir):
3036

3137
@pytest.fixture(scope="function")
3238
def tmp_commitizen_project(tmp_git_project):
33-
with tmp_git_project.as_cwd():
34-
tmp_commitizen_cfg_file = tmp_git_project.join("pyproject.toml")
35-
tmp_commitizen_cfg_file.write("[tool.commitizen]\n" 'version="0.1.0"\n')
39+
tmp_commitizen_cfg_file = tmp_git_project.join("pyproject.toml")
40+
tmp_commitizen_cfg_file.write("[tool.commitizen]\n" 'version="0.1.0"\n')
3641

37-
yield tmp_git_project
42+
yield tmp_git_project
3843

3944

4045
def _get_gpg_keyid(signer_mail):
+35-38
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
import sys
32
from pathlib import Path
43
from textwrap import dedent
@@ -28,18 +27,19 @@ def test_create_tag(test_input, expected):
2827

2928

3029
@pytest.mark.parametrize("retry", (True, False))
31-
def test_bump_pre_commit_changelog(tmp_commitizen_project, mocker, freezer, retry):
30+
@pytest.mark.usefixtures("tmp_commitizen_project")
31+
def test_bump_pre_commit_changelog(mocker, freezer, retry):
3232
freezer.move_to("2022-04-01")
3333
testargs = ["cz", "bump", "--changelog", "--yes"]
3434
if retry:
3535
testargs.append("--retry")
3636
else:
3737
pytest.xfail("it will fail because pre-commit will reformat CHANGELOG.md")
3838
mocker.patch.object(sys, "argv", testargs)
39-
with tmp_commitizen_project.as_cwd():
40-
# Configure prettier as a pre-commit hook
41-
Path(".pre-commit-config.yaml").write_text(
42-
"""
39+
# Configure prettier as a pre-commit hook
40+
Path(".pre-commit-config.yaml").write_text(
41+
dedent(
42+
"""\
4343
repos:
4444
- repo: https://github.com/pre-commit/mirrors-prettier
4545
rev: v2.6.2
@@ -48,44 +48,43 @@ def test_bump_pre_commit_changelog(tmp_commitizen_project, mocker, freezer, retr
4848
stages: [commit]
4949
"""
5050
)
51-
# Prettier inherits editorconfig
52-
Path(".editorconfig").write_text(
53-
"""
51+
)
52+
# Prettier inherits editorconfig
53+
Path(".editorconfig").write_text(
54+
dedent(
55+
"""\
5456
[*]
5557
indent_size = 4
5658
"""
5759
)
58-
cmd.run("git add -A")
59-
if os.name == "nt":
60-
cmd.run('git commit -m "fix: _test"')
61-
else:
62-
cmd.run("git commit -m 'fix: _test'")
63-
cmd.run("pre-commit install")
64-
cli.main()
65-
# Pre-commit fixed last line adding extra indent and "\" char
66-
assert Path("CHANGELOG.md").read_text() == dedent(
67-
"""\
68-
## 0.1.1 (2022-04-01)
60+
)
61+
cmd.run("git add -A")
62+
cmd.run('git commit -m "fix: _test"')
63+
cmd.run("pre-commit install")
64+
cli.main()
65+
# Pre-commit fixed last line adding extra indent and "\" char
66+
assert Path("CHANGELOG.md").read_text() == dedent(
67+
"""\
68+
## 0.1.1 (2022-04-01)
6969
70-
### Fix
70+
### Fix
7171
72-
- \\_test
73-
"""
74-
)
72+
- \\_test
73+
"""
74+
)
7575

7676

7777
@pytest.mark.parametrize("retry", (True, False))
78-
def test_bump_pre_commit_changelog_fails_always(
79-
tmp_commitizen_project, mocker, freezer, retry
80-
):
78+
@pytest.mark.usefixtures("tmp_commitizen_project")
79+
def test_bump_pre_commit_changelog_fails_always(mocker, freezer, retry):
8180
freezer.move_to("2022-04-01")
8281
testargs = ["cz", "bump", "--changelog", "--yes"]
8382
if retry:
8483
testargs.append("--retry")
8584
mocker.patch.object(sys, "argv", testargs)
86-
with tmp_commitizen_project.as_cwd():
87-
Path(".pre-commit-config.yaml").write_text(
88-
"""
85+
Path(".pre-commit-config.yaml").write_text(
86+
dedent(
87+
"""\
8988
repos:
9089
- repo: local
9190
hooks:
@@ -96,11 +95,9 @@ def test_bump_pre_commit_changelog_fails_always(
9695
files: CHANGELOG.md
9796
"""
9897
)
99-
cmd.run("git add -A")
100-
if os.name == "nt":
101-
cmd.run('git commit -m "feat: forbid changelogs"')
102-
else:
103-
cmd.run("git commit -m 'feat: forbid changelogs'")
104-
cmd.run("pre-commit install")
105-
with pytest.raises(exceptions.BumpCommitFailedError):
106-
cli.main()
98+
)
99+
cmd.run("git add -A")
100+
cmd.run('git commit -m "feat: forbid changelogs"')
101+
cmd.run("pre-commit install")
102+
with pytest.raises(exceptions.BumpCommitFailedError):
103+
cli.main()

tests/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def create_file_and_commit(message: str, filename: Optional[str] = None):
1717
if not filename:
1818
filename = str(uuid.uuid4())
1919

20-
Path(f"./{filename}").touch()
20+
Path(filename).touch()
2121
c = cmd.run("git add .")
2222
if c.return_code != 0:
2323
raise exceptions.CommitError(c.err)

0 commit comments

Comments
 (0)