summaryrefslogtreecommitdiff
path: root/test_hgdistver.py
blob: e3433b286a09b16dcd83fe0902a1ed9f2d59dd15 (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
from hgdistver import _data_from_archival, \
    _archival_to_version
import os
import hgdistver
from hgdistver import hg
import py
from subprocess import call, Popen, PIPE

def get_version(path, method='get_version', **kw):
    call = getattr(hgdistver, method)
    root = str(path)
    return call(root=root, **kw)



def test_data_from_archival(tmpdir):
    tmpfile = tmpdir.join('test.archival')
    tmpfile.write('name: test\nrevision: 1')

    res = _data_from_archival(tmpfile)
    assert res == {
        'name': 'test',
        'revision': '1',
    }


archival_mapping = {
    '1.0': {'tag': '1.0'},
    '1.0.dev3-000000000000': {
        'latesttag': '1.0',
        'latesttagdistance': '3',
        'node': '0'*20,
    },
    '0'*12: {
        'node': '0'*20,
    },

}

def test_archival_to_version():
    for expected, data in archival_mapping.items():
        assert _archival_to_version(data) == expected


def pytest_generate_tests(metafunc):
    if hasattr(metafunc.function, 'methods'):
        for method in metafunc.function.methods.args:
            metafunc.addcall(
                id=method,
                param=method,
            )

def pytest_funcarg__get_log_version(request):
    def get_log_version(path):
        return get_version(path, method=request.param)
    return get_log_version

@py.test.mark.methods('version_from_hg15_parents', 'version_from_hg_log_with_tags')
def test_version_from_hg_id(tmpdir, get_log_version):
    cwd = str(tmpdir)
    hg('init', cwd)
    initial = get_log_version(cwd)
    assert initial.startswith('0.0.dev0-' + '0'*12 ) #uses node when no tag
    tmpdir.join('test.txt').write('test')
    hg('add test.txt', cwd)
    hg('commit -m commit -u test -d "0 0"', cwd)

    after_first_commit = get_log_version(tmpdir)

    assert after_first_commit.startswith('0.0.dev1-')

    hg('tag 0.1 -u test -d "0 0"', cwd)
    after_tag_01 = get_log_version(cwd)
    assert after_tag_01.startswith('0.1.dev1-')

    hg('up 0.1', cwd)
    at_tag_01 = get_version(cwd)
    assert at_tag_01 == '0.1'

def test_version_from_archival(tmpdir):
    tmpdir.join('.hg_archival.txt').write(
        'node: 000000000000\n'
        'tag: 0.1\n'
    )
    subdir = tmpdir.join('test').ensure(dir=True)
    assert get_version(tmpdir) == '0.1'
    assert get_version(subdir) == '0.1'

    tmpdir.join('.hg_archival.txt').write(
        'node: 000000000000\n'
        'latesttag: 0.1\n'
        'latesttagdistance: 3\n'
    )

    assert get_version(tmpdir) == '0.1.dev3-000000000000'


def test_version_from_cachefile(tmpdir):
    tmpdir.join('test.txt').write(
        '# comment\n'
        'version = "1.0"'
    )

    assert get_version(tmpdir, cachefile='test.txt') == '1.0'


def test_version_from_pkginfo(tmpdir):
    tmpdir.join('PKG-INFO').write('Version: 0.1')
    assert get_version(tmpdir, method='version_from_sdist_pkginfo') == '0.1'


def test_root_parameter_creation(monkeypatch):
    def assert_cwd(root, cachefile=None):
        assert root == os.getcwd()
    monkeypatch.setattr(hgdistver, 'methods', [assert_cwd])
    hgdistver.get_version()

def test_root_parameter_pass_by(monkeypatch):
    def assert_root_tmp(root, cachefile):
        assert root == '/tmp'
    monkeypatch.setattr(hgdistver, 'methods', [assert_root_tmp])
    hgdistver.get_version(root='/tmp')

def test_cachefiile_join(monkeypatch):
    def assert_join(root, cachefile):
        assert cachefile == os.path.join('tmp', 'cachefile')
    monkeypatch.setattr(hgdistver, 'methods', [assert_join])
    hgdistver.get_version(root='tmp', cachefile='cachefile')