forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_config.py
584 lines (473 loc) · 16.7 KB
/
test_config.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# (c) 2012-2014 Continuum Analytics, Inc. / http://continuum.io
# All Rights Reserved
#
# conda is distributed under the terms of the BSD 3-clause license.
# Consult LICENSE.txt or http://opensource.org/licenses/BSD-3-Clause.
import os
import unittest
from os.path import dirname, join, exists
import yaml
import pytest
import conda.config as config
from tests.helpers import run_conda_command
# use condarc from source tree to run these tests against
config.rc_path = join(dirname(__file__), 'condarc')
def _get_default_urls():
return ['http://repo.continuum.io/pkgs/free',
'http://repo.continuum.io/pkgs/pro']
config.get_default_urls = _get_default_urls
# unset CIO_TEST
try:
del os.environ['CIO_TEST']
except KeyError:
pass
class TestConfig(unittest.TestCase):
# These tests are mostly to ensure API stability
def __init__(self, *args, **kwargs):
config.rc = config.load_condarc(config.rc_path)
# Otherwise normalization tests will fail if the user is logged into
# binstar.
config.rc['add_binstar_token'] = False
super(TestConfig, self).__init__(*args, **kwargs)
def test_globals(self):
self.assertTrue(config.root_dir)
self.assertTrue(config.pkgs_dirs)
self.assertTrue(config.envs_dirs)
self.assertTrue(config.default_prefix)
self.assertTrue(config.platform)
self.assertTrue(config.subdir)
self.assertTrue(config.arch_name)
self.assertTrue(config.bits in (32, 64))
def test_pkgs_dir_from_envs_dir(self):
root_dir = config.root_dir
root_pkgs = join(root_dir, 'pkgs')
for pi, po in [
(join(root_dir, 'envs'), root_pkgs),
('/usr/local/foo/envs' if config.platform != 'win' else 'C:\envs',
'/usr/local/foo/envs/.pkgs' if config.platform != 'win' else 'C:\envs\.pkgs'),
]:
self.assertEqual(config.pkgs_dir_from_envs_dir(pi), po)
def test_proxy_settings(self):
self.assertEqual(config.get_proxy_servers(),
{'http': 'http://user:[email protected]:8080',
'https': 'https://user:[email protected]:8080'})
def test_normalize_urls(self):
current_platform = config.subdir
assert config.DEFAULT_CHANNEL_ALIAS == 'https://conda.anaconda.org/'
assert config.rc.get('channel_alias') == 'https://your.repo/'
for channel in config.normalize_urls(['defaults', 'system',
'https://anaconda.org/username', 'file:///Users/username/repo',
'username']):
assert (channel.endswith('/%s/' % current_platform) or
channel.endswith('/noarch/'))
self.assertEqual(config.normalize_urls([
'defaults', 'system', 'https://conda.anaconda.org/username',
'file:///Users/username/repo', 'username'
], 'osx-64'),
[
'http://repo.continuum.io/pkgs/free/osx-64/',
'http://repo.continuum.io/pkgs/free/noarch/',
'http://repo.continuum.io/pkgs/pro/osx-64/',
'http://repo.continuum.io/pkgs/pro/noarch/',
'https://your.repo/binstar_username/osx-64/',
'https://your.repo/binstar_username/noarch/',
'http://some.custom/channel/osx-64/',
'http://some.custom/channel/noarch/',
'http://repo.continuum.io/pkgs/free/osx-64/',
'http://repo.continuum.io/pkgs/free/noarch/',
'http://repo.continuum.io/pkgs/pro/osx-64/',
'http://repo.continuum.io/pkgs/pro/noarch/',
'https://conda.anaconda.org/username/osx-64/',
'https://conda.anaconda.org/username/noarch/',
'file:///Users/username/repo/osx-64/',
'file:///Users/username/repo/noarch/',
'https://your.repo/username/osx-64/',
'https://your.repo/username/noarch/',
])
test_condarc = os.path.join(os.path.dirname(__file__), 'test_condarc')
def _read_test_condarc():
with open(test_condarc) as f:
return f.read()
# Tests for the conda config command
# FIXME This shoiuld be multiple individual tests
@pytest.mark.slow
def test_config_command_basics():
try:
# Test that creating the file adds the defaults channel
assert not os.path.exists('test_condarc')
stdout, stderr = run_conda_command('config', '--file', test_condarc, '--add',
'channels', 'test')
assert stdout == stderr == ''
assert _read_test_condarc() == """\
channels:
- test
- defaults
"""
os.unlink(test_condarc)
# When defaults is explicitly given, it should not be added
stdout, stderr = run_conda_command('config', '--file', test_condarc, '--add',
'channels', 'test', '--add', 'channels', 'defaults')
assert stdout == stderr == ''
assert _read_test_condarc() == """\
channels:
- defaults
- test
"""
os.unlink(test_condarc)
# Duplicate keys should not be added twice
stdout, stderr = run_conda_command('config', '--file', test_condarc, '--add',
'channels', 'test')
assert stdout == stderr == ''
stdout, stderr = run_conda_command('config', '--file', test_condarc, '--add',
'channels', 'test')
assert stdout == ''
assert stderr == "Skipping channels: test, item already exists\n"
assert _read_test_condarc() == """\
channels:
- test
- defaults
"""
os.unlink(test_condarc)
# Test creating a new file with --set
stdout, stderr = run_conda_command('config', '--file', test_condarc,
'--set', 'always_yes', 'yes')
assert stdout == stderr == ''
assert _read_test_condarc() == """\
always_yes: yes
"""
os.unlink(test_condarc)
finally:
try:
pass
os.unlink(test_condarc)
except OSError:
pass
# FIXME Break into multiple tests
@pytest.mark.slow
def test_config_command_get():
try:
# Test --get
with open(test_condarc, 'w') as f:
f.write("""\
channels:
- test
- defaults
create_default_packages:
- ipython
- numpy
changeps1: no
always_yes: yes
invalid_key: yes
channel_alias: http://alpha.conda.anaconda.org
""")
stdout, stderr = run_conda_command('config', '--file', test_condarc, '--get')
assert stdout == """\
--set always_yes True
--set changeps1 False
--set channel_alias http://alpha.conda.anaconda.org
--add channels 'defaults'
--add channels 'test'
--add create_default_packages 'numpy'
--add create_default_packages 'ipython'
"""
assert stderr == "unknown key invalid_key\n"
stdout, stderr = run_conda_command('config', '--file', test_condarc,
'--get', 'channels')
assert stdout == """\
--add channels 'defaults'
--add channels 'test'
"""
assert stderr == ""
stdout, stderr = run_conda_command('config', '--file', test_condarc,
'--get', 'changeps1')
assert stdout == """\
--set changeps1 False
"""
assert stderr == ""
stdout, stderr = run_conda_command('config', '--file', test_condarc,
'--get', 'changeps1', 'channels')
assert stdout == """\
--set changeps1 False
--add channels 'defaults'
--add channels 'test'
"""
assert stderr == ""
stdout, stderr = run_conda_command('config', '--file', test_condarc,
'--get', 'allow_softlinks')
assert stdout == ""
assert stderr == ""
stdout, stderr = run_conda_command('config', '--file', test_condarc,
'--get', 'track_features')
assert stdout == ""
assert stderr == ""
stdout, stderr = run_conda_command('config', '--file', test_condarc,
'--get', 'invalid_key')
assert stdout == ""
assert "invalid choice: 'invalid_key'" in stderr
stdout, stderr = run_conda_command('config', '--file', test_condarc,
'--get', 'not_valid_key')
assert stdout == ""
assert "invalid choice: 'not_valid_key'" in stderr
os.unlink(test_condarc)
finally:
try:
pass
os.unlink(test_condarc)
except OSError:
pass
# FIXME Break into multiple tests
@pytest.mark.slow
def test_config_command_parser():
try:
# Now test the YAML "parser"
condarc = """\
channels : \n\
- test
- defaults \n\
create_default_packages:
- ipython
- numpy
changeps1 : no
# Here is a comment
always_yes: yes \n\
"""
# First verify that this itself is valid YAML
assert yaml.load(condarc) == {'channels': ['test', 'defaults'],
'create_default_packages': ['ipython', 'numpy'], 'changeps1':
False, 'always_yes': True}
with open(test_condarc, 'w') as f:
f.write(condarc)
stdout, stderr = run_conda_command('config', '--file', test_condarc, '--get')
assert stdout == """\
--set always_yes True
--set changeps1 False
--add channels 'defaults'
--add channels 'test'
--add create_default_packages 'numpy'
--add create_default_packages 'ipython'
"""
assert stderr == ''
# List keys with nonstandard whitespace are not yet supported. For
# now, just test that it doesn't muck up the file.
stdout, stderr = run_conda_command('config', '--file', test_condarc, '--add',
'create_default_packages', 'sympy')
assert stdout == ''
assert stderr == """\
Error: Could not parse the yaml file. Use -f to use the
yaml parser (this will remove any structure or comments from the existing
.condarc file). Reason: modified yaml doesn't match what it should be
"""
assert _read_test_condarc() == condarc
# assert _read_test_condarc() == """\
# channels : \n\
# - test
# - defaults \n\
#
# create_default_packages:
# - sympy
# - ipython
# - numpy
#
# changeps1 : no
#
# # Here is a comment
# always_yes: yes \n\
# """
# New keys when the keys are indented are not yet supported either.
stdout, stderr = run_conda_command('config', '--file', test_condarc, '--add',
'disallow', 'perl')
assert stdout == ''
assert stderr == """\
Error: Could not parse the yaml file. Use -f to use the
yaml parser (this will remove any structure or comments from the existing
.condarc file). Reason: couldn't parse modified yaml
"""
assert _read_test_condarc() == condarc
# assert _read_test_condarc() == """\
# channels : \n\
# - test
# - defaults \n\
#
# create_default_packages:
# - sympy
# - ipython
# - numpy
#
# changeps1 : no
#
# # Here is a comment
# always_yes: yes \n\
# disallow:
# - perl
# """
stdout, stderr = run_conda_command('config', '--file', test_condarc, '--add',
'channels', 'mychannel')
assert stdout == stderr == ''
assert _read_test_condarc() == """\
channels : \n\
- mychannel
- test
- defaults \n\
create_default_packages:
- ipython
- numpy
changeps1 : no
# Here is a comment
always_yes: yes \n\
"""
stdout, stderr = run_conda_command('config', '--file', test_condarc,
'--set', 'changeps1', 'yes')
assert stdout == stderr == ''
assert _read_test_condarc() == """\
channels : \n\
- mychannel
- test
- defaults \n\
create_default_packages:
- ipython
- numpy
changeps1 : yes
# Here is a comment
always_yes: yes \n\
"""
os.unlink(test_condarc)
# Test adding a new list key. We couldn't test this above because it
# doesn't work yet with odd whitespace
condarc = """\
channels:
- test
- defaults
always_yes: yes
"""
with open(test_condarc, 'w') as f:
f.write(condarc)
stdout, stderr = run_conda_command('config', '--file', test_condarc, '--add',
'disallow', 'perl')
assert stdout == stderr == ''
assert _read_test_condarc() == condarc + """\
disallow:
- perl
"""
os.unlink(test_condarc)
finally:
try:
pass
os.unlink(test_condarc)
except OSError:
pass
# FIXME Break into multiple tests
@pytest.mark.slow
def test_config_command_remove_force():
try:
# Finally, test --remove, --remove-key, and --force (right now
# --remove and --remove-key require --force)
run_conda_command('config', '--file', test_condarc, '--add',
'channels', 'test')
run_conda_command('config', '--file', test_condarc, '--set',
'always_yes', 'yes')
stdout, stderr = run_conda_command('config', '--file', test_condarc,
'--remove', 'channels', 'test', '--force')
assert stdout == stderr == ''
assert yaml.load(_read_test_condarc()) == {'channels': ['defaults'],
'always_yes': True}
stdout, stderr = run_conda_command('config', '--file', test_condarc,
'--remove', 'channels', 'test', '--force')
assert stdout == ''
assert stderr == "Error: 'test' is not in the 'channels' key of the config file\n"
stdout, stderr = run_conda_command('config', '--file', test_condarc,
'--remove', 'disallow', 'python', '--force')
assert stdout == ''
assert stderr == "Error: key 'disallow' is not in the config file\n"
stdout, stderr = run_conda_command('config', '--file', test_condarc,
'--remove-key', 'always_yes', '--force')
assert stdout == stderr == ''
assert yaml.load(_read_test_condarc()) == {'channels': ['defaults']}
stdout, stderr = run_conda_command('config', '--file', test_condarc,
'--remove-key', 'always_yes', '--force')
assert stdout == ''
assert stderr == "Error: key 'always_yes' is not in the config file\n"
os.unlink(test_condarc)
finally:
try:
pass
os.unlink(test_condarc)
except OSError:
pass
# FIXME Break into multiple tests
@pytest.mark.slow
def test_config_command_bad_args():
try:
stdout, stderr = run_conda_command('config', '--file', test_condarc, '--add',
'notarealkey', 'test')
assert stdout == ''
assert not exists(test_condarc)
stdout, stderr = run_conda_command('config', '--file', test_condarc, '--set',
'notarealkey', 'yes')
assert stdout == ''
assert not exists(test_condarc)
finally:
try:
pass
os.unlink(test_condarc)
except OSError:
pass
def test_invalid_rc():
# Some tests for unexpected input in the condarc, like keys that are the
# wrong type
try:
condarc = """\
channels:
"""
with open(test_condarc, 'w') as f:
f.write(condarc)
stdout, stderr = run_conda_command('config', '--file', test_condarc,
'--add', 'channels', 'test')
assert stdout == ''
assert stderr == """\
Error: Could not parse the yaml file. Use -f to use the
yaml parser (this will remove any structure or comments from the existing
.condarc file). Reason: key 'channels' should be a list, not NoneType.
"""
assert _read_test_condarc() == condarc
os.unlink(test_condarc)
finally:
try:
pass
os.unlink(test_condarc)
except OSError:
pass
def test_config_set():
# Test the config set command
# Make sure it accepts only boolean values for boolean keys and any value for string keys
try:
stdout, stderr = run_conda_command('config', '--file', test_condarc,
'--set', 'always_yes', 'yep')
assert stdout == ''
assert stderr == 'Error: Key: always_yes; yep is not a YAML boolean.\n'
finally:
try:
os.unlink(test_condarc)
except OSError:
pass
def test_set_rc_string():
# Test setting string keys in .condarc
# We specifically test ssl_verify since it can be either a boolean or a string
try:
stdout, stderr = run_conda_command('config', '--file', test_condarc,
'--set', 'ssl_verify', 'yes')
assert stdout == ''
assert stderr == ''
verify = yaml.load(open(test_condarc, 'r'))['ssl_verify']
assert verify == True
stdout, stderr = run_conda_command('config', '--file', test_condarc,
'--set', 'ssl_verify', 'test_string.crt')
assert stdout == ''
assert stderr == ''
verify = yaml.load(open(test_condarc, 'r'))['ssl_verify']
assert verify == 'test_string.crt'
os.unlink(test_condarc)
finally:
try:
os.unlink(test_condarc)
except OSError:
pass