Skip to content

Commit a0e978f

Browse files
noirbizarreLee-W
authored andcommitted
test: improve git isolation and fixes test missing a repository
1 parent 6faf7fc commit a0e978f

6 files changed

+57
-52
lines changed

Diff for: tests/commands/test_bump_command.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ def test_bump_on_git_with_hooks_no_verify_enabled(mocker: MockFixture):
278278
assert tag_exists is True
279279

280280

281-
def test_bump_when_bumpping_is_not_support(mocker: MockFixture, tmp_commitizen_project):
281+
@pytest.mark.usefixtures("tmp_commitizen_project")
282+
def test_bump_when_bumpping_is_not_support(mocker: MockFixture):
282283
create_file_and_commit(
283284
"feat: new user interface\n\nBREAKING CHANGE: age is no longer supported"
284285
)
@@ -432,7 +433,8 @@ def test_bump_local_version(mocker: MockFixture, tmp_commitizen_project):
432433
assert "4.5.1+0.2.0" in f.read()
433434

434435

435-
def test_bump_dry_run(mocker: MockFixture, capsys, tmp_commitizen_project):
436+
@pytest.mark.usefixtures("tmp_commitizen_project")
437+
def test_bump_dry_run(mocker: MockFixture, capsys):
436438
create_file_and_commit("feat: new file")
437439

438440
testargs = ["cz", "bump", "--yes", "--dry-run"]
@@ -475,7 +477,7 @@ def test_none_increment_exit_is_exception():
475477

476478
@pytest.mark.usefixtures("tmp_commitizen_project")
477479
def test_none_increment_should_not_call_git_tag_and_error_code_is_not_zero(
478-
mocker: MockFixture, tmp_commitizen_project
480+
mocker: MockFixture,
479481
):
480482
create_file_and_commit("test(test_get_all_droplets): fix bad comparison test")
481483
testargs = ["cz", "bump", "--yes"]
@@ -531,9 +533,8 @@ def test_bump_with_changelog_config(mocker: MockFixture, changelog_path, config_
531533
assert "0.2.0" in out
532534

533535

534-
def test_prevent_prerelease_when_no_increment_detected(
535-
mocker: MockFixture, capsys, tmp_commitizen_project
536-
):
536+
@pytest.mark.usefixtures("tmp_commitizen_project")
537+
def test_prevent_prerelease_when_no_increment_detected(mocker: MockFixture, capsys):
537538
create_file_and_commit("feat: new file")
538539

539540
testargs = ["cz", "bump", "--yes"]
@@ -690,6 +691,7 @@ def test_bump_changelog_command_commits_untracked_changelog_and_version_files(
690691
["cz", "bump", "--increment", "PATCH", "1.2.3"],
691692
],
692693
)
694+
@pytest.mark.usefixtures("tmp_commitizen_project")
693695
def test_bump_invalid_manual_args_raises_exception(mocker, testargs):
694696
mocker.patch.object(sys, "argv", testargs)
695697

Diff for: tests/commands/test_changelog_command.py

+1
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ def test_changelog_without_revision(mocker: MockFixture, tmp_commitizen_project)
345345
cli.main()
346346

347347

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

Diff for: tests/commands/test_commit_command.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717

1818

1919
@pytest.fixture
20-
def staging_is_clean(mocker: MockFixture):
20+
def staging_is_clean(mocker: MockFixture, tmp_git_project):
2121
is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean")
2222
is_staging_clean_mock.return_value = False
23+
return tmp_git_project
2324

2425

2526
@pytest.mark.usefixtures("staging_is_clean")
@@ -128,6 +129,7 @@ def test_commit_command_with_signoff_option(config, mocker: MockFixture):
128129
success_mock.assert_called_once()
129130

130131

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

Diff for: 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):

Diff for: tests/test_bump_create_commit_message.py

+35-40
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
@@ -29,20 +28,19 @@ def test_create_tag(test_input, expected):
2928

3029

3130
@pytest.mark.parametrize("retry", (True, False))
32-
def test_bump_pre_commit_changelog(
33-
tmp_commitizen_project, mocker: MockFixture, freezer, retry
34-
):
31+
@pytest.mark.usefixtures("tmp_commitizen_project")
32+
def test_bump_pre_commit_changelog(mocker: MockFixture, freezer, retry):
3533
freezer.move_to("2022-04-01")
3634
testargs = ["cz", "bump", "--changelog", "--yes"]
3735
if retry:
3836
testargs.append("--retry")
3937
else:
4038
pytest.xfail("it will fail because pre-commit will reformat CHANGELOG.md")
4139
mocker.patch.object(sys, "argv", testargs)
42-
with tmp_commitizen_project.as_cwd():
43-
# Configure prettier as a pre-commit hook
44-
Path(".pre-commit-config.yaml").write_text(
45-
"""
40+
# Configure prettier as a pre-commit hook
41+
Path(".pre-commit-config.yaml").write_text(
42+
dedent(
43+
"""\
4644
repos:
4745
- repo: https://github.com/pre-commit/mirrors-prettier
4846
rev: v2.6.2
@@ -51,44 +49,43 @@ def test_bump_pre_commit_changelog(
5149
stages: [commit]
5250
"""
5351
)
54-
# Prettier inherits editorconfig
55-
Path(".editorconfig").write_text(
56-
"""
52+
)
53+
# Prettier inherits editorconfig
54+
Path(".editorconfig").write_text(
55+
dedent(
56+
"""\
5757
[*]
5858
indent_size = 4
5959
"""
6060
)
61-
cmd.run("git add -A")
62-
if os.name == "nt":
63-
cmd.run('git commit -m "fix: _test"')
64-
else:
65-
cmd.run("git commit -m 'fix: _test'")
66-
cmd.run("pre-commit install")
67-
cli.main()
68-
# Pre-commit fixed last line adding extra indent and "\" char
69-
assert Path("CHANGELOG.md").read_text() == dedent(
70-
"""\
71-
## 0.1.1 (2022-04-01)
61+
)
62+
cmd.run("git add -A")
63+
cmd.run('git commit -m "fix: _test"')
64+
cmd.run("pre-commit install")
65+
cli.main()
66+
# Pre-commit fixed last line adding extra indent and "\" char
67+
assert Path("CHANGELOG.md").read_text() == dedent(
68+
"""\
69+
## 0.1.1 (2022-04-01)
7270
73-
### Fix
71+
### Fix
7472
75-
- \\_test
76-
"""
77-
)
73+
- \\_test
74+
"""
75+
)
7876

7977

8078
@pytest.mark.parametrize("retry", (True, False))
81-
def test_bump_pre_commit_changelog_fails_always(
82-
tmp_commitizen_project, mocker: MockFixture, freezer, retry
83-
):
79+
@pytest.mark.usefixtures("tmp_commitizen_project")
80+
def test_bump_pre_commit_changelog_fails_always(mocker: MockFixture, freezer, retry):
8481
freezer.move_to("2022-04-01")
8582
testargs = ["cz", "bump", "--changelog", "--yes"]
8683
if retry:
8784
testargs.append("--retry")
8885
mocker.patch.object(sys, "argv", testargs)
89-
with tmp_commitizen_project.as_cwd():
90-
Path(".pre-commit-config.yaml").write_text(
91-
"""
86+
Path(".pre-commit-config.yaml").write_text(
87+
dedent(
88+
"""\
9289
repos:
9390
- repo: local
9491
hooks:
@@ -99,11 +96,9 @@ def test_bump_pre_commit_changelog_fails_always(
9996
files: CHANGELOG.md
10097
"""
10198
)
102-
cmd.run("git add -A")
103-
if os.name == "nt":
104-
cmd.run('git commit -m "feat: forbid changelogs"')
105-
else:
106-
cmd.run("git commit -m 'feat: forbid changelogs'")
107-
cmd.run("pre-commit install")
108-
with pytest.raises(exceptions.BumpCommitFailedError):
109-
cli.main()
99+
)
100+
cmd.run("git add -A")
101+
cmd.run('git commit -m "feat: forbid changelogs"')
102+
cmd.run("pre-commit install")
103+
with pytest.raises(exceptions.BumpCommitFailedError):
104+
cli.main()

Diff for: 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)