|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
from mock import patch |
|
4
|
|
|
import pytest |
|
5
|
|
|
|
|
6
|
|
|
from git_app_version.git import Git |
|
7
|
|
|
from datetime import datetime |
|
8
|
|
|
import pytz |
|
9
|
|
|
|
|
10
|
|
|
@patch('git_app_version.helper.process.subprocess') |
|
11
|
|
|
@pytest.mark.parametrize("cmd_result,expected", [ |
|
12
|
|
|
(0, True), |
|
13
|
|
|
(1, False), |
|
14
|
|
|
]) |
|
15
|
|
|
def test_is_git_repo(mock_sub_process, cmd_result, expected): |
|
16
|
|
|
git = Git() |
|
17
|
|
|
mock_sub_process.call.return_value = cmd_result |
|
18
|
|
|
|
|
19
|
|
|
assert git.is_git_repo(None) == expected |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
@patch('git_app_version.helper.date.datetime') |
|
23
|
|
|
@pytest.mark.parametrize("mock_dt_now,expected", [ |
|
24
|
|
|
(datetime(2015, 12, 21, 11, 33, 45), datetime(2015, 12, 21, 11, 33, 45)), |
|
25
|
|
|
]) |
|
26
|
|
|
def test_get_deploy_date(mock_dt, mock_dt_now, expected): |
|
27
|
|
|
git = Git() |
|
28
|
|
|
tz = pytz.utc |
|
29
|
|
|
|
|
30
|
|
|
expectedDate = tz.localize(expected) |
|
31
|
|
|
mock_dt.now.return_value = tz.localize(mock_dt_now) |
|
32
|
|
|
|
|
33
|
|
|
assert expectedDate == git.get_deploy_date() |
|
34
|
|
|
|
|
35
|
|
|
@patch('git_app_version.helper.process.subprocess') |
|
36
|
|
|
@pytest.mark.parametrize("cmd_result,expected", [ |
|
37
|
|
|
('40aaf83', '40aaf83'), |
|
38
|
|
|
('', ''), |
|
39
|
|
|
]) |
|
40
|
|
|
def test_get_abbrev_commit(mock_sub_process, cmd_result, expected): |
|
41
|
|
|
git = Git() |
|
42
|
|
|
mock_sub_process.check_output.return_value = cmd_result |
|
43
|
|
|
|
|
44
|
|
|
assert git.get_abbrev_commit() == expected |
|
45
|
|
|
|
|
46
|
|
|
@patch('git_app_version.helper.process.subprocess') |
|
47
|
|
|
@pytest.mark.parametrize("cmd_result,expected", [ |
|
48
|
|
|
('40aaf83894b98898895d478f8b7cc4a866b1d62c', '40aaf83894b98898895d478f8b7cc4a866b1d62c'), |
|
49
|
|
|
('', ''), |
|
50
|
|
|
]) |
|
51
|
|
|
def test_get_full_commit(mock_sub_process, cmd_result, expected): |
|
52
|
|
|
git = Git() |
|
53
|
|
|
mock_sub_process.check_output.return_value = cmd_result |
|
54
|
|
|
|
|
55
|
|
|
assert git.get_full_commit() == expected |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
@patch('git_app_version.helper.process.subprocess') |
|
59
|
|
|
@pytest.mark.parametrize("cmd_result,expected", [ |
|
60
|
|
|
('Paul Dupond', 'Paul Dupond'), |
|
61
|
|
|
('', ''), |
|
62
|
|
|
]) |
|
63
|
|
|
def test_get_committer_name(mock_sub_process, cmd_result, expected): |
|
64
|
|
|
git = Git() |
|
65
|
|
|
mock_sub_process.check_output.return_value = cmd_result |
|
66
|
|
|
assert git.get_committer_name() == expected |
|
67
|
|
|
|
|
68
|
|
|
@patch('git_app_version.helper.process.subprocess') |
|
69
|
|
|
@pytest.mark.parametrize("cmd_result,expected", [ |
|
70
|
|
|
('[email protected]', '[email protected]'), |
|
71
|
|
|
('', ''), |
|
72
|
|
|
]) |
|
73
|
|
|
def test_get_committer_email(mock_sub_process, cmd_result, expected): |
|
74
|
|
|
git = Git() |
|
75
|
|
|
mock_sub_process.check_output.return_value = cmd_result |
|
76
|
|
|
assert git.get_committer_email() == expected |
|
77
|
|
|
|
|
78
|
|
|
@patch('git_app_version.helper.process.subprocess') |
|
79
|
|
|
@pytest.mark.parametrize("cmd_result,expected", [ |
|
80
|
|
|
('Paul Dupond', 'Paul Dupond'), |
|
81
|
|
|
('', ''), |
|
82
|
|
|
]) |
|
83
|
|
|
def test_get_author_name(mock_sub_process, cmd_result, expected): |
|
84
|
|
|
git = Git() |
|
85
|
|
|
mock_sub_process.check_output.return_value = cmd_result |
|
86
|
|
|
assert git.get_author_name() == expected |
|
87
|
|
|
|
|
88
|
|
|
@patch('git_app_version.helper.process.subprocess') |
|
89
|
|
|
@pytest.mark.parametrize("cmd_result,expected", [ |
|
90
|
|
|
('[email protected]', '[email protected]'), |
|
91
|
|
|
('', ''), |
|
92
|
|
|
]) |
|
93
|
|
|
def test_get_author_email(mock_sub_process, cmd_result, expected): |
|
94
|
|
|
git = Git() |
|
95
|
|
|
mock_sub_process.check_output.return_value = cmd_result |
|
96
|
|
|
assert git.get_author_email() == expected |
|
97
|
|
|
|
|
98
|
|
|
@patch('git_app_version.helper.process.subprocess') |
|
99
|
|
|
@pytest.mark.parametrize("cmd_result,expected,expectedTZ", [ |
|
100
|
|
|
('2016-01-01 00:33:33 +0100', datetime(2016, 1, 1, 0, 33, 33), 'Europe/Paris'), |
|
101
|
|
|
('', None, None), |
|
102
|
|
|
]) |
|
103
|
|
|
def test_get_commit_date(mock_sub_process, cmd_result, expected, expectedTZ): |
|
104
|
|
|
git = Git() |
|
105
|
|
|
mock_sub_process.check_output.return_value = cmd_result |
|
106
|
|
|
|
|
107
|
|
|
if isinstance(expected, datetime): |
|
108
|
|
|
tz = pytz.timezone(expectedTZ) |
|
109
|
|
|
expected = tz.localize(expected) |
|
110
|
|
|
|
|
111
|
|
|
assert git.get_commit_date() == expected |
|
112
|
|
|
|
|
113
|
|
|
@patch('git_app_version.helper.process.subprocess') |
|
114
|
|
|
@pytest.mark.parametrize("cmd_result,expected,expectedTZ", [ |
|
115
|
|
|
('2016-01-01 00:33:33 +0100', datetime(2016, 1, 1, 0, 33, 33), 'Europe/Paris'), |
|
116
|
|
|
('', None, None), |
|
117
|
|
|
]) |
|
118
|
|
|
def test_get_author_date(mock_sub_process, cmd_result, expected, expectedTZ): |
|
119
|
|
|
git = Git() |
|
120
|
|
|
mock_sub_process.check_output.return_value = cmd_result |
|
121
|
|
|
|
|
122
|
|
|
if isinstance(expected, datetime): |
|
123
|
|
|
tz = pytz.timezone(expectedTZ) |
|
124
|
|
|
expected = tz.localize(expected) |
|
125
|
|
|
|
|
126
|
|
|
assert git.get_author_date() == expected |
|
127
|
|
|
|
|
128
|
|
|
@patch('git_app_version.helper.process.subprocess') |
|
129
|
|
|
@pytest.mark.parametrize("cmd_results,default,expected", [ |
|
130
|
|
|
(('v1.1.0-3-g439e52', '40aaf83'), None, 'v1.1.0-3-g439e52'), |
|
131
|
|
|
(('','40aaf83'), None, '40aaf83'), |
|
132
|
|
|
(('','40aaf83'), '8fa82b6', '8fa82b6'), |
|
133
|
|
|
]) |
|
134
|
|
|
def test_get_version(mock_sub_process, cmd_results, default, expected): |
|
135
|
|
|
git = Git() |
|
136
|
|
|
mock_sub_process.check_output.side_effect = cmd_results |
|
137
|
|
|
|
|
138
|
|
|
assert git.get_version(default=default) == expected |
|
139
|
|
|
|
|
140
|
|
|
@patch('git_app_version.helper.process.subprocess') |
|
141
|
|
|
@pytest.mark.parametrize("cmd_result,commit,expected", [ |
|
142
|
|
|
('', 'HEAD', []), |
|
143
|
|
|
(" origin/HEAD -> origin/master\n origin/master\n origin/feature/my_feature\n", 'HEAD', ['origin/master', 'origin/feature/my_feature']) |
|
144
|
|
|
]) |
|
145
|
|
|
def test_get_branches(mock_sub_process, cmd_result, commit, expected): |
|
146
|
|
|
git = Git() |
|
147
|
|
|
mock_sub_process.check_output.return_value = cmd_result |
|
148
|
|
|
|
|
149
|
|
|
assert git.get_branches(commit=commit) == expected |
|
150
|
|
|
|
|
151
|
|
|
@patch('git_app_version.helper.process.subprocess') |
|
152
|
|
|
@pytest.mark.parametrize("cmd_results,branches,abbrevCommit,expected", [ |
|
153
|
|
|
(('40aaf83', 'a7b5290'), ['origin/master', 'origin/feature/my_feature'], '40aaf83', ['origin/master']), |
|
154
|
|
|
]) |
|
155
|
|
|
def test_get_top_branches(mock_sub_process, cmd_results, branches, abbrevCommit, expected): |
|
156
|
|
|
git = Git() |
|
157
|
|
|
mock_sub_process.check_output.side_effect = cmd_results |
|
158
|
|
|
|
|
159
|
|
|
assert git.get_top_branches(branches=branches, abbrev_commit=abbrevCommit) == expected |
|
160
|
|
|
|
|
161
|
|
|
@pytest.mark.parametrize("branches,expected", [ |
|
162
|
|
|
(['origin/master', 'origin/feature/my_feature'], ['master', 'feature/my_feature']), |
|
163
|
|
|
]) |
|
164
|
|
|
def test_remove_remote_prefix(branches, expected): |
|
165
|
|
|
git = Git() |
|
166
|
|
|
|
|
167
|
|
|
assert git.remove_remote_prefix(branches=branches) == expected |
|
168
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
@patch('git_app_version.helper.date.datetime') |
|
171
|
|
|
@patch('git_app_version.helper.process.subprocess') |
|
172
|
|
|
@pytest.mark.parametrize("cmd_results,now,expected", [ |
|
173
|
|
|
( |
|
174
|
|
|
( |
|
175
|
|
|
'40aaf83', |
|
176
|
|
|
'2016-03-01 10:33:33 +0100', |
|
177
|
|
|
'2016-03-02 14:10:48 +0100', |
|
178
|
|
|
" origin/HEAD -> origin/master\n origin/master\n origin/feature/my_feature\n", |
|
179
|
|
|
'40aaf83', |
|
180
|
|
|
'83bc5f1', |
|
181
|
|
|
'v1.1.0-3-g439e52', |
|
182
|
|
|
'40aaf83894b98898895d478f8b7cc4a866b1d62c', |
|
183
|
|
|
'Martin Durand', |
|
184
|
|
|
'[email protected]', |
|
185
|
|
|
'Paul Dupont', |
|
186
|
|
|
'[email protected]' |
|
187
|
|
|
), |
|
188
|
|
|
datetime(2016, 3, 2, 11, 33, 45), |
|
189
|
|
|
{ |
|
190
|
|
|
'version': 'v1.1.0-3-g439e52', |
|
191
|
|
|
'abbrev_commit': '40aaf83', |
|
192
|
|
|
'full_commit': '40aaf83894b98898895d478f8b7cc4a866b1d62c', |
|
193
|
|
|
'author_name': 'Martin Durand', |
|
194
|
|
|
'author_email': '[email protected]', |
|
195
|
|
|
'committer_name': 'Paul Dupont', |
|
196
|
|
|
'committer_email': '[email protected]', |
|
197
|
|
|
'branches': ['master', 'feature/my_feature'], |
|
198
|
|
|
'top_branches': ['master'], |
|
199
|
|
|
'commit_date': '2016-03-01T09:33:33+0000', |
|
200
|
|
|
'commit_timestamp': '1456824813', |
|
201
|
|
|
'author_date': '2016-03-02T13:10:48+0000', |
|
202
|
|
|
'author_timestamp': '1456924248', |
|
203
|
|
|
'deploy_date': '2016-03-02T11:33:45+0000', |
|
204
|
|
|
'deploy_timestamp': '1456918425' |
|
205
|
|
|
} |
|
206
|
|
|
) |
|
207
|
|
|
]) |
|
208
|
|
|
def test_get_infos(mock_sub_process, mock_dt, now, cmd_results, expected): |
|
209
|
|
|
git = Git() |
|
210
|
|
|
|
|
211
|
|
|
tz = pytz.utc |
|
212
|
|
|
mock_dt.now.return_value = tz.localize(now) |
|
213
|
|
|
mock_dt.side_effect = lambda *args, **kw: datetime(*args, **kw) |
|
214
|
|
|
|
|
215
|
|
|
mock_sub_process.check_output.side_effect = cmd_results |
|
216
|
|
|
|
|
217
|
|
|
assert git.get_infos() == expected |
|
218
|
|
|
|