summaryrefslogtreecommitdiff
path: root/testing/test_basic_api.py
blob: bb6a614256623d5736e366b96f9e8f2f30e8cb45 (plain)
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
from __future__ import annotations

import os
import sys
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 testing.wd_wrapper import WorkDir


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_text("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.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, "_do_parse", 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-"})
"""
    )
    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"})
"""
    )
    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"})
"""
    )
    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"})
"""
    )
    res = run([sys.executable, "setup.py", "--version"], p)
    assert res.stdout == "12.34"


@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.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:
    dump_version(tmp_path, "1.0", "first.txt")

    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")
    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")
    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

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

    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):
            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):
        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"