Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions micromamba/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand All @@ -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()
Expand Down Expand Up @@ -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")))


Expand Down
28 changes: 13 additions & 15 deletions micromamba/tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ def test_target_prefix(
similar_non_canonical,
non_canonical_position,
root_prefix_env_exists,
monkeypatch,
):
cmd = []

Expand Down Expand Up @@ -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)
Expand All @@ -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"
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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")
Expand All @@ -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")

Expand Down
9 changes: 3 additions & 6 deletions micromamba/tests/test_env.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import re
import shutil

Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 3 additions & 8 deletions micromamba/tests/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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":
Expand Down
5 changes: 2 additions & 3 deletions micromamba/tests/test_menuinst.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand Down
Loading
Loading