forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_activate.py
755 lines (652 loc) · 32.6 KB
/
test_activate.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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
# -*- coding: utf-8 -*-
from __future__ import print_function, absolute_import
import os
from os.path import dirname, join, pathsep
import shutil
import shlex
import stat
import subprocess
import sys
import pytest
from conda.compat import TemporaryDirectory
from conda.config import root_dir, platform
from conda.install import symlink_conda
from conda.utils import (win_path_to_unix, unix_path_to_win, win_path_to_cygwin,
cygwin_path_to_win, translate_stream)
from conda.cli.activate import pathlist_to_str
import subprocess
import tempfile
from tests.helpers import assert_equals, assert_in, assert_not_in
# make pathsep unicode for sake of windows backslash string formatting
pathsep = u"%s" % pathsep
def path_identity(path):
"""Used as a dummy path converter where no conversion necessary"""
return path
# defaults for unix shells. Note: missing "exe" entry, which should be set to
# either an executable on PATH, or a full path to an executable for a shell
unix_shell_base = dict(ps_var="PS1",
echo="echo",
test_echo_extra="",
var_format="${}",
binpath="/bin/", # mind the trailing slash.
source_setup="source",
nul='2>/dev/null',
set_var='export ',
shell_suffix="",
env_script_suffix=".sh",
printps1='echo $PS1',
printdefaultenv='echo $CONDA_DEFAULT_ENV',
printpath="echo $PATH",
raw_ps=os.getenv("PS1", ""),
shell_args=["-l", "-c"],
path_from=path_identity,
path_to=path_identity,
slash_convert=("\\", "/"),
)
if sys.platform == "win32":
shells = {
#"powershell": dict(
# echo="echo",
# test_echo_extra=" .",
# ps_var="PS1",
# var_format="${var}",
# binpath="/bin/", # mind the trailing slash.
# source_setup="source",
# nul='2>/dev/null',
# set_var='export ',
# shell_suffix=".ps",
# env_script_suffix=".ps",
# printps1='echo $PS1',
# printdefaultenv='echo $CONDA_DEFAULT_ENV',
# printpath="echo %PATH%",
# raw_ps=os.getenv("PROMPT", ""),
# exe="powershell.exe",
# path_from=path_identity,
# path_to=path_identity,
# slash_convert = ("/", "\\"),
#),
"cmd.exe": dict(
echo="echo",
ps_var="PROMPT",
var_format="%{}%",
binpath="\\Scripts\\", # mind the trailing slash.
source_setup="call",
test_echo_extra="",
nul='1>NUL 2>&1',
set_var='set ',
shell_suffix=".bat",
env_script_suffix=".bat",
printps1="echo %PROMPT%",
printdefaultenv='IF NOT "%CONDA_DEFAULT_ENV%" == "" (\n'
'echo %CONDA_DEFAULT_ENV% ) ELSE (\n'
'echo()', # parens mismatched intentionally. See http://stackoverflow.com/questions/20691060/how-do-i-echo-a-blank-empty-line-to-the-console-from-a-windows-batch-file
printpath="echo %PATH%",
raw_ps=os.getenv("PROMPT", ""),
exe="cmd.exe",
shell_args=["/d", "/c"],
path_from=path_identity,
path_to=path_identity,
slash_convert = ("/", "\\"),
),
"cygwin": dict(unix_shell_base,
exe="bash",
path_from=cygwin_path_to_win,
path_to=win_path_to_cygwin),
# bash is whichever bash is on PATH. If using Cygwin, you should use the cygwin entry instead.
# The only major difference is that it handle's cywin's /cygdrive filesystem root.
"bash": dict(unix_shell_base,
exe="bash",
path_from=unix_path_to_win,
path_to=win_path_to_unix),
}
else:
shells = {
"bash": dict(unix_shell_base, exe="bash"),
"zsh": dict(unix_shell_base, exe="zsh"),
#"fish": dict(unix_shell_base, exe="fish",
# shell_suffix=".fish",
# source_setup=""),
}
def run_in(command, shell):
if shell == 'cmd.exe':
cmd_script = tempfile.NamedTemporaryFile(suffix='.bat', mode='wt', delete=False)
cmd_script.write(command)
cmd_script.close()
cmd_bits = [shells[shell]["exe"]] + shells[shell]["shell_args"] + [cmd_script.name]
try:
p = subprocess.Popen(cmd_bits, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
finally:
os.unlink(cmd_script.name)
elif shell == 'powershell':
raise NotImplementedError
else:
cmd_bits = [shells[shell]["exe"]] + shells[shell]["shell_args"] + [translate_stream(command, shells[shell]["path_to"])]
p = subprocess.Popen(cmd_bits, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
output_translator = shells[shell]["path_from"]
streams = [u"%s" % translate_stream(stream.strip().decode('utf-8').replace('\r\n', '\n'), output_translator)
for stream in (stdout, stderr)]
return streams
def gen_test_env_paths(envs, shell, num_test_folders=3):
"""People need not use all the test folders listed here.
This is only for shortening the environment string generation.
Also encapsulates paths in double quotes.
"""
paths = [join(envs, "test {}".format(test_folder+1)) for test_folder in range(num_test_folders)]
for path in paths[:2]: # Create symlinks ONLY for the first two folders.
symlink_conda(path, sys.prefix, shell)
converter = shells[shell]["path_to"]
paths = [converter(path) for path in paths]
return paths
def _envpaths(env_root, env_name=""):
"""Supply the appropriate platform executable folders. rstrip on root removes
trailing slash if env_name is empty (the default)"""
if 'win' in platform:
paths = [join(env_root, env_name).rstrip("\\"),
join(env_root, env_name, 'Scripts'),
join(env_root, env_name, 'Library', 'bin'),
]
else:
paths = [join(env_root, env_name).rstrip("/"),
join(env_root, env_name, 'bin'), ]
return paths
PYTHONPATH = os.path.dirname(os.path.dirname(__file__))
# Make sure the subprocess activate calls this python
syspath = pathsep.join(_envpaths(root_dir))
echo = "echo"
escape_curly = lambda x: x.replace("{", "{{").replace("}", "}}")
def print_ps1(env_dirs, shell, number):
return u" ".join([u"(({}))".format(os.path.split(env_dirs[number])[-1]), escape_curly(os.getenv(shells[shell]["ps_var"], ""))]).strip()
CONDA_ENTRY_POINT = """\
#!{syspath}/python
import sys
from conda.cli import main
sys.exit(main())
"""
def _format_vars(shell):
shelldict = shells[shell]
command_setup = """\
set {ps_var}={raw_ps}
set PYTHONPATH={PYTHONPATH}
set CONDARC=
""".format(here=dirname(__file__), PYTHONPATH=PYTHONPATH,
ps_var=shelldict["ps_var"], raw_ps=shelldict['raw_ps'])
if shelldict["shell_suffix"] == '.bat':
command_setup = "@echo off\n" + command_setup
base_path, _ = run_in(command_setup + shelldict['printpath'], shell)
return {
'echo': shelldict['echo'],
'nul': shelldict['nul'],
'printpath': shelldict['printpath'],
'printdefaultenv': shelldict['printdefaultenv'],
'printps1': shelldict['printps1'],
'raw_ps': shelldict["raw_ps"],
'set_var': shelldict['set_var'],
'source': shelldict['source_setup'],
'binpath': shelldict['binpath'],
'shell_suffix': shelldict['shell_suffix'],
'syspath': sys.prefix,
'binpath': shelldict['binpath'],
'command_setup': command_setup,
'base_path': base_path,
}
@pytest.mark.slow
def test_activate_test1(shell):
shell_vars = _format_vars(shell)
with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs:
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate{shell_suffix}" "{env_dirs[0]}"
{printpath}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stderr, u'prepending {envpaths} to PATH'\
.format(envpaths=pathlist_to_str(_envpaths(envs, 'test1'), False)), shell)
assert_in(pathsep.join(_envpaths(envs, 'test1')), shells[shell]["path_from"](stdout), shell)
@pytest.mark.slow
def test_activate_env_from_env_with_root_activate(shell):
shell_vars = _format_vars(shell)
with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs:
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}" {nul}
{source} "{syspath}{binpath}activate" "{env_dirs[1]}"
{printpath}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stderr, u'prepending {envpaths2} to PATH'\
.format(envpaths2=pathlist_to_str(_envpaths(envs, 'test2'))))
assert_in(shells[shell]["path_from"](pathsep.join(_envpaths(envs, 'test2'))),
stdout)
@pytest.mark.slow
def test_activate_bad_directory(shell):
shell_vars = _format_vars(shell)
with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs:
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[2]}"
{printpath}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_not_in(shells[shell]["path_to"](_envpaths(envs, 'test3')[0]), stdout)
assert_equals(stderr, u'Error: could not find environment: {envpaths3}'.format(envpaths3=_envpaths(envs, 'test3')[0]))
@pytest.mark.slow
def test_activate_bad_env_keeps_existing_good_env(shell):
shell_vars = _format_vars(shell)
with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs:
commands = (shell_vars['command_setup'] + """
{source} {syspath}{binpath}activate "{env_dirs[0]}" {nul}
{source} "{syspath}{binpath}activate" "{env_dirs[2]}"
{printpath}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_in(pathsep.join(_envpaths(envs, 'test1')),shells[shell]["path_from"](stdout))
@pytest.mark.slow
def test_activate_deactivate(shell):
shell_vars = _format_vars(shell)
with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs:
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}" {nul}
{source} "{syspath}{binpath}deactivate"
{printpath}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, u"%s" % shell_vars['base_path'])
@pytest.mark.slow
def test_activate_root(shell):
shell_vars = _format_vars(shell)
with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs:
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" root
{printpath}
""").format(envs=envs, **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_in(shells[shell]["path_to"](pathsep.join(_envpaths(root_dir))), stdout)
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" root
{source} "{syspath}{binpath}deactivate"
{printpath}
""").format(envs=envs, **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, u"%s" % shell_vars['base_path'], stderr)
def test_activate_root_env_from_other_env(shell):
shell_vars = _format_vars(shell)
with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs:
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}" {nul}
{source} "{syspath}{binpath}activate" root
{printpath}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_in(shells[shell]["path_to"](pathsep.join(_envpaths(root_dir))),
stdout)
assert_not_in(shells[shell]["path_to"](pathsep.join(_envpaths(envs, 'test1'))),
stdout)
@pytest.mark.slow
def test_wrong_args(shell):
shell_vars = _format_vars(shell)
with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs:
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" two args
{printpath}
""").format(envs=envs, **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stderr, u'Error: did not expect more than one argument.')
assert_equals(stdout, shell_vars['base_path'], stderr)
@pytest.mark.slow
def test_activate_help(shell):
shell_vars = _format_vars(shell)
with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs:
if not platform.startswith("win"):
commands = (shell_vars['command_setup'] + """
"{syspath}{binpath}activate" Zanzibar
""").format(envs=envs, **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, '')
assert_in("activate must be sourced", stderr)
assert_in("Usage: source activate ENV", stderr)
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" --help
""").format(envs=envs, **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, '')
if platform.startswith("win") and shell in ["cmd.exe", "powershell"]:
assert_in("Usage: activate ENV", stderr)
else:
assert_in("Usage: source activate ENV", stderr)
commands = (shell_vars['command_setup'] + """
{syspath}{binpath}deactivate
""").format(envs=envs, **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, '')
assert_in("deactivate must be sourced", stderr)
assert_in("Usage: source deactivate", stderr)
commands = (shell_vars['command_setup'] + """
{source} {syspath}{binpath}deactivate --help
""").format(envs=envs, **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, '')
if platform == 'win' and shell in ["cmd.exe", "powershell"]:
assert_in("Usage: deactivate", stderr)
else:
assert_in("Usage: source deactivate", stderr)
@pytest.mark.slow
def test_activate_symlinking(shell):
"""Symlinks or bat file redirects are created at activation time. Make sure that the
files/links exist, and that they point where they should."""
shell_vars = _format_vars(shell)
with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs:
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}"
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stderr, u'prepending {envpaths1} to PATH'\
.format(syspath=pathlist_to_str(_envpaths(root_dir)),
envpaths1=pathlist_to_str(_envpaths(envs, 'test1'))))
where = 'Scripts' if sys.platform == 'win32' else 'bin'
for env in gen_test_env_paths(envs, shell)[:2]:
scripts = ["conda", "activate", "deactivate"]
for f in scripts:
if sys.platform == "win32":
file_path = shells[shell]["slash_convert"][1].join([env, where, f + shells[shell]["shell_suffix"]])
print(file_path)
# must translate path to windows representation for Python's sake
file_path = shells[shell]["path_from"](file_path)
print(file_path)
assert(os.path.lexists(file_path))
else:
file_path = join(env, where, f)
assert(os.path.lexists(file_path))
s = os.lstat(file_path)
assert(stat.S_ISLNK(s.st_mode))
assert(os.readlink(file_path) == '{root_path}'.format(root_path=join(sys.prefix, where, f)))
if platform != 'win':
# Test activate when there are no write permissions in the
# env.
prefix_bin_path = join(gen_test_env_paths(envs, shell)[2], 'bin')
commands = (shell_vars['command_setup'] + """
mkdir -p {prefix_bin_path}
chmod 000 {prefix_bin_path}
{source} activate "{env_dirs[2]}"
""").format(prefix_bin_path=prefix_bin_path, envs=envs,
env_dirs=gen_test_env_paths(envs, shell),
**shell_vars)
stdout, stderr = run_in(commands, shell)
assert_in("do not have write access", stderr)
# restore permissions so the dir will get cleaned up
run_in("chmod 777 {prefix_bin_path}".format(prefix_bin_path=prefix_bin_path), shell)
def test_PS1(shell):
shell_vars = _format_vars(shell)
with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs:
# activate changes PS1 correctly
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}"
{printps1}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, print_ps1(env_dirs=gen_test_env_paths(envs, shell), shell=shell, number=0), stderr)
# second activate replaces earlier actived env PS1
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}" {nul}
{source} "{syspath}{binpath}activate" "{env_dirs[1]}"
{printps1}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, sterr = run_in(commands, shell)
assert_equals(stdout, print_ps1(env_dirs=gen_test_env_paths(envs, shell), shell=shell,number=1), stderr)
# failed activate does not touch raw PS1
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[2]}"
{printps1}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, shell_vars['raw_ps'], stderr)
# ensure that a failed activate does not touch PS1 (envs[3] folders do not exist.)
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}" {nul}
{source} "{syspath}{binpath}activate" "{env_dirs[2]}"
{printps1}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, print_ps1(env_dirs=gen_test_env_paths(envs, shell), shell=shell,number=0), stderr)
# deactivate doesn't do anything bad to PS1 when no env active to deactivate
commands = (shell_vars['command_setup'] + """
{source} {syspath}{binpath}deactivate
{printps1}
""").format(envs=envs, **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, shell_vars['raw_ps'], stderr)
# deactivate script in activated env returns us to raw PS1
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}" {nul}
{source} "{env_dirs[0]}{binpath}deactivate"
{printps1}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, shell_vars['raw_ps'], stderr)
# make sure PS1 is unchanged by faulty activate input
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" two args
{printps1}
""").format(envs=envs, **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, shell_vars['raw_ps'], stderr)
@pytest.mark.slow
def test_PS1_no_changeps1(shell):
"""Ensure that people's PS1 remains unchanged if they have that setting in their RC file."""
shell_vars = _format_vars(shell)
with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs:
with open(join(envs, '.condarc'), 'w') as f:
f.write("""\
changeps1: no
""")
condarc = """
{set_var}CONDARC={condarc}
"""
commands = (shell_vars['command_setup'] + condarc + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}"
{printps1}
""").format(condarc=join(envs, ".condarc"), envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, shell_vars['raw_ps'], stderr)
commands = (shell_vars['command_setup'] + condarc + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}" {nul}
{source} "{syspath}{binpath}activate" "{env_dirs[1]}"
{printps1}
""").format(condarc=join(envs, ".condarc"), envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, shell_vars['raw_ps'], stderr)
commands = (shell_vars['command_setup'] + condarc + """
{source} "{syspath}{binpath}activate" "{env_dirs[2]}"
{printps1}
""").format(condarc=join(envs, ".condarc"), envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, shell_vars['raw_ps'], stderr)
commands = (shell_vars['command_setup'] + condarc + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}" {nul}
{source} "{syspath}{binpath}activate" "{env_dirs[2]}"
{printps1}
""").format(condarc=join(envs, ".condarc"), envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, shell_vars['raw_ps'], stderr)
commands = (shell_vars['command_setup'] + condarc + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}" {nul}
{source} "{env_dirs[0]}{binpath}deactivate"
{printps1}
""").format(condarc=join(envs, ".condarc"), envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, shell_vars['raw_ps'], stderr)
commands = (shell_vars['command_setup'] + condarc + """
{source} "{syspath}{binpath}activate" two args
{printps1}
""").format(condarc=join(envs, ".condarc"), envs=envs, **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, shell_vars['raw_ps'], stderr)
@pytest.mark.slow
def test_CONDA_DEFAULT_ENV(shell):
shell_vars = _format_vars(shell)
with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs:
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}"
{printdefaultenv}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, u'{env_dirs[0]}'.format(envs=envs, env_dirs=gen_test_env_paths(envs, shell)), stderr)
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}" {nul}
{source} "{syspath}{binpath}activate" "{env_dirs[1]}"
{printdefaultenv}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, u'{env_dirs[1]}'.format(env_dirs=gen_test_env_paths(envs, shell)), stderr)
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[2]}"
{printdefaultenv}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, '', stderr)
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}" {nul}
{source} "{syspath}{binpath}activate" "{env_dirs[2]}"
{printdefaultenv}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, '{env_dirs[0]}'.format(env_dirs=gen_test_env_paths(envs, shell)), stderr)
commands = (shell_vars['command_setup'] + """
{source} {syspath}{binpath}deactivate
{printdefaultenv}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, '', stderr)
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}" {nul}
{source} "{env_dirs[0]}{binpath}deactivate"
{printdefaultenv}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, '', stderr)
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" two args
{printdefaultenv}
""").format(envs=envs, **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, '', stderr)
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" root {nul}
{printdefaultenv}
""").format(envs=envs, **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, u"%s" % sys.prefix, stderr)
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" root {nul}
{source} "{env_dirs[0]}{binpath}deactivate" {nul}
{printdefaultenv}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, '', stderr)
@pytest.mark.slow
def test_activate_from_env(shell):
"""Tests whether the activate bat file or link in the activated environment works OK"""
shell_vars = _format_vars(shell)
with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs:
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}"
{source} "{env_dirs[0]}{binpath}activate" "{env_dirs[1]}"
{printdefaultenv}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, u'{env_dirs[1]}'.format(envs=envs, env_dirs=gen_test_env_paths(envs, shell)), stderr)
@pytest.mark.slow
def test_deactivate_from_env(shell):
"""Tests whether the deactivate bat file or link in the activated environment works OK"""
shell_vars = _format_vars(shell)
with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs:
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}"
{source} "{env_dirs[0]}{binpath}deactivate"
{printdefaultenv}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, u'', stderr)
@pytest.mark.slow
def test_activate_relative_path(shell):
shell_vars = _format_vars(shell)
with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs:
start_dir = os.getcwd()
env_dirs = gen_test_env_paths(envs, shell)
os.chdir(envs)
env_dir = os.path.basename(env_dirs[0])
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dir}"
{printdefaultenv}
""").format(envs=envs, env_dir=env_dir, **shell_vars)
stdout, stderr = run_in(commands, shell)
os.chdir(start_dir)
assert_equals(stdout, u'{env_dirs[0]}'.format(envs=envs, env_dirs=env_dirs), stderr)
@pytest.mark.slow
def test_activate_does_not_leak_echo_setting(shell):
"""Test that activate's setting of echo to off does not disrupt later echo calls"""
if sys.platform != "win32" or shell != "cmd.exe":
pytest.skip("test only relevant for cmd.exe on win")
shell_vars = _format_vars(shell)
with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs:
commands = (shell_vars['command_setup'] + """
@echo on
call "{syspath}{binpath}activate.bat" "{env_dirs[0]}"
@echo
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, u'ECHO is on.', stderr)
@pytest.mark.slow
def test_activate_non_ascii_char_in_path(shell):
if shell.lower() not in ["cmd.exe", "powershell"]:
pytest.xfail("subprocess with python 2.7 is broken with unicode")
shell_vars = _format_vars(shell)
with TemporaryDirectory(prefix='Ånvs', dir=dirname(__file__)) as envs:
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}"
{source} "{env_dirs[0]}{binpath}deactivate"
{printdefaultenv}
""").format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, u'', stderr)
@pytest.mark.slow
def test_activate_has_extra_env_vars(shell):
"""Test that environment variables in activate.d show up when activated"""
shell_vars = _format_vars(shell)
with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs:
env_dirs=gen_test_env_paths(envs, shell)
act_path = shells[shell]["path_from"](join(env_dirs[0], "etc", "conda", "activate.d"))
deact_path = shells[shell]["path_from"](join(env_dirs[0], "etc", "conda", "deactivate.d"))
os.makedirs(act_path)
os.makedirs(deact_path)
with open(join(act_path, "test" + shells[shell]["env_script_suffix"]), "w") as f:
f.write(shells[shell]["set_var"] + "TEST_VAR=test\n")
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}"
{echo} {var}
""").format(envs=envs, env_dirs=env_dirs, var=shells[shell]["var_format"].format("TEST_VAR"), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, u'test', stderr)
# Make sure the variable is reset after deactivation
with open(join(deact_path, "test" + shells[shell]["env_script_suffix"]), "w") as f:
f.write(shells[shell]["set_var"] + "TEST_VAR=\n")
commands = (shell_vars['command_setup'] + """
{source} "{syspath}{binpath}activate" "{env_dirs[0]}"
{source} "{env_dirs[0]}{binpath}deactivate"
{echo} {var}
""").format(envs=envs, env_dirs=env_dirs, var=shells[shell]["var_format"].format("TEST_VAR"), **shell_vars)
stdout, stderr = run_in(commands, shell)
assert_equals(stdout, u'', stderr)
# This test depends on files that are copied/linked in the conda recipe. It is unfortunately not going to run after
# a setup.py install step
# @pytest.mark.slow
# def test_activate_from_exec_folder(shell):
# """The exec folder contains only the activate and conda commands. It is for users
# who want to avoid conda packages shadowing system ones."""
# shell_vars = _format_vars(shell)
# with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs:
# env_dirs=gen_test_env_paths(envs, shell)
# commands = (shell_vars['command_setup'] + """
# {source} "{syspath}/exec/activate" "{env_dirs[0]}"
# {echo} {var}
# """).format(envs=envs, env_dirs=env_dirs, var=shells[shell]["var_format"].format("TEST_VAR"), **shell_vars)
# stdout, stderr = run_in(commands, shell)
# assert_equals(stdout, u'test', stderr)