Skip to content

Commit 16b8ba3

Browse files
committed
test: extract copy_sample_file_to_tmpdir as test utils
1 parent 96786a4 commit 16b8ba3

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

Diff for: tests/providers/test_uv_provider.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
from typing import TYPE_CHECKING
44

5+
import pytest
6+
57
from commitizen.config.base_config import BaseConfig
68
from commitizen.providers import get_provider
79
from commitizen.providers.uv_provider import UvProvider
10+
from tests.utils import copy_sample_file_to_tmpdir
811

912
if TYPE_CHECKING:
1013
from pytest_regressions.file_regression import FileRegressionFixture
@@ -21,18 +24,21 @@
2124
"""
2225

2326

27+
@pytest.fixture(scope="function")
28+
def commitizen_config_file(tmpdir):
29+
return copy_sample_file_to_tmpdir(tmpdir, "sample_uv.lock", "uv.lock")
30+
31+
2432
def test_uv_provider(
2533
config: BaseConfig, tmpdir, file_regression: FileRegressionFixture
2634
):
27-
with open("tests/data/sample_uv.lock") as input_file:
28-
uv_lock_content = input_file.read()
35+
copy_sample_file_to_tmpdir(tmpdir, "sample_uv.lock", "uv.lock")
2936

3037
with tmpdir.as_cwd():
3138
pyproject_toml_file = tmpdir / UvProvider.filename
3239
pyproject_toml_file.write_text(PYPROJECT_TOML, encoding="utf-8")
3340

3441
uv_lock_file = tmpdir / UvProvider.lock_filename
35-
uv_lock_file.write_text(uv_lock_content, encoding="utf-8")
3642

3743
config.settings["version_provider"] = "uv"
3844

Diff for: tests/test_bump_update_version_in_files.py

+8-18
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,49 @@
1-
from pathlib import Path
21
from shutil import copyfile
32

43
import pytest
54

65
from commitizen import bump
76
from commitizen.exceptions import CurrentVersionNotFoundError
7+
from tests.utils import copy_sample_file_to_tmpdir
88

99
MULTIPLE_VERSIONS_INCREASE_STRING = 'version = "1.2.9"\n' * 30
1010
MULTIPLE_VERSIONS_REDUCE_STRING = 'version = "1.2.10"\n' * 30
1111

1212
TESTING_FILE_PREFIX = "tests/data"
1313

1414

15-
def _copy_sample_file_to_tmpdir(
16-
tmp_path: Path, source_filename: str, dest_filename: str
17-
) -> Path:
18-
tmp_file = tmp_path / dest_filename
19-
copyfile(f"{TESTING_FILE_PREFIX}/{source_filename}", tmp_file)
20-
return tmp_file
21-
22-
2315
@pytest.fixture(scope="function")
2416
def commitizen_config_file(tmpdir):
25-
return _copy_sample_file_to_tmpdir(
26-
tmpdir, "sample_pyproject.toml", "pyproject.toml"
27-
)
17+
return copy_sample_file_to_tmpdir(tmpdir, "sample_pyproject.toml", "pyproject.toml")
2818

2919

3020
@pytest.fixture(scope="function")
3121
def python_version_file(tmpdir, request):
32-
return _copy_sample_file_to_tmpdir(tmpdir, "sample_version.py", "__version__.py")
22+
return copy_sample_file_to_tmpdir(tmpdir, "sample_version.py", "__version__.py")
3323

3424

3525
@pytest.fixture(scope="function")
3626
def inconsistent_python_version_file(tmpdir):
37-
return _copy_sample_file_to_tmpdir(
27+
return copy_sample_file_to_tmpdir(
3828
tmpdir, "inconsistent_version.py", "__version__.py"
3929
)
4030

4131

4232
@pytest.fixture(scope="function")
4333
def random_location_version_file(tmpdir):
44-
return _copy_sample_file_to_tmpdir(tmpdir, "sample_cargo.lock", "Cargo.lock")
34+
return copy_sample_file_to_tmpdir(tmpdir, "sample_cargo.lock", "Cargo.lock")
4535

4636

4737
@pytest.fixture(scope="function")
4838
def version_repeated_file(tmpdir):
49-
return _copy_sample_file_to_tmpdir(
39+
return copy_sample_file_to_tmpdir(
5040
tmpdir, "repeated_version_number.json", "package.json"
5141
)
5242

5343

5444
@pytest.fixture(scope="function")
5545
def docker_compose_file(tmpdir):
56-
return _copy_sample_file_to_tmpdir(
46+
return copy_sample_file_to_tmpdir(
5747
tmpdir, "sample_docker_compose.yaml", "docker-compose.yaml"
5848
)
5949

@@ -67,7 +57,7 @@ def docker_compose_file(tmpdir):
6757
ids=("with_eol", "without_eol"),
6858
)
6959
def multiple_versions_to_update_poetry_lock(tmpdir, request):
70-
return _copy_sample_file_to_tmpdir(tmpdir, request.param, "pyproject.toml")
60+
return copy_sample_file_to_tmpdir(tmpdir, request.param, "pyproject.toml")
7161

7262

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

Diff for: tests/utils.py

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55
import uuid
66
from pathlib import Path
7+
from shutil import copyfile
78

89
import pytest
910
from deprecated import deprecated
@@ -20,6 +21,8 @@
2021
reason="The output message of argparse is different between Python 3.13 and lower than Python 3.13",
2122
)
2223

24+
TESTING_FILE_PREFIX = "tests/data"
25+
2326

2427
class FakeCommand:
2528
def __init__(self, out=None, err=None, return_code=0):
@@ -74,6 +77,14 @@ def create_tag(tag: str, message: str | None = None) -> None:
7477
raise exceptions.CommitError(c.err)
7578

7679

80+
def copy_sample_file_to_tmpdir(
81+
tmp_path: Path, source_filename: str, dest_filename: str
82+
) -> Path:
83+
tmp_file = tmp_path / dest_filename
84+
copyfile(f"{TESTING_FILE_PREFIX}/{source_filename}", tmp_file)
85+
return tmp_file
86+
87+
7788
@deprecated(
7889
reason="\n\
7990
Prefer using `create_file_and_commit(filename, committer_date={your_date})` to influence the order of tags.\n\

0 commit comments

Comments
 (0)