forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_exceptions.py
479 lines (401 loc) · 19.8 KB
/
test_exceptions.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
import json
from unittest import TestCase
import sys
from conda import text_type
from conda._vendor.auxlib.collection import AttrDict
from conda._vendor.auxlib.ish import dals
from conda.base.context import context, reset_context
from conda.common.compat import on_win
from conda.common.io import captured, env_var
from conda.exceptions import BasicClobberError, BinaryPrefixReplacementError, CommandNotFoundError, \
CondaHTTPError, CondaKeyError, CondaRevisionError, DirectoryNotFoundError, \
KnownPackageClobberError, MD5MismatchError, PackagesNotFoundError, PathNotFoundError, \
SharedLinkPathClobberError, TooFewArgumentsError, TooManyArgumentsError, \
UnknownPackageClobberError, conda_exception_handler, ExceptionHandler
try:
from unittest.mock import Mock, patch
except ImportError:
from mock import Mock, patch
def _raise_helper(exception):
raise exception
class ExceptionTests(TestCase):
def test_TooManyArgumentsError(self):
expected = 2
received = 5
offending_arguments = "groot"
exc = TooManyArgumentsError(expected, received, offending_arguments)
with env_var("CONDA_JSON", "yes", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
json_obj = json.loads(c.stdout)
assert not c.stderr
assert json_obj['exception_type'] == "<class 'conda.exceptions.TooManyArgumentsError'>"
assert json_obj['exception_name'] == 'TooManyArgumentsError'
assert json_obj['message'] == text_type(exc)
assert json_obj['error'] == repr(exc)
assert json_obj['expected'] == 2
assert json_obj['received'] == 5
assert json_obj['offending_arguments'] == "groot"
with env_var("CONDA_JSON", "no", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
assert not c.stdout
assert c.stderr.strip() == "TooManyArgumentsError: Got 5 arguments (g, r, o, o, t) but expected 2."
def test_TooFewArgumentsError(self):
expected = 5
received = 2
exc = TooFewArgumentsError(expected, received)
with env_var("CONDA_JSON", "yes", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
json_obj = json.loads(c.stdout)
assert not c.stderr
assert json_obj['exception_type'] == "<class 'conda.exceptions.TooFewArgumentsError'>"
assert json_obj['exception_name'] == 'TooFewArgumentsError'
assert json_obj['message'] == text_type(exc)
assert json_obj['error'] == repr(exc)
assert json_obj['expected'] == 5
assert json_obj['received'] == 2
with env_var("CONDA_JSON", "no", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
assert not c.stdout
assert c.stderr.strip() == "TooFewArgumentsError: Got 2 arguments but expected 5."
def test_BasicClobberError(self):
source_path = "some/path/on/goodwin.ave"
target_path = "some/path/to/wright.st"
exc = BasicClobberError(source_path, target_path, context)
with env_var("CONDA_PATH_CONFLICT", "prevent", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
assert not c.stdout
assert c.stderr.strip() == dals("""
ClobberError: Conda was asked to clobber an existing path.
source path: some/path/on/goodwin.ave
target path: some/path/to/wright.st
""").strip()
def test_KnownPackageClobberError(self):
target_path = "some/where/on/goodwin.ave"
colliding_dist_being_linked = "Groot"
colliding_linked_dist = "Liquid"
exc = KnownPackageClobberError(target_path, colliding_dist_being_linked, colliding_linked_dist, context)
with env_var("CONDA_PATH_CONFLICT", "prevent", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
assert not c.stdout
assert c.stderr.strip() == dals("""
ClobberError: The package 'Groot' cannot be installed due to a
path collision for 'some/where/on/goodwin.ave'.
This path already exists in the target prefix, and it won't be removed by
an uninstall action in this transaction. The path appears to be coming from
the package 'Liquid', which is already installed in the prefix.
""").strip()
def test_UnknownPackageClobberError(self):
target_path = "siebel/center/for/c.s"
colliding_dist_being_linked = "Groot"
exc = UnknownPackageClobberError(target_path, colliding_dist_being_linked, context)
with env_var("CONDA_PATH_CONFLICT", "prevent", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
assert not c.stdout
assert c.stderr.strip() == dals("""
ClobberError: The package 'Groot' cannot be installed due to a
path collision for 'siebel/center/for/c.s'.
This path already exists in the target prefix, and it won't be removed
by an uninstall action in this transaction. The path is one that conda
doesn't recognize. It may have been created by another package manager.
""").strip()
def test_SharedLinkPathClobberError(self):
target_path = "some/where/in/shampoo/banana"
incompatible_package_dists = "Groot"
exc = SharedLinkPathClobberError(target_path, incompatible_package_dists, context)
with env_var("CONDA_PATH_CONFLICT", "prevent", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
assert not c.stdout
assert c.stderr.strip() == dals("""
ClobberError: This transaction has incompatible packages due to a shared path.
packages: G, r, o, o, t
path: 'some/where/in/shampoo/banana'
""").strip()
def test_CondaFileNotFoundError(self):
filename = "Groot"
exc = PathNotFoundError(filename)
with env_var("CONDA_JSON", "yes", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
json_obj = json.loads(c.stdout)
assert not c.stderr
assert json_obj['exception_type'] == "<class 'conda.exceptions.PathNotFoundError'>"
assert json_obj['exception_name'] == 'PathNotFoundError'
assert json_obj['message'] == text_type(exc)
assert json_obj['error'] == repr(exc)
with env_var("CONDA_JSON", "no", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
assert not c.stdout
assert c.stderr.strip() == "PathNotFoundError: Groot"
def test_DirectoryNotFoundError(self):
directory = "Groot"
exc = DirectoryNotFoundError(directory)
with env_var("CONDA_JSON", "yes", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
json_obj = json.loads(c.stdout)
assert not c.stderr
assert json_obj['exception_type'] == "<class 'conda.exceptions.DirectoryNotFoundError'>"
assert json_obj['exception_name'] == 'DirectoryNotFoundError'
assert json_obj['message'] == text_type(exc)
assert json_obj['error'] == repr(exc)
assert json_obj['path'] == "Groot"
with env_var("CONDA_JSON", "no", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
assert not c.stdout
assert c.stderr.strip() == "DirectoryNotFoundError: Groot"
def test_MD5MismatchError(self):
url = "https://download.url/path/to/file.tar.bz2"
target_full_path = "/some/path/on/disk/another-name.tar.bz2"
expected_md5sum = "abc123"
actual_md5sum = "deadbeef"
exc = MD5MismatchError(url, target_full_path, expected_md5sum, actual_md5sum)
with env_var("CONDA_JSON", "yes", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
json_obj = json.loads(c.stdout)
assert not c.stderr
assert json_obj['exception_type'] == "<class 'conda.exceptions.MD5MismatchError'>"
assert json_obj['exception_name'] == 'MD5MismatchError'
assert json_obj['message'] == text_type(exc)
assert json_obj['error'] == repr(exc)
assert json_obj['url'] == url
assert json_obj['target_full_path'] == target_full_path
assert json_obj['expected_md5sum'] == expected_md5sum
assert json_obj['actual_md5sum'] == actual_md5sum
with env_var("CONDA_JSON", "no", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
assert not c.stdout
assert c.stderr.strip() == dals("""
MD5MismatchError: Conda detected a mismatch between the expected content and downloaded content
for url 'https://download.url/path/to/file.tar.bz2'.
download saved to: /some/path/on/disk/another-name.tar.bz2
expected md5 sum: abc123
actual md5 sum: deadbeef
""").strip()
def test_PackageNotFoundError(self):
package = "Potato"
with env_var("CONDA_JSON", "yes", reset_context):
with captured() as c:
exc = PackagesNotFoundError((package,))
conda_exception_handler(_raise_helper, exc)
json_obj = json.loads(c.stdout)
assert not c.stderr
assert json_obj['exception_type'] == "<class 'conda.exceptions.PackagesNotFoundError'>"
assert json_obj['message'] == text_type(exc)
assert json_obj['error'] == repr(exc)
with env_var("CONDA_JSON", "no", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
assert not c.stdout
assert c.stderr.strip() == dals("""
PackagesNotFoundError: The following packages are missing from the target environment:
- Potato
""").strip()
def test_CondaRevisionError(self):
message = "Potato"
exc = CondaRevisionError(message)
with env_var("CONDA_JSON", "yes", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
json_obj = json.loads(c.stdout)
assert not c.stderr
assert json_obj['exception_type'] == "<class 'conda.exceptions.CondaRevisionError'>"
assert json_obj['exception_name'] == 'CondaRevisionError'
assert json_obj['message'] == text_type(exc)
assert json_obj['error'] == repr(exc)
with env_var("CONDA_JSON", "no", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
assert not c.stdout
assert c.stderr.strip() == "CondaRevisionError: Potato."
def test_CondaKeyError(self):
key = "Potato"
message = "Potato is not a key."
exc = CondaKeyError(key, message)
with env_var("CONDA_JSON", "yes", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
json_obj = json.loads(c.stdout)
assert not c.stderr
assert json_obj['exception_type'] == "<class 'conda.exceptions.CondaKeyError'>"
assert json_obj['exception_name'] == 'CondaKeyError'
assert json_obj['message'] == text_type(exc)
assert json_obj['error'] == repr(exc)
assert json_obj['key'] == "Potato"
with env_var("CONDA_JSON", "no", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
assert not c.stdout
assert c.stderr.strip() == "CondaKeyError: 'Potato': Potato is not a key."
def test_CondaHTTPError(self):
msg = "Potato"
url = "https://download.url/path/to/Potato.tar.gz"
status_code = "Potato"
reason = "COULD NOT CONNECT"
elapsed_time = 1.24
exc = CondaHTTPError(msg, url, status_code, reason, elapsed_time)
with env_var("CONDA_JSON", "yes", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
json_obj = json.loads(c.stdout)
assert not c.stderr
assert json_obj['exception_type'] == "<class 'conda.exceptions.CondaHTTPError'>"
assert json_obj['exception_name'] == 'CondaHTTPError'
assert json_obj['message'] == text_type(exc)
assert json_obj['error'] == repr(exc)
assert json_obj['url'] == url
assert json_obj['status_code'] == status_code
assert json_obj['reason'] == reason
assert json_obj['elapsed_time'] == elapsed_time
with env_var("CONDA_JSON", "no", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
assert not c.stdout
assert dals("""
CondaHTTPError: HTTP Potato COULD NOT CONNECT for url <https://download.url/path/to/Potato.tar.gz>
Elapsed: 1.24
Potato
""").strip() in c.stderr.strip()
def test_CommandNotFoundError_simple(self):
cmd = "instate"
exc = CommandNotFoundError(cmd)
with env_var("CONDA_JSON", "yes", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
json_obj = json.loads(c.stdout)
assert not c.stderr
assert json_obj['exception_type'] == "<class 'conda.exceptions.CommandNotFoundError'>"
assert json_obj['message'] == text_type(exc)
assert json_obj['error'] == repr(exc)
with env_var("CONDA_JSON", "no", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
assert not c.stdout
assert c.stderr.strip() == ("CommandNotFoundError: No command 'conda instate'.\n"
"Did you mean 'conda install'?")
def test_CommandNotFoundError_conda_build(self):
cmd = "build"
exc = CommandNotFoundError(cmd)
with env_var("CONDA_JSON", "yes", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
json_obj = json.loads(c.stdout)
assert not c.stderr
assert json_obj['exception_type'] == "<class 'conda.exceptions.CommandNotFoundError'>"
assert json_obj['message'] == text_type(exc)
assert json_obj['error'] == repr(exc)
with env_var("CONDA_JSON", "no", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
assert not c.stdout
assert c.stderr.strip() == ("CommandNotFoundError: To use 'conda build', install conda-build.")
@patch('requests.post', side_effect=(
AttrDict(headers=AttrDict(Location='somewhere.else'), status_code=302,
raise_for_status=lambda: None),
AttrDict(raise_for_status=lambda: None),
))
def test_print_unexpected_error_message_upload_1(self, post_mock):
with env_var('CONDA_REPORT_ERRORS', 'true', reset_context):
with captured() as c:
ExceptionHandler()(_raise_helper, AssertionError())
assert post_mock.call_count == 2
assert c.stdout == ''
assert "conda version" in c.stderr
@patch('requests.post', side_effect=(
AttrDict(headers=AttrDict(Location='somewhere.else'), status_code=302,
raise_for_status=lambda: None),
AttrDict(headers=AttrDict(Location='somewhere.again'), status_code=301,
raise_for_status=lambda: None),
AttrDict(raise_for_status=lambda: None),
))
def test_print_unexpected_error_message_upload_2(self, post_mock):
with env_var('CONDA_JSON', 'true', reset_context):
with env_var('CONDA_YES', 'yes', reset_context):
with captured() as c:
ExceptionHandler()(_raise_helper, AssertionError())
assert post_mock.call_count == 3
assert len(json.loads(c.stdout)['conda_info']['channels']) >= 2
assert not c.stderr
@patch('requests.post', side_effect=(
AttrDict(headers=AttrDict(Location='somewhere.else'), status_code=302,
raise_for_status=lambda: None),
AttrDict(raise_for_status=lambda: None),
))
@patch('conda.exceptions.input', return_value='y')
@patch('conda.exceptions.os.isatty', return_value=True)
def test_print_unexpected_error_message_upload_3(self, isatty_mock, input_mock, post_mock):
with captured() as c:
ExceptionHandler()(_raise_helper, AssertionError())
assert input_mock.call_count == 1
assert post_mock.call_count == 2
assert c.stdout == ''
assert "conda version" in c.stderr
@patch('requests.post', return_value=None)
@patch('conda.exceptions.input', return_value='n')
def test_print_unexpected_error_message_opt_out_1(self, input_mock, post_mock):
with env_var('CONDA_REPORT_ERRORS', 'false', reset_context):
e = AssertionError()
with captured() as c:
ExceptionHandler()(_raise_helper, AssertionError())
assert input_mock.call_count == 0
assert post_mock.call_count == 0
assert c.stdout == ''
print(c.stderr, file=sys.stderr)
assert "conda version" in c.stderr
@patch('requests.post', return_value=None)
@patch('conda.exceptions.input', return_value='n')
@patch('conda.exceptions.os.isatty', return_value=True)
def test_print_unexpected_error_message_opt_out_2(self, isatty_mock, input_mock, post_mock):
with captured() as c:
ExceptionHandler()(_raise_helper, AssertionError())
assert input_mock.call_count == 1
assert post_mock.call_count == 0
assert c.stdout == ''
assert "conda version" in c.stderr
def test_BinaryPrefixReplacementError(self):
new_data_length = 1104
original_data_length = 1404
new_prefix = "some/where/on/goodwin.ave"
path = "some/where/by/boneyard/creek"
placeholder = "save/my/spot/in/374"
exc = BinaryPrefixReplacementError(path, placeholder, new_prefix,
original_data_length, new_data_length)
with env_var("CONDA_JSON", "yes", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
json_obj = json.loads(c.stdout)
assert not c.stderr
assert json_obj['exception_type'] == "<class 'conda.exceptions.BinaryPrefixReplacementError'>"
assert json_obj['exception_name'] == 'BinaryPrefixReplacementError'
assert json_obj['message'] == text_type(exc)
assert json_obj['error'] == repr(exc)
assert json_obj['new_data_length'] == 1104
assert json_obj['original_data_length'] == 1404
assert json_obj['new_prefix'] == new_prefix
assert json_obj['path'] == path
assert json_obj['placeholder'] == placeholder
with env_var("CONDA_JSON", "no", reset_context):
with captured() as c:
conda_exception_handler(_raise_helper, exc)
assert not c.stdout
assert c.stderr.strip() == dals("""
BinaryPrefixReplacementError: Refusing to replace mismatched data length in binary file.
path: some/where/by/boneyard/creek
placeholder: save/my/spot/in/374
new prefix: some/where/on/goodwin.ave
original data Length: 1404
new data length: 1104
""").strip()