1
|
|
|
# pylint: disable=no-self-use,redefined-outer-name,misplaced-comparison-constant |
2
|
|
|
|
3
|
|
|
from unittest.mock import patch, Mock |
4
|
|
|
from copy import copy |
5
|
|
|
|
6
|
|
|
import pytest |
7
|
|
|
|
8
|
|
|
from gitman.models import Source |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
@pytest.fixture |
12
|
|
|
def source(): |
13
|
|
|
return Source('repo', 'name', rev='rev', link='link') |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class TestSource: |
17
|
|
|
|
18
|
|
|
def test_init_defaults(self): |
19
|
|
|
"""Verify a source has a default revision.""" |
20
|
|
|
source = Source('http://mock.git', 'mock_dir') |
21
|
|
|
|
22
|
|
|
assert 'http://mock.git' == source.repo |
23
|
|
|
assert 'mock_dir' == source.name |
24
|
|
|
assert 'master' == source.rev |
25
|
|
|
assert None is source.link |
26
|
|
|
|
27
|
|
|
def test_init_rev(self): |
28
|
|
|
"""Verify the revision can be customized.""" |
29
|
|
|
source = Source('http://mock.git', 'mock_dir', 'v1.0') |
30
|
|
|
|
31
|
|
|
assert 'v1.0' == source.rev |
32
|
|
|
|
33
|
|
|
def test_init_link(self): |
34
|
|
|
"""Verify the link can be set.""" |
35
|
|
|
source = Source('http://mock.git', 'mock_dir', link='mock/link') |
36
|
|
|
|
37
|
|
|
assert 'mock/link' == source.link |
38
|
|
|
|
39
|
|
|
def test_init_error(self): |
40
|
|
|
"""Verify the repository and directory are required.""" |
41
|
|
|
with pytest.raises(ValueError): |
42
|
|
|
Source('', 'mock_dir') |
43
|
|
|
with pytest.raises(ValueError): |
44
|
|
|
Source('http://mock.git', '') |
45
|
|
|
|
46
|
|
|
def test_repr(self, source): |
47
|
|
|
"""Verify sources can be represented.""" |
48
|
|
|
assert "<source 'repo' @ 'rev' in 'name' <- 'link'>" == repr(source) |
49
|
|
|
|
50
|
|
|
def test_repr_no_link(self, source): |
51
|
|
|
"""Verify sources can be represented.""" |
52
|
|
|
source.link = None |
53
|
|
|
|
54
|
|
|
assert "<source 'repo' @ 'rev' in 'name'>" == repr(source) |
55
|
|
|
|
56
|
|
|
def test_eq(self, source): |
57
|
|
|
source2 = copy(source) |
58
|
|
|
assert source == source2 |
59
|
|
|
|
60
|
|
|
source2.name = "dir2" |
61
|
|
|
assert source != source2 |
62
|
|
|
|
63
|
|
|
def test_lt(self): |
64
|
|
|
sources = [ |
65
|
|
|
Source('zzz', '123'), |
66
|
|
|
Source('bbb', '456'), |
67
|
|
|
Source('ccc', '456'), |
68
|
|
|
Source('BBB', 'AAA'), |
69
|
|
|
Source('AAA', 'AAA'), |
70
|
|
|
] |
71
|
|
|
|
72
|
|
|
assert sources == sorted(sources) |
73
|
|
|
|
74
|
|
|
def test_identify_missing(self, source, tmpdir): |
75
|
|
|
"""Verify a missing source identifies as unknown.""" |
76
|
|
|
tmpdir.chdir() |
77
|
|
|
with patch('os.path.isdir', Mock(return_value=False)): |
78
|
|
|
assert (str(tmpdir), '<missing>', '<unknown>') == source.identify() |
79
|
|
|
|
80
|
|
|
def test_lock_uses_the_identity_rev(self, source): |
81
|
|
|
source.identify = Mock(return_value=('path2', 'dir2', 'abc123')) |
82
|
|
|
|
83
|
|
|
source2 = source.lock() |
84
|
|
|
|
85
|
|
|
assert 'abc123' == source2.rev |
86
|
|
|
assert 'name' == source2.name |
87
|
|
|
|