1
|
|
|
# pylint: disable=no-self-use |
|
|
|
|
2
|
|
|
|
3
|
|
|
from unittest.mock import Mock, patch |
4
|
|
|
import logging |
5
|
|
|
|
6
|
|
|
import pytest |
7
|
|
|
|
8
|
|
|
from gdm import cli |
9
|
|
|
from gdm.common import _Config |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class TestMain: |
13
|
|
|
|
14
|
|
|
"""Unit tests for the top-level arguments.""" |
15
|
|
|
|
16
|
|
|
def test_main(self): |
17
|
|
|
"""Verify the top-level command can be run.""" |
18
|
|
|
mock_function = Mock(return_value=True) |
19
|
|
|
|
20
|
|
|
cli.main([], mock_function) |
21
|
|
|
|
22
|
|
|
mock_function.assert_called_once_with(root=None) |
23
|
|
|
|
24
|
|
|
def test_main_fail(self): |
25
|
|
|
"""Verify error in commands are detected.""" |
26
|
|
|
with pytest.raises(SystemExit): |
27
|
|
|
cli.main([], Mock(return_value=False)) |
28
|
|
|
|
29
|
|
|
def test_main_help(self): |
30
|
|
|
"""Verify the help text can be displayed.""" |
31
|
|
|
with pytest.raises(SystemExit): |
32
|
|
|
cli.main(['--help']) |
33
|
|
|
|
34
|
|
|
def test_main_none(self): |
35
|
|
|
"""Verify it's an error to specify no command.""" |
36
|
|
|
with pytest.raises(SystemExit): |
37
|
|
|
cli.main([]) |
38
|
|
|
|
39
|
|
|
def test_main_interrupt(self): |
40
|
|
|
"""Verify a command can be interrupted.""" |
41
|
|
|
with pytest.raises(SystemExit): |
42
|
|
|
cli.main([], Mock(side_effect=KeyboardInterrupt)) |
43
|
|
|
|
44
|
|
|
def test_main_error(self): |
45
|
|
|
"""Verify runtime errors are handled.""" |
46
|
|
|
with pytest.raises(SystemExit): |
47
|
|
|
cli.main([], Mock(side_effect=RuntimeError)) |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
class TestInstall: |
51
|
|
|
|
52
|
|
|
"""Unit tests for the `install` command.""" |
53
|
|
|
|
54
|
|
|
@patch('gdm.commands.install') |
55
|
|
|
def test_install(self, mock_install): |
56
|
|
|
"""Verify the 'install' command can be run.""" |
57
|
|
|
cli.main(['install']) |
58
|
|
|
|
59
|
|
|
mock_install.assert_called_once_with( |
60
|
|
|
root=None, depth=None, force=False, fetch=False, clean=False) |
61
|
|
|
|
62
|
|
|
@patch('gdm.commands.install') |
63
|
|
|
def test_install_root(self, mock_install): |
64
|
|
|
"""Verify the project's root can be specified.""" |
65
|
|
|
cli.main(['install', '--root', 'mock/path/to/root']) |
66
|
|
|
|
67
|
|
|
mock_install.assert_called_once_with( |
68
|
|
|
root='mock/path/to/root', depth=None, |
69
|
|
|
force=False, fetch=False, clean=False) |
70
|
|
|
|
71
|
|
|
@patch('gdm.commands.install') |
72
|
|
|
def test_install_force(self, mock_install): |
73
|
|
|
"""Verify dependencies can be force-installed.""" |
74
|
|
|
cli.main(['install', '--force']) |
75
|
|
|
|
76
|
|
|
mock_install.assert_called_once_with( |
77
|
|
|
root=None, depth=None, force=True, fetch=False, clean=False) |
78
|
|
|
|
79
|
|
|
@patch('gdm.commands.install') |
80
|
|
|
def test_install_fetch(self, mock_install): |
81
|
|
|
"""Verify fetching can be enabled.""" |
82
|
|
|
cli.main(['install', '--fetch']) |
83
|
|
|
|
84
|
|
|
mock_install.assert_called_once_with( |
85
|
|
|
root=None, depth=None, force=False, fetch=True, clean=False) |
86
|
|
|
|
87
|
|
|
@patch('gdm.commands.install') |
88
|
|
|
def test_install_clean(self, mock_install): |
89
|
|
|
"""Verify dependency cleaning can be enabled.""" |
90
|
|
|
cli.main(['install', '--clean']) |
91
|
|
|
|
92
|
|
|
mock_install.assert_called_once_with( |
93
|
|
|
root=None, depth=None, force=False, fetch=False, clean=True) |
94
|
|
|
|
95
|
|
|
@patch('gdm.commands.install') |
96
|
|
|
def test_install_specific_sources(self, mock_install): |
97
|
|
|
"""Verify individual dependencies can be installed.""" |
98
|
|
|
cli.main(['install', 'foo', 'bar']) |
99
|
|
|
|
100
|
|
|
mock_install.assert_called_once_with( |
101
|
|
|
'foo', 'bar', root=None, depth=None, |
102
|
|
|
force=False, fetch=False, clean=False) |
103
|
|
|
|
104
|
|
|
@patch('gdm.commands.install') |
105
|
|
|
def test_install_with_depth(self, mock_update): |
106
|
|
|
"""Verify the 'install' command can be limited by depth.""" |
107
|
|
|
cli.main(['install', '--depth', '5']) |
108
|
|
|
|
109
|
|
|
mock_update.assert_called_once_with( |
110
|
|
|
root=None, depth=5, force=False, fetch=False, clean=False) |
111
|
|
|
|
112
|
|
|
@patch('gdm.commands.install', Mock()) |
113
|
|
|
def test_install_with_depth_invalid(self): |
114
|
|
|
"""Verify depths below 1 are rejected.""" |
115
|
|
|
with pytest.raises(SystemExit): |
116
|
|
|
cli.main(['install', '--depth', '0']) |
117
|
|
|
with pytest.raises(SystemExit): |
118
|
|
|
cli.main(['install', '--depth', '-1']) |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
class TestUpdate: |
122
|
|
|
|
123
|
|
|
"""Unit tests for the `update` command.""" |
124
|
|
|
|
125
|
|
|
@patch('gdm.commands.update') |
126
|
|
|
def test_update(self, mock_update): |
127
|
|
|
"""Verify the 'update' command can be run.""" |
128
|
|
|
cli.main(['update']) |
129
|
|
|
|
130
|
|
|
mock_update.assert_called_once_with( |
131
|
|
|
root=None, depth=None, |
132
|
|
|
force=False, clean=False, recurse=False, lock=None) |
133
|
|
|
|
134
|
|
|
@patch('gdm.commands.update') |
135
|
|
|
def test_update_recursive(self, mock_update): |
136
|
|
|
"""Verify the 'update' command can be run recursively.""" |
137
|
|
|
cli.main(['update', '--all']) |
138
|
|
|
|
139
|
|
|
mock_update.assert_called_once_with( |
140
|
|
|
root=None, depth=None, |
141
|
|
|
force=False, clean=False, recurse=True, lock=None) |
142
|
|
|
|
143
|
|
|
@patch('gdm.commands.update') |
144
|
|
|
def test_update_no_lock(self, mock_update): |
145
|
|
|
"""Verify the 'update' command can disable locking.""" |
146
|
|
|
cli.main(['update', '--no-lock']) |
147
|
|
|
|
148
|
|
|
mock_update.assert_called_once_with( |
149
|
|
|
root=None, depth=None, |
150
|
|
|
force=False, clean=False, recurse=False, lock=False) |
151
|
|
|
|
152
|
|
|
@patch('gdm.commands.update') |
153
|
|
|
def test_update_lock(self, mock_update): |
154
|
|
|
"""Verify the 'update' command can enable locking.""" |
155
|
|
|
cli.main(['update', '--lock']) |
156
|
|
|
|
157
|
|
|
mock_update.assert_called_once_with( |
158
|
|
|
root=None, depth=None, |
159
|
|
|
force=False, clean=False, recurse=False, lock=True) |
160
|
|
|
|
161
|
|
|
def test_update_lock_conflict(self): |
162
|
|
|
"""Verify the 'update' command cannot specify both locking options.""" |
163
|
|
|
with pytest.raises(SystemExit): |
164
|
|
|
cli.main(['update', '--lock', '--no-lock']) |
165
|
|
|
|
166
|
|
|
@patch('gdm.commands.update') |
167
|
|
|
def test_update_specific_sources(self, mock_install): |
168
|
|
|
"""Verify individual dependencies can be installed.""" |
169
|
|
|
cli.main(['update', 'foo', 'bar']) |
170
|
|
|
|
171
|
|
|
mock_install.assert_called_once_with( |
172
|
|
|
'foo', 'bar', root=None, depth=None, |
173
|
|
|
force=False, clean=False, recurse=False, lock=None) |
174
|
|
|
|
175
|
|
|
@patch('gdm.commands.update') |
176
|
|
|
def test_update_with_depth(self, mock_update): |
177
|
|
|
"""Verify the 'update' command can be limited by depth.""" |
178
|
|
|
cli.main(['update', '--depth', '5']) |
179
|
|
|
|
180
|
|
|
mock_update.assert_called_once_with( |
181
|
|
|
root=None, depth=5, |
182
|
|
|
force=False, clean=False, recurse=False, lock=None) |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
class TestList: |
186
|
|
|
|
187
|
|
|
"""Unit tests for the `list` command.""" |
188
|
|
|
|
189
|
|
|
@patch('gdm.commands.display') |
190
|
|
|
def test_list(self, mock_display): |
191
|
|
|
"""Verify the 'list' command can be run.""" |
192
|
|
|
cli.main(['list']) |
193
|
|
|
|
194
|
|
|
mock_display.assert_called_once_with( |
195
|
|
|
root=None, depth=None, allow_dirty=True) |
196
|
|
|
|
197
|
|
|
@patch('gdm.commands.display') |
198
|
|
|
def test_list_root(self, mock_display): |
199
|
|
|
"""Verify the project's root can be specified.""" |
200
|
|
|
cli.main(['list', '--root', 'mock/path/to/root']) |
201
|
|
|
|
202
|
|
|
mock_display.assert_called_once_with( |
203
|
|
|
root='mock/path/to/root', depth=None, allow_dirty=True) |
204
|
|
|
|
205
|
|
|
@patch('gdm.commands.display') |
206
|
|
|
def test_list_no_dirty(self, mock_display): |
207
|
|
|
"""Verify the 'list' command can be set to fail when dirty.""" |
208
|
|
|
cli.main(['list', '--no-dirty']) |
209
|
|
|
|
210
|
|
|
mock_display.assert_called_once_with( |
211
|
|
|
root=None, depth=None, allow_dirty=False) |
212
|
|
|
|
213
|
|
|
@patch('gdm.commands.display') |
214
|
|
|
def test_update_with_depth(self, mock_update): |
215
|
|
|
"""Verify the 'list' command can be limited by depth.""" |
216
|
|
|
cli.main(['list', '--depth', '5']) |
217
|
|
|
|
218
|
|
|
mock_update.assert_called_once_with( |
219
|
|
|
root=None, depth=5, allow_dirty=True) |
220
|
|
|
|
221
|
|
|
|
222
|
|
|
def describe_lock(): |
|
|
|
|
223
|
|
|
# pylint: disable=unused-variable |
224
|
|
|
|
225
|
|
|
@patch('gdm.commands.lock') |
226
|
|
|
def with_no_arguments(lock): |
|
|
|
|
227
|
|
|
cli.main(['lock']) |
228
|
|
|
lock.assert_called_once_with(root=None) |
229
|
|
|
|
230
|
|
|
@patch('gdm.commands.lock') |
231
|
|
|
def with_dependencies(lock): |
|
|
|
|
232
|
|
|
cli.main(['lock', 'foo', 'bar']) |
233
|
|
|
lock.assert_called_once_with('foo', 'bar', root=None) |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
class TestUninstall: |
237
|
|
|
|
238
|
|
|
"""Unit tests for the `uninstall` command.""" |
239
|
|
|
|
240
|
|
|
@patch('gdm.commands.delete') |
241
|
|
|
def test_uninstall(self, mock_uninstall): |
242
|
|
|
"""Verify the 'uninstall' command can be run.""" |
243
|
|
|
cli.main(['uninstall']) |
244
|
|
|
|
245
|
|
|
mock_uninstall.assert_called_once_with( |
246
|
|
|
root=None, force=False) |
247
|
|
|
|
248
|
|
|
@patch('gdm.commands.delete') |
249
|
|
|
def test_uninstall_root(self, mock_uninstall): |
250
|
|
|
"""Verify the project's root can be specified.""" |
251
|
|
|
cli.main(['uninstall', '--root', 'mock/path/to/root']) |
252
|
|
|
|
253
|
|
|
mock_uninstall.assert_called_once_with( |
254
|
|
|
root='mock/path/to/root', force=False) |
255
|
|
|
|
256
|
|
|
@patch('gdm.commands.delete') |
257
|
|
|
def test_uninstall_force(self, mock_uninstall): |
258
|
|
|
"""Verify the 'uninstall' command can be forced.""" |
259
|
|
|
cli.main(['uninstall', '--force']) |
260
|
|
|
|
261
|
|
|
mock_uninstall.assert_called_once_with( |
262
|
|
|
root=None, force=True) |
263
|
|
|
|
264
|
|
|
|
265
|
|
|
class TestLogging: |
266
|
|
|
|
267
|
|
|
"""Unit tests for logging.""" |
268
|
|
|
|
269
|
|
|
arg_verbosity = [ |
270
|
|
|
('', 0), |
271
|
|
|
('-v', 1), |
272
|
|
|
('-vv', 2), |
273
|
|
|
('-vvv', 3), |
274
|
|
|
('-vvvv', 4), |
275
|
|
|
('-vvvvv', 4), |
276
|
|
|
('-q', -1), |
277
|
|
|
] |
278
|
|
|
|
279
|
|
|
@staticmethod |
280
|
|
|
def mock_function(*args, **kwargs): |
281
|
|
|
"""Placeholder logic for logging tests.""" |
282
|
|
|
logging.debug(args) |
283
|
|
|
logging.debug(kwargs) |
284
|
|
|
logging.warning("warning") |
285
|
|
|
logging.error("error") |
286
|
|
|
return True |
287
|
|
|
|
288
|
|
|
@pytest.mark.parametrize("arg,verbosity", arg_verbosity) |
289
|
|
|
def test_level(self, arg, verbosity): |
290
|
|
|
"""Verify verbose level can be set.""" |
291
|
|
|
cli.main([arg] if arg else [], self.mock_function) |
292
|
|
|
assert verbosity == _Config.verbosity |
293
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.