1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
from __future__ import annotations
import os
import sys
from datetime import date
from pathlib import Path
import pytest
import setuptools_scm
from setuptools_scm import Configuration
from setuptools_scm import dump_version
from setuptools_scm._run_cmd import run
from setuptools_scm.integration import data_from_mime
from setuptools_scm.version import ScmVersion
from setuptools_scm.version import meta
from testing.wd_wrapper import WorkDir
c = Configuration()
template = """\
__version__ = version = {version!r}
__version_tuple__ = version_tuple = {version_tuple!r}
__sha__ = {scm_version.node!r}
"""
def test_run_plain(tmp_path: Path) -> None:
run([sys.executable, "-c", "print(1)"], cwd=tmp_path)
def test_data_from_mime(tmp_path: Path) -> None:
tmpfile = tmp_path.joinpath("test.archival")
tmpfile.write_bytes(b"name: test\nrevision: 1")
res = data_from_mime(str(tmpfile))
assert res == {"name": "test", "revision": "1"}
def test_version_from_pkginfo(wd: WorkDir) -> None:
wd.write("PKG-INFO", "Version: 0.1")
assert wd.get_version() == "0.1"
# replicate issue 167
assert wd.get_version(version_scheme="1.{0.distance}.0".format) == "0.1"
def assert_root(monkeypatch: pytest.MonkeyPatch, expected_root: str) -> None:
"""
Patch version_from_scm to simply assert that root is expected root
"""
def assertion(config: Configuration) -> ScmVersion:
assert config.absolute_root == expected_root
return ScmVersion("1.0", config=config)
monkeypatch.setattr(setuptools_scm._get_version_impl, "parse_version", assertion)
def test_root_parameter_creation(monkeypatch: pytest.MonkeyPatch) -> None:
assert_root(monkeypatch, os.getcwd())
setuptools_scm.get_version()
def test_root_parameter_pass_by(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
assert_root(monkeypatch, os.fspath(tmp_path))
setuptools_scm.get_version(root=os.fspath(tmp_path))
setuptools_scm.get_version(
os.fspath(tmp_path)
) # issue 669 - posarg difference between Configuration and get_version
def test_parentdir_prefix(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("SETUPTOOLS_SCM_DEBUG")
p = tmp_path.joinpath("projectname-v12.34")
p.mkdir()
p.joinpath("setup.py").write_text(
"""from setuptools import setup
setup(use_scm_version={"parentdir_prefix_version": "projectname-"})
""",
encoding="utf-8",
)
res = run([sys.executable, "setup.py", "--version"], p)
assert res.stdout == "12.34"
def test_fallback(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("SETUPTOOLS_SCM_DEBUG")
p = tmp_path / "sub/package"
p.mkdir(parents=True)
p.joinpath("setup.py").write_text(
"""from setuptools import setup
setup(use_scm_version={"fallback_version": "12.34"})
""",
encoding="utf-8",
)
res = run([sys.executable, "setup.py", "--version"], p)
assert res.stdout == "12.34"
def test_empty_pretend_version(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("SETUPTOOLS_SCM_DEBUG")
monkeypatch.setenv("SETUPTOOLS_SCM_PRETEND_VERSION", "")
p = tmp_path / "sub/package"
p.mkdir(parents=True)
p.joinpath("setup.py").write_text(
"""from setuptools import setup
setup(use_scm_version={"fallback_version": "12.34"})
""",
encoding="utf-8",
)
res = run([sys.executable, "setup.py", "--version"], p)
assert res.stdout == "12.34"
def test_empty_pretend_version_named(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.delenv("SETUPTOOLS_SCM_DEBUG")
monkeypatch.setenv("SETUPTOOLS_SCM_PRETEND_VERSION", "1.23")
monkeypatch.setenv("SETUPTOOLS_SCM_PRETEND_VERSION_FOR_MYSCM", "")
p = tmp_path.joinpath("sub/package")
p.mkdir(parents=True)
p.joinpath("setup.py").write_text(
"""from setuptools import setup
setup(name="myscm", use_scm_version={"fallback_version": "12.34"})
""",
encoding="utf-8",
)
res = run([sys.executable, "setup.py", "--version"], p)
assert res.stdout == "12.34"
def test_get_version_blank_tag_regex() -> None:
with pytest.warns(
DeprecationWarning, match="empty regex for tag regex is invalid, using default"
):
setuptools_scm.get_version(tag_regex="")
@pytest.mark.parametrize(
"version", ["1.0", "1.2.3.dev1+ge871260", "1.2.3.dev15+ge871260.d20180625", "2345"]
)
def test_pretended(version: str, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv(setuptools_scm._overrides.PRETEND_KEY, version)
assert setuptools_scm.get_version() == version
def test_root_relative_to(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
tmp_path.joinpath("setup.cfg").touch()
assert_root(monkeypatch, str(tmp_path / "alt"))
module = tmp_path / "module/file.py"
module.parent.mkdir()
module.touch()
setuptools_scm.get_version(
root="../alt",
relative_to=str(module),
)
with pytest.warns(UserWarning, match="relative_to is expected to be a file.*"):
setuptools_scm.get_version(
root="../alt",
relative_to=str(module.parent),
)
def test_dump_version(tmp_path: Path) -> None:
version = "1.0"
scm_version = meta(version, config=c)
dump_version(tmp_path, version, "first.txt", scm_version=scm_version)
def read(name: str) -> str:
return tmp_path.joinpath(name).read_text(encoding="utf-8")
assert read("first.txt") == "1.0"
version = "1.0.dev42"
scm_version = meta("1.0", distance=42, config=c)
dump_version(tmp_path, version, "first.py", scm_version=scm_version)
lines = read("first.py").splitlines()
assert lines[-2:] == [
"__version__ = version = '1.0.dev42'",
"__version_tuple__ = version_tuple = (1, 0, 'dev42')",
]
version = "1.0.1+g4ac9d2c"
scm_version = meta("1.0.1", node="g4ac9d2c", config=c)
dump_version(
tmp_path, version, "second.py", scm_version=scm_version, template=template
)
lines = read("second.py").splitlines()
assert "__version__ = version = '1.0.1+g4ac9d2c'" in lines
assert "__version_tuple__ = version_tuple = (1, 0, 1, 'g4ac9d2c')" in lines
assert "__sha__ = 'g4ac9d2c'" in lines
version = "1.2.3.dev18+gb366d8b.d20210415"
scm_version = meta(
"1.2.3", node="gb366d8b", distance=18, node_date=date(2021, 4, 15), config=c
)
dump_version(
tmp_path, version, "third.py", scm_version=scm_version, template=template
)
lines = read("third.py").splitlines()
assert "__version__ = version = '1.2.3.dev18+gb366d8b.d20210415'" in lines
assert (
"__version_tuple__ = version_tuple = (1, 2, 3, 'dev18', 'gb366d8b.d20210415')"
in lines
)
assert "__sha__ = 'gb366d8b'" in lines
import ast
ast.parse(read("third.py"))
def test_parse_plain_fails(recwarn: pytest.WarningsRecorder) -> None:
def parse(root: object) -> str:
return "tricked you"
with pytest.raises(TypeError):
setuptools_scm.get_version(parse=parse)
def test_custom_version_cls() -> None:
"""Test that `normalize` and `version_cls` work as expected"""
class MyVersion:
def __init__(self, tag_str: str) -> None:
self.version = tag_str
def __repr__(self) -> str:
return f"hello,{self.version}"
# you can not use normalize=False and version_cls at the same time
with pytest.raises(
ValueError,
match="Providing a custom `version_cls`"
" is not permitted when `normalize=False`",
):
setuptools_scm.get_version(normalize=False, version_cls=MyVersion)
# TODO unfortunately with PRETEND_KEY the preformatted flag becomes True
# which bypasses our class. which other mechanism would be ok to use here
# to create a test?
# monkeypatch.setenv(setuptools_scm.PRETEND_KEY, "1.0.1")
# assert setuptools_scm.get_version(version_cls=MyVersion) == "1"
def test_internal_get_version_warns_for_version_files(tmp_path: Path) -> None:
tmp_path.joinpath("PKG-INFO").write_bytes(b"Version: 0.1")
c = Configuration(root=tmp_path, fallback_root=tmp_path)
with pytest.warns(
DeprecationWarning,
match="force_write_version_files ought to be set,"
" presuming the legacy True value",
):
ver = setuptools_scm._get_version(c)
assert ver == "0.1"
# force write won't write as no version file is configured
assert setuptools_scm._get_version(c, force_write_version_files=False) == ver
assert setuptools_scm._get_version(c, force_write_version_files=True) == ver
|