-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_cli.py
367 lines (320 loc) · 10.1 KB
/
test_cli.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# pylint: disable=unused-variable, redefined-outer-name
from typing import Callable, Optional, Tuple
from unittest import mock
import pytest
import typer
from packaging import version
from pytest_test_utils import TmpDir
from typer.main import get_command_from_info
from gto.cli import app
from gto.exceptions import GTOException
from tests.conftest import Runner
def _check_output_contains(output: str, search_value: str) -> bool:
return search_value in output
def _check_output_exact_match(output: str, search_value: str) -> bool:
return search_value == output
def _check_successful_cmd(
cmd: str,
args: list,
expected_stdout: Optional[str],
search_func: Optional[Callable] = _check_output_exact_match,
):
runner = Runner()
result = runner.invoke([cmd] + args)
assert result.exit_code == 0, (result.stdout, result.stderr, result.exception)
if expected_stdout is not None:
if len(expected_stdout):
assert len(result.stdout) > 0, "Output is empty, but should not be"
assert search_func(result.stdout, expected_stdout)
def _check_failing_cmd(
cmd: str,
args: list,
expected_stderr: str,
search_func: Optional[Callable] = _check_output_exact_match,
):
runner = Runner()
result = runner.invoke([cmd] + args)
assert result.exit_code != 0, (result.stdout, result.stderr, result.exception)
if expected_stderr is not None:
assert len(result.stderr) > 0, "Output is empty, but should not be"
assert search_func(result.stderr, expected_stderr)
@pytest.fixture
def app_cmd():
return app.registered_commands
@pytest.fixture
def app_cli_cmd(app_cmd):
if version.parse(typer.__version__) < version.parse("0.6.0"):
return (
get_command_from_info(c) # pylint: disable=missing-kwoa
for c in app_cmd
)
return (
get_command_from_info( # pylint: disable=unexpected-keyword-arg
c,
pretty_exceptions_short=False,
rich_markup_mode="rich",
)
for c in app_cmd
)
def test_commands_help(app_cli_cmd):
no_help = [cli_cmd.name for cli_cmd in app_cli_cmd if cli_cmd.help is None]
assert not no_help, f"{no_help} cli command do not have help!"
def test_commands_args_help(app_cli_cmd):
no_help = []
for cmd in app_cli_cmd:
no_help.extend(
f"{cmd.name}:{arg.name}"
for arg in cmd.params
if arg.help is None or arg.help == ""
)
assert not no_help, f"{no_help} cli command args do not have help!"
def test_show(empty_git_repo: str):
_check_successful_cmd(
"show",
["-r", empty_git_repo],
"Nothing found in the current workspace\n",
)
_check_successful_cmd(
"history",
["-r", empty_git_repo],
"Nothing found in the current workspace\n",
)
EXPECTED_DESCRIBE_OUTPUT = """{
"type": "model",
"path": "models/random-forest.pkl",
"virtual": false
}
"""
# this is one function because showcase fixture takes some time to be created
def test_commands(tmp_dir: TmpDir, showcase: Tuple[str, str]):
first_commit, second_commit = showcase
_check_successful_cmd(
"show",
["-r", tmp_dir, "rf@greatest", "--version"],
"v1.2.4\n",
)
_check_successful_cmd(
"show",
["-r", tmp_dir, "rf@latest", "--ref"],
"[email protected]\n",
)
_check_successful_cmd(
"show",
["-r", tmp_dir, "rf#production", "--version"],
"v1.2.3\n",
)
_check_successful_cmd(
"show",
["-r", tmp_dir, "rf#production", "--vs", "-1", "--version"],
"v1.2.4\nv1.2.3\n",
)
_check_successful_cmd(
"show",
["-r", tmp_dir, "rf#staging", "--vs", "-1", "--version"],
"v1.2.4\n",
)
_check_successful_cmd(
"show",
["-r", tmp_dir, "rf#production", "--ref"],
"[email protected]\n",
)
# None because of random order - fix this
_check_successful_cmd("stages", ["-r", tmp_dir], None)
# None because of output randomness and complexity
_check_successful_cmd(
"show",
["-r", tmp_dir],
None,
)
# None because of output randomness and complexity
_check_successful_cmd(
"history",
["-r", tmp_dir],
None,
)
# check-ref
_check_successful_cmd(
"check-ref",
["-r", tmp_dir, "rf#production#3", "--name"],
"rf\n",
)
_check_successful_cmd(
"check-ref",
["-r", tmp_dir, "refs/tags/rf#production#3", "--name"],
"rf\n",
)
_check_successful_cmd(
"check-ref",
["-r", tmp_dir, "rf#production#3", "--stage"],
"production\n",
)
_check_successful_cmd(
"check-ref",
["-r", tmp_dir, "rf#production#3", "--version"],
"v1.2.4\n",
)
_check_successful_cmd(
"check-ref",
["-r", tmp_dir, "[email protected]", "--version"],
"v1.2.4\n",
)
_check_successful_cmd(
"check-ref",
["-r", tmp_dir, "[email protected]", "--event"],
"registration\n",
)
_check_successful_cmd(
"check-ref",
["-r", tmp_dir, "[email protected]"],
'✅ Version "v1.2.4" of artifact "rf" was registered\n',
)
_check_successful_cmd(
"check-ref",
["-r", tmp_dir, "rf#production#3", "--event"],
"assignment\n",
)
_check_successful_cmd(
"check-ref",
["-r", tmp_dir, "rf#production#3"],
'✅ Stage "production" was assigned to version "v1.2.4" of artifact "rf"\n',
)
# TODO: make unsuccessful
_check_successful_cmd(
"check-ref",
["-r", tmp_dir, "this-tag-does-not-exist"],
"",
)
_check_successful_cmd(
"parse-tag",
["dvclive/[email protected]"],
"dvclive/dsd:nn",
_check_output_contains,
)
_check_successful_cmd(
"doctor",
["-r", tmp_dir],
None,
)
EXPECTED_DESCRIBE_OUTPUT_2 = """{
"type": "new-type",
"path": "new/path",
"labels": [
"another-label",
"new-label",
"some-label"
],
"description": "new description"
}
"""
def test_register(repo_with_commit: str):
_check_successful_cmd(
"register",
["-r", repo_with_commit, "a1"],
"Created git tag '[email protected]' that registers version\n"
"To push the changes upstream, run:\n"
" git push origin [email protected]\n",
)
_check_successful_cmd(
"deprecate",
["-r", repo_with_commit, "a1", "v0.0.1", "--delete"],
"Deleted git tag '[email protected]'\n"
"To push the changes upstream, run:\n"
" git push --delete origin [email protected]\n",
)
_check_successful_cmd(
"register",
["-r", repo_with_commit, "a2", "--version", "v1.2.3"],
"Created git tag '[email protected]' that registers version\n"
"To push the changes upstream, run:\n"
" git push origin [email protected]\n",
)
_check_successful_cmd(
"deprecate",
["-r", repo_with_commit, "a2", "v1.2.3"],
"Created git tag '[email protected]!' that deregisters version\n"
"To push the changes upstream, run:\n"
" git push origin [email protected]!\n",
)
_check_successful_cmd(
"register",
["-r", repo_with_commit, "a2", "--simple", "false"],
"Created git tag '[email protected]#1' that registers version\n"
"To push the changes upstream, run:\n"
" git push origin [email protected]#1\n",
)
_check_failing_cmd(
"register",
["-r", repo_with_commit, "a3", "--version", "1.2.3"],
"❌ Supplied version '1.2.3' cannot be parsed\n",
)
_check_successful_cmd(
"register",
["-r", repo_with_commit, "classification/dvclive:models/nn"],
"classification/dvclive=models/[email protected]",
search_func=_check_output_contains,
)
def test_assign(repo_with_commit: str):
_check_successful_cmd(
"register",
["-r", repo_with_commit, "nn1"],
"Created git tag '[email protected]' that registers version\n"
"To push the changes upstream, run:\n"
" git push origin [email protected]\n",
)
# this check depends on the previous one
_check_successful_cmd(
"assign",
["-r", repo_with_commit, "nn1", "HEAD", "--stage", "prod"],
"Created git tag 'nn1#prod#1' that assigns stage to version 'v0.0.1'\n"
"To push the changes upstream, run:\n"
" git push origin nn1#prod#1\n",
)
# this check depends on the previous assignment
_check_failing_cmd(
"assign",
[
"-r",
repo_with_commit,
"nn1",
"HEAD",
"--version",
"v1.0.0",
"--stage",
"stage",
],
"❌ Can't register 'v1.0.0', since 'v0.0.1' is registered already at this ref\n",
)
_check_failing_cmd(
"assign",
[
"-r",
repo_with_commit,
"nn2",
"HEAD",
"--version",
"1.0.0",
"--stage",
"prod",
],
"❌ Version '1.0.0' is not valid. Example of valid version: 'v1.0.0'\n",
)
_check_successful_cmd(
"assign",
["-r", repo_with_commit, "nn2", "HEAD", "--stage", "prod"],
"Created git tag '[email protected]' that registers version\n"
"To push the changes upstream, run:\n"
" git push origin [email protected]\n"
"Created git tag 'nn2#prod#1' that assigns stage to version 'v0.0.1'\n"
"To push the changes upstream, run:\n"
" git push origin nn2#prod#1\n",
)
GTO_EXCEPTION_MESSAGE = "Test GTOException Message"
def test_stderr_gto_exception():
# patch gto show to throw gto exception.
with mock.patch("gto.api.show", side_effect=GTOException(GTO_EXCEPTION_MESSAGE)):
_check_failing_cmd("show", [], GTO_EXCEPTION_MESSAGE, _check_output_contains)
EXCEPTION_MESSAGE = "Test Exception Message"
def test_stderr_exception():
# patch gto show to throw exception.
with mock.patch("gto.api.show", side_effect=Exception(EXCEPTION_MESSAGE)):
_check_failing_cmd("show", [], EXCEPTION_MESSAGE, _check_output_contains)