Skip to content

Commit

Permalink
harden test_version_from git and more type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
RonnyPfannschmidt committed Apr 15, 2022
1 parent c72ee05 commit 422f83c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 49 deletions.
85 changes: 43 additions & 42 deletions testing/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
from datetime import datetime
from datetime import timezone
from os.path import join as opj
from textwrap import dedent
from typing import Dict
from unittest.mock import Mock
from unittest.mock import patch

import pytest

from .wd_wrapper import WorkDir
from setuptools_scm import Configuration
from setuptools_scm import format_version
from setuptools_scm import git
Expand All @@ -19,7 +22,6 @@
from setuptools_scm.utils import do
from setuptools_scm.utils import has_command


pytestmark = pytest.mark.skipif(
not has_command("git", warn=False), reason="git executable not found"
)
Expand Down Expand Up @@ -58,11 +60,11 @@ def test_root_relative_to(tmpdir, wd, monkeypatch):
"relative_to": __file__})
"""
)
res = do((sys.executable, "setup.py", "--version"), p)
res = do([sys.executable, "setup.py", "--version"], p)
assert res == "0.1.dev0"


def test_root_search_parent_directories(tmpdir, wd, monkeypatch):
def test_root_search_parent_directories(tmpdir, wd: WorkDir, monkeypatch):
monkeypatch.delenv("SETUPTOOLS_SCM_DEBUG")
p = wd.cwd.joinpath("sub/package")
p.mkdir(parents=True)
Expand All @@ -71,7 +73,7 @@ def test_root_search_parent_directories(tmpdir, wd, monkeypatch):
setup(use_scm_version={"search_parent_directories": True})
"""
)
res = do((sys.executable, "setup.py", "--version"), p)
res = do([sys.executable, "setup.py", "--version"], p)
assert res == "0.1.dev0"


Expand Down Expand Up @@ -137,60 +139,59 @@ def test_version_from_git(wd):
)


@pytest.mark.parametrize("with_class", [False, type, str])
def test_git_version_unnormalized_setuptools(with_class, tmpdir, wd, monkeypatch):
setup_py_with_normalize: Dict[str, str] = {
"false": """
from setuptools import setup
setup(use_scm_version={'normalize': False, 'write_to': 'VERSION.txt'})
""",
"with_created_class": """
from setuptools import setup
class MyVersion:
def __init__(self, tag_str: str):
self.version = tag_str
def __repr__(self):
return self.version
setup(use_scm_version={'version_cls': MyVersion, 'write_to': 'VERSION.txt'})
""",
"with_named_import": """
from setuptools import setup
setup(use_scm_version={
'version_cls': 'setuptools_scm.NonNormalizedVersion',
'write_to': 'VERSION.txt'
})
""",
}


@pytest.mark.parametrize(
"setup_py_txt",
[pytest.param(text, id=key) for key, text in setup_py_with_normalize.items()],
)
def test_git_version_unnormalized_setuptools(
setup_py_txt: str, wd: WorkDir, monkeypatch
):
"""
Test that when integrating with setuptools without normalization,
the version is not normalized in write_to files,
but still normalized by setuptools for the final dist metadata.
"""
monkeypatch.delenv("SETUPTOOLS_SCM_DEBUG")
p = wd.cwd

# create a setup.py
dest_file = str(tmpdir.join("VERSION.txt")).replace("\\", "/")
if with_class is False:
# try normalize = False
setup_py = """
from setuptools import setup
setup(use_scm_version={'normalize': False, 'write_to': '%s'})
"""
elif with_class is type:
# custom non-normalizing class
setup_py = """
from setuptools import setup
class MyVersion:
def __init__(self, tag_str: str):
self.version = tag_str
def __repr__(self):
return self.version
setup(use_scm_version={'version_cls': MyVersion, 'write_to': '%s'})
"""
elif with_class is str:
# non-normalizing class referenced by name
setup_py = """from setuptools import setup
setup(use_scm_version={
'version_cls': 'setuptools_scm.NonNormalizedVersion',
'write_to': '%s'
})
"""

# finally write the setup.py file
p.joinpath("setup.py").write_text(setup_py % dest_file)
wd.write("setup.py", dedent(setup_py_txt))

# do git operations and tag
wd.commit_testfile()
wd("git tag 17.33.0-rc1")

# setuptools still normalizes using packaging.Version (removing the dash)
res = do((sys.executable, "setup.py", "--version"), p)
res = wd([sys.executable, "setup.py", "--version"])
assert res == "17.33.0rc1"

# but the version tag in the file is non-normalized (with the dash)
assert tmpdir.join("VERSION.txt").read() == "17.33.0-rc1"
assert wd.cwd.joinpath("VERSION.txt").read_text() == "17.33.0-rc1"


@pytest.mark.issue(179)
Expand Down
17 changes: 10 additions & 7 deletions testing/wd_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import itertools
from pathlib import Path
from typing import List


class WorkDir:
Expand All @@ -12,20 +13,22 @@ class WorkDir:
def __repr__(self):
return f"<WD {self.cwd}>"

def __init__(self, cwd: Path):
def __init__(self, cwd: Path) -> None:
self.cwd = cwd
self.__counter = itertools.count()

def __call__(self, cmd, **kw):
def __call__(self, cmd: "List[str] | str", **kw):
if kw:
assert isinstance(cmd, str), "formatting the command requires text input"
cmd = cmd.format(**kw)
from setuptools_scm.utils import do

return do(cmd, self.cwd)

def write(self, name, value, **kw):
def write(self, name: str, value: "str | bytes", **kw: object) -> Path:
filename = self.cwd / name
if kw:
assert isinstance(value, str)
value = value.format(**kw)
if isinstance(value, bytes):
filename.write_bytes(value)
Expand All @@ -39,22 +42,22 @@ def _reason(self, given_reason: "str | None") -> str:
else:
return given_reason

def add_and_commit(self, reason=None, **kwargs):
def add_and_commit(self, reason: "str | None" = None, **kwargs):
self(self.add_command)
self.commit(reason, **kwargs)

def commit(self, reason=None, signed=False):
def commit(self, reason: "str | None" = None, signed: bool = False) -> None:
reason = self._reason(reason)
self(
self.commit_command if not signed else self.signed_commit_command,
reason=reason,
)

def commit_testfile(self, reason=None, **kwargs):
def commit_testfile(self, reason: "str | None" = None, signed: bool = False):
reason = self._reason(reason)
self.write("test.txt", "test {reason}", reason=reason)
self(self.add_command)
self.commit(reason=reason, **kwargs)
self.commit(reason=reason, signed=signed)

def get_version(self, **kw):
__tracebackhide__ = True
Expand Down

0 comments on commit 422f83c

Please sign in to comment.