def test_target_prefix(self): with tempdir() as prefix: mkdir_p(join(prefix, 'first', 'envs')) mkdir_p(join(prefix, 'second', 'envs')) create_package_cache_directory(join(prefix, 'first', 'pkgs')) create_package_cache_directory(join(prefix, 'second', 'pkgs')) envs_dirs = (join(prefix, 'first', 'envs'), join(prefix, 'second', 'envs')) with env_var('CONDA_ENVS_DIRS', os.pathsep.join(envs_dirs), reset_context): # with both dirs writable, choose first reset_context((), argparse_args=AttrDict(name='blarg', func='create')) assert context.target_prefix == join(envs_dirs[0], 'blarg') # with first dir read-only, choose second PackageCacheData._cache_.clear() make_read_only(join(prefix, 'first', 'pkgs', 'urls.txt')) reset_context((), argparse_args=AttrDict(name='blarg', func='create')) assert context.target_prefix == join(envs_dirs[1], 'blarg') # if first dir is read-only but environment exists, choose first PackageCacheData._cache_.clear() mkdir_p(join(envs_dirs[0], 'blarg')) touch(join(envs_dirs[0], 'blarg', 'history')) reset_context((), argparse_args=AttrDict(name='blarg', func='create')) assert context.target_prefix == join(envs_dirs[0], 'blarg')
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)) old_prefix = '/old/prefix' with env_var('CONDA_SHLVL', '1'): with env_var('CONDA_PREFIX', old_prefix): activator = Activator('posix') builder = activator.build_activate(td) new_path = activator.pathsep_join(activator._add_prefix_to_path(td)) assert builder['unset_vars'] == () set_vars = { 'PATH': new_path, 'CONDA_PREFIX': td, 'CONDA_PREFIX_1': old_prefix, 'CONDA_SHLVL': 2, 'CONDA_DEFAULT_ENV': td, 'CONDA_PROMPT_MODIFIER': "(%s) " % td, } assert builder['set_vars'] == set_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)) with env_var('CONDA_SHLVL', '0'): with env_var('CONDA_PREFIX', ''): activator = Activator('posix') 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', '') assert builder['unset_vars'] == () set_vars = { 'PS1': ps1, } export_vars = { 'CONDA_PYTHON_EXE': activator.path_conversion(sys.executable), 'PATH': new_path, 'CONDA_PREFIX': td, 'CONDA_SHLVL': 1, 'CONDA_DEFAULT_ENV': td, 'CONDA_PROMPT_MODIFIER': conda_prompt_modifier, } 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_soft_link(self): path1_real_file = join(self.test_dir, 'path1_real_file') path2_symlink = join(self.test_dir, 'path2_symlink') touch(path1_real_file) assert isfile(path1_real_file) assert not islink(path1_real_file) if not softlink_supported(path1_real_file, self.test_dir) and on_win: pytest.skip("softlink not supported") symlink(path1_real_file, path2_symlink) assert exists(path2_symlink) assert lexists(path2_symlink) assert islink(path2_symlink) assert readlink(path2_symlink).endswith(path1_real_file) # for win py27, readlink actually gives something that starts with \??\ # \??\c:\users\appveyor\appdata\local\temp\1\c571cb0c\path1_real_file assert stat_nlink(path1_real_file) == stat_nlink(path2_symlink) == 1 os.unlink(path1_real_file) assert not isfile(path1_real_file) assert not lexists(path1_real_file) assert not exists(path1_real_file) assert lexists(path2_symlink) if not (on_win and PY2): # I guess I'm not surprised this exist vs lexist is different for win py2 # consider adding a fix in the future assert not exists(path2_symlink) os.unlink(path2_symlink) assert not lexists(path2_symlink) assert not exists(path2_symlink)
def test_build_deactivate_shlvl_1(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)) with env_var('CONDA_SHLVL', '1'): with env_var('CONDA_PREFIX', td): activator = Activator('posix') original_path = tuple(activator._get_starting_path_list()) builder = activator.build_deactivate() assert builder['unset_vars'] == ( 'CONDA_PREFIX', 'CONDA_DEFAULT_ENV', 'CONDA_PYTHON_EXE', 'CONDA_PROMPT_MODIFIER', ) new_path = activator.pathsep_join(activator.path_conversion(original_path)) assert builder['set_vars'] == { 'PATH': new_path, 'CONDA_SHLVL': 0, } assert builder['activate_scripts'] == () assert builder['deactivate_scripts'] == (activator.path_conversion(deactivate_d_1),)
def test_soft_link(self): path1_real_file = join(self.test_dir, 'path1_real_file') path2_symlink = join(self.test_dir, 'path2_symlink') touch(path1_real_file) assert isfile(path1_real_file) assert not islink(path1_real_file) symlink(path1_real_file, path2_symlink) assert exists(path2_symlink) assert lexists(path2_symlink) assert islink(path2_symlink) assert readlink(path2_symlink).endswith(path1_real_file) # for win py27, readlink actually gives something that starts with \??\ # \??\c:\users\appveyor\appdata\local\temp\1\c571cb0c\path1_real_file assert stat_nlink(path1_real_file) == stat_nlink(path2_symlink) == 1 os.unlink(path1_real_file) assert not isfile(path1_real_file) assert not lexists(path1_real_file) assert not exists(path1_real_file) assert lexists(path2_symlink) if not (on_win and PY2): # I guess I'm not surprised this exist vs lexist is different for win py2 # consider adding a fix in the future assert not exists(path2_symlink) os.unlink(path2_symlink) assert not lexists(path2_symlink) assert not exists(path2_symlink)
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)) with env_var('CONDA_SHLVL', '0'): with env_var('CONDA_PREFIX', ''): activator = Activator('posix') builder = activator.build_activate(td) new_path = activator.pathsep_join(activator._add_prefix_to_path(td)) assert builder['unset_vars'] == () set_vars = { 'CONDA_PYTHON_EXE': sys.executable, 'PATH': new_path, 'CONDA_PREFIX': td, 'CONDA_SHLVL': 1, 'CONDA_DEFAULT_ENV': td, 'CONDA_PROMPT_MODIFIER': "(%s) " % td, } assert builder['set_vars'] == set_vars assert builder['activate_scripts'] == [activate_d_1] assert builder['deactivate_scripts'] == ()
def test_soft_link(self): path1_real_file = join(self.test_dir, 'path1_real_file') path2_symlink = join(self.test_dir, 'path2_symlink') touch(path1_real_file) assert isfile(path1_real_file) assert not islink(path1_real_file) if not softlink_supported(path1_real_file, self.test_dir) and on_win: pytest.skip("softlink not supported") symlink(path1_real_file, path2_symlink) assert exists(path2_symlink) assert lexists(path2_symlink) assert islink(path2_symlink) assert readlink(path2_symlink).endswith(path1_real_file) # Windows Python >3.7, readlink actually gives something that starts with \\?\ # \\?\C:\users\appveyor\appdata\local\temp\1\c571cb0c\path1_real_file assert os.lstat(path1_real_file).st_nlink == os.lstat( path2_symlink).st_nlink == 1 os.unlink(path1_real_file) assert not isfile(path1_real_file) assert not lexists(path1_real_file) assert not exists(path1_real_file) assert lexists(path2_symlink) assert not exists(path2_symlink) os.unlink(path2_symlink) assert not lexists(path2_symlink) assert not exists(path2_symlink)
def test_register_unregister_location_env(self): user_environments_txt_file = get_user_environments_txt_file() if (not os.path.exists(user_environments_txt_file) or user_environments_txt_file == os.devnull): pytest.skip('user environments.txt file {} does not exist'.format( user_environments_txt_file)) gascon_location = join(self.prefix, 'gascon') touch(join(gascon_location, PREFIX_MAGIC_FILE), mkdir=True) assert gascon_location not in list_all_known_prefixes() touch(user_environments_txt_file, mkdir=True, sudo_safe=True) register_env(gascon_location) assert gascon_location in yield_lines(user_environments_txt_file) assert len( tuple(x for x in yield_lines(user_environments_txt_file) if paths_equal(gascon_location, x))) == 1 register_env(gascon_location) # should be completely idempotent assert len( tuple(x for x in yield_lines(user_environments_txt_file) if x == gascon_location)) == 1 unregister_env(gascon_location) assert gascon_location not in list_all_known_prefixes() unregister_env(gascon_location) # should be idempotent assert gascon_location not in list_all_known_prefixes()
def test_target_prefix(self): with tempdir() as prefix: mkdir_p(join(prefix, 'first', 'envs')) mkdir_p(join(prefix, 'second', 'envs')) create_package_cache_directory(join(prefix, 'first', 'pkgs')) create_package_cache_directory(join(prefix, 'second', 'pkgs')) envs_dirs = (join(prefix, 'first', 'envs'), join(prefix, 'second', 'envs')) with env_var('CONDA_ENVS_DIRS', os.pathsep.join(envs_dirs), reset_context): # with both dirs writable, choose first reset_context((), argparse_args=AttrDict(name='blarg', func='create')) assert context.target_prefix == join(envs_dirs[0], 'blarg') # with first dir read-only, choose second PackageCacheData._cache_.clear() make_read_only(join(envs_dirs[0], '.conda_envs_dir_test')) reset_context((), argparse_args=AttrDict(name='blarg', func='create')) assert context.target_prefix == join(envs_dirs[1], 'blarg') # if first dir is read-only but environment exists, choose first PackageCacheData._cache_.clear() mkdir_p(join(envs_dirs[0], 'blarg')) touch(join(envs_dirs[0], 'blarg', 'history')) reset_context((), argparse_args=AttrDict(name='blarg', func='create')) assert context.target_prefix == join(envs_dirs[0], 'blarg')
def setUp(self): tempdirdir = gettempdir() prefix_dirname = str(uuid4())[:4] + ' ' + str(uuid4())[:4] self.prefix = join(tempdirdir, prefix_dirname) mkdir_p(join(self.prefix, 'conda-meta')) assert isdir(self.prefix) touch(join(self.prefix, 'conda-meta', 'history'))
def test_backoff_unlink(): from conda.gateways.disk.delete import backoff_rmdir with tempdir() as td: test_path = join(td, 'test_path') touch(test_path) _try_open(test_path) assert isdir(td) backoff_rmdir(td) assert not isdir(td)
def test_move_path_to_trash_couldnt(): from conda.gateways.disk.delete import move_path_to_trash with tempdir() as td: test_path = join(td, 'test_path') touch(test_path) _try_open(test_path) assert isdir(td) assert isfile(test_path) assert move_path_to_trash(test_path)
def test_backoff_unlink_doesnt_exist(): from conda.gateways.disk.delete import backoff_rmdir with tempdir() as td: test_path = join(td, 'test_path') touch(test_path) try: backoff_rmdir(join(test_path, 'some', 'path', 'in', 'utopia')) except Exception as e: assert e.value.errno == ENOENT
def test_move_to_trash(): with tempdir() as td: test_path = join(td, 'test_path') touch(test_path) _try_open(test_path) assert isdir(td) assert isfile(test_path) move_to_trash(td, test_path) assert not isfile(test_path)
def test_remove_file_to_trash(): with tempdir() as td: test_path = join(td, 'test_path') touch(test_path) assert isfile(test_path) _try_open(test_path) _make_read_only(test_path) pytest.raises((IOError, OSError), _try_open, test_path) assert rm_rf(test_path) assert not isfile(test_path)
def test_make_executable(): from conda.gateways.disk.permissions import make_executable with tempdir() as td: test_path = join(td, 'test_path') touch(test_path) assert isfile(test_path) _try_open(test_path) _make_read_only(test_path) assert not _can_write_file(test_path, "welcome to the ministry of silly walks") assert not _can_execute(test_path) make_executable(test_path)
def setUp(self): tempdirdir = gettempdir() prefix_dirname = str(uuid4())[:4] + ' ' + str(uuid4())[:4] self.prefix = join(tempdirdir, prefix_dirname) mkdir_p(join(self.prefix, 'conda-meta')) assert isdir(self.prefix) touch(join(self.prefix, 'conda-meta', 'history')) self.hold_environ = os.environ.copy() for var in POP_THESE: os.environ.pop(var, None)
def test_clone_offline_multichannel_with_untracked(self): with make_temp_env("python=3.5") as prefix: run_command(Commands.CONFIG, prefix, "--add channels https://repo.continuum.io/pkgs/free") run_command(Commands.CONFIG, prefix, "--remove channels defaults") run_command(Commands.INSTALL, prefix, "-c conda-test flask") touch(join(prefix, 'test.file')) # untracked file with make_temp_env("--clone", prefix, "--offline") as clone_prefix: assert context.offline assert_package_is_installed(clone_prefix, 'python-3.5') assert_package_is_installed(clone_prefix, 'flask-0.11.1-py_0') assert isfile(join(clone_prefix, 'test.file')) # untracked file
def test_remove_dir(): with tempdir() as td: test_path = join(td, 'test_path') touch(test_path) _try_open(test_path) assert isfile(test_path) assert isdir(td) assert not islink(test_path) assert rm_rf(td) assert rm_rf(test_path) assert not isdir(td) assert not isfile(test_path) assert not lexists(test_path)
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', '2'): with env_var('CONDA_PREFIX', old_prefix): activator = Activator('posix') builder = activator.build_activate(td) assert builder['unset_vars'] == () assert builder['set_vars'] == {} assert builder['activate_scripts'] == [activate_d_1] assert builder['deactivate_scripts'] == [deactivate_d_1]
def test_make_writable(): from conda.gateways.disk.permissions import make_writable with tempdir() as td: test_path = join(td, 'test_path') touch(test_path) assert isfile(test_path) _try_open(test_path) _make_read_only(test_path) pytest.raises((IOError, OSError), _try_open, test_path) make_writable(test_path) _try_open(test_path) assert _can_write_file(test_path, "welcome to the ministry of silly walks") os.remove(test_path) assert not isfile(test_path)
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 = Activator('posix') builder = activator.build_activate(td) set_vars = { 'CONDA_PROMPT_MODIFIER': "(%s) " % td, 'CONDA_SHLVL': 1, } assert builder['unset_vars'] == () assert builder['set_vars'] == set_vars assert builder['activate_scripts'] == (activator.path_conversion(activate_d_1),) assert builder['deactivate_scripts'] == (activator.path_conversion(deactivate_d_1),)
def test_rewrite_environments_txt_file(self): mkdir_p(join(self.prefix, 'conda-meta')) touch(join(self.prefix, 'conda-meta', 'history')) doesnt_exist = join(self.prefix, 'blarg') environments_txt_path = join(self.prefix, 'environments.txt') with open(environments_txt_path, 'w') as fh: fh.write(self.prefix + '\n') fh.write(doesnt_exist + '\n') cleaned_1 = _clean_environments_txt(environments_txt_path) assert cleaned_1 == (self.prefix, ) with patch('conda.core.envs_manager._rewrite_environments_txt' ) as _rewrite_patch: cleaned_2 = _clean_environments_txt(environments_txt_path) assert cleaned_2 == (self.prefix, ) assert _rewrite_patch.call_count == 0
def make_temp_package_cache(): prefix = make_temp_prefix(use_restricted_unicode=on_win) pkgs_dir = join(prefix, "pkgs") mkdir_p(pkgs_dir) touch(join(pkgs_dir, PACKAGE_CACHE_MAGIC_FILE)) try: with env_var("CONDA_PKGS_DIRS", pkgs_dir, stack_callback=conda_tests_ctxt_mgmt_def_pol): assert context.pkgs_dirs == (pkgs_dir, ) yield pkgs_dir finally: rmtree(prefix, ignore_errors=True) if pkgs_dir in PackageCacheData._cache_: del PackageCacheData._cache_[pkgs_dir]
def gen_test_env_paths(envs, shell, num_test_folders=5): """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 = [os.path.join(envs, "test {}".format(test_folder+1)) for test_folder in range(num_test_folders)] for path in paths[:2]: # These tests assume only the first two paths can be activated # Create symlinks ONLY for the first two folders. # symlink_conda(path, sys.prefix, shell) mkdir_p(join(path, 'conda-meta')) touch(join(path, 'conda-meta', 'history')) converter = shells[shell]["path_to"] paths = [converter(path) for path in paths] return paths
def make_dot_d_files(self, extension): mkdir_p(join(self.prefix, 'etc', 'conda', 'activate.d')) mkdir_p(join(self.prefix, 'etc', 'conda', 'deactivate.d')) touch(join(self.prefix, 'etc', 'conda', 'activate.d', 'ignore.txt')) touch(join(self.prefix, 'etc', 'conda', 'deactivate.d', 'ignore.txt')) touch(join(self.prefix, 'etc', 'conda', 'activate.d', 'activate1' + extension)) touch(join(self.prefix, 'etc', 'conda', 'deactivate.d', 'deactivate1' + extension))
def test_list_all_known_prefixes_with_permission_error(mock_clean_env, mock_get_user_env, mock_context, tmp_path): # Mock context myenv_dir = tmp_path / "envs" myenv_dir.mkdir() mock_context.envs_dirs = str(myenv_dir) mock_context.root_prefix = "root_prefix" # Mock get_user_environments_txt_file to return a file env_txt_file = tmp_path / "environment.txt" touch(env_txt_file) mock_get_user_env.return_value = env_txt_file # Mock _clean_environments_txt to raise PermissionError mock_clean_env.side_effect = PermissionError() all_env_paths = list_all_known_prefixes() # On Windows, all_env_paths can contain more paths (like '\\Miniconda') assert "root_prefix" in all_env_paths
def test_rewrite_environments_txt_file(self): mkdir_p(join(self.prefix, 'conda-meta')) touch(join(self.prefix, 'conda-meta', 'history')) doesnt_exist = join(self.prefix, 'blarg') environments_txt_path = join(self.prefix, 'environments.txt') with open(environments_txt_path, 'w') as fh: fh.write(self.prefix + '\n') fh.write(doesnt_exist + '\n') cleaned_1 = _clean_environments_txt(environments_txt_path) assert cleaned_1 == (self.prefix,) with patch('conda.core.envs_manager._rewrite_environments_txt') as _rewrite_patch: cleaned_2 = _clean_environments_txt(environments_txt_path) assert cleaned_2 == (self.prefix,) assert _rewrite_patch.call_count == 0
def test_hard_link(self): path1_real_file = join(self.test_dir, 'path1_real_file') path2_second_inode = join(self.test_dir, 'path2_second_inode') touch(path1_real_file) assert isfile(path1_real_file) assert not islink(path1_real_file) link(path1_real_file, path2_second_inode) assert isfile(path2_second_inode) assert not islink(path2_second_inode) path1_stat = os.lstat(path1_real_file) path2_stat = os.lstat(path2_second_inode) assert path1_stat.st_ino == path2_stat.st_ino assert path1_stat.st_nlink == path2_stat.st_nlink os.unlink(path2_second_inode) assert not lexists(path2_second_inode) assert os.lstat(path1_real_file).st_nlink == 1 os.unlink(path1_real_file) assert not lexists(path1_real_file)
def test_register_unregister_location_env(self): gascon_location = join(self.prefix, 'gascon') touch(join(gascon_location, PREFIX_MAGIC_FILE), mkdir=True) assert gascon_location not in list_all_known_prefixes() touch(USER_ENVIRONMENTS_TXT_FILE, mkdir=True, sudo_safe=True) register_env(gascon_location) assert gascon_location in yield_lines(USER_ENVIRONMENTS_TXT_FILE) assert len( tuple(x for x in yield_lines(USER_ENVIRONMENTS_TXT_FILE) if paths_equal(gascon_location, x))) == 1 register_env(gascon_location) # should be completely idempotent assert len( tuple(x for x in yield_lines(USER_ENVIRONMENTS_TXT_FILE) if x == gascon_location)) == 1 unregister_env(gascon_location) assert gascon_location not in list_all_known_prefixes() unregister_env(gascon_location) # should be idempotent assert gascon_location not in list_all_known_prefixes()
def test_hard_link(self): path1_real_file = join(self.test_dir, 'path1_real_file') path2_second_inode = join(self.test_dir, 'path2_second_inode') touch(path1_real_file) assert isfile(path1_real_file) assert not islink(path1_real_file) link(path1_real_file, path2_second_inode) assert isfile(path2_second_inode) assert not islink(path2_second_inode) path1_stat = os.stat(path1_real_file) path2_stat = os.stat(path2_second_inode) assert path1_stat.st_ino == path2_stat.st_ino assert stat_nlink(path1_real_file) == stat_nlink(path2_second_inode) os.unlink(path2_second_inode) assert not lexists(path2_second_inode) assert stat_nlink(path1_real_file) == 1 os.unlink(path1_real_file) assert not lexists(path1_real_file)
def test_register_unregister_location_env(self): user_environments_txt_file = get_user_environments_txt_file() if (not os.path.exists(user_environments_txt_file) or user_environments_txt_file == os.devnull): pytest.skip('user environments.txt file {} does not exist'.format(user_environments_txt_file)) gascon_location = join(self.prefix, 'gascon') touch(join(gascon_location, PREFIX_MAGIC_FILE), mkdir=True) assert gascon_location not in list_all_known_prefixes() touch(user_environments_txt_file, mkdir=True, sudo_safe=True) register_env(gascon_location) assert gascon_location in yield_lines(user_environments_txt_file) assert len(tuple(x for x in yield_lines(user_environments_txt_file) if paths_equal(gascon_location, x))) == 1 register_env(gascon_location) # should be completely idempotent assert len(tuple(x for x in yield_lines(user_environments_txt_file) if x == gascon_location)) == 1 unregister_env(gascon_location) assert gascon_location not in list_all_known_prefixes() unregister_env(gascon_location) # should be idempotent assert gascon_location not in list_all_known_prefixes()
def test_name_cli_flag(self): envs_dirs = (join(self.prefix, 'first-envs-dir'), join(self.prefix, 'seconds-envs-dir')) with env_var('CONDA_ENVS_DIRS', os.pathsep.join(envs_dirs), reset_context): # with both dirs writable, choose first reset_context((), argparse_args=AttrDict(name='blarg')) assert context.target_prefix == join(envs_dirs[0], 'blarg') # with first dir read-only, choose second EnvsDirectory(envs_dirs[0])._is_writable = False reset_context((), argparse_args=AttrDict(name='blarg')) assert context.target_prefix == join(envs_dirs[1], 'blarg') # if first dir is read-only but environment exists, choose first EnvsDirectory._cache_.pop(envs_dirs[0]) mkdir_p(join(envs_dirs[0], 'blarg')) touch(join(envs_dirs[0], 'blarg', 'history')) reset_context((), argparse_args=AttrDict(name='blarg')) assert context.target_prefix == join(envs_dirs[0], 'blarg') EnvsDirectory._cache_ = {}
def test_remove_link_to_dir(): with tempdir() as td: dst_link = join(td, "test_link") src_dir = join(td, "test_dir") test_file = join(td, "test_file") mkdir_p(src_dir) touch(test_file) assert isdir(src_dir) assert not islink(src_dir) assert not islink(dst_link) if not softlink_supported(test_file, td) and on_win: pytest.skip("softlink not supported") symlink(src_dir, dst_link) assert islink(dst_link) assert rm_rf(dst_link) assert not isdir(dst_link) assert not islink(dst_link) assert not lexists(dst_link) assert isdir(src_dir) assert rm_rf(src_dir) assert not isdir(src_dir) assert not islink(src_dir)