forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_config.py
507 lines (418 loc) · 16.6 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
# (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
from os.path import dirname, join, exists
from contextlib import contextmanager
from tempfile import mktemp
import unittest
import pytest
import conda.config as config
from conda.utils import get_yaml
from tests.helpers import run_conda_command
yaml = get_yaml()
# use condarc from source tree to run these tests against
config.rc_path = join(dirname(__file__), 'condarc')
# unset 'default_channels' and override config.defaults_ so that
# get_default_channels has predictable behavior
try:
del config.sys_rc['default_channels']
except KeyError:
pass
config.defaults_ = ['http://repo.continuum.io/pkgs/free',
'http://repo.continuum.io/pkgs/pro']
# unset CIO_TEST. This is a Continuum-internal variable that draws packages from an internal server instead of
# repo.continuum.io
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
config.channel_alias = config.rc['channel_alias']
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/'
assert config.channel_alias == 'https://your.repo/'
normurls = config.normalize_urls([
'defaults', 'system', 'https://conda.anaconda.org/username',
'file:///Users/username/repo', 'username'
], 'osx-64')
assert normurls == [
'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/']
priurls = config.prioritize_channels(normurls)
assert dict(priurls) == {
'file:///Users/username/repo/noarch/': ('file:///Users/username/repo', 5),
'file:///Users/username/repo/osx-64/': ('file:///Users/username/repo', 5),
'http://repo.continuum.io/pkgs/free/noarch/': ('defaults', 1),
'http://repo.continuum.io/pkgs/free/osx-64/': ('defaults', 1),
'http://repo.continuum.io/pkgs/pro/noarch/': ('defaults', 1),
'http://repo.continuum.io/pkgs/pro/osx-64/': ('defaults', 1),
'http://some.custom/channel/noarch/': ('http://some.custom/channel', 3),
'http://some.custom/channel/osx-64/': ('http://some.custom/channel', 3),
'https://conda.anaconda.org/username/noarch/': ('https://conda.anaconda.org/username', 4),
'https://conda.anaconda.org/username/osx-64/': ('https://conda.anaconda.org/username', 4),
'https://your.repo/binstar_username/noarch/': ('binstar_username', 2),
'https://your.repo/binstar_username/osx-64/': ('binstar_username', 2),
'https://your.repo/username/noarch/': ('username', 6),
'https://your.repo/username/osx-64/': ('username', 6)}
@contextmanager
def make_temp_condarc(value=None):
try:
tempfile = mktemp()
if value:
with open(tempfile, 'w') as f:
f.write(value)
yield tempfile
finally:
if exists(tempfile):
os.remove(tempfile)
def _read_test_condarc(rc):
with open(rc) 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():
# Test that creating the file adds the defaults channel
with make_temp_condarc() as rc:
stdout, stderr = run_conda_command('config', '--file', rc, '--add',
'channels', 'test')
assert stdout == stderr == ''
assert _read_test_condarc(rc) == """\
channels:
- test
- defaults
"""
with make_temp_condarc() as rc:
# When defaults is explicitly given, it should not be added
stdout, stderr = run_conda_command('config', '--file', rc, '--add',
'channels', 'test', '--add', 'channels', 'defaults')
assert stdout == ''
assert stderr == "Warning: 'defaults' already in 'channels' list, moving to the front"
assert _read_test_condarc(rc) == """\
channels:
- defaults
- test
"""
# Duplicate keys should not be added twice
with make_temp_condarc() as rc:
stdout, stderr = run_conda_command('config', '--file', rc, '--add',
'channels', 'test')
assert stdout == stderr == ''
stdout, stderr = run_conda_command('config', '--file', rc, '--add',
'channels', 'test')
assert stdout == ''
assert stderr == "Warning: 'test' already in 'channels' list, moving to the front"
assert _read_test_condarc(rc) == """\
channels:
- test
- defaults
"""
# Test append
with make_temp_condarc() as rc:
stdout, stderr = run_conda_command('config', '--file', rc, '--add',
'channels', 'test')
assert stdout == stderr == ''
stdout, stderr = run_conda_command('config', '--file', rc, '--append',
'channels', 'test')
assert stdout == ''
assert stderr == "Warning: 'test' already in 'channels' list, moving to the back"
assert _read_test_condarc(rc) == """\
channels:
- defaults
- test
"""
# Test duoble remove of defaults
with make_temp_condarc() as rc:
stdout, stderr = run_conda_command('config', '--file', rc, '--remove',
'channels', 'defaults')
assert stdout == stderr == ''
stdout, stderr = run_conda_command('config', '--file', rc, '--remove',
'channels', 'defaults')
assert stdout == ''
assert stderr == "Error: 'defaults' is not in the 'channels' key of the config file"
# Test creating a new file with --set
with make_temp_condarc() as rc:
stdout, stderr = run_conda_command('config', '--file', rc,
'--set', 'always_yes', 'true')
assert stdout == stderr == ''
assert _read_test_condarc(rc) == """\
always_yes: true
"""
# FIXME Break into multiple tests
@pytest.mark.slow
def test_config_command_get():
# Test --get
condarc = """\
channels:
- test
- defaults
create_default_packages:
- ipython
- numpy
changeps1: false
always_yes: true
invalid_key: true
channel_alias: http://alpha.conda.anaconda.org
"""
with make_temp_condarc(condarc) as rc:
stdout, stderr = run_conda_command('config', '--file', rc, '--get')
assert stdout == """\
--set always_yes True
--set changeps1 False
--set channel_alias http://alpha.conda.anaconda.org
--add channels 'defaults' # lowest priority
--add channels 'test' # highest priority
--add create_default_packages 'numpy'
--add create_default_packages 'ipython'\
"""
assert stderr == "unknown key invalid_key"
stdout, stderr = run_conda_command('config', '--file', rc,
'--get', 'channels')
assert stdout == """\
--add channels 'defaults' # lowest priority
--add channels 'test' # highest priority\
"""
assert stderr == ""
stdout, stderr = run_conda_command('config', '--file', rc,
'--get', 'changeps1')
assert stdout == """\
--set changeps1 False\
"""
assert stderr == ""
stdout, stderr = run_conda_command('config', '--file', rc,
'--get', 'changeps1', 'channels')
assert stdout == """\
--set changeps1 False
--add channels 'defaults' # lowest priority
--add channels 'test' # highest priority\
"""
assert stderr == ""
stdout, stderr = run_conda_command('config', '--file', rc,
'--get', 'allow_softlinks')
assert stdout == ""
assert stderr == ""
stdout, stderr = run_conda_command('config', '--file', rc,
'--get', 'track_features')
assert stdout == ""
assert stderr == ""
stdout, stderr = run_conda_command('config', '--file', rc,
'--get', 'invalid_key')
assert stdout == ""
assert "invalid choice: 'invalid_key'" in stderr
stdout, stderr = run_conda_command('config', '--file', rc,
'--get', 'not_valid_key')
assert stdout == ""
assert "invalid choice: 'not_valid_key'" in stderr
# FIXME Break into multiple tests
@pytest.mark.slow
def test_config_command_parser():
# Now test the YAML "parser"
# Channels is normal content.
# create_default_packages has extra spaces in list items
condarc = """\
channels:
- test
- defaults
create_default_packages :
- ipython
- numpy
changeps1: false
# Here is a comment
always_yes: yes
"""
# First verify that this itself is valid YAML
assert yaml.load(condarc, Loader=yaml.RoundTripLoader) == {'channels': ['test', 'defaults'],
'create_default_packages': ['ipython', 'numpy'], 'changeps1':
False, 'always_yes': 'yes'}
with make_temp_condarc(condarc) as rc:
stdout, stderr = run_conda_command('config', '--file', rc, '--get')
print(stdout)
assert stdout == """\
--set always_yes True
--set changeps1 False
--add channels 'defaults' # lowest priority
--add channels 'test' # highest priority
--add create_default_packages 'numpy'
--add create_default_packages 'ipython'\
"""
stdout, stderr = run_conda_command('config', '--file', rc, '--add',
'channels', 'mychannel')
assert stdout == stderr == ''
assert _read_test_condarc(rc) == """\
channels:
- mychannel
- test
- defaults
create_default_packages:
- ipython
- numpy
changeps1: false
# Here is a comment
always_yes: true
"""
stdout, stderr = run_conda_command('config', '--file', rc,
'--set', 'changeps1', 'true')
assert stdout == stderr == ''
assert _read_test_condarc(rc) == """\
channels:
- mychannel
- test
- defaults
create_default_packages:
- ipython
- numpy
changeps1: true
# Here is a comment
always_yes: true
"""
# 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: true
"""
with make_temp_condarc(condarc) as rc:
stdout, stderr = run_conda_command('config', '--file', rc, '--add',
'disallow', 'perl')
assert stdout == stderr == ''
assert _read_test_condarc(rc) == condarc + """\
disallow:
- perl
"""
# FIXME Break into multiple tests
@pytest.mark.slow
def test_config_command_remove_force():
# Finally, test --remove, --remove-key
with make_temp_condarc() as rc:
run_conda_command('config', '--file', rc, '--add',
'channels', 'test')
run_conda_command('config', '--file', rc, '--set',
'always_yes', 'true')
stdout, stderr = run_conda_command('config', '--file', rc,
'--remove', 'channels', 'test')
assert stdout == stderr == ''
assert yaml.load(_read_test_condarc(rc), Loader=yaml.RoundTripLoader) == {'channels': ['defaults'],
'always_yes': True}
stdout, stderr = run_conda_command('config', '--file', rc,
'--remove', 'channels', 'test', '--force')
assert stdout == ''
assert stderr == "Error: 'test' is not in the 'channels' key of the config file"
stdout, stderr = run_conda_command('config', '--file', rc,
'--remove', 'disallow', 'python', '--force')
assert stdout == ''
assert stderr == "Error: key 'disallow' is not in the config file"
stdout, stderr = run_conda_command('config', '--file', rc,
'--remove-key', 'always_yes', '--force')
assert stdout == stderr == ''
assert yaml.load(_read_test_condarc(rc), Loader=yaml.RoundTripLoader) == {'channels': ['defaults']}
stdout, stderr = run_conda_command('config', '--file', rc,
'--remove-key', 'always_yes', '--force')
assert stdout == ''
assert stderr == "Error: key 'always_yes' is not in the config file"
# FIXME Break into multiple tests
@pytest.mark.slow
def test_config_command_bad_args():
with make_temp_condarc() as rc:
stdout, stderr = run_conda_command('config', '--file', rc, '--add',
'notarealkey', 'test')
assert stdout == ''
assert not exists(rc)
stdout, stderr = run_conda_command('config', '--file', rc, '--set',
'notarealkey', 'true')
assert stdout == ''
assert not exists(rc)
def test_invalid_rc():
# Some tests for unexpected input in the condarc, like keys that are the
# wrong type
condarc = """\
channels:
"""
with make_temp_condarc(condarc) as rc:
stdout, stderr = run_conda_command('config', '--file', rc,
'--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(rc) == condarc
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
with make_temp_condarc() as rc:
stdout, stderr = run_conda_command('config', '--file', rc,
'--set', 'always_yes', 'yes')
assert stdout == ''
assert stderr == ''
stdout, stderr = run_conda_command('config', '--file', rc,
'--set', 'always_yes', 'no')
assert stdout == ''
assert stderr == ''
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
with make_temp_condarc() as rc:
stdout, stderr = run_conda_command('config', '--file', rc,
'--set', 'ssl_verify', 'yes')
assert stdout == ''
assert stderr == ''
verify = yaml.load(open(rc, 'r'), Loader=yaml.RoundTripLoader)['ssl_verify']
assert verify is True
stdout, stderr = run_conda_command('config', '--file', rc,
'--set', 'ssl_verify', 'test_string.crt')
assert stdout == ''
assert stderr == ''
verify = yaml.load(open(rc, 'r'), Loader=yaml.RoundTripLoader)['ssl_verify']
assert verify == 'test_string.crt'