diff --git a/micromamba/tests/test_config.py b/micromamba/tests/test_config.py index 205c70b8d3..0e77d0bc74 100644 --- a/micromamba/tests/test_config.py +++ b/micromamba/tests/test_config.py @@ -68,6 +68,7 @@ def rc_file( tmp_prefix, tmp_path, user_config_dir, + monkeypatch, ): """Parametrizable fixture to create an rc file at the desired location. @@ -85,7 +86,7 @@ def rc_file( elif where == "user_config_dir": rc_file = user_config_dir / rc_filename elif where == "env_set_xdg": - os.environ["XDG_CONFIG_HOME"] = str(tmp_home / "custom_xdg_config_dir") + monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_home / "custom_xdg_config_dir")) rc_file = tmp_home / "custom_xdg_config_dir" / "mamba" / rc_filename elif where == "absolute": rc_file = Path(rc_filename) @@ -255,31 +256,27 @@ def test_list_with_groups(self, rc_file, group_flag): " - channel1\n - channel2\n".splitlines() ) - def test_env_vars(self): - os.environ["MAMBA_OFFLINE"] = "true" + def test_env_vars(self, monkeypatch): + monkeypatch.setenv("MAMBA_OFFLINE", "true") assert ( config("list", "offline", "--no-rc", "-s").splitlines() == "offline: true # 'MAMBA_OFFLINE'".splitlines() ) - os.environ["MAMBA_OFFLINE"] = "false" + monkeypatch.setenv("MAMBA_OFFLINE", "false") assert ( config("list", "offline", "--no-rc", "-s").splitlines() == "offline: false # 'MAMBA_OFFLINE'".splitlines() ) - os.environ.pop("MAMBA_OFFLINE") - - def test_no_env(self): - os.environ["MAMBA_OFFLINE"] = "false" + def test_no_env(self, monkeypatch): + monkeypatch.setenv("MAMBA_OFFLINE", "false") assert ( config("list", "offline", "--no-rc", "--no-env", "-s", "--offline").splitlines() == "offline: true # 'CLI'".splitlines() ) - os.environ.pop("MAMBA_OFFLINE") - - def test_precedence(self): + def test_precedence(self, monkeypatch): rc_dir = os.path.expanduser(os.path.join("~", "test_mamba", helpers.random_string())) os.makedirs(rc_dir, exist_ok=True) rc_file = os.path.join(rc_dir, ".mambarc") @@ -289,15 +286,12 @@ def test_precedence(self): f.write("offline: true") try: - if "MAMBA_OFFLINE" in os.environ: - os.environ.pop("MAMBA_OFFLINE") - assert ( config("list", "offline", f"--rc-file={rc_file}", "-s").splitlines() == f"offline: true # '{short_rc_file}'".splitlines() ) - os.environ["MAMBA_OFFLINE"] = "false" + monkeypatch.setenv("MAMBA_OFFLINE", "false") assert ( config("list", "offline", "--no-rc", "-s").splitlines() == "offline: false # 'MAMBA_OFFLINE'".splitlines() @@ -334,8 +328,6 @@ def test_precedence(self): == "offline: true # 'CLI'".splitlines() ) finally: - if "MAMBA_OFFLINE" in os.environ: - os.environ.pop("MAMBA_OFFLINE") shutil.rmtree(os.path.expanduser(os.path.join("~", "test_mamba"))) diff --git a/micromamba/tests/test_create.py b/micromamba/tests/test_create.py index 829960d508..3e9720e333 100644 --- a/micromamba/tests/test_create.py +++ b/micromamba/tests/test_create.py @@ -354,6 +354,7 @@ def test_target_prefix( similar_non_canonical, non_canonical_position, root_prefix_env_exists, + monkeypatch, ): cmd = [] @@ -412,12 +413,12 @@ def test_target_prefix( cmd += ["-f", spec_file] if env_var: - os.environ["MAMBA_TARGET_PREFIX"] = str(p) + monkeypatch.setenv("MAMBA_TARGET_PREFIX", str(p)) if not current_target_prefix_fallback: - os.environ.pop("CONDA_PREFIX", None) + monkeypatch.delenv("CONDA_PREFIX", raising=False) else: - os.environ["CONDA_PREFIX"] = str(p) + monkeypatch.setenv("CONDA_PREFIX", str(p)) if ( (cli_prefix and cli_env_name) @@ -436,7 +437,7 @@ def test_target_prefix( @pytest.mark.parametrize("yaml", (False, True)) @pytest.mark.parametrize("env_var", (False, True)) @pytest.mark.parametrize("rc_file", (False, True)) -def test_channels(tmp_home, tmp_root_prefix, tmp_path, cli, yaml, env_var, rc_file): +def test_channels(tmp_home, tmp_root_prefix, tmp_path, cli, yaml, env_var, rc_file, monkeypatch): env_prefix = tmp_path / "myenv" spec_file = tmp_path / "env.yaml" rc_file = tmp_path / "rc.yaml" @@ -460,7 +461,7 @@ def test_channels(tmp_home, tmp_root_prefix, tmp_path, cli, yaml, env_var, rc_fi expected_channels += ["yaml"] if env_var: - os.environ["CONDA_CHANNELS"] = "env_var" + monkeypatch.setenv("CONDA_CHANNELS", "env_var") expected_channels += ["env_var"] if rc_file: @@ -682,9 +683,9 @@ def test_create_base(tmp_home, tmp_root_prefix, already_exists, is_conda_env, ha reason="Running only ultra-dry tests", ) @pytest.mark.parametrize("outside_root_prefix", (False, True)) -def test_classic_specs(tmp_home, tmp_root_prefix, tmp_path, outside_root_prefix): +def test_classic_specs(tmp_home, tmp_root_prefix, tmp_path, outside_root_prefix, monkeypatch): tmp_pkgs_dirs = tmp_path / "cache" - os.environ["CONDA_PKGS_DIRS"] = str(tmp_pkgs_dirs) + monkeypatch.setenv("CONDA_PKGS_DIRS", str(tmp_pkgs_dirs)) if outside_root_prefix: p = tmp_path / "myenv" else: @@ -1001,18 +1002,15 @@ def assert_env_exists(prefix_path): ) @pytest.mark.parametrize("shared_pkgs_dirs", [True], indirect=True) @pytest.mark.parametrize("source", ["cli", "env_var", "rc_file"]) -def test_always_yes(tmp_home, tmp_root_prefix, tmp_path, source): +def test_always_yes(tmp_home, tmp_root_prefix, tmp_path, source, monkeypatch): env_name = "myenv" helpers.create("-n", env_name, "xtensor", no_dry_run=True) if source == "cli": res = helpers.create("-n", env_name, "xtensor", "--json", always_yes=True) elif source == "env_var": - try: - os.environ["MAMBA_ALWAYS_YES"] = "true" - res = helpers.create("-n", env_name, "xtensor", "--json", always_yes=False) - finally: - os.environ.pop("MAMBA_ALWAYS_YES") + monkeypatch.setenv("MAMBA_ALWAYS_YES", "true") + res = helpers.create("-n", env_name, "xtensor", "--json", always_yes=False) else: # rc_file rc_file = tmp_path / "config.yaml" with open(rc_file, "w") as f: @@ -1150,7 +1148,7 @@ def test_spec_with_multichannel(tmp_home, tmp_root_prefix): helpers.create("-n", "myenv", "defaults::zlib", "--dry-run") -def test_spec_with_slash_in_channel(tmp_home, tmp_root_prefix): +def test_spec_with_slash_in_channel(tmp_home, tmp_root_prefix, monkeypatch): "https://github.com/mamba-org/mamba/pull/2926" with pytest.raises(subprocess.CalledProcessError) as info: helpers.create("-n", "env1", "pkgs/main/noarch::python", "--dry-run") @@ -1161,7 +1159,7 @@ def test_spec_with_slash_in_channel(tmp_home, tmp_root_prefix): assert "The following package could not be installed" in msg assert "python" in msg - os.environ["CONDA_SUBDIR"] = "linux-64" + monkeypatch.setenv("CONDA_SUBDIR", "linux-64") helpers.create("-n", "env2", "pkgs/main/linux-64::python", "--dry-run") helpers.create("-n", "env3", "pkgs/main::python", "--dry-run") diff --git a/micromamba/tests/test_env.py b/micromamba/tests/test_env.py index 9c7ca2a3a6..623b613375 100644 --- a/micromamba/tests/test_env.py +++ b/micromamba/tests/test_env.py @@ -1,4 +1,3 @@ -import os import re import shutil @@ -22,7 +21,7 @@ def test_env_list(tmp_home, tmp_root_prefix, tmp_empty_env): assert str(tmp_empty_env) in env_json["envs"] -def test_env_list_table(tmp_home, tmp_root_prefix, tmp_prefix): +def test_env_list_table(tmp_home, tmp_root_prefix, tmp_prefix, monkeypatch): res = helpers.run_env("list") assert "Name" in res @@ -35,8 +34,6 @@ def test_env_list_table(tmp_home, tmp_root_prefix, tmp_prefix): active_env_l = line assert str(tmp_root_prefix) in active_env_l - os.environ["CONDA_PREFIX"] = str(tmp_prefix) - res = helpers.run_env("list") all_lines = res.splitlines() @@ -470,10 +467,10 @@ def test_env_create_whitespace(tmp_home, tmp_root_prefix, tmp_path): @pytest.mark.parametrize("shared_pkgs_dirs", [True], indirect=True) -def test_env_update_empty_base(tmp_home, tmp_root_prefix, tmp_path): +def test_env_update_empty_base(tmp_home, tmp_root_prefix, tmp_path, monkeypatch): env_prefix = tmp_path / "env-update-empty-base" - os.environ["MAMBA_ROOT_PREFIX"] = str(env_prefix) + monkeypatch.setenv("MAMBA_ROOT_PREFIX", str(env_prefix)) env_file_yml = tmp_path / "test_env_empty_base.yaml" env_file_yml.write_text(env_yaml_content_to_update_empty_base) diff --git a/micromamba/tests/test_info.py b/micromamba/tests/test_info.py index 563571bb44..7d4a3610ea 100644 --- a/micromamba/tests/test_info.py +++ b/micromamba/tests/test_info.py @@ -40,7 +40,7 @@ def test_env(tmp_home, tmp_root_prefix, tmp_env_name, tmp_prefix, prefix_selecti @pytest.mark.parametrize("existing_prefix", [False, True]) @pytest.mark.parametrize("prefix_selection", [None, "env_var", "prefix", "name"]) -def test_not_env(tmp_home, tmp_root_prefix, prefix_selection, existing_prefix): +def test_not_env(tmp_home, tmp_root_prefix, prefix_selection, existing_prefix, monkeypatch): name = "not_an_env" prefix = tmp_root_prefix / "envs" / name @@ -52,10 +52,9 @@ def test_not_env(tmp_home, tmp_root_prefix, prefix_selection, existing_prefix): elif prefix_selection == "name": infos = helpers.info("-n", name) elif prefix_selection == "env_var": - os.environ["CONDA_PREFIX"] = str(prefix) + monkeypatch.setenv("CONDA_PREFIX", str(prefix)) infos = helpers.info() else: - os.environ.pop("CONDA_PREFIX", "") infos = helpers.info() if prefix_selection is None: @@ -131,11 +130,7 @@ def flags_test(tmp_root_prefix, infos, base_flag, envs_flag, json_flag): @pytest.mark.parametrize("envs_flag", ["", "-e", "--envs"]) @pytest.mark.parametrize("json_flag", ["", "--json"]) @pytest.mark.parametrize("prefix_selection", [None, "prefix", "name"]) -def test_base_flags( - tmp_home, tmp_root_prefix, prefix_selection, base_flag, envs_flag, json_flag, monkeypatch -): - monkeypatch.setenv("CONDA_PREFIX", str(tmp_root_prefix)) - +def test_base_flags(tmp_home, tmp_root_prefix, prefix_selection, base_flag, envs_flag, json_flag): if prefix_selection == "prefix": infos = helpers.info("-p", tmp_root_prefix, base_flag, envs_flag, json_flag) elif prefix_selection == "name": diff --git a/micromamba/tests/test_menuinst.py b/micromamba/tests/test_menuinst.py index 2691d09e3c..dbecf32893 100644 --- a/micromamba/tests/test_menuinst.py +++ b/micromamba/tests/test_menuinst.py @@ -59,9 +59,9 @@ def test_simple_shortcut(self): not sys.platform.startswith("win"), reason="skipping windows-only tests", ) - def test_shortcut_weird_env(self): + def test_shortcut_weird_env(self, monkeypatch): # note Umlauts do not work yet - os.environ["MAMBA_ROOT_PREFIX"] = str(Path("./compl i c ted").absolute()) + monkeypatch.setenv("MAMBA_ROOT_PREFIX", str(Path("./compl i c ted").absolute())) root_prefix = os.environ["MAMBA_ROOT_PREFIX"] env_name = random_string() @@ -97,7 +97,6 @@ def test_shortcut_weird_env(self): assert not os.path.exists(lnk) shutil.rmtree(root_prefix) - os.environ["MAMBA_ROOT_PREFIX"] = self.root_prefix # Testing a package (spyder) using menuinst v2 schema @pytest.mark.skipif( diff --git a/micromamba/tests/test_pkg_cache.py b/micromamba/tests/test_pkg_cache.py index 2c4721e6f4..8d0df5758e 100644 --- a/micromamba/tests/test_pkg_cache.py +++ b/micromamba/tests/test_pkg_cache.py @@ -274,12 +274,18 @@ def same_repodata_json_solv(cache: Path): class TestMultiplePkgCaches: @pytest.mark.parametrize("which_cache", ["tmp_cache", "tmp_cache_alt"]) def test_different_caches( - self, tmp_home, tmp_root_prefix, tmp_cache, tmp_cache_alt, which_cache + self, + tmp_home, + tmp_root_prefix, + tmp_cache, + tmp_cache_alt, + which_cache, + monkeypatch, ): # Test parametrization cache = {"tmp_cache": tmp_cache, "tmp_cache_alt": tmp_cache_alt}[which_cache] - os.environ["CONDA_PKGS_DIRS"] = f"{cache}" + monkeypatch.setenv("CONDA_PKGS_DIRS", f"{cache}") env_name = "some_env" res = helpers.create( "-n", env_name, package_to_check_requirements(), "-v", "--json", no_dry_run=True @@ -307,8 +313,9 @@ def test_first_writable( tmp_cache_writable, tmp_cache, tmp_cache_alt, + monkeypatch, ): - os.environ["CONDA_PKGS_DIRS"] = f"{tmp_cache},{tmp_cache_alt}" + monkeypatch.setenv("CONDA_PKGS_DIRS", f"{tmp_cache},{tmp_cache_alt}") env_name = "some_env" res = helpers.create( @@ -333,15 +340,17 @@ def test_first_writable( assert linked_file.stat().st_dev == cache_file.stat().st_dev assert linked_file.stat().st_ino == cache_file.stat().st_ino - def test_no_writable(self, tmp_home, tmp_root_prefix, tmp_cache, tmp_cache_alt): + def test_no_writable(self, tmp_home, tmp_root_prefix, tmp_cache, tmp_cache_alt, monkeypatch): helpers.rmtree(tmp_cache / find_pkg_build(tmp_cache, package_to_check)) helpers.recursive_chmod(tmp_cache, 0o500) - os.environ["CONDA_PKGS_DIRS"] = f"{tmp_cache},{tmp_cache_alt}" + monkeypatch.setenv("CONDA_PKGS_DIRS", f"{tmp_cache},{tmp_cache_alt}") helpers.create("-n", "myenv", package_to_check_requirements(), "--json", no_dry_run=True) - def test_no_writable_extracted_dir_corrupted(self, tmp_home, tmp_root_prefix, tmp_cache): + def test_no_writable_extracted_dir_corrupted( + self, tmp_home, tmp_root_prefix, tmp_cache, monkeypatch + ): old_cache_dir = tmp_cache / find_pkg_build(tmp_cache, package_to_check) if old_cache_dir.is_dir(): files = glob.glob( @@ -351,7 +360,7 @@ def test_no_writable_extracted_dir_corrupted(self, tmp_home, tmp_root_prefix, tm file.unlink() helpers.recursive_chmod(tmp_cache, 0o500) - os.environ["CONDA_PKGS_DIRS"] = f"{tmp_cache}" + monkeypatch.setenv("CONDA_PKGS_DIRS", f"{tmp_cache}") with pytest.raises(subprocess.CalledProcessError): helpers.create( @@ -359,7 +368,7 @@ def test_no_writable_extracted_dir_corrupted(self, tmp_home, tmp_root_prefix, tm ) def test_first_writable_extracted_dir_corrupted( - self, tmp_home, tmp_root_prefix, tmp_cache, tmp_cache_alt + self, tmp_home, tmp_root_prefix, tmp_cache, tmp_cache_alt, monkeypatch ): test_pkg_bld = find_pkg_build(tmp_cache, package_to_check) helpers.rmtree(tmp_cache) # convenience for cache teardown @@ -373,7 +382,7 @@ def test_first_writable_extracted_dir_corrupted( for file in files: helpers.rmtree(file) - os.environ["CONDA_PKGS_DIRS"] = f"{tmp_cache},{tmp_cache_alt}" + monkeypatch.setenv("CONDA_PKGS_DIRS", f"{tmp_cache},{tmp_cache_alt}") env_name = "myenv" helpers.create( @@ -414,13 +423,14 @@ def test_extracted_tarball_only_in_non_writable_cache( tmp_cache, tmp_cache_alt, tmp_cache_test_package_dir, + monkeypatch, ): test_pkg_bld = find_pkg_build(tmp_cache, package_to_check) helpers.rmtree(find_cache_archive(tmp_cache, test_pkg_bld)) helpers.rmtree(tmp_cache_alt) helpers.recursive_chmod(tmp_cache, 0o500) - os.environ["CONDA_PKGS_DIRS"] = f"{tmp_cache},{tmp_cache_alt}" + monkeypatch.setenv("CONDA_PKGS_DIRS", f"{tmp_cache},{tmp_cache_alt}") env_name = "myenv" helpers.create("-n", env_name, package_to_check_requirements(), "--json", no_dry_run=True) @@ -453,14 +463,14 @@ def test_extracted_tarball_only_in_non_writable_cache( assert linked_file.stat().st_ino == non_writable_cache_file.stat().st_ino def test_missing_extracted_dir_in_non_writable_cache( - self, tmp_home, tmp_root_prefix, tmp_cache, tmp_cache_alt + self, tmp_home, tmp_root_prefix, tmp_cache, tmp_cache_alt, monkeypatch ): test_pkg_bld = find_pkg_build(tmp_cache, package_to_check) helpers.rmtree(tmp_cache / test_pkg_bld) helpers.rmtree(tmp_cache_alt) helpers.recursive_chmod(tmp_cache, 0o500) - os.environ["CONDA_PKGS_DIRS"] = f"{tmp_cache},{tmp_cache_alt}" + monkeypatch.setenv("CONDA_PKGS_DIRS", f"{tmp_cache},{tmp_cache_alt}") env_name = "myenv" helpers.create("-n", env_name, package_to_check_requirements(), "--json", no_dry_run=True) @@ -493,7 +503,7 @@ def test_missing_extracted_dir_in_non_writable_cache( assert linked_file.stat().st_ino == writable_cache_file.stat().st_ino def test_corrupted_extracted_dir_in_non_writable_cache( - self, tmp_home, tmp_root_prefix, tmp_cache, tmp_cache_alt + self, tmp_home, tmp_root_prefix, tmp_cache, tmp_cache_alt, monkeypatch ): test_pkg_bld = find_pkg_build(tmp_cache, package_to_check) tmp_cache_test_pkg_dir = Path(tmp_cache / test_pkg_bld) @@ -506,7 +516,7 @@ def test_corrupted_extracted_dir_in_non_writable_cache( os.makedirs(tmp_cache_alt) helpers.recursive_chmod(tmp_cache, 0o500) - os.environ["CONDA_PKGS_DIRS"] = f"{tmp_cache},{tmp_cache_alt}" + monkeypatch.setenv("CONDA_PKGS_DIRS", f"{tmp_cache},{tmp_cache_alt}") env_name = "myenv" helpers.create( @@ -545,12 +555,12 @@ def test_corrupted_extracted_dir_in_non_writable_cache( assert linked_file.stat().st_ino == writable_cache_file.stat().st_ino def test_expired_but_valid_repodata_in_non_writable_cache( - self, tmp_home, tmp_root_prefix, tmp_cache, tmp_cache_alt + self, tmp_home, tmp_root_prefix, tmp_cache, tmp_cache_alt, monkeypatch ): helpers.rmtree(tmp_cache_alt) helpers.recursive_chmod(tmp_cache, 0o500) - os.environ["CONDA_PKGS_DIRS"] = f"{tmp_cache},{tmp_cache_alt}" + monkeypatch.setenv("CONDA_PKGS_DIRS", f"{tmp_cache},{tmp_cache_alt}") env_name = "myenv" test_pkg_bld = find_pkg_build(tmp_cache, package_to_check) diff --git a/micromamba/tests/test_remove.py b/micromamba/tests/test_remove.py index d184837046..8f31cc11cf 100644 --- a/micromamba/tests/test_remove.py +++ b/micromamba/tests/test_remove.py @@ -235,13 +235,15 @@ def test_remove_config_target_prefix( cli_env_name, env_var, current_target_prefix_fallback, + monkeypatch, ): (tmp_root_prefix / "conda-meta").mkdir(parents=True, exist_ok=True) cmd = [] if use_root_prefix in (None, "cli"): - os.environ["MAMBA_DEFAULT_ROOT_PREFIX"] = os.environ.pop("MAMBA_ROOT_PREFIX") + monkeypatch.setenv("MAMBA_DEFAULT_ROOT_PREFIX", os.environ["MAMBA_ROOT_PREFIX"]) + monkeypatch.delenv("MAMBA_ROOT_PREFIX") if use_root_prefix == "cli": cmd += ["-r", str(tmp_root_prefix)] @@ -262,12 +264,12 @@ def test_remove_config_target_prefix( cmd += ["-n", n] if env_var: - os.environ["MAMBA_TARGET_PREFIX"] = p + monkeypatch.setenv("MAMBA_TARGET_PREFIX", p) if not current_target_prefix_fallback: - os.environ.pop("CONDA_PREFIX") + monkeypatch.delenv("CONDA_PREFIX") else: - os.environ["CONDA_PREFIX"] = p + monkeypatch.setenv("CONDA_PREFIX", p) if (cli_prefix and cli_env_name) or not ( cli_prefix or cli_env_name or env_var or current_target_prefix_fallback diff --git a/micromamba/tests/test_run.py b/micromamba/tests/test_run.py index 1f2d892a06..ba999788dd 100644 --- a/micromamba/tests/test_run.py +++ b/micromamba/tests/test_run.py @@ -28,9 +28,6 @@ def simple_short_program(): class TestRun: - current_root_prefix = os.environ["MAMBA_ROOT_PREFIX"] - current_prefix = os.environ["CONDA_PREFIX"] - @pytest.mark.parametrize("option_flag", common_simple_flags) @pytest.mark.parametrize("make_label_flags", next_label_flags) def test_fail_without_command(self, option_flag, make_label_flags): @@ -100,22 +97,17 @@ def test_run_non_existing_cwd(self): @pytest.fixture() -def temp_env_prefix(): - previous_root_prefix = os.environ["MAMBA_ROOT_PREFIX"] - previous_prefix = os.environ["CONDA_PREFIX"] - +def temp_env_prefix(monkeypatch): env_name = random_string() root_prefix = os.path.expanduser(os.path.join("~", "tmproot" + random_string())) prefix = os.path.join(root_prefix, "envs", env_name) - os.environ["MAMBA_ROOT_PREFIX"] = root_prefix + monkeypatch.setenv("MAMBA_ROOT_PREFIX", root_prefix) create("-p", prefix, "python") yield prefix shutil.rmtree(prefix) - os.environ["MAMBA_ROOT_PREFIX"] = previous_root_prefix - os.environ["CONDA_PREFIX"] = previous_prefix class TestRunVenv: diff --git a/micromamba/tests/test_update.py b/micromamba/tests/test_update.py index 5d9577f156..732a831420 100644 --- a/micromamba/tests/test_update.py +++ b/micromamba/tests/test_update.py @@ -25,15 +25,13 @@ class TestUpdate: medium_old_version = "0.22" @staticmethod - @pytest.fixture(scope="class") - def root(existing_cache): - os.environ["MAMBA_ROOT_PREFIX"] = TestUpdate.root_prefix - os.environ["CONDA_PREFIX"] = TestUpdate.prefix + @pytest.fixture + def root(existing_cache, monkeypatch): + monkeypatch.setenv("MAMBA_ROOT_PREFIX", TestUpdate.root_prefix) + monkeypatch.setenv("CONDA_PREFIX", TestUpdate.prefix) yield - os.environ["MAMBA_ROOT_PREFIX"] = TestUpdate.current_root_prefix - os.environ["CONDA_PREFIX"] = TestUpdate.current_prefix shutil.rmtree(TestUpdate.root_prefix) @staticmethod @@ -239,31 +237,26 @@ class TestUpdateConfig: prefix = os.path.join(root_prefix, "envs", env_name) @staticmethod - @pytest.fixture(scope="class") - def root(existing_cache): - os.environ["MAMBA_ROOT_PREFIX"] = TestUpdateConfig.root_prefix - os.environ["CONDA_PREFIX"] = TestUpdateConfig.prefix + @pytest.fixture + def root(existing_cache, monkeypatch): + monkeypatch.setenv("MAMBA_ROOT_PREFIX", TestUpdateConfig.root_prefix) + monkeypatch.setenv("CONDA_PREFIX", TestUpdateConfig.prefix) + helpers.create("-n", "base", no_dry_run=True) helpers.create("-n", TestUpdateConfig.env_name, "--offline", no_dry_run=True) yield - os.environ["MAMBA_ROOT_PREFIX"] = TestUpdateConfig.current_root_prefix - os.environ["CONDA_PREFIX"] = TestUpdateConfig.current_prefix shutil.rmtree(TestUpdateConfig.root_prefix) @staticmethod @pytest.fixture - def env_created(root): - os.environ["MAMBA_ROOT_PREFIX"] = TestUpdateConfig.root_prefix - os.environ["CONDA_PREFIX"] = TestUpdateConfig.prefix + def env_created(root, monkeypatch): + monkeypatch.setenv("MAMBA_ROOT_PREFIX", TestUpdateConfig.root_prefix) + monkeypatch.setenv("CONDA_PREFIX", TestUpdateConfig.prefix) yield - for v in ("CONDA_CHANNELS", "MAMBA_TARGET_PREFIX"): - if v in os.environ: - os.environ.pop(v) - @classmethod def config_tests(cls, res, root_prefix=root_prefix, target_prefix=prefix): assert res["root_prefix"] == root_prefix @@ -346,11 +339,13 @@ def test_target_prefix( env_var, current_target_prefix_fallback, env_created, + monkeypatch, ): cmd = [] if root_prefix in (None, "cli"): - os.environ["MAMBA_DEFAULT_ROOT_PREFIX"] = os.environ.pop("MAMBA_ROOT_PREFIX") + monkeypatch.setenv("MAMBA_DEFAULT_ROOT_PREFIX", os.environ["MAMBA_ROOT_PREFIX"]) + monkeypatch.delenv("MAMBA_ROOT_PREFIX") if root_prefix == "cli": cmd += ["-r", TestUpdateConfig.root_prefix] @@ -393,13 +388,13 @@ def test_target_prefix( cmd += ["-f", spec_file] if env_var: - os.environ["MAMBA_TARGET_PREFIX"] = p + monkeypatch.setenv("MAMBA_TARGET_PREFIX", p) if not current_target_prefix_fallback: - os.environ.pop("CONDA_PREFIX") - os.environ.pop("CONDA_DEFAULT_ENV") + monkeypatch.delenv("CONDA_PREFIX") + monkeypatch.delenv("CONDA_DEFAULT_ENV") else: - os.environ["CONDA_PREFIX"] = p + monkeypatch.setenv("CONDA_PREFIX", p) if (cli_prefix and cli_env_name) or (yaml_name == "prefix"): with pytest.raises(helpers.subprocess.CalledProcessError): @@ -417,21 +412,22 @@ def test_target_prefix( def test_target_prefix_with_no_settings( self, existing_cache, + monkeypatch, ): # Specify no arg cmd = [] # Get the actual set MAMBA_ROOT_PREFIX when setting up `TestUpdateConfig` class - os.environ["MAMBA_DEFAULT_ROOT_PREFIX"] = os.environ.pop("MAMBA_ROOT_PREFIX") - os.environ.pop("CONDA_PREFIX") - os.environ.pop("CONDA_DEFAULT_ENV") + monkeypatch.setenv("MAMBA_DEFAULT_ROOT_PREFIX", os.environ["MAMBA_ROOT_PREFIX"]) + for env_var in ("MAMBA_ROOT_PREFIX", "CONDA_PREFIX", "CONDA_DEFAULT_ENV"): + monkeypatch.delenv(env_var) # Fallback on root prefix res = helpers.install(*cmd, "--print-config-only") TestUpdateConfig.config_tests( res, - root_prefix=TestUpdateConfig.root_prefix, - target_prefix=TestUpdateConfig.root_prefix, + root_prefix=TestUpdateConfig.current_root_prefix, + target_prefix=TestUpdateConfig.current_root_prefix, ) @pytest.mark.skipif( @@ -441,13 +437,13 @@ def test_target_prefix_with_no_settings( def test_target_prefix_with_no_settings_and_no_env_var( self, existing_cache, + monkeypatch, ): # Specify no arg cmd = [] - os.environ.pop("MAMBA_ROOT_PREFIX") - os.environ.pop("CONDA_PREFIX") - os.environ.pop("CONDA_DEFAULT_ENV") + for env_var in ("MAMBA_ROOT_PREFIX", "CONDA_PREFIX", "CONDA_DEFAULT_ENV"): + monkeypatch.delenv(env_var) # Fallback on root prefix res = helpers.install(*cmd, "--print-config-only") @@ -462,7 +458,7 @@ def test_target_prefix_with_no_settings_and_no_env_var( @pytest.mark.parametrize("yaml", (False, True)) @pytest.mark.parametrize("env_var", (False, True)) @pytest.mark.parametrize("rc_file", (False, True)) - def test_channels(self, cli, yaml, env_var, rc_file, env_created): + def test_channels(self, cli, yaml, env_var, rc_file, env_created, monkeypatch): cmd = [] expected_channels = [] @@ -485,7 +481,7 @@ def test_channels(self, cli, yaml, env_var, rc_file, env_created): expected_channels += ["yaml"] if env_var: - os.environ["CONDA_CHANNELS"] = "env_var" + monkeypatch.setenv("CONDA_CHANNELS", "env_var") expected_channels += ["env_var"] if rc_file: