Skip to content

Commit d6d74d8

Browse files
committed
maint: Handle environment vars in micromamba/tests with monkeypatch
1 parent 7cdf568 commit d6d74d8

File tree

9 files changed

+92
-111
lines changed

9 files changed

+92
-111
lines changed

micromamba/tests/test_config.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def rc_file(
6868
tmp_prefix,
6969
tmp_path,
7070
user_config_dir,
71+
monkeypatch,
7172
):
7273
"""Parametrizable fixture to create an rc file at the desired location.
7374
@@ -85,7 +86,7 @@ def rc_file(
8586
elif where == "user_config_dir":
8687
rc_file = user_config_dir / rc_filename
8788
elif where == "env_set_xdg":
88-
os.environ["XDG_CONFIG_HOME"] = str(tmp_home / "custom_xdg_config_dir")
89+
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_home / "custom_xdg_config_dir"))
8990
rc_file = tmp_home / "custom_xdg_config_dir" / "mamba" / rc_filename
9091
elif where == "absolute":
9192
rc_file = Path(rc_filename)
@@ -255,31 +256,27 @@ def test_list_with_groups(self, rc_file, group_flag):
255256
" - channel1\n - channel2\n".splitlines()
256257
)
257258

258-
def test_env_vars(self):
259-
os.environ["MAMBA_OFFLINE"] = "true"
259+
def test_env_vars(self, monkeypatch):
260+
monkeypatch.setenv("MAMBA_OFFLINE", "true")
260261
assert (
261262
config("list", "offline", "--no-rc", "-s").splitlines()
262263
== "offline: true # 'MAMBA_OFFLINE'".splitlines()
263264
)
264265

265-
os.environ["MAMBA_OFFLINE"] = "false"
266+
monkeypatch.setenv("MAMBA_OFFLINE", "false")
266267
assert (
267268
config("list", "offline", "--no-rc", "-s").splitlines()
268269
== "offline: false # 'MAMBA_OFFLINE'".splitlines()
269270
)
270-
os.environ.pop("MAMBA_OFFLINE")
271-
272-
def test_no_env(self):
273-
os.environ["MAMBA_OFFLINE"] = "false"
274271

272+
def test_no_env(self, monkeypatch):
273+
monkeypatch.setenv("MAMBA_OFFLINE", "false")
275274
assert (
276275
config("list", "offline", "--no-rc", "--no-env", "-s", "--offline").splitlines()
277276
== "offline: true # 'CLI'".splitlines()
278277
)
279278

280-
os.environ.pop("MAMBA_OFFLINE")
281-
282-
def test_precedence(self):
279+
def test_precedence(self, monkeypatch):
283280
rc_dir = os.path.expanduser(os.path.join("~", "test_mamba", helpers.random_string()))
284281
os.makedirs(rc_dir, exist_ok=True)
285282
rc_file = os.path.join(rc_dir, ".mambarc")
@@ -289,15 +286,12 @@ def test_precedence(self):
289286
f.write("offline: true")
290287

291288
try:
292-
if "MAMBA_OFFLINE" in os.environ:
293-
os.environ.pop("MAMBA_OFFLINE")
294-
295289
assert (
296290
config("list", "offline", f"--rc-file={rc_file}", "-s").splitlines()
297291
== f"offline: true # '{short_rc_file}'".splitlines()
298292
)
299293

300-
os.environ["MAMBA_OFFLINE"] = "false"
294+
monkeypatch.setenv("MAMBA_OFFLINE", "false")
301295
assert (
302296
config("list", "offline", "--no-rc", "-s").splitlines()
303297
== "offline: false # 'MAMBA_OFFLINE'".splitlines()
@@ -334,8 +328,6 @@ def test_precedence(self):
334328
== "offline: true # 'CLI'".splitlines()
335329
)
336330
finally:
337-
if "MAMBA_OFFLINE" in os.environ:
338-
os.environ.pop("MAMBA_OFFLINE")
339331
shutil.rmtree(os.path.expanduser(os.path.join("~", "test_mamba")))
340332

341333

micromamba/tests/test_create.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ def test_target_prefix(
294294
similar_non_canonical,
295295
non_canonical_position,
296296
root_prefix_env_exists,
297+
monkeypatch,
297298
):
298299
cmd = []
299300

@@ -352,12 +353,12 @@ def test_target_prefix(
352353
cmd += ["-f", spec_file]
353354

354355
if env_var:
355-
os.environ["MAMBA_TARGET_PREFIX"] = str(p)
356+
monkeypatch.setenv("MAMBA_TARGET_PREFIX", str(p))
356357

357358
if not current_target_prefix_fallback:
358-
os.environ.pop("CONDA_PREFIX", None)
359+
monkeypatch.delenv("CONDA_PREFIX", raising=False)
359360
else:
360-
os.environ["CONDA_PREFIX"] = str(p)
361+
monkeypatch.setenv("CONDA_PREFIX", str(p))
361362

362363
if (
363364
(cli_prefix and cli_env_name)
@@ -376,7 +377,7 @@ def test_target_prefix(
376377
@pytest.mark.parametrize("yaml", (False, True))
377378
@pytest.mark.parametrize("env_var", (False, True))
378379
@pytest.mark.parametrize("rc_file", (False, True))
379-
def test_channels(tmp_home, tmp_root_prefix, tmp_path, cli, yaml, env_var, rc_file):
380+
def test_channels(tmp_home, tmp_root_prefix, tmp_path, cli, yaml, env_var, rc_file, monkeypatch):
380381
env_prefix = tmp_path / "myenv"
381382
spec_file = tmp_path / "env.yaml"
382383
rc_file = tmp_path / "rc.yaml"
@@ -400,7 +401,7 @@ def test_channels(tmp_home, tmp_root_prefix, tmp_path, cli, yaml, env_var, rc_fi
400401
expected_channels += ["yaml"]
401402

402403
if env_var:
403-
os.environ["CONDA_CHANNELS"] = "env_var"
404+
monkeypatch.setenv("CONDA_CHANNELS", "env_var")
404405
expected_channels += ["env_var"]
405406

406407
if rc_file:
@@ -622,9 +623,9 @@ def test_create_base(tmp_home, tmp_root_prefix, already_exists, is_conda_env, ha
622623
reason="Running only ultra-dry tests",
623624
)
624625
@pytest.mark.parametrize("outside_root_prefix", (False, True))
625-
def test_classic_specs(tmp_home, tmp_root_prefix, tmp_path, outside_root_prefix):
626+
def test_classic_specs(tmp_home, tmp_root_prefix, tmp_path, outside_root_prefix, monkeypatch):
626627
tmp_pkgs_dirs = tmp_path / "cache"
627-
os.environ["CONDA_PKGS_DIRS"] = str(tmp_pkgs_dirs)
628+
monkeypatch.setenv("CONDA_PKGS_DIRS", str(tmp_pkgs_dirs))
628629
if outside_root_prefix:
629630
p = tmp_path / "myenv"
630631
else:
@@ -941,18 +942,15 @@ def assert_env_exists(prefix_path):
941942
)
942943
@pytest.mark.parametrize("shared_pkgs_dirs", [True], indirect=True)
943944
@pytest.mark.parametrize("source", ["cli", "env_var", "rc_file"])
944-
def test_always_yes(tmp_home, tmp_root_prefix, tmp_path, source):
945+
def test_always_yes(tmp_home, tmp_root_prefix, tmp_path, source, monkeypatch):
945946
env_name = "myenv"
946947
helpers.create("-n", env_name, "xtensor", no_dry_run=True)
947948

948949
if source == "cli":
949950
res = helpers.create("-n", env_name, "xtensor", "--json", always_yes=True)
950951
elif source == "env_var":
951-
try:
952-
os.environ["MAMBA_ALWAYS_YES"] = "true"
953-
res = helpers.create("-n", env_name, "xtensor", "--json", always_yes=False)
954-
finally:
955-
os.environ.pop("MAMBA_ALWAYS_YES")
952+
monkeypatch.setenv("MAMBA_ALWAYS_YES", "true")
953+
res = helpers.create("-n", env_name, "xtensor", "--json", always_yes=False)
956954
else: # rc_file
957955
rc_file = tmp_path / "config.yaml"
958956
with open(rc_file, "w") as f:
@@ -1090,7 +1088,7 @@ def test_spec_with_multichannel(tmp_home, tmp_root_prefix):
10901088
helpers.create("-n", "myenv", "defaults::zlib", "--dry-run")
10911089

10921090

1093-
def test_spec_with_slash_in_channel(tmp_home, tmp_root_prefix):
1091+
def test_spec_with_slash_in_channel(tmp_home, tmp_root_prefix, monkeypatch):
10941092
"https://github.com/mamba-org/mamba/pull/2926"
10951093
with pytest.raises(subprocess.CalledProcessError) as info:
10961094
helpers.create("-n", "env1", "pkgs/main/noarch::python", "--dry-run")
@@ -1101,7 +1099,7 @@ def test_spec_with_slash_in_channel(tmp_home, tmp_root_prefix):
11011099
assert "The following package could not be installed" in msg
11021100
assert "python" in msg
11031101

1104-
os.environ["CONDA_SUBDIR"] = "linux-64"
1102+
monkeypatch.setenv("CONDA_SUBDIR", "linux-64")
11051103
helpers.create("-n", "env2", "pkgs/main/linux-64::python", "--dry-run")
11061104
helpers.create("-n", "env3", "pkgs/main::python", "--dry-run")
11071105

micromamba/tests/test_env.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
import re
32
import shutil
43

@@ -22,7 +21,7 @@ def test_env_list(tmp_home, tmp_root_prefix, tmp_empty_env):
2221
assert str(tmp_empty_env) in env_json["envs"]
2322

2423

25-
def test_env_list_table(tmp_home, tmp_root_prefix, tmp_prefix):
24+
def test_env_list_table(tmp_home, tmp_root_prefix, tmp_prefix, monkeypatch):
2625
res = helpers.run_env("list")
2726

2827
assert "Name" in res
@@ -35,8 +34,6 @@ def test_env_list_table(tmp_home, tmp_root_prefix, tmp_prefix):
3534
active_env_l = line
3635
assert str(tmp_root_prefix) in active_env_l
3736

38-
os.environ["CONDA_PREFIX"] = str(tmp_prefix)
39-
4037
res = helpers.run_env("list")
4138

4239
all_lines = res.splitlines()
@@ -470,10 +467,10 @@ def test_env_create_whitespace(tmp_home, tmp_root_prefix, tmp_path):
470467

471468

472469
@pytest.mark.parametrize("shared_pkgs_dirs", [True], indirect=True)
473-
def test_env_update_empty_base(tmp_home, tmp_root_prefix, tmp_path):
470+
def test_env_update_empty_base(tmp_home, tmp_root_prefix, tmp_path, monkeypatch):
474471
env_prefix = tmp_path / "env-update-empty-base"
475472

476-
os.environ["MAMBA_ROOT_PREFIX"] = str(env_prefix)
473+
monkeypatch.setenv("MAMBA_ROOT_PREFIX", str(env_prefix))
477474

478475
env_file_yml = tmp_path / "test_env_empty_base.yaml"
479476
env_file_yml.write_text(env_yaml_content_to_update_empty_base)

micromamba/tests/test_info.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_env(tmp_home, tmp_root_prefix, tmp_env_name, tmp_prefix, prefix_selecti
4040

4141
@pytest.mark.parametrize("existing_prefix", [False, True])
4242
@pytest.mark.parametrize("prefix_selection", [None, "env_var", "prefix", "name"])
43-
def test_not_env(tmp_home, tmp_root_prefix, prefix_selection, existing_prefix):
43+
def test_not_env(tmp_home, tmp_root_prefix, prefix_selection, existing_prefix, monkeypatch):
4444
name = "not_an_env"
4545
prefix = tmp_root_prefix / "envs" / name
4646

@@ -52,10 +52,9 @@ def test_not_env(tmp_home, tmp_root_prefix, prefix_selection, existing_prefix):
5252
elif prefix_selection == "name":
5353
infos = helpers.info("-n", name)
5454
elif prefix_selection == "env_var":
55-
os.environ["CONDA_PREFIX"] = str(prefix)
55+
monkeypatch.setenv("CONDA_PREFIX", str(prefix))
5656
infos = helpers.info()
5757
else:
58-
os.environ.pop("CONDA_PREFIX", "")
5958
infos = helpers.info()
6059

6160
if prefix_selection is None:
@@ -131,11 +130,7 @@ def flags_test(tmp_root_prefix, infos, base_flag, envs_flag, json_flag):
131130
@pytest.mark.parametrize("envs_flag", ["", "-e", "--envs"])
132131
@pytest.mark.parametrize("json_flag", ["", "--json"])
133132
@pytest.mark.parametrize("prefix_selection", [None, "prefix", "name"])
134-
def test_base_flags(
135-
tmp_home, tmp_root_prefix, prefix_selection, base_flag, envs_flag, json_flag, monkeypatch
136-
):
137-
monkeypatch.setenv("CONDA_PREFIX", str(tmp_root_prefix))
138-
133+
def test_base_flags(tmp_home, tmp_root_prefix, prefix_selection, base_flag, envs_flag, json_flag):
139134
if prefix_selection == "prefix":
140135
infos = helpers.info("-p", tmp_root_prefix, base_flag, envs_flag, json_flag)
141136
elif prefix_selection == "name":

micromamba/tests/test_menuinst.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ def test_simple_shortcut(self):
5959
not sys.platform.startswith("win"),
6060
reason="skipping windows-only tests",
6161
)
62-
def test_shortcut_weird_env(self):
62+
def test_shortcut_weird_env(self, monkeypatch):
6363
# note Umlauts do not work yet
64-
os.environ["MAMBA_ROOT_PREFIX"] = str(Path("./compl i c ted").absolute())
64+
monkeypatch.setenv("MAMBA_ROOT_PREFIX", str(Path("./compl i c ted").absolute()))
6565
root_prefix = os.environ["MAMBA_ROOT_PREFIX"]
6666

6767
env_name = random_string()
@@ -97,7 +97,6 @@ def test_shortcut_weird_env(self):
9797
assert not os.path.exists(lnk)
9898

9999
shutil.rmtree(root_prefix)
100-
os.environ["MAMBA_ROOT_PREFIX"] = self.root_prefix
101100

102101
# Testing a package (spyder) using menuinst v2 schema
103102
@pytest.mark.skipif(

0 commit comments

Comments
 (0)