Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass ScmVersion to dump_version to provide scm_version as extra template variable #854

Merged
merged 12 commits into from
May 31, 2023
17 changes: 15 additions & 2 deletions src/setuptools_scm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from ._version_cls import NonNormalizedVersion
from ._version_cls import Version
from .version import format_version as _format_version
from .version import ScmVersion

if TYPE_CHECKING:
from typing import NoReturn
Expand All @@ -46,6 +47,7 @@ def dump_version(
version: str,
write_to: _t.PathT,
template: str | None = None,
scm_version: ScmVersion | None = None,
) -> None:
assert isinstance(version, str)
target = os.path.normpath(os.path.join(root, write_to))
Expand All @@ -62,8 +64,18 @@ def dump_version(
)
version_tuple = _version_as_tuple(version)

with open(target, "w") as fp:
fp.write(template.format(version=version, version_tuple=version_tuple))
if scm_version is not None:
with open(target, "w") as fp:
fp.write(
template.format(
version=version,
version_tuple=version_tuple,
scm_version=scm_version,
)
)
else:
with open(target, "w") as fp:
fp.write(template.format(version=version, version_tuple=version_tuple))


def _do_parse(config: Configuration) -> _t.SCMVERSION | None:
Expand Down Expand Up @@ -164,6 +176,7 @@ def _get_version(config: Configuration) -> str | None:
dump_version(
root=config.root,
version=version_string,
scm_version=parsed_version,
write_to=config.write_to,
template=config.write_to_template,
)
Expand Down
35 changes: 31 additions & 4 deletions testing/test_basic_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import sys
from datetime import date
from pathlib import Path

import pytest
Expand All @@ -11,10 +12,20 @@
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 meta
from setuptools_scm.version import ScmVersion
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)

Expand Down Expand Up @@ -155,30 +166,46 @@ def test_root_relative_to(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> No


def test_dump_version(tmp_path: Path) -> None:
dump_version(tmp_path, "1.0", "first.txt")
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()

assert read("first.txt") == "1.0"

dump_version(tmp_path, "1.0.dev42", "first.py")
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 "__version__ = version = '1.0.dev42'" in lines
assert "__version_tuple__ = version_tuple = (1, 0, 'dev42')" in lines

dump_version(tmp_path, "1.0.1+g4ac9d2c", "second.py")
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

dump_version(tmp_path, "1.2.3.dev18+gb366d8b.d20210415", "third.py")
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

Expand Down
3 changes: 2 additions & 1 deletion testing/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ def test_format_version(
def test_dump_version_doesnt_bail_on_value_error(tmp_path: Path) -> None:
write_to = "VERSION"
version = str(VERSIONS["exact"].tag)
scm_version = meta(VERSIONS["exact"].tag, config=c)
with pytest.raises(ValueError, match="^bad file format:"):
dump_version(tmp_path, version, write_to)
dump_version(tmp_path, version, write_to, scm_version=scm_version)


@pytest.mark.parametrize(
Expand Down