Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ test = [
"pytest-regressions>=2.4.0",
"pytest-freezer>=0.4.6",
"pytest-xdist>=3.1.0",
"pytest-gitconfig>=0.9.0",
]

linters = [
Expand Down Expand Up @@ -177,9 +178,7 @@ env_list = ["3.10", "3.11", "3.12", "3.13", "3.14"]

[tool.tox.env_run_base]
description = "Run tests suite against Python {base_python}"
skip_install = true
deps = ["poetry>=2.0"]
commands_pre = [["poetry", "install", "--only", "main,test"]]
dependency_groups = ["test"]
commands = [["pytest", { replace = "posargs", extend = true }]]

[tool.ruff]
Expand Down
46 changes: 24 additions & 22 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import re
import tempfile
from pathlib import Path
from typing import TYPE_CHECKING

import pytest
Expand All @@ -15,45 +16,46 @@
from commitizen.config import BaseConfig
from commitizen.cz import registry
from commitizen.cz.base import BaseCommitizen
from tests.utils import create_file_and_commit

if TYPE_CHECKING:
from collections.abc import Iterator, Mapping
from pathlib import Path

from pytest_mock import MockerFixture

from commitizen.question import CzQuestion
from tests.utils import create_file_and_commit


SIGNER = "GitHub Action"
SIGNER_MAIL = "action@github.com"


@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)
@pytest.fixture
def repo_root() -> Path:
return Path(__file__).parent.parent


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

monkeypatch.setenv("GIT_CONFIG_GLOBAL", str(gitconfig))

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

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

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


@pytest.fixture
Expand Down
File renamed without changes.
55 changes: 26 additions & 29 deletions tests/test_bump_update_version_in_files.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from collections.abc import Callable
from pathlib import Path
from shutil import copyfile
from typing import TypeAlias

import pytest
from _pytest.fixtures import FixtureRequest
Expand All @@ -10,53 +12,48 @@
MULTIPLE_VERSIONS_INCREASE_STRING = 'version = "1.2.9"\n' * 30
MULTIPLE_VERSIONS_REDUCE_STRING = 'version = "1.2.10"\n' * 30

TESTING_FILE_PREFIX = "tests/data"

SampleFileFixture: TypeAlias = Callable[[str, str], Path]

def _copy_sample_file_to_tmpdir(
tmp_path: Path, source_filename: str, dest_filename: str
) -> Path:
tmp_file = tmp_path / dest_filename
copyfile(f"{TESTING_FILE_PREFIX}/{source_filename}", tmp_file)
return tmp_file

@pytest.fixture
def sample_file(tmp_path: Path, data_dir: Path) -> SampleFileFixture:
def fixture(source: str, destination: str) -> Path:
tmp_file = tmp_path / destination
copyfile(data_dir / source, tmp_file)
return tmp_file

return fixture


@pytest.fixture(scope="function")
def commitizen_config_file(tmp_path: Path) -> Path:
return _copy_sample_file_to_tmpdir(
tmp_path, "sample_pyproject.toml", "pyproject.toml"
)
def commitizen_config_file(sample_file: SampleFileFixture) -> Path:
return sample_file("sample_pyproject.toml", "pyproject.toml")


@pytest.fixture(scope="function")
def python_version_file(tmp_path: Path, request: FixtureRequest) -> Path:
return _copy_sample_file_to_tmpdir(tmp_path, "sample_version.py", "__version__.py")
def python_version_file(sample_file: SampleFileFixture) -> Path:
return sample_file("sample_version.py", "__version__.py")


@pytest.fixture(scope="function")
def inconsistent_python_version_file(tmp_path: Path) -> Path:
return _copy_sample_file_to_tmpdir(
tmp_path, "inconsistent_version.py", "__version__.py"
)
def inconsistent_python_version_file(sample_file: SampleFileFixture) -> Path:
return sample_file("inconsistent_version.py", "__version__.py")


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


@pytest.fixture(scope="function")
def version_repeated_file(tmp_path: Path) -> Path:
return _copy_sample_file_to_tmpdir(
tmp_path, "repeated_version_number.json", "package.json"
)
def version_repeated_file(sample_file: SampleFileFixture) -> Path:
return sample_file("repeated_version_number.json", "package.json")


@pytest.fixture(scope="function")
def docker_compose_file(tmp_path: Path) -> Path:
return _copy_sample_file_to_tmpdir(
tmp_path, "sample_docker_compose.yaml", "docker-compose.yaml"
)
def docker_compose_file(sample_file: SampleFileFixture) -> Path:
return sample_file("sample_docker_compose.yaml", "docker-compose.yaml")


@pytest.fixture(
Expand All @@ -68,9 +65,9 @@ def docker_compose_file(tmp_path: Path) -> Path:
ids=("with_eol", "without_eol"),
)
def multiple_versions_to_update_poetry_lock(
tmp_path: Path, request: FixtureRequest
sample_file: SampleFileFixture, request: FixtureRequest
) -> Path:
return _copy_sample_file_to_tmpdir(tmp_path, request.param, "pyproject.toml")
return sample_file(request.param, "pyproject.toml")


@pytest.fixture(scope="function")
Expand Down
7 changes: 4 additions & 3 deletions tests/test_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,9 @@ def tags() -> list[git.GitTag]:


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


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


@pytest.mark.usefixtures("in_repo_root")
def test_changelog_file_name_from_args_and_config():
mock_config = Mock(spec=BaseConfig)
mock_path = Mock(spec=Path)
Expand Down
1 change: 1 addition & 0 deletions tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def config_files_manager(request, tmpdir):
yield


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

Expand Down
31 changes: 12 additions & 19 deletions tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import inspect
import os
import platform
import shutil
from typing import TYPE_CHECKING

import pytest
Expand All @@ -19,6 +18,7 @@
)

if TYPE_CHECKING:
from pytest_gitconfig import GitConfig
from pytest_mock import MockFixture


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


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

try:
# temporarily turn on --show-signature
cmd.run("git config log.showsignature true")

# retrieve a commit that we know has a signature
commit = git.get_commits(
start="bec20ebf433f2281c70f1eb4b0b6a1d0ed83e9b2",
end="9eae518235d051f145807ddf971ceb79ad49953a",
)[0]

assert commit.title.startswith("fix")
finally:
# restore the repo's original config
shutil.move(config_backup, config_file)
# retrieve a commit that we know has a signature
commit = git.get_commits(
start="bec20ebf433f2281c70f1eb4b0b6a1d0ed83e9b2",
end="9eae518235d051f145807ddf971ceb79ad49953a",
)[0]

assert commit.title.startswith("fix")


def test_get_tag_names_has_correct_arrow_annotation():
Expand Down