forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_activate.py
3232 lines (2872 loc) · 125 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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
import json
import os
import platform
import sys
from functools import lru_cache
from itertools import chain
from logging import getLogger
from os.path import dirname, isdir, join
from pathlib import Path
from re import escape
from subprocess import CalledProcessError, check_output
from tempfile import gettempdir
from unittest import TestCase
from uuid import uuid4
import pytest
from conda import CONDA_PACKAGE_ROOT, CONDA_SOURCE_ROOT
from conda import __version__ as conda_version
from conda.activate import (
CmdExeActivator,
CshActivator,
FishActivator,
PosixActivator,
PowerShellActivator,
XonshActivator,
_build_activator_cls,
activator_map,
native_path_to_unix,
)
from conda.auxlib.ish import dals
from conda.base.constants import (
CONDA_ENV_VARS_UNSET_VAR,
PACKAGE_ENV_VARS_DIR,
PREFIX_STATE_FILE,
ROOT_ENV_NAME,
)
from conda.base.context import conda_tests_ctxt_mgmt_def_pol, context
from conda.cli.main import main_sourced
from conda.common.compat import ensure_text_type, on_win
from conda.common.io import captured, env_var, env_vars
from conda.common.path import which
from conda.exceptions import EnvironmentLocationNotFound, EnvironmentNameNotFound
from conda.gateways.disk.create import mkdir_p
from conda.gateways.disk.delete import rm_rf
from conda.gateways.disk.update import touch
from conda.testing.helpers import tempdir
from conda.testing.integration import (
SPACER_CHARACTER,
Commands,
make_temp_env,
run_command,
)
log = getLogger(__name__)
# Here, by removing --dev you can try weird situations that you may want to test, upgrade paths
# and the like? What will happen is that the conda being run and the shell scripts it's being run
# against will be essentially random and will vary over the course of activating and deactivating
# environments. You will have absolutely no idea what's going on as you change code or scripts and
# encounter some different code that ends up being run (some of the time). You will go slowly mad.
# No, you are best off keeping --dev on the end of these. For sure, if conda bundled its own tests
# module then we could remove --dev if we detect we are being run in that way.
dev_arg = "--dev"
activate_args = ["activate", dev_arg]
reactivate_args = ["reactivate", dev_arg]
deactivate_args = ["deactivate", dev_arg]
if on_win:
import ctypes
PYTHONIOENCODING = "cp" + str(ctypes.cdll.kernel32.GetACP())
else:
PYTHONIOENCODING = None
POP_THESE = (
"CONDA_SHLVL",
"CONDA_DEFAULT_ENV",
"CONDA_PREFIX",
"CONDA_PREFIX_0",
"CONDA_PREFIX_1",
"CONDA_PREFIX_2",
"PS1",
"prompt",
)
ENV_VARS_FILE = """
{
"version": 1,
"env_vars": {
"ENV_ONE": "one",
"ENV_TWO": "you",
"ENV_THREE": "me",
"ENV_WITH_SAME_VALUE": "with_same_value"
}
}"""
PKG_A_ENV_VARS = """
{
"PKG_A_ENV": "yerp"
}
"""
PKG_B_ENV_VARS = """
{
"PKG_B_ENV": "berp"
}
"""
HDF5_VERSION = "1.12.1"
@lru_cache(maxsize=None)
def bash_unsupported_because():
bash = which("bash")
reason = ""
if not bash:
reason = "bash: was not found on PATH"
elif on_win:
try:
output = check_output(bash + " -c " + '"uname -v"')
except CalledProcessError as exc:
reason = f"bash: something went wrong while running bash, output:\n{exc.output}\n"
else:
if b"Microsoft" in output:
reason = "bash: WSL is not yet supported. Pull requests welcome."
else:
output = check_output(bash + " --version")
if b"msys" not in output and b"cygwin" not in output:
reason = f"bash: Only MSYS2 and Cygwin bash are supported on Windows, found:\n{output}\n"
elif bash.startswith(sys.prefix):
reason = (
f"bash: MSYS2 bash installed from m2-bash in prefix {sys.prefix}.\n"
"This is unsupportable due to Git-for-Windows conflicts.\n"
"Please use upstream MSYS2 and have it on PATH. ."
)
return reason
def bash_unsupported():
return True if bash_unsupported_because() else False
def bash_unsupported_win_because():
if on_win:
return (
"You are using Windows. These tests involve setting PATH to POSIX values\n"
"but our Python is a Windows program and Windows doesn't understand POSIX values."
)
return bash_unsupported_because()
def bash_unsupported_win():
return True if bash_unsupported_win_because() else False
class ActivatorUnitTests(TestCase):
def setUp(self):
self.hold_environ = os.environ.copy()
for var in POP_THESE:
os.environ.pop(var, None)
def tearDown(self):
os.environ.clear()
os.environ.update(self.hold_environ)
def write_pkg_env_vars(self, prefix):
activate_pkg_env_vars = join(prefix, PACKAGE_ENV_VARS_DIR)
mkdir_p(activate_pkg_env_vars)
with open(join(activate_pkg_env_vars, "pkg_a.json"), "w") as f:
f.write(PKG_A_ENV_VARS)
with open(join(activate_pkg_env_vars, "pkg_b.json"), "w") as f:
f.write(PKG_B_ENV_VARS)
def test_activate_environment_not_found(self):
activator = PosixActivator()
with tempdir() as td:
with pytest.raises(EnvironmentLocationNotFound):
activator.build_activate(td)
with pytest.raises(EnvironmentLocationNotFound):
activator.build_activate("/not/an/environment")
with pytest.raises(EnvironmentNameNotFound):
activator.build_activate("wontfindmeIdontexist_abc123")
def test_wrong_args(self):
pass
def test_activate_help(self):
pass
def test_PS1(self):
with env_var(
"CONDA_CHANGEPS1", "yes", stack_callback=conda_tests_ctxt_mgmt_def_pol
):
activator = PosixActivator()
assert (
activator._prompt_modifier("/dont/matter", ROOT_ENV_NAME)
== "(%s) " % ROOT_ENV_NAME
)
instructions = activator.build_activate("base")
assert (
instructions["export_vars"]["CONDA_PROMPT_MODIFIER"]
== "(%s) " % ROOT_ENV_NAME
)
def test_PS1_no_changeps1(self):
with env_var(
"CONDA_CHANGEPS1", "no", stack_callback=conda_tests_ctxt_mgmt_def_pol
):
activator = PosixActivator()
assert activator._prompt_modifier("/dont/matter", "root") == ""
instructions = activator.build_activate("base")
assert instructions["export_vars"]["CONDA_PROMPT_MODIFIER"] == ""
def test_add_prefix_to_path_posix(self):
if on_win and "PWD" not in os.environ:
pytest.skip("This test cannot be run from the cmd.exe shell.")
activator = PosixActivator()
path_dirs = activator.path_conversion(
["/path1/bin", "/path2/bin", "/usr/local/bin", "/usr/bin", "/bin"]
)
assert len(path_dirs) == 5
test_prefix = "/usr/mytest/prefix"
added_paths = activator.path_conversion(activator._get_path_dirs(test_prefix))
if isinstance(added_paths, str):
added_paths = (added_paths,)
new_path = activator._add_prefix_to_path(test_prefix, path_dirs)
condabin_dir = activator.path_conversion(
os.path.join(context.conda_prefix, "condabin")
)
assert new_path == added_paths + (condabin_dir,) + path_dirs
@pytest.mark.skipif(not on_win, reason="windows-specific test")
def test_add_prefix_to_path_cmdexe(self):
activator = CmdExeActivator()
path_dirs = activator.path_conversion(
["C:\\path1", "C:\\Program Files\\Git\\cmd", "C:\\WINDOWS\\system32"]
)
assert len(path_dirs) == 3
test_prefix = "/usr/mytest/prefix"
added_paths = activator.path_conversion(activator._get_path_dirs(test_prefix))
if isinstance(added_paths, str):
added_paths = (added_paths,)
new_path = activator._add_prefix_to_path(test_prefix, path_dirs)
assert new_path[: len(added_paths)] == added_paths
assert new_path[-len(path_dirs) :] == path_dirs
assert len(new_path) == len(added_paths) + len(path_dirs) + 1
assert new_path[len(added_paths)].endswith("condabin")
def test_remove_prefix_from_path_1(self):
activator = PosixActivator()
original_path = tuple(activator._get_starting_path_list())
keep_path = activator.path_conversion("/keep/this/path")
final_path = (keep_path,) + original_path
final_path = activator.path_conversion(final_path)
test_prefix = join(os.getcwd(), "mytestpath")
new_paths = tuple(activator._get_path_dirs(test_prefix))
prefix_added_path = (keep_path,) + new_paths + original_path
new_path = activator._remove_prefix_from_path(test_prefix, prefix_added_path)
assert final_path == new_path
def test_remove_prefix_from_path_2(self):
# this time prefix doesn't actually exist in path
activator = PosixActivator()
original_path = tuple(activator._get_starting_path_list())
keep_path = activator.path_conversion("/keep/this/path")
final_path = (keep_path,) + original_path
final_path = activator.path_conversion(final_path)
test_prefix = join(os.getcwd(), "mytestpath")
prefix_added_path = (keep_path,) + original_path
new_path = activator._remove_prefix_from_path(test_prefix, prefix_added_path)
assert final_path == new_path
def test_replace_prefix_in_path_1(self):
activator = PosixActivator()
original_path = tuple(activator._get_starting_path_list())
new_prefix = join(os.getcwd(), "mytestpath-new")
new_paths = activator.path_conversion(activator._get_path_dirs(new_prefix))
if isinstance(new_paths, str):
new_paths = (new_paths,)
keep_path = activator.path_conversion("/keep/this/path")
final_path = (keep_path,) + new_paths + original_path
final_path = activator.path_conversion(final_path)
replace_prefix = join(os.getcwd(), "mytestpath")
replace_paths = tuple(activator._get_path_dirs(replace_prefix))
prefix_added_path = (keep_path,) + replace_paths + original_path
new_path = activator._replace_prefix_in_path(
replace_prefix, new_prefix, prefix_added_path
)
assert final_path == new_path
@pytest.mark.skipif(not on_win, reason="windows-specific test")
def test_replace_prefix_in_path_2(self):
path1 = join("c:\\", "temp", "6663 31e0")
path2 = join("c:\\", "temp", "6663 31e0", "envs", "charizard")
one_more = join("d:\\", "one", "more")
# old_prefix: c:\users\builder\appdata\local\temp\6663 31e0
# new_prefix: c:\users\builder\appdata\local\temp\6663 31e0\envs\charizard
activator = CmdExeActivator()
old_path = activator.pathsep_join(activator._add_prefix_to_path(path1))
old_path = one_more + ";" + old_path
with env_var("PATH", old_path):
activator = PosixActivator()
path_elements = activator._replace_prefix_in_path(path1, path2)
old_path = native_path_to_unix(old_path.split(";"))
assert path_elements[0] == native_path_to_unix(one_more)
assert path_elements[1] == native_path_to_unix(
next(activator._get_path_dirs(path2))
)
assert len(path_elements) == len(old_path)
def test_default_env(self):
activator = PosixActivator()
assert ROOT_ENV_NAME == activator._default_env(context.root_prefix)
with tempdir() as td:
assert td == activator._default_env(td)
p = mkdir_p(join(td, "envs", "named-env"))
assert "named-env" == activator._default_env(p)
def test_build_activate_dont_activate_unset_var(self):
with tempdir() as td:
mkdir_p(join(td, "conda-meta"))
activate_d_dir = mkdir_p(join(td, "etc", "conda", "activate.d"))
activate_d_1 = join(activate_d_dir, "see-me.sh")
activate_d_2 = join(activate_d_dir, "dont-see-me.bat")
touch(join(activate_d_1))
touch(join(activate_d_2))
env_vars_file = (
"""
{
"version": 1,
"env_vars": {
"ENV_ONE": "one",
"ENV_TWO": "you",
"ENV_THREE": "%s"
}
}"""
% CONDA_ENV_VARS_UNSET_VAR
)
activate_env_vars = join(td, PREFIX_STATE_FILE)
with open(activate_env_vars, "w") as f:
f.write(env_vars_file)
self.write_pkg_env_vars(td)
with env_var("CONDA_SHLVL", "0"):
with env_var("CONDA_PREFIX", ""):
activator = PosixActivator()
builder = activator.build_activate(td)
new_path = activator.pathsep_join(activator._add_prefix_to_path(td))
conda_prompt_modifier = "(%s) " % td
ps1 = conda_prompt_modifier + os.environ.get("PS1", "")
unset_vars = []
set_vars = {"PS1": ps1}
export_vars = {
"PATH": new_path,
"CONDA_PREFIX": td,
"CONDA_SHLVL": 1,
"CONDA_DEFAULT_ENV": td,
"CONDA_PROMPT_MODIFIER": conda_prompt_modifier,
"PKG_A_ENV": "yerp",
"PKG_B_ENV": "berp",
"ENV_ONE": "one",
"ENV_TWO": "you",
}
export_vars, unset_vars = activator.add_export_unset_vars(
export_vars, unset_vars
)
assert builder["unset_vars"] == unset_vars
assert builder["set_vars"] == set_vars
assert builder["export_vars"] == export_vars
assert builder["activate_scripts"] == (
activator.path_conversion(activate_d_1),
)
assert builder["deactivate_scripts"] == ()
def test_build_activate_shlvl_warn_clobber_vars(self):
with tempdir() as td:
mkdir_p(join(td, "conda-meta"))
activate_d_dir = mkdir_p(join(td, "etc", "conda", "activate.d"))
activate_d_1 = join(activate_d_dir, "see-me.sh")
activate_d_2 = join(activate_d_dir, "dont-see-me.bat")
touch(join(activate_d_1))
touch(join(activate_d_2))
env_vars_file = """
{
"version": 1,
"env_vars": {
"ENV_ONE": "one",
"ENV_TWO": "you",
"ENV_THREE": "me",
"PKG_A_ENV": "teamnope"
}
}"""
activate_env_vars = join(td, PREFIX_STATE_FILE)
with open(activate_env_vars, "w") as f:
f.write(env_vars_file)
self.write_pkg_env_vars(td)
with env_var("CONDA_SHLVL", "0"):
with env_var("CONDA_PREFIX", ""):
activator = PosixActivator()
builder = activator.build_activate(td)
new_path = activator.pathsep_join(activator._add_prefix_to_path(td))
conda_prompt_modifier = "(%s) " % td
ps1 = conda_prompt_modifier + os.environ.get("PS1", "")
unset_vars = []
set_vars = {"PS1": ps1}
export_vars = {
"PATH": new_path,
"CONDA_PREFIX": td,
"CONDA_SHLVL": 1,
"CONDA_DEFAULT_ENV": td,
"CONDA_PROMPT_MODIFIER": conda_prompt_modifier,
"PKG_A_ENV": "teamnope",
"PKG_B_ENV": "berp",
"ENV_ONE": "one",
"ENV_TWO": "you",
"ENV_THREE": "me",
}
export_vars, unset_vars = activator.add_export_unset_vars(
export_vars, unset_vars
)
assert builder["unset_vars"] == unset_vars
assert builder["set_vars"] == set_vars
assert builder["export_vars"] == export_vars
assert builder["activate_scripts"] == (
activator.path_conversion(activate_d_1),
)
assert builder["deactivate_scripts"] == ()
def test_build_activate_shlvl_0(self):
with tempdir() as td:
mkdir_p(join(td, "conda-meta"))
activate_d_dir = mkdir_p(join(td, "etc", "conda", "activate.d"))
activate_d_1 = join(activate_d_dir, "see-me.sh")
activate_d_2 = join(activate_d_dir, "dont-see-me.bat")
touch(join(activate_d_1))
touch(join(activate_d_2))
activate_env_vars = join(td, PREFIX_STATE_FILE)
with open(activate_env_vars, "w") as f:
f.write(ENV_VARS_FILE)
self.write_pkg_env_vars(td)
with env_var("CONDA_SHLVL", "0"):
with env_var("CONDA_PREFIX", ""):
activator = PosixActivator()
builder = activator.build_activate(td)
new_path = activator.pathsep_join(activator._add_prefix_to_path(td))
conda_prompt_modifier = "(%s) " % td
ps1 = conda_prompt_modifier + os.environ.get("PS1", "")
unset_vars = []
set_vars = {"PS1": ps1}
export_vars = {
"PATH": new_path,
"CONDA_PREFIX": td,
"CONDA_SHLVL": 1,
"CONDA_DEFAULT_ENV": td,
"CONDA_PROMPT_MODIFIER": conda_prompt_modifier,
"PKG_A_ENV": "yerp",
"PKG_B_ENV": "berp",
"ENV_ONE": "one",
"ENV_TWO": "you",
"ENV_THREE": "me",
"ENV_WITH_SAME_VALUE": "with_same_value",
}
export_vars, unset_vars = activator.add_export_unset_vars(
export_vars, unset_vars
)
assert builder["unset_vars"] == unset_vars
assert builder["set_vars"] == set_vars
assert builder["export_vars"] == export_vars
assert builder["activate_scripts"] == (
activator.path_conversion(activate_d_1),
)
assert builder["deactivate_scripts"] == ()
@pytest.mark.skipif(bash_unsupported_win(), reason=bash_unsupported_win_because())
def test_build_activate_shlvl_1(self):
with tempdir() as td:
mkdir_p(join(td, "conda-meta"))
activate_d_dir = mkdir_p(join(td, "etc", "conda", "activate.d"))
activate_d_1 = join(activate_d_dir, "see-me.sh")
activate_d_2 = join(activate_d_dir, "dont-see-me.bat")
touch(join(activate_d_1))
touch(join(activate_d_2))
activate_env_vars = join(td, PREFIX_STATE_FILE)
with open(activate_env_vars, "w") as f:
f.write(ENV_VARS_FILE)
self.write_pkg_env_vars(td)
old_prefix = "/old/prefix"
activator = PosixActivator()
old_path = activator.pathsep_join(activator._add_prefix_to_path(old_prefix))
with env_vars(
{
"CONDA_SHLVL": "1",
"CONDA_PREFIX": old_prefix,
"PATH": old_path,
"CONDA_ENV_PROMPT": "({default_env})",
},
stack_callback=conda_tests_ctxt_mgmt_def_pol,
):
activator = PosixActivator()
builder = activator.build_activate(td)
new_path = activator.pathsep_join(
activator._replace_prefix_in_path(old_prefix, td)
)
conda_prompt_modifier = "(%s)" % td
ps1 = conda_prompt_modifier + os.environ.get("PS1", "")
assert activator.path_conversion(td) in new_path
assert old_prefix not in new_path
unset_vars = []
set_vars = {"PS1": ps1}
export_vars = {
"PATH": new_path,
"CONDA_PREFIX": td,
"CONDA_SHLVL": 2,
"CONDA_DEFAULT_ENV": td,
"CONDA_PROMPT_MODIFIER": conda_prompt_modifier,
"PKG_A_ENV": "yerp",
"PKG_B_ENV": "berp",
"ENV_ONE": "one",
"ENV_TWO": "you",
"ENV_THREE": "me",
"ENV_WITH_SAME_VALUE": "with_same_value",
}
export_vars, _ = activator.add_export_unset_vars(export_vars, None)
export_vars["CONDA_PREFIX_1"] = old_prefix
export_vars, unset_vars = activator.add_export_unset_vars(
export_vars, unset_vars
)
assert builder["unset_vars"] == unset_vars
assert builder["set_vars"] == set_vars
assert builder["export_vars"] == export_vars
assert builder["activate_scripts"] == (
activator.path_conversion(activate_d_1),
)
assert builder["deactivate_scripts"] == ()
with env_vars(
{
"PATH": new_path,
"CONDA_PREFIX": td,
"CONDA_PREFIX_1": old_prefix,
"CONDA_SHLVL": 2,
"CONDA_DEFAULT_ENV": td,
"CONDA_PROMPT_MODIFIER": conda_prompt_modifier,
"PKG_B_ENV": "berp",
"PKG_A_ENV": "yerp",
"ENV_ONE": "one",
"ENV_TWO": "you",
"ENV_THREE": "me",
"ENV_WITH_SAME_VALUE": "with_same_value",
}
):
activator = PosixActivator()
builder = activator.build_deactivate()
unset_vars = [
"CONDA_PREFIX_1",
"PKG_A_ENV",
"PKG_B_ENV",
"ENV_ONE",
"ENV_TWO",
"ENV_THREE",
"ENV_WITH_SAME_VALUE",
]
assert builder["set_vars"] == {
"PS1": "(/old/prefix)",
}
export_vars = {
"CONDA_PREFIX": old_prefix,
"CONDA_SHLVL": 1,
"CONDA_DEFAULT_ENV": old_prefix,
"CONDA_PROMPT_MODIFIER": "(%s)" % old_prefix,
}
export_path = {
"PATH": old_path,
}
export_vars, unset_vars = activator.add_export_unset_vars(
export_vars, unset_vars
)
assert builder["unset_vars"] == unset_vars
assert builder["export_vars"] == export_vars
assert builder["export_path"] == export_path
assert builder["activate_scripts"] == ()
assert builder["deactivate_scripts"] == ()
@pytest.mark.skipif(bash_unsupported_win(), reason=bash_unsupported_win_because())
def test_build_stack_shlvl_1(self):
with tempdir() as td:
mkdir_p(join(td, "conda-meta"))
activate_d_dir = mkdir_p(join(td, "etc", "conda", "activate.d"))
activate_d_1 = join(activate_d_dir, "see-me.sh")
activate_d_2 = join(activate_d_dir, "dont-see-me.bat")
touch(join(activate_d_1))
touch(join(activate_d_2))
activate_env_vars = join(td, PREFIX_STATE_FILE)
with open(activate_env_vars, "w") as f:
f.write(ENV_VARS_FILE)
self.write_pkg_env_vars(td)
old_prefix = "/old/prefix"
activator = PosixActivator()
old_path = activator.pathsep_join(activator._add_prefix_to_path(old_prefix))
with env_vars(
{
"CONDA_SHLVL": "1",
"CONDA_PREFIX": old_prefix,
"PATH": old_path,
"CONDA_ENV_PROMPT": "({default_env})",
},
stack_callback=conda_tests_ctxt_mgmt_def_pol,
):
activator = PosixActivator()
builder = activator.build_stack(td)
new_path = activator.pathsep_join(activator._add_prefix_to_path(td))
conda_prompt_modifier = "(%s)" % td
ps1 = conda_prompt_modifier + os.environ.get("PS1", "")
assert td in new_path
assert old_prefix in new_path
set_vars = {"PS1": ps1}
export_vars = {
"PATH": new_path,
"CONDA_PREFIX": td,
"CONDA_SHLVL": 2,
"CONDA_DEFAULT_ENV": td,
"CONDA_PROMPT_MODIFIER": conda_prompt_modifier,
"PKG_A_ENV": "yerp",
"PKG_B_ENV": "berp",
"ENV_ONE": "one",
"ENV_TWO": "you",
"ENV_THREE": "me",
"ENV_WITH_SAME_VALUE": "with_same_value",
}
export_vars, unset_vars = activator.add_export_unset_vars(
export_vars, []
)
export_vars["CONDA_PREFIX_1"] = old_prefix
export_vars["CONDA_STACKED_2"] = "true"
assert builder["unset_vars"] == unset_vars
assert builder["set_vars"] == set_vars
assert builder["export_vars"] == export_vars
assert builder["activate_scripts"] == (
activator.path_conversion(activate_d_1),
)
assert builder["deactivate_scripts"] == ()
with env_vars(
{
"PATH": new_path,
"CONDA_PREFIX": td,
"CONDA_PREFIX_1": old_prefix,
"CONDA_SHLVL": 2,
"CONDA_DEFAULT_ENV": td,
"CONDA_PROMPT_MODIFIER": conda_prompt_modifier,
"CONDA_STACKED_2": "true",
"PKG_A_ENV": "yerp",
"PKG_B_ENV": "berp",
"ENV_ONE": "one",
"ENV_TWO": "you",
"ENV_THREE": "me",
}
):
activator = PosixActivator()
builder = activator.build_deactivate()
unset_vars = [
"CONDA_PREFIX_1",
"CONDA_STACKED_2",
"PKG_A_ENV",
"PKG_B_ENV",
"ENV_ONE",
"ENV_TWO",
"ENV_THREE",
"ENV_WITH_SAME_VALUE",
]
assert builder["set_vars"] == {
"PS1": "(/old/prefix)",
}
export_vars = {
"CONDA_PREFIX": old_prefix,
"CONDA_SHLVL": 1,
"CONDA_DEFAULT_ENV": old_prefix,
"CONDA_PROMPT_MODIFIER": f"({old_prefix})",
}
export_vars, unset_vars = activator.add_export_unset_vars(
export_vars, unset_vars
)
assert builder["unset_vars"] == unset_vars
assert builder["export_vars"] == export_vars
assert builder["activate_scripts"] == ()
assert builder["deactivate_scripts"] == ()
def test_activate_same_environment(self):
with tempdir() as td:
mkdir_p(join(td, "conda-meta"))
activate_d_dir = mkdir_p(join(td, "etc", "conda", "activate.d"))
activate_d_1 = join(activate_d_dir, "see-me.sh")
activate_d_2 = join(activate_d_dir, "dont-see-me.bat")
touch(join(activate_d_1))
touch(join(activate_d_2))
old_prefix = td
deactivate_d_dir = mkdir_p(join(old_prefix, "etc", "conda", "deactivate.d"))
deactivate_d_1 = join(deactivate_d_dir, "see-me.sh")
deactivate_d_2 = join(deactivate_d_dir, "dont-see-me.bat")
touch(join(deactivate_d_1))
touch(join(deactivate_d_2))
with env_var("CONDA_SHLVL", "1"):
with env_var("CONDA_PREFIX", old_prefix):
activator = PosixActivator()
builder = activator.build_activate(td)
new_path_parts = activator._replace_prefix_in_path(
old_prefix, old_prefix
)
conda_prompt_modifier = "(%s) " % old_prefix
ps1 = conda_prompt_modifier + os.environ.get("PS1", "")
set_vars = {"PS1": ps1}
export_vars = {
"PATH": activator.pathsep_join(new_path_parts),
"CONDA_SHLVL": 1,
"CONDA_PROMPT_MODIFIER": "(%s) " % td,
}
assert builder["unset_vars"] == ()
assert builder["set_vars"] == set_vars
assert builder["export_vars"] == export_vars
assert builder["activate_scripts"] == (
activator.path_conversion(activate_d_1),
)
assert builder["deactivate_scripts"] == (
activator.path_conversion(deactivate_d_1),
)
@pytest.mark.skipif(bash_unsupported_win(), reason=bash_unsupported_win_because())
def test_build_deactivate_shlvl_2_from_stack(self):
with tempdir() as td:
mkdir_p(join(td, "conda-meta"))
deactivate_d_dir = mkdir_p(join(td, "etc", "conda", "deactivate.d"))
deactivate_d_1 = join(deactivate_d_dir, "see-me-deactivate.sh")
deactivate_d_2 = join(deactivate_d_dir, "dont-see-me.bat")
touch(join(deactivate_d_1))
touch(join(deactivate_d_2))
activate_env_vars = join(td, PREFIX_STATE_FILE)
with open(activate_env_vars, "w") as f:
f.write(ENV_VARS_FILE)
activate_pkg_env_vars_a = join(td, PACKAGE_ENV_VARS_DIR)
mkdir_p(activate_pkg_env_vars_a)
with open(join(activate_pkg_env_vars_a, "pkg_a.json"), "w") as f:
f.write(PKG_A_ENV_VARS)
old_prefix = join(td, "old")
mkdir_p(join(old_prefix, "conda-meta"))
activate_d_dir = mkdir_p(join(old_prefix, "etc", "conda", "activate.d"))
activate_d_1 = join(activate_d_dir, "see-me-activate.sh")
activate_d_2 = join(activate_d_dir, "dont-see-me.bat")
touch(join(activate_d_1))
touch(join(activate_d_2))
activate_env_vars_old = join(old_prefix, PREFIX_STATE_FILE)
with open(activate_env_vars_old, "w") as f:
f.write(
"""
{
"version": 1,
"env_vars": {
"ENV_FOUR": "roar",
"ENV_FIVE": "hive"
}
}
"""
)
activate_pkg_env_vars_b = join(old_prefix, PACKAGE_ENV_VARS_DIR)
mkdir_p(activate_pkg_env_vars_b)
with open(join(activate_pkg_env_vars_b, "pkg_b.json"), "w") as f:
f.write(PKG_B_ENV_VARS)
activator = PosixActivator()
original_path = activator.pathsep_join(
activator._add_prefix_to_path(old_prefix)
)
with env_var("PATH", original_path):
activator = PosixActivator()
starting_path = activator.pathsep_join(
activator._add_prefix_to_path(td)
)
with env_vars(
{
"CONDA_SHLVL": "2",
"CONDA_PREFIX_1": old_prefix,
"CONDA_PREFIX": td,
"CONDA_STACKED_2": "true",
"PATH": starting_path,
"ENV_ONE": "one",
"ENV_TWO": "you",
"ENV_THREE": "me",
"ENV_FOUR": "roar",
"ENV_FIVE": "hive",
"PKG_A_ENV": "yerp",
"PKG_B_ENV": "berp",
},
stack_callback=conda_tests_ctxt_mgmt_def_pol,
):
activator = PosixActivator()
builder = activator.build_deactivate()
unset_vars = [
"CONDA_PREFIX_1",
"CONDA_STACKED_2",
"PKG_A_ENV",
"ENV_ONE",
"ENV_TWO",
"ENV_THREE",
"ENV_WITH_SAME_VALUE",
]
conda_prompt_modifier = "(%s) " % old_prefix
ps1 = conda_prompt_modifier + os.environ.get("PS1", "")
set_vars = {"PS1": ps1}
export_vars = {
"CONDA_PREFIX": old_prefix,
"CONDA_SHLVL": 1,
"CONDA_DEFAULT_ENV": old_prefix,
"CONDA_PROMPT_MODIFIER": conda_prompt_modifier,
"PKG_B_ENV": "berp",
"ENV_FOUR": "roar",
"ENV_FIVE": "hive",
}
export_path = {
"PATH": original_path,
}
export_vars, unset_vars = activator.add_export_unset_vars(
export_vars, unset_vars
)
assert builder["unset_vars"] == unset_vars
assert builder["set_vars"] == set_vars
assert builder["export_vars"] == export_vars
assert builder["export_path"] == export_path
assert builder["activate_scripts"] == (
activator.path_conversion(activate_d_1),
)
assert builder["deactivate_scripts"] == (
activator.path_conversion(deactivate_d_1),
)
@pytest.mark.skipif(bash_unsupported_win(), reason=bash_unsupported_win_because())
def test_build_deactivate_shlvl_2_from_activate(self):
with tempdir() as td:
mkdir_p(join(td, "conda-meta"))
deactivate_d_dir = mkdir_p(join(td, "etc", "conda", "deactivate.d"))
deactivate_d_1 = join(deactivate_d_dir, "see-me-deactivate.sh")
deactivate_d_2 = join(deactivate_d_dir, "dont-see-me.bat")
touch(join(deactivate_d_1))
touch(join(deactivate_d_2))
activate_env_vars = join(td, PREFIX_STATE_FILE)
with open(activate_env_vars, "w") as f:
f.write(ENV_VARS_FILE)
activate_pkg_env_vars_a = join(td, PACKAGE_ENV_VARS_DIR)
mkdir_p(activate_pkg_env_vars_a)
with open(join(activate_pkg_env_vars_a, "pkg_a.json"), "w") as f:
f.write(PKG_A_ENV_VARS)
old_prefix = join(td, "old")
mkdir_p(join(old_prefix, "conda-meta"))
activate_d_dir = mkdir_p(join(old_prefix, "etc", "conda", "activate.d"))
activate_d_1 = join(activate_d_dir, "see-me-activate.sh")
activate_d_2 = join(activate_d_dir, "dont-see-me.bat")
touch(join(activate_d_1))
touch(join(activate_d_2))
activate_env_vars_old = join(old_prefix, PREFIX_STATE_FILE)
with open(activate_env_vars_old, "w") as f:
f.write(
"""
{
"version": 1,
"env_vars": {
"ENV_FOUR": "roar",
"ENV_FIVE": "hive"
}
}
"""
)
activate_pkg_env_vars_b = join(old_prefix, PACKAGE_ENV_VARS_DIR)
mkdir_p(activate_pkg_env_vars_b)
with open(join(activate_pkg_env_vars_b, "pkg_b.json"), "w") as f:
f.write(PKG_B_ENV_VARS)
activator = PosixActivator()
original_path = activator.pathsep_join(
activator._add_prefix_to_path(old_prefix)
)
new_path = activator.pathsep_join(activator._add_prefix_to_path(td))
with env_vars(
{
"CONDA_SHLVL": "2",
"CONDA_PREFIX_1": old_prefix,
"CONDA_PREFIX": td,
"PATH": new_path,
"ENV_ONE": "one",
"ENV_TWO": "you",
"ENV_THREE": "me",
"PKG_A_ENV": "yerp",
"PKG_B_ENV": "berp",
},
stack_callback=conda_tests_ctxt_mgmt_def_pol,
):
activator = PosixActivator()
builder = activator.build_deactivate()
unset_vars = [
"CONDA_PREFIX_1",
"PKG_A_ENV",
"ENV_ONE",
"ENV_TWO",
"ENV_THREE",
"ENV_WITH_SAME_VALUE",
]
conda_prompt_modifier = "(%s) " % old_prefix
ps1 = conda_prompt_modifier + os.environ.get("PS1", "")
set_vars = {"PS1": ps1}
export_vars = {
"CONDA_PREFIX": old_prefix,
"CONDA_SHLVL": 1,
"CONDA_DEFAULT_ENV": old_prefix,
"CONDA_PROMPT_MODIFIER": conda_prompt_modifier,
"PKG_B_ENV": "berp",
"ENV_FOUR": "roar",
"ENV_FIVE": "hive",
}
export_path = {
"PATH": original_path,
}
export_vars, unset_vars = activator.add_export_unset_vars(
export_vars, unset_vars
)
assert builder["unset_vars"] == unset_vars
assert builder["set_vars"] == set_vars
assert builder["export_vars"] == export_vars
assert builder["export_path"] == export_path
assert builder["activate_scripts"] == (
activator.path_conversion(activate_d_1),