def _format_vars(shell): shelldict = shells[shell] base_path, _ = run_in(shelldict['printpath'], shell) # windows forces Library/bin onto PATH when starting up. Strip it for the purposes of this test. if sys.platform == "win32": base_path = strip_leading_library_bin(base_path, shelldict) raw_ps, _ = run_in(shelldict["printps1"], shell) command_setup = """\ {set} PYTHONPATH="{PYTHONPATH}" {set} CONDARC= {set} CONDA_PATH_BACKUP= """.format(here=dirname(__file__), PYTHONPATH=shelldict['path_to'](PYTHONPATH), set=shelldict["set_var"]) if shelldict["shell_suffix"] == '.bat': command_setup = "@echo off\n" + command_setup return { 'echo': shelldict['echo'], 'nul': shelldict['nul'], 'printpath': shelldict['printpath'], 'printdefaultenv': shelldict['printdefaultenv'], 'printps1': shelldict['printps1'], 'raw_ps': raw_ps, 'set_var': shelldict['set_var'], 'source': shelldict['source_setup'], 'binpath': shelldict['binpath'], 'shell_suffix': shelldict['shell_suffix'], 'syspath': shelldict['path_to'](sys.prefix), 'binpath': shelldict['binpath'], 'command_setup': command_setup, 'base_path': base_path, }
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) for path in ["activate", "deactivate"]: dir = os.path.join(shells[shell]['path_from'](env_dirs[0]), "etc", "conda", "%s.d" % path) os.makedirs(dir) file = os.path.join(dir, "test" + shells[shell]["env_script_suffix"]) setting = "test" if path == "activate" else "" with open(file, 'w') as f: f.write(shells[shell]["set_var"] + "TEST_VAR=%s\n" % setting) 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 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) # period here is because when var is blank, windows prints out the current echo setting. assert_equals(stdout, u'.', stderr)
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, shelldict=shells[shell])), envpaths1=shells[shell]["path_to"](pathlist_to_str(_envpaths(envs, 'test 1', shelldict=shells[shell]))))) 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 = os.path.join(env, where, f + shells[shell]["shell_suffix"]) # must translate path to windows representation for Python's sake file_path = shells[shell]["path_from"](file_path) assert (os.path.lexists(file_path)) else: file_path = os.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=os.path.join(sys.prefix, where, f))) if platform != 'win': # Test activate when there are no write permissions in the # env. prefix_bin_path = os.path.join( gen_test_env_paths(envs, shell)[2], 'bin') commands = (shell_vars['command_setup'] + """ mkdir -p "{prefix_bin_path}" chmod 444 "{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("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_activate_relative_path(shell): """ current directory should be searched for environments """ shell_vars = _format_vars(shell) with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs: env_dirs = gen_test_env_paths(envs, shell) env_dir = os.path.basename(env_dirs[0]) work_dir = os.path.dirname(env_dir) commands = (shell_vars['command_setup'] + """ cd {work_dir} {source} "{syspath}{binpath}activate" "{env_dir}" {printdefaultenv} """).format(work_dir=envs, envs=envs, env_dir=env_dir, **shell_vars) cwd = os.getcwd() # this is not effective for running bash on windows. It starts # in your home dir no matter what. That's what the cd is for above. os.chdir(envs) try: stdout, stderr = run_in(commands, shell, cwd=envs) except: raise finally: os.chdir(cwd) assert_equals(stdout.rstrip(), env_dirs[0], stderr)
def test_activate_relative_path(shell): """ current directory should be searched for environments """ shell_vars = _format_vars(shell) with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs: env_dirs = gen_test_env_paths(envs, shell) env_dir = os.path.basename(env_dirs[0]) work_dir = os.path.dirname(env_dir) commands = (shell_vars['command_setup'] + """ cd {work_dir} {source} "{syspath}{binpath}activate" "{env_dir}" {printdefaultenv} """).format(work_dir=envs, envs=envs, env_dir=env_dir, **shell_vars) cwd = os.getcwd() # this is not effective for running bash on windows. It starts # in your home dir no matter what. That's what the cd is for above. os.chdir(envs) try: stdout, stderr = run_in(commands, shell, cwd=envs) except: raise finally: os.chdir(cwd) assert_equals(stdout.rstrip(), env_dir, stderr)
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, shelldict=shells[shell])), envpaths1=shells[shell]["path_to"](pathlist_to_str(_envpaths(envs, 'test 1', shelldict=shells[shell]))))) 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 = os.path.join(env, where, f + shells[shell]["shell_suffix"]) # must translate path to windows representation for Python's sake file_path = shells[shell]["path_from"](file_path) assert(os.path.lexists(file_path)) else: file_path = os.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=os.path.join(sys.prefix, where, f))) if platform != 'win': # Test activate when there are no write permissions in the # env. prefix_bin_path = os.path.join(gen_test_env_paths(envs, shell)[2], 'bin') commands = (shell_vars['command_setup'] + """ mkdir -p "{prefix_bin_path}" chmod 444 "{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("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 get_path(shelldict): """Get path using a subprocess call. os.getenv path isn't good for us, since bash on windows has a wildly different path from Windows. This returns PATH in the native representation of the shell - not necessarily the native representation of the platform """ return run_in(shelldict["printpath"], shelldict)[0]
def test_activate_non_ascii_char_in_path(shell): 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)
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]['pathsep'].join(_envpaths(root_dir, shelldict=shells[shell])), stdout, stderr) 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) stdout = strip_leading_library_bin(stdout, shells[shell]) assert_equals(stdout, u"%s" % shell_vars['base_path'], stderr)
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)
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(shells[shell]['pathsep'].join(_envpaths(envs, 'test 1', shelldict=shells[shell])),stdout)
def test_activate_help(shell): shell_vars = _format_vars(shell) with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs: if shell not in ['powershell.exe', 'cmd.exe']: 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 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 shell in ["cmd.exe", "powershell"]: assert_in("Usage: deactivate", stderr) else: assert_in("Usage: source deactivate", stderr)
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) stdout = strip_leading_library_bin(stdout, shells[shell]) assert_equals(stdout, u"%s" % shell_vars['base_path'])
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) stdout = strip_leading_library_bin(stdout, shells[shell]) assert_equals(stderr, u'Error: did not expect more than one argument.\n (got two args)') assert_equals(stdout, shell_vars['base_path'], stderr)
def test_activate_keeps_PATH_order(shell): if sys.platform != "win32" or shell != "cmd.exe": pytest.xfail("test only implemented for cmd.exe on win") shell_vars = _format_vars(shell) with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs: commands = (shell_vars['command_setup'] + """ @set "PATH=somepath;CONDA_PATH_PLACEHOLDER;%PATH%" @call "{syspath}{binpath}activate.bat" {printpath} """).format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars) stdout, stderr = run_in(commands, shell) assert stdout.startswith("somepath;" + sys.prefix)
def test_deactivate_placeholder(shell): if not on_win or shell != "cmd.exe": pytest.xfail("test only implemented for cmd.exe on win") shell_vars = _format_vars(shell) with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs: commands = (shell_vars['command_setup'] + """ @set "PATH=flag;%PATH%" @call "{syspath}{binpath}activate.bat" @call "{syspath}{binpath}deactivate.bat" "hold" {printpath} """).format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars) stdout, stderr = run_in(commands, shell) assert stdout.startswith("CONDA_PATH_PLACEHOLDER;flag")
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: env_dirs=gen_test_env_paths(envs, shell) 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=env_dirs, **shell_vars) stdout, stderr = run_in(commands, shell) # rstrip on output is because the printing to console picks up an extra space assert_equals(stdout.rstrip(), env_dirs[1], stderr)
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: env_dirs = gen_test_env_paths(envs, shell) 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=env_dirs, **shell_vars) stdout, stderr = run_in(commands, shell) # rstrip on output is because the printing to console picks up an extra space assert_equals(stdout.rstrip(), env_dirs[1], stderr)
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, 'test 2', shelldict=shells[shell])))) assert_in(shells[shell]['pathsep'].join(_envpaths(envs, 'test 2', shelldict=shells[shell])), stdout)
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)
def test_activate_bad_directory(shell): shell_vars = _format_vars(shell) with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs: env_dirs = gen_test_env_paths(envs, shell) # Strange semicolons are here to defeat MSYS' automatic path conversion. # See http://www.mingw.org/wiki/Posix_path_conversion commands = (shell_vars['command_setup'] + """ {source} "{syspath}{binpath}activate" "{env_dirs[2]}" {printpath} """).format(envs=envs, env_dirs=env_dirs, **shell_vars) stdout, stderr = run_in(commands, shell) # another semicolon here for comparison reasons with one above. assert 'could not find environment' in stderr assert_not_in(env_dirs[2], stdout)
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, 'test 1', shelldict=shells[shell]), escape_backslashes=True)), shell) assert_in(shells[shell]['pathsep'].join(_envpaths(envs, 'test 1', shelldict=shells[shell])), stdout, shell)
def test_PS1_no_changeps1(shell, bash_profile): """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: rc_file = os.path.join(envs, ".condarc") with open(rc_file, 'w') as f: f.write("changeps1: False\n") condarc = "{set_var}CONDARC=%s\n" % rc_file commands = (shell_vars['command_setup'] + condarc + """ {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, 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(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(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(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(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(envs=envs, **shell_vars) stdout, stderr = run_in(commands, shell) assert_equals(stdout, shell_vars['raw_ps'], stderr)
def test_activate_relative_path(shell): """ current directory should be searched for environments """ shell_vars = _format_vars(shell) with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs: env_dirs = gen_test_env_paths(envs, shell) 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) cwd = os.getcwd() os.chdir(envs) try: stdout, stderr = run_in(commands, shell, cwd=envs) finally: os.chdir(cwd) assert_equals(stdout.rstrip(), env_dirs[0], stderr)
def test_PS1(shell, bash_profile): 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), raw_ps=shell_vars["raw_ps"], number=0), stderr) # second activate replaces earlier activated 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), raw_ps=shell_vars["raw_ps"], 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), raw_ps=shell_vars["raw_ps"], 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)
def test_CONDA_DEFAULT_ENV(shell): shell_vars = _format_vars(shell) shelldict = shells[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}{binpath}activate" "{env_dirs[0]}" {printdefaultenv} """).format(envs=envs, env_dirs=env_dirs, **shell_vars) stdout, stderr = run_in(commands, shell) assert_equals(stdout.rstrip(), env_dirs[0], 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=env_dirs, **shell_vars) stdout, stderr = run_in(commands, shell) assert_equals(stdout.rstrip(), env_dirs[1], 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.rstrip(), env_dirs[0], 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.rstrip(), shells[shell]['path_to'](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)
def main(): from conda.config import root_env_name, root_dir, changeps1 import conda.install if '-h' in sys.argv or '--help' in sys.argv: help(sys.argv[1]) path = None shell = find_parent_shell(path=False) shelldict = shells[shell] if sys.argv[1] == '..activate': path = get_path(shelldict) if len(sys.argv) == 2 or sys.argv[2].lower() == root_env_name.lower(): binpath = binpath_from_arg(root_env_name, shelldict=shelldict) rootpath = None elif len(sys.argv) == 3: binpath = binpath_from_arg(sys.argv[2], shelldict=shelldict) rootpath = binpath_from_arg(root_env_name, shelldict=shelldict) else: sys.exit("Error: did not expect more than one argument") pathlist_str = pathlist_to_str(binpath) sys.stderr.write("prepending %s to PATH\n" % shelldict['path_to'](pathlist_str)) # Clear the root path if it is present if rootpath: path = path.replace(shelldict['pathsep'].join(rootpath), "") # prepend our new entries onto the existing path and make sure that the separator is native path = shelldict['pathsep'].join(binpath + [ path, ]) # deactivation is handled completely in shell scripts - it restores backups of env variables. # It is done in shell scripts because they handle state much better than we can here. elif sys.argv[1] == '..checkenv': if len(sys.argv) < 3: sys.argv.append(root_env_name) if len(sys.argv) > 3: sys.exit("Error: did not expect more than one argument.") if sys.argv[2].lower() == root_env_name.lower(): # no need to check root env and try to install a symlink there sys.exit(0) # this should throw an error and exit if the env or path can't be found. try: binpath = binpath_from_arg(sys.argv[2], shelldict=shelldict) except ValueError as e: sys.exit(getattr(e, 'message', e)) # Make sure an env always has the conda symlink try: conda.install.symlink_conda(shelldict['path_from'](binpath[0]), root_dir, shell) except (IOError, OSError) as e: if e.errno == errno.EPERM or e.errno == errno.EACCES: msg = ( "Cannot activate environment {0}, not have write access to conda symlink" .format(sys.argv[2])) sys.exit(msg) raise sys.exit(0) elif sys.argv[1] == '..setps1': # path is a bit of a misnomer here. It is the prompt setting. However, it is returned # below by printing. That is why it is named "path" # DO NOT use os.getenv for this. One Windows especially, it shows cmd.exe settings # for bash shells. This method uses the shell directly. path, _ = run_in(shelldict['printps1'], shelldict, env=os.environ.copy()) # failsafes if not path: if shelldict['exe'] == 'cmd.exe': path = '$P$G' # strip off previous prefix, if any: path = re.sub(".*\(\(.*\)\)\ ", "", path, count=1) env_path = sys.argv[2] if changeps1 and env_path: path = "(({})) {}".format(os.path.split(env_path)[-1], path) else: # This means there is a bug in main.py raise ValueError("unexpected command") # This print is actually what sets the PATH or PROMPT variable. The shell # script gets this value, and finishes the job. print(path)
def main(): from conda.config import root_env_name, root_dir, changeps1 import conda.install if '-h' in sys.argv or '--help' in sys.argv: help(sys.argv[1]) path = None shell = find_parent_shell(path=False) shelldict = shells[shell] if sys.argv[1] == '..activate': path = get_path(shelldict) if len(sys.argv) == 2 or sys.argv[2].lower() == root_env_name.lower(): binpath = binpath_from_arg(root_env_name, shelldict=shelldict) rootpath = None elif len(sys.argv) == 3: binpath = binpath_from_arg(sys.argv[2], shelldict=shelldict) rootpath = binpath_from_arg(root_env_name, shelldict=shelldict) else: sys.exit("Error: did not expect more than one argument") pathlist_str = pathlist_to_str(binpath) sys.stderr.write("prepending %s to PATH\n" % shelldict['path_to'](pathlist_str)) # Clear the root path if it is present if rootpath: path = path.replace(shelldict['pathsep'].join(rootpath), "") # prepend our new entries onto the existing path and make sure that the separator is native path = shelldict['pathsep'].join(binpath + [path, ]) # deactivation is handled completely in shell scripts - it restores backups of env variables. # It is done in shell scripts because they handle state much better than we can here. elif sys.argv[1] == '..checkenv': if len(sys.argv) < 3: sys.argv.append(root_env_name) if len(sys.argv) > 3: sys.exit("Error: did not expect more than one argument.") if sys.argv[2].lower() == root_env_name.lower(): # no need to check root env and try to install a symlink there sys.exit(0) # this should throw an error and exit if the env or path can't be found. try: binpath = binpath_from_arg(sys.argv[2], shelldict=shelldict) except ValueError as e: sys.exit(getattr(e, 'message', e)) # Make sure an env always has the conda symlink try: conda.install.symlink_conda(shelldict['path_from'](binpath[0]), root_dir, shell) except (IOError, OSError) as e: if e.errno == errno.EPERM or e.errno == errno.EACCES: msg = ("Cannot activate environment {0}, not have write access to conda symlink" .format(sys.argv[2])) sys.exit(msg) raise sys.exit(0) elif sys.argv[1] == '..setps1': # path is a bit of a misnomer here. It is the prompt setting. However, it is returned # below by printing. That is why it is named "path" # DO NOT use os.getenv for this. One Windows especially, it shows cmd.exe settings # for bash shells. This method uses the shell directly. path, _ = run_in(shelldict['printps1'], shelldict, env=os.environ.copy()) # failsafes if not path: if shelldict['exe'] == 'cmd.exe': path = '$P$G' # strip off previous prefix, if any: path = re.sub(".*\(\(.*\)\)\ ", "", path, count=1) env_path = sys.argv[2] if changeps1 and env_path: path = "(({})) {}".format(os.path.split(env_path)[-1], path) else: # This means there is a bug in main.py raise ValueError("unexpected command") # This print is actually what sets the PATH or PROMPT variable. The shell # script gets this value, and finishes the job. print(path)
def test_PS1(shell, bash_profile): 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), raw_ps=shell_vars["raw_ps"], number=0), stderr) # second activate replaces earlier activated 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), raw_ps=shell_vars["raw_ps"], 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), raw_ps=shell_vars["raw_ps"], 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)
def test_CONDA_DEFAULT_ENV(shell): 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}{binpath}activate" "{env_dirs[0]}" {printdefaultenv} """).format(envs=envs, env_dirs=env_dirs, **shell_vars) stdout, stderr = run_in(commands, shell) assert_equals(stdout.rstrip(), env_dirs[0], 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=env_dirs, **shell_vars) stdout, stderr = run_in(commands, shell) assert_equals(stdout.rstrip(), env_dirs[1], 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.rstrip(), env_dirs[0], 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.rstrip(), 'root', 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)