Skip to content

Commit 11467ce

Browse files
noirbizarrebearomorphism
authored andcommitted
test(tox): ensure all tests can run from any directory
1 parent 392a654 commit 11467ce

File tree

8 files changed

+83
-73
lines changed

8 files changed

+83
-73
lines changed

poetry.lock

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ test = [
9595
"pytest-regressions>=2.4.0",
9696
"pytest-freezer>=0.4.6",
9797
"pytest-xdist>=3.1.0",
98+
"pytest-gitconfig>=0.9.0",
9899
]
99100

100101
linters = [

tests/conftest.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import re
55
import tempfile
6+
from pathlib import Path
67
from typing import TYPE_CHECKING
78

89
import pytest
@@ -15,45 +16,46 @@
1516
from commitizen.config import BaseConfig
1617
from commitizen.cz import registry
1718
from commitizen.cz.base import BaseCommitizen
19+
from tests.utils import create_file_and_commit
1820

1921
if TYPE_CHECKING:
2022
from collections.abc import Iterator, Mapping
21-
from pathlib import Path
2223

2324
from pytest_mock import MockerFixture
2425

2526
from commitizen.question import CzQuestion
26-
from tests.utils import create_file_and_commit
27+
2728

2829
SIGNER = "GitHub Action"
2930
SIGNER_MAIL = "[email protected]"
3031

3132

32-
@pytest.fixture(autouse=True)
33-
def git_sandbox(monkeypatch: pytest.MonkeyPatch, tmp_path: Path):
34-
"""Ensure git commands are executed without the current user settings"""
35-
# Clear any GIT_ prefixed environment variable
36-
for var in os.environ:
37-
if var.startswith("GIT_"):
38-
monkeypatch.delenv(var)
33+
@pytest.fixture
34+
def repo_root() -> Path:
35+
return Path(__file__).parent.parent
36+
3937

40-
# Define a dedicated temporary git config
41-
gitconfig = tmp_path / ".git" / "config"
42-
if not gitconfig.parent.exists():
43-
gitconfig.parent.mkdir()
38+
@pytest.fixture
39+
def in_repo_root(repo_root: Path) -> Iterator[Path]:
40+
cwd = os.getcwd()
41+
os.chdir(repo_root)
42+
yield repo_root
43+
os.chdir(cwd)
4444

45-
monkeypatch.setenv("GIT_CONFIG_GLOBAL", str(gitconfig))
4645

47-
r = cmd.run(f"git config --file {gitconfig} user.name {SIGNER}")
48-
assert r.return_code == 0, r.err
49-
r = cmd.run(f"git config --file {gitconfig} user.email {SIGNER_MAIL}")
50-
assert r.return_code == 0, r.err
46+
@pytest.fixture
47+
def data_dir(repo_root: Path) -> Path:
48+
return repo_root / "tests" / "data"
5149

52-
r = cmd.run(f"git config --file {gitconfig} safe.directory '*'")
53-
assert r.return_code == 0, r.err
5450

55-
r = cmd.run("git config --global init.defaultBranch master")
56-
assert r.return_code == 0, r.err
51+
@pytest.fixture(scope="session")
52+
def set_default_gitconfig() -> dict[str, str]:
53+
return {
54+
"user.name": "SIGNER",
55+
"user.email": SIGNER_MAIL,
56+
"safe.cirectory": "*",
57+
"init.defaultBranch": "master",
58+
}
5759

5860

5961
@pytest.fixture
File renamed without changes.

tests/test_bump_update_version_in_files.py

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from collections.abc import Callable
12
from pathlib import Path
23
from shutil import copyfile
4+
from typing import TypeAlias
35

46
import pytest
57
from _pytest.fixtures import FixtureRequest
@@ -10,53 +12,48 @@
1012
MULTIPLE_VERSIONS_INCREASE_STRING = 'version = "1.2.9"\n' * 30
1113
MULTIPLE_VERSIONS_REDUCE_STRING = 'version = "1.2.10"\n' * 30
1214

13-
TESTING_FILE_PREFIX = "tests/data"
1415

16+
SampleFileFixture: TypeAlias = Callable[[str, str], Path]
1517

16-
def _copy_sample_file_to_tmpdir(
17-
tmp_path: Path, source_filename: str, dest_filename: str
18-
) -> Path:
19-
tmp_file = tmp_path / dest_filename
20-
copyfile(f"{TESTING_FILE_PREFIX}/{source_filename}", tmp_file)
21-
return tmp_file
18+
19+
@pytest.fixture
20+
def sample_file(tmp_path: Path, data_dir: Path) -> SampleFileFixture:
21+
def fixture(source: str, destination: str) -> Path:
22+
tmp_file = tmp_path / destination
23+
copyfile(data_dir / source, tmp_file)
24+
return tmp_file
25+
26+
return fixture
2227

2328

2429
@pytest.fixture(scope="function")
25-
def commitizen_config_file(tmp_path: Path) -> Path:
26-
return _copy_sample_file_to_tmpdir(
27-
tmp_path, "sample_pyproject.toml", "pyproject.toml"
28-
)
30+
def commitizen_config_file(sample_file: SampleFileFixture) -> Path:
31+
return sample_file("sample_pyproject.toml", "pyproject.toml")
2932

3033

3134
@pytest.fixture(scope="function")
32-
def python_version_file(tmp_path: Path, request: FixtureRequest) -> Path:
33-
return _copy_sample_file_to_tmpdir(tmp_path, "sample_version.py", "__version__.py")
35+
def python_version_file(sample_file: SampleFileFixture) -> Path:
36+
return sample_file("sample_version.py", "__version__.py")
3437

3538

3639
@pytest.fixture(scope="function")
37-
def inconsistent_python_version_file(tmp_path: Path) -> Path:
38-
return _copy_sample_file_to_tmpdir(
39-
tmp_path, "inconsistent_version.py", "__version__.py"
40-
)
40+
def inconsistent_python_version_file(sample_file: SampleFileFixture) -> Path:
41+
return sample_file("inconsistent_version.py", "__version__.py")
4142

4243

4344
@pytest.fixture(scope="function")
44-
def random_location_version_file(tmp_path: Path) -> Path:
45-
return _copy_sample_file_to_tmpdir(tmp_path, "sample_cargo.lock", "Cargo.lock")
45+
def random_location_version_file(sample_file: SampleFileFixture) -> Path:
46+
return sample_file("sample_cargo.lock", "Cargo.lock")
4647

4748

4849
@pytest.fixture(scope="function")
49-
def version_repeated_file(tmp_path: Path) -> Path:
50-
return _copy_sample_file_to_tmpdir(
51-
tmp_path, "repeated_version_number.json", "package.json"
52-
)
50+
def version_repeated_file(sample_file: SampleFileFixture) -> Path:
51+
return sample_file("repeated_version_number.json", "package.json")
5352

5453

5554
@pytest.fixture(scope="function")
56-
def docker_compose_file(tmp_path: Path) -> Path:
57-
return _copy_sample_file_to_tmpdir(
58-
tmp_path, "sample_docker_compose.yaml", "docker-compose.yaml"
59-
)
55+
def docker_compose_file(sample_file: SampleFileFixture) -> Path:
56+
return sample_file("sample_docker_compose.yaml", "docker-compose.yaml")
6057

6158

6259
@pytest.fixture(
@@ -68,9 +65,9 @@ def docker_compose_file(tmp_path: Path) -> Path:
6865
ids=("with_eol", "without_eol"),
6966
)
7067
def multiple_versions_to_update_poetry_lock(
71-
tmp_path: Path, request: FixtureRequest
68+
sample_file: SampleFileFixture, request: FixtureRequest
7269
) -> Path:
73-
return _copy_sample_file_to_tmpdir(tmp_path, request.param, "pyproject.toml")
70+
return sample_file(request.param, "pyproject.toml")
7471

7572

7673
@pytest.fixture(scope="function")

tests/test_changelog.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,9 @@ def tags() -> list[git.GitTag]:
578578

579579

580580
@pytest.fixture
581-
def changelog_content() -> str:
582-
changelog_path = "tests/CHANGELOG_FOR_TEST.md"
583-
with open(changelog_path, encoding="utf-8") as f:
581+
def changelog_content(data_dir: Path) -> str:
582+
changelog = data_dir / "CHANGELOG_FOR_TEST.md"
583+
with changelog.open(encoding="utf-8") as f:
584584
return f.read()
585585

586586

@@ -1657,6 +1657,7 @@ def test_tags_rules_get_version_tags(capsys: pytest.CaptureFixture):
16571657
assert captured.err.count("not-a-version") == 2
16581658

16591659

1660+
@pytest.mark.usefixtures("in_repo_root")
16601661
def test_changelog_file_name_from_args_and_config():
16611662
mock_config = Mock(spec=BaseConfig)
16621663
mock_path = Mock(spec=Path)

tests/test_conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ def config_files_manager(request, tmpdir):
164164
yield
165165

166166

167+
@pytest.mark.usefixtures("in_repo_root")
167168
def test_find_git_project_root(tmpdir):
168169
assert git.find_git_project_root() == Path(os.getcwd())
169170

tests/test_git.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import inspect
44
import os
55
import platform
6-
import shutil
76
from typing import TYPE_CHECKING
87

98
import pytest
@@ -19,6 +18,7 @@
1918
)
2019

2120
if TYPE_CHECKING:
21+
from pytest_gitconfig import GitConfig
2222
from pytest_mock import MockFixture
2323

2424

@@ -260,25 +260,18 @@ def test_get_commits_with_and_without_parents(mocker: MockFixture):
260260
assert commits[2].parents == []
261261

262262

263-
def test_get_commits_with_signature():
264-
config_file = ".git/config"
265-
config_backup = ".git/config.bak"
266-
shutil.copy(config_file, config_backup)
263+
@pytest.mark.usefixtures("in_repo_root")
264+
def test_get_commits_with_signature(gitconfig: GitConfig):
265+
# temporarily turn on --show-signature
266+
gitconfig.set("log.showsignature", "true")
267267

268-
try:
269-
# temporarily turn on --show-signature
270-
cmd.run("git config log.showsignature true")
271-
272-
# retrieve a commit that we know has a signature
273-
commit = git.get_commits(
274-
start="bec20ebf433f2281c70f1eb4b0b6a1d0ed83e9b2",
275-
end="9eae518235d051f145807ddf971ceb79ad49953a",
276-
)[0]
277-
278-
assert commit.title.startswith("fix")
279-
finally:
280-
# restore the repo's original config
281-
shutil.move(config_backup, config_file)
268+
# retrieve a commit that we know has a signature
269+
commit = git.get_commits(
270+
start="bec20ebf433f2281c70f1eb4b0b6a1d0ed83e9b2",
271+
end="9eae518235d051f145807ddf971ceb79ad49953a",
272+
)[0]
273+
274+
assert commit.title.startswith("fix")
282275

283276

284277
def test_get_tag_names_has_correct_arrow_annotation():

0 commit comments

Comments
 (0)