forked from pypa/setuptools_scm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_basic_api.py
139 lines (100 loc) · 3.86 KB
/
test_basic_api.py
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
import os
import sys
import py
import pytest
import setuptools_scm
from setuptools_scm import dump_version
from setuptools_scm.utils import data_from_mime, do
@pytest.mark.parametrize("cmd", ["ls", "dir"])
def test_do(cmd, tmpdir):
if not py.path.local.sysfind(cmd):
pytest.skip(cmd + " not found")
do(cmd, str(tmpdir))
def test_data_from_mime(tmpdir):
tmpfile = tmpdir.join("test.archival")
tmpfile.write("name: test\nrevision: 1")
res = data_from_mime(str(tmpfile))
assert res == {"name": "test", "revision": "1"}
def test_version_from_pkginfo(wd, monkeypatch):
wd.write("PKG-INFO", "Version: 0.1")
assert wd.version == "0.1"
# replicate issue 167
assert wd.get_version(version_scheme="1.{0.distance}.0".format) == "0.1"
def assert_root(monkeypatch, expected_root):
"""
Patch version_from_scm to simply assert that root is expected root
"""
def assertion(config):
assert config.absolute_root == expected_root
monkeypatch.setattr(setuptools_scm, "_do_parse", assertion)
def test_root_parameter_creation(monkeypatch):
assert_root(monkeypatch, os.getcwd())
setuptools_scm.get_version()
def test_version_from_scm(wd):
with pytest.warns(DeprecationWarning, match=".*version_from_scm.*"):
setuptools_scm.version_from_scm(str(wd))
def test_root_parameter_pass_by(monkeypatch, tmpdir):
assert_root(monkeypatch, tmpdir)
setuptools_scm.get_version(root=tmpdir.strpath)
def test_parentdir_prefix(tmpdir, monkeypatch):
monkeypatch.delenv("SETUPTOOLS_SCM_DEBUG")
p = tmpdir.ensure("projectname-v12.34", dir=True)
p.join("setup.py").write(
"""from setuptools import setup
setup(use_scm_version={"parentdir_prefix_version": "projectname-"})
"""
)
res = do((sys.executable, "setup.py", "--version"), p)
assert res == "12.34"
def test_fallback(tmpdir, monkeypatch):
monkeypatch.delenv("SETUPTOOLS_SCM_DEBUG")
p = tmpdir.ensure("sub/package", dir=1)
p.join("setup.py").write(
"""from setuptools import setup
setup(use_scm_version={"fallback_version": "12.34"})
"""
)
res = do((sys.executable, "setup.py", "--version"), p)
assert res == "12.34"
@pytest.mark.parametrize(
"version", ["1.0", "1.2.3.dev1+ge871260", "1.2.3.dev15+ge871260.d20180625", "2345"]
)
def test_pretended(version, monkeypatch):
monkeypatch.setenv(setuptools_scm.PRETEND_KEY, version)
assert setuptools_scm.get_version() == version
def test_root_relative_to(monkeypatch, tmp_path):
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(tmpdir):
sp = tmpdir.strpath
dump_version(sp, "1.0", "first.txt")
assert tmpdir.join("first.txt").read() == "1.0"
dump_version(sp, "1.0.dev42", "first.py")
content = tmpdir.join("first.py").read()
lines = content.splitlines()
assert "version = '1.0.dev42'" in lines
assert "version_tuple = (1, 0, 'dev42')" in lines
import ast
ast.parse(content)
def test_parse_plain_fails(recwarn):
def parse(root):
return "tricked you"
with pytest.raises(TypeError):
setuptools_scm.get_version(parse=parse)
# def test_custom_version_cls(monkeypatch):
#
# monkeypatch.setenv(setuptools_scm.PRETEND_KEY, "1.0.1")
#
# class MyVersion:
# def __init__(self, tag_str: str):
# # todo
# pass
#
# # TODO unfortunately with PRETEND_KEY the preformatted flag becomes True which bypasses our class.
# # which other mechanism would be ok to use ?
# assert setuptools_scm.get_version(version_cls=MyVersion) == "1"