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_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_symlinks_created_with_env(): bindir = 'Scripts' if on_win else 'bin' with TemporaryDirectory() as tmp: check_call( ["conda", "create", '-y', '-p', join(tmp, 'conda'), "python=2.7"]) assert isfile(join(tmp, 'conda', bindir, 'activate')) assert isfile(join(tmp, 'conda', bindir, 'deactivate')) assert isfile(join(tmp, 'conda', bindir, 'conda')) if on_win: assert isfile(join(tmp, 'conda', bindir, 'activate.bat')) assert isfile(join(tmp, 'conda', bindir, 'deactivate.bat')) assert isfile(join(tmp, 'conda', bindir, 'conda.bat'))
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 not on_win 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_test1_test3(): for shell in shells: with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs: activate, deactivate, conda = _write_entry_points(envs) commands = (command_setup + """ source {activate} {envs}/test1 2> /dev/null source {activate} {envs}/test3 printf $PATH """).format(envs=envs, activate=activate) stdout, stderr = run_in(commands, shell) assert stdout == envs + "/test1/bin:" + PATH assert stderr == 'Error: no such directory: {envs}/test3/bin\n'.format( envs=envs)
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_PREFIX, dir=dirname(__file__)) as envs: commands = (shell_vars['command_setup'] + """ {source} "{syspath}{binpath}activate" "{env_dirs[0]}" {source} deactivate {printdefaultenv} """).format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars) stdout, stderr = run_in(commands, shell) assert not stderr assert_equals(stdout, u'', stderr)
def test_activate_test1_test2(): for shell in shells: with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs: activate, deactivate, conda = write_entry_points(envs) commands = (command_setup + """ source {activate} {envs}/test1 2> /dev/null source {activate} {envs}/test2 printf $PATH """).format(envs=envs, activate=activate) stdout, stderr = run_in(commands, shell) assert stdout == envs + "/test2/bin:" + PATH assert stderr == 'discarding {envs}/test1/bin from PATH\nprepending {envs}/test2/bin to PATH\n'.format( envs=envs)
def test_wrong_args(shell): if shell == 'bash.exe': pytest.skip("usage of cygpath in win_path_to_unix messes this test up") shell_vars = _format_vars(shell) with TemporaryDirectory(prefix=ENVS_PREFIX, 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_in("activate does not accept more than one argument", stderr) assert_equals(stdout, shell_vars['base_path'], stderr)
def test_activate_keeps_PATH_order(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=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_skeleton_pypi(): """published in docs at http://conda.pydata.org/docs/build_tutorials/pkgs.html""" cwd = os.getcwd() with TemporaryDirectory() as tmp: os.chdir(tmp) try: cmd = 'conda skeleton pypi pyinstrument' subprocess.check_call(cmd.split()) cmd = 'conda build pyinstrument' subprocess.check_call(cmd.split()) except: raise finally: os.chdir(cwd)
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_PREFIX, 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} activate "{env_dirs[1]}" {printdefaultenv} """).format(envs=envs, env_dirs=env_dirs, **shell_vars) stdout, stderr = run_in(commands, shell) assert not stderr # rstrip on output is because the printing to console picks up an extra space assert_equals(stdout.rstrip(), make_win_ok(env_dirs[1]), stderr)
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)
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))
def test_activate_test1(shell): shell_vars = _format_vars(shell) with TemporaryDirectory(prefix=ENVS_PREFIX, 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_in( shells[shell]['pathsep'].join(_envpaths(envs, 'test 1', shell)), stdout, shell)
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_PREFIX, 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_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_in( shells[shell]['pathsep'].join( _envpaths(envs, 'test 2', shelldict=shells[shell])), stdout)
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_bad_directory(): for shell in shells: with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs: commands = (command_setup + """ {source} {syspath}{binpath}activate "{env_dirs[2]}" {printpath} """).format(envs=envs, env_dirs=gen_test_env_paths(envs), **_format_vars) stdout, stderr = run_in(commands, shell) assert_equals(stdout, u"%s" % BASE_PATH) assert_equals( stderr, u'Error: no such directory: {envpaths3}'.format( envpaths3=_envpaths(envs, 'test3')[0]))
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}{cmd_path}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)
def test_activate_deactivate(): for shell in shells: with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs: commands = (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), **_format_vars) stdout, stderr = run_in(commands, shell) assert_equals(stdout, u"%s" % BASE_PATH) assert_equals(stderr, u'discarding {envpaths1} from PATH'\ .format(envpaths1=pathlist_to_str(_envpaths(envs, 'test1'))))
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}{cmd_path}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)
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}{cmd_path}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]))
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} "{syspath}{binpath}deactivate" {printdefaultenv}. """).format(envs=envs, env_dirs=gen_test_env_paths(envs, shell), **shell_vars) stdout, stderr = run_in(commands, shell) assert not stderr if shell == 'cmd.exe': assert_equals(stdout, u'', stderr) else: assert_equals(stdout, u'.', stderr)
def test_no_anaconda_upload_condarc(service_name): with TemporaryDirectory() as tmp: rcfile = os.path.join(tmp, ".condarc") with open(rcfile, 'w') as f: f.write("{}_upload: False\n".format(service_name)) env = os.environ.copy() env["CONDARC"] = rcfile cmd = "conda build {}/empty_sections".format(metadata_dir) process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) output, error = process.communicate() output = output.decode('utf-8') error = error.decode('utf-8') assert "Automatic uploading is disabled" in output, error
def test_activate_bad_env_keeps_existing_good_env(): for shell in shells: with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs: commands = (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), **_format_vars) stdout, stderr = run_in(commands, shell) assert_equals( stdout, pathsep.join(_envpaths(envs, 'test1')) + pathsep + BASE_PATH, stderr)
def test_activate_deactivate(shell): # if shell == "bash.exe" and datetime.now() < datetime(2017, 5, 1): # pytest.xfail("fix this soon") shell_vars = _format_vars(shell) with TemporaryDirectory(prefix=ENVS_PREFIX, 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_activate_symlinking(): for shell in shells: with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs: commands = (command_setup + """ {source} {syspath}{binpath}activate "{env_dirs[0]}" """).format(envs=envs, env_dirs=gen_test_env_paths(envs), **_format_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')))) for env in gen_test_env_paths(envs)[:2]: for f in ['conda', 'activate', 'deactivate']: if platform.startswith('win'): file_path = join(env, "Scripts", f + ".bat") assert os.path.exists(file_path) with open(file_path) as batfile: assert root_dir in "".join(batfile.readlines()) else: file_path = join(env, "bin", f) assert os.path.lexists(file_path) assert os.path.exists(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, "bin", f)) if platform != 'win': # Test activate when there are no write permissions in the # env. prefix_bin_path = gen_test_env_paths(envs)[2] + binpath commands = (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), **_format_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))
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_from_env(): """Tests whether the activate bat file or link in the activated environment works OK""" for shell in shells: with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs: commands = (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), **_format_vars) stdout, stderr = run_in(commands, shell) assert_equals( stdout, u'{env_dirs[1]}'.format(envs=envs, env_dirs=gen_test_env_paths(envs)), stderr)
def test_activate_root_env_from_other_env(): for shell in shells: with TemporaryDirectory(prefix='envs', dir=dirname(__file__)) as envs: commands = (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), **_format_vars) stdout, stderr = run_in(commands, shell) assert_equals( stdout, u"%s" % pathsep.join(_envpaths(root_dir) + [ BASE_PATH, ]), stderr)