Skip to content

Commit 422f83c

Browse files
harden test_version_from git and more type fixes
1 parent c72ee05 commit 422f83c

File tree

2 files changed

+53
-49
lines changed

2 files changed

+53
-49
lines changed

testing/test_git.py

+43-42
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
from datetime import datetime
55
from datetime import timezone
66
from os.path import join as opj
7+
from textwrap import dedent
8+
from typing import Dict
79
from unittest.mock import Mock
810
from unittest.mock import patch
911

1012
import pytest
1113

14+
from .wd_wrapper import WorkDir
1215
from setuptools_scm import Configuration
1316
from setuptools_scm import format_version
1417
from setuptools_scm import git
@@ -19,7 +22,6 @@
1922
from setuptools_scm.utils import do
2023
from setuptools_scm.utils import has_command
2124

22-
2325
pytestmark = pytest.mark.skipif(
2426
not has_command("git", warn=False), reason="git executable not found"
2527
)
@@ -58,11 +60,11 @@ def test_root_relative_to(tmpdir, wd, monkeypatch):
5860
"relative_to": __file__})
5961
"""
6062
)
61-
res = do((sys.executable, "setup.py", "--version"), p)
63+
res = do([sys.executable, "setup.py", "--version"], p)
6264
assert res == "0.1.dev0"
6365

6466

65-
def test_root_search_parent_directories(tmpdir, wd, monkeypatch):
67+
def test_root_search_parent_directories(tmpdir, wd: WorkDir, monkeypatch):
6668
monkeypatch.delenv("SETUPTOOLS_SCM_DEBUG")
6769
p = wd.cwd.joinpath("sub/package")
6870
p.mkdir(parents=True)
@@ -71,7 +73,7 @@ def test_root_search_parent_directories(tmpdir, wd, monkeypatch):
7173
setup(use_scm_version={"search_parent_directories": True})
7274
"""
7375
)
74-
res = do((sys.executable, "setup.py", "--version"), p)
76+
res = do([sys.executable, "setup.py", "--version"], p)
7577
assert res == "0.1.dev0"
7678

7779

@@ -137,60 +139,59 @@ def test_version_from_git(wd):
137139
)
138140

139141

140-
@pytest.mark.parametrize("with_class", [False, type, str])
141-
def test_git_version_unnormalized_setuptools(with_class, tmpdir, wd, monkeypatch):
142+
setup_py_with_normalize: Dict[str, str] = {
143+
"false": """
144+
from setuptools import setup
145+
setup(use_scm_version={'normalize': False, 'write_to': 'VERSION.txt'})
146+
""",
147+
"with_created_class": """
148+
from setuptools import setup
149+
150+
class MyVersion:
151+
def __init__(self, tag_str: str):
152+
self.version = tag_str
153+
154+
def __repr__(self):
155+
return self.version
156+
157+
setup(use_scm_version={'version_cls': MyVersion, 'write_to': 'VERSION.txt'})
158+
""",
159+
"with_named_import": """
160+
from setuptools import setup
161+
setup(use_scm_version={
162+
'version_cls': 'setuptools_scm.NonNormalizedVersion',
163+
'write_to': 'VERSION.txt'
164+
})
165+
""",
166+
}
167+
168+
169+
@pytest.mark.parametrize(
170+
"setup_py_txt",
171+
[pytest.param(text, id=key) for key, text in setup_py_with_normalize.items()],
172+
)
173+
def test_git_version_unnormalized_setuptools(
174+
setup_py_txt: str, wd: WorkDir, monkeypatch
175+
):
142176
"""
143177
Test that when integrating with setuptools without normalization,
144178
the version is not normalized in write_to files,
145179
but still normalized by setuptools for the final dist metadata.
146180
"""
147181
monkeypatch.delenv("SETUPTOOLS_SCM_DEBUG")
148-
p = wd.cwd
149-
150-
# create a setup.py
151-
dest_file = str(tmpdir.join("VERSION.txt")).replace("\\", "/")
152-
if with_class is False:
153-
# try normalize = False
154-
setup_py = """
155-
from setuptools import setup
156-
setup(use_scm_version={'normalize': False, 'write_to': '%s'})
157-
"""
158-
elif with_class is type:
159-
# custom non-normalizing class
160-
setup_py = """
161-
from setuptools import setup
162-
163-
class MyVersion:
164-
def __init__(self, tag_str: str):
165-
self.version = tag_str
166-
167-
def __repr__(self):
168-
return self.version
169-
170-
setup(use_scm_version={'version_cls': MyVersion, 'write_to': '%s'})
171-
"""
172-
elif with_class is str:
173-
# non-normalizing class referenced by name
174-
setup_py = """from setuptools import setup
175-
setup(use_scm_version={
176-
'version_cls': 'setuptools_scm.NonNormalizedVersion',
177-
'write_to': '%s'
178-
})
179-
"""
180182

181-
# finally write the setup.py file
182-
p.joinpath("setup.py").write_text(setup_py % dest_file)
183+
wd.write("setup.py", dedent(setup_py_txt))
183184

184185
# do git operations and tag
185186
wd.commit_testfile()
186187
wd("git tag 17.33.0-rc1")
187188

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

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

195196

196197
@pytest.mark.issue(179)

testing/wd_wrapper.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import itertools
22
from pathlib import Path
3+
from typing import List
34

45

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

15-
def __init__(self, cwd: Path):
16+
def __init__(self, cwd: Path) -> None:
1617
self.cwd = cwd
1718
self.__counter = itertools.count()
1819

19-
def __call__(self, cmd, **kw):
20+
def __call__(self, cmd: "List[str] | str", **kw):
2021
if kw:
22+
assert isinstance(cmd, str), "formatting the command requires text input"
2123
cmd = cmd.format(**kw)
2224
from setuptools_scm.utils import do
2325

2426
return do(cmd, self.cwd)
2527

26-
def write(self, name, value, **kw):
28+
def write(self, name: str, value: "str | bytes", **kw: object) -> Path:
2729
filename = self.cwd / name
2830
if kw:
31+
assert isinstance(value, str)
2932
value = value.format(**kw)
3033
if isinstance(value, bytes):
3134
filename.write_bytes(value)
@@ -39,22 +42,22 @@ def _reason(self, given_reason: "str | None") -> str:
3942
else:
4043
return given_reason
4144

42-
def add_and_commit(self, reason=None, **kwargs):
45+
def add_and_commit(self, reason: "str | None" = None, **kwargs):
4346
self(self.add_command)
4447
self.commit(reason, **kwargs)
4548

46-
def commit(self, reason=None, signed=False):
49+
def commit(self, reason: "str | None" = None, signed: bool = False) -> None:
4750
reason = self._reason(reason)
4851
self(
4952
self.commit_command if not signed else self.signed_commit_command,
5053
reason=reason,
5154
)
5255

53-
def commit_testfile(self, reason=None, **kwargs):
56+
def commit_testfile(self, reason: "str | None" = None, signed: bool = False):
5457
reason = self._reason(reason)
5558
self.write("test.txt", "test {reason}", reason=reason)
5659
self(self.add_command)
57-
self.commit(reason=reason, **kwargs)
60+
self.commit(reason=reason, signed=signed)
5861

5962
def get_version(self, **kw):
6063
__tracebackhide__ = True

0 commit comments

Comments
 (0)