|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import re |
| 4 | +import sys |
| 5 | +import textwrap |
| 6 | +from typing import TYPE_CHECKING, Any |
| 7 | + |
| 8 | +import pytest |
| 9 | +import tomli_w |
| 10 | + |
| 11 | +from setuptools_git_versioning import metadata |
| 12 | +from tests.lib.util import create_file, create_tag, execute |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from pathlib import Path |
| 16 | + |
| 17 | +pytestmark = [pytest.mark.all, pytest.mark.important] |
| 18 | + |
| 19 | + |
| 20 | +def write_config(repo: Path, config: dict[str, Any] | None) -> None: |
| 21 | + """Write a pyproject.toml with only the [tool.setuptools-git-versioning] section we need.""" |
| 22 | + cfg: dict[str, Any] = {"project": {"name": "mypkg", "dynamic": ["version"]}} |
| 23 | + if config is not None: |
| 24 | + cfg["tool"] = {"setuptools-git-versioning": config} |
| 25 | + create_file(repo, "pyproject.toml", tomli_w.dumps(cfg)) |
| 26 | + |
| 27 | + |
| 28 | +def test_untagged_repo_returns_starting_version(repo, monkeypatch): |
| 29 | + write_config(repo, {"enabled": True}) |
| 30 | + monkeypatch.chdir(repo) |
| 31 | + |
| 32 | + assert metadata.dynamic_metadata("version") == "0.0.1" |
| 33 | + |
| 34 | + |
| 35 | +def test_tagged_repo_returns_tag(repo, monkeypatch): |
| 36 | + write_config(repo, {"enabled": True}) |
| 37 | + create_tag(repo, "1.2.3") |
| 38 | + monkeypatch.chdir(repo) |
| 39 | + |
| 40 | + assert metadata.dynamic_metadata("version") == "1.2.3" |
| 41 | + |
| 42 | + |
| 43 | +def test_dev_template_used_after_tag(repo, monkeypatch): |
| 44 | + write_config(repo, {"enabled": True}) |
| 45 | + create_tag(repo, "1.2.3") |
| 46 | + create_file(repo, "extra.txt", "extra") |
| 47 | + monkeypatch.chdir(repo) |
| 48 | + |
| 49 | + result = metadata.dynamic_metadata("version") |
| 50 | + assert re.fullmatch(r"1\.2\.3\.post1\+git\.[0-9a-f]{8}", result), result |
| 51 | + |
| 52 | + |
| 53 | +def test_reads_project_name_from_pyproject(repo, monkeypatch): |
| 54 | + write_config(repo, {"enabled": True}) |
| 55 | + monkeypatch.chdir(repo) |
| 56 | + |
| 57 | + captured: dict[str, Any] = {} |
| 58 | + |
| 59 | + def fake_version_from_git(package_name=None, **_kwargs): |
| 60 | + from packaging.version import Version |
| 61 | + |
| 62 | + captured["package_name"] = package_name |
| 63 | + return Version("9.9.9") |
| 64 | + |
| 65 | + monkeypatch.setattr(metadata, "version_from_git", fake_version_from_git) |
| 66 | + |
| 67 | + assert metadata.dynamic_metadata("version") == "9.9.9" |
| 68 | + assert captured["package_name"] == "mypkg" |
| 69 | + |
| 70 | + |
| 71 | +def test_no_project_section_means_no_package_name(repo, monkeypatch): |
| 72 | + create_file( |
| 73 | + repo, |
| 74 | + "pyproject.toml", |
| 75 | + tomli_w.dumps({"tool": {"setuptools-git-versioning": {"enabled": True}}}), |
| 76 | + ) |
| 77 | + monkeypatch.chdir(repo) |
| 78 | + |
| 79 | + captured: dict[str, Any] = {} |
| 80 | + |
| 81 | + def fake_version_from_git(package_name=None, **_kwargs): |
| 82 | + from packaging.version import Version |
| 83 | + |
| 84 | + captured["package_name"] = package_name |
| 85 | + return Version("0.0.1") |
| 86 | + |
| 87 | + monkeypatch.setattr(metadata, "version_from_git", fake_version_from_git) |
| 88 | + |
| 89 | + metadata.dynamic_metadata("version") |
| 90 | + assert captured["package_name"] is None |
| 91 | + |
| 92 | + |
| 93 | +def test_rejects_non_version_field(repo, monkeypatch): |
| 94 | + write_config(repo, {"enabled": True}) |
| 95 | + monkeypatch.chdir(repo) |
| 96 | + |
| 97 | + with pytest.raises(ValueError, match="Only the 'version' field"): |
| 98 | + metadata.dynamic_metadata("description") |
| 99 | + |
| 100 | + |
| 101 | +def test_rejects_inline_settings(repo, monkeypatch): |
| 102 | + write_config(repo, {"enabled": True}) |
| 103 | + monkeypatch.chdir(repo) |
| 104 | + |
| 105 | + with pytest.raises(ValueError, match="Inline configuration"): |
| 106 | + metadata.dynamic_metadata("version", {"template": "{tag}"}) |
| 107 | + |
| 108 | + |
| 109 | +def test_rejects_missing_section(repo, monkeypatch): |
| 110 | + # pyproject.toml exists but has no [tool.setuptools-git-versioning] section |
| 111 | + create_file(repo, "pyproject.toml", textwrap.dedent('[project]\nname = "mypkg"\n')) |
| 112 | + monkeypatch.chdir(repo) |
| 113 | + |
| 114 | + with pytest.raises(ValueError, match=r"Missing \[tool\.setuptools-git-versioning\]"): |
| 115 | + metadata.dynamic_metadata("version") |
| 116 | + |
| 117 | + |
| 118 | +def test_rejects_enabled_false(repo, monkeypatch): |
| 119 | + write_config(repo, {"enabled": False}) |
| 120 | + monkeypatch.chdir(repo) |
| 121 | + |
| 122 | + with pytest.raises(ValueError, match="enabled = false"): |
| 123 | + metadata.dynamic_metadata("version") |
| 124 | + |
| 125 | + |
| 126 | +def test_get_requires_for_dynamic_metadata(): |
| 127 | + assert metadata.get_requires_for_dynamic_metadata() == ["setuptools-git-versioning"] |
| 128 | + assert metadata.get_requires_for_dynamic_metadata({"anything": True}) == ["setuptools-git-versioning"] |
| 129 | + |
| 130 | + |
| 131 | +def test_end_to_end_build_via_scikit_build_core(repo): |
| 132 | + """Drive the actual scikit-build-core backend to verify the provider protocol matches.""" |
| 133 | + pytest.importorskip("scikit_build_core") |
| 134 | + |
| 135 | + create_file( |
| 136 | + repo, |
| 137 | + "pyproject.toml", |
| 138 | + tomli_w.dumps( |
| 139 | + { |
| 140 | + "build-system": { |
| 141 | + "requires": ["scikit-build-core", "setuptools-git-versioning"], |
| 142 | + "build-backend": "scikit_build_core.build", |
| 143 | + }, |
| 144 | + "project": {"name": "mypkg", "dynamic": ["version"]}, |
| 145 | + "tool": { |
| 146 | + "scikit-build": { |
| 147 | + "experimental": True, |
| 148 | + "metadata": { |
| 149 | + "version": {"provider": "setuptools_git_versioning.metadata"}, |
| 150 | + }, |
| 151 | + }, |
| 152 | + "setuptools-git-versioning": {"enabled": True}, |
| 153 | + }, |
| 154 | + }, |
| 155 | + ), |
| 156 | + ) |
| 157 | + create_file( |
| 158 | + repo, |
| 159 | + "CMakeLists.txt", |
| 160 | + textwrap.dedent( |
| 161 | + """ |
| 162 | + cmake_minimum_required(VERSION 3.15) |
| 163 | + project(mypkg LANGUAGES NONE) |
| 164 | + install(FILES mypkg/__init__.py DESTINATION mypkg) |
| 165 | + """, |
| 166 | + ), |
| 167 | + ) |
| 168 | + (repo / "mypkg").mkdir() |
| 169 | + create_file(repo, "mypkg/__init__.py", "") |
| 170 | + create_tag(repo, "1.2.3") |
| 171 | + |
| 172 | + execute(repo, sys.executable, "-m", "build", "--sdist", "--no-isolation") |
| 173 | + |
| 174 | + sdists = list((repo / "dist").glob("mypkg-*.tar.gz")) |
| 175 | + assert len(sdists) == 1, sdists |
| 176 | + assert sdists[0].name == "mypkg-1.2.3.tar.gz" |
0 commit comments