summaryrefslogtreecommitdiff
path: root/testing/test_functions.py
blob: cfb9eb98bd0eb932f38628742da2ccfd3e49e964 (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
import pytest
import pkg_resources
from setuptools_scm import dump_version, get_version, PRETEND_KEY
from setuptools_scm.version import guess_next_version, meta, format_version


class MockTime(object):
    def __format__(self, *k):
        return 'time'


@pytest.mark.parametrize('tag, expected', [
    ('1.1', '1.2.dev0'),
    ('1.2.dev', '1.2.dev0'),
    ('1.1a2', '1.1a3.dev0'),
    ])
def test_next_tag(tag, expected):
    version = pkg_resources.parse_version(tag)
    assert guess_next_version(version, 0) == expected


VERSIONS = {
    'exact': meta('1.1', None, False),
    'zerodistance': meta('1.1', 0, False),
    'dirty': meta('1.1', None, True),
    'distance': meta('1.1', 3, False),
    'distancedirty': meta('1.1', 3, True),
}


@pytest.mark.parametrize('version,scheme,expected', [
    ('exact', 'guess-next-dev node-and-date', '1.1'),
    ('zerodistance', 'guess-next-dev node-and-date', '1.2.dev0+nNone'),
    ('dirty', 'guess-next-dev node-and-date', '1.2.dev0+nNone.dtime'),
    ('distance', 'guess-next-dev node-and-date', '1.2.dev3+nNone'),
    ('distancedirty', 'guess-next-dev node-and-date', '1.2.dev3+nNone.dtime'),
    ('exact', 'post-release node-and-date', '1.1'),
    ('zerodistance', 'post-release node-and-date', '1.1.post0+nNone'),
    ('dirty', 'post-release node-and-date', '1.1.post0+nNone.dtime'),
    ('distance', 'post-release node-and-date', '1.1.post3+nNone'),
    ('distancedirty', 'post-release node-and-date', '1.1.post3+nNone.dtime'),
])
def test_format_version(version, monkeypatch, scheme, expected):
    version = VERSIONS[version]
    monkeypatch.setattr(version, 'time', MockTime())
    vs, ls = scheme.split()
    assert format_version(
        version,
        version_scheme=vs,
        local_scheme=ls) == expected


def test_dump_version_doesnt_bail_on_value_error(tmpdir):
    write_to = "VERSION"
    version = VERSIONS['exact']
    with pytest.raises(ValueError) as exc_info:
        dump_version(tmpdir.strpath, version, write_to)
    assert str(exc_info.value).startswith("bad file format:")


def test_dump_version_works_with_pretend(tmpdir, monkeypatch):
    monkeypatch.setenv(PRETEND_KEY, '1.0')
    get_version(write_to=str(tmpdir.join('VERSION.txt')))
    assert tmpdir.join('VERSION.txt').read() == '1.0'