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
9 changes: 6 additions & 3 deletions libmamba/data/mamba.xsh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ _REACTIVATE_COMMANDS = ('install', 'update', 'upgrade', 'remove', 'uninstall')
def _parse_args(args=None):
from argparse import ArgumentParser
p = ArgumentParser(add_help=False)
p.add_argument('command')
p.add_argument('command', nargs='?')
p.add_argument('-h', '--help', dest='help', action='store_true', default=False)
p.add_argument('-v', '--version', dest='version', action='store_true', default=False)
ns, _ = p.parse_known_args(args)
if ns.command == 'activate':
p.add_argument('env_name_or_prefix', default='base')
Expand All @@ -44,9 +46,9 @@ def _raise_pipeline_error(pipeline):
def _mamba_activate_handler(env_name_or_prefix=None):
if env_name_or_prefix == 'base' or not env_name_or_prefix:
env_name_or_prefix = $MAMBA_ROOT_PREFIX
__xonsh__.execer.exec($($MAMBA_EXE shell activate -s xonsh -p @(env_name_or_prefix)),
__xonsh__.execer.exec($($MAMBA_EXE shell activate -s xonsh @(env_name_or_prefix)),
glbs=__xonsh__.ctx,
filename="$($MAMBA_EXE shell activate -s xonsh -p " + env_name_or_prefix + ")")
filename="$($MAMBA_EXE shell activate -s xonsh " + env_name_or_prefix + ")")


def _mamba_deactivate_handler():
Expand Down Expand Up @@ -91,6 +93,7 @@ if 'CONDA_SHLVL' not in ${...}:


aliases['micromamba'] = _micromamba_main
aliases['mamba'] = _micromamba_main


@contextual_command_completer
Expand Down
2 changes: 1 addition & 1 deletion libmamba/src/core/activation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ namespace mamba

for (const std::string& uvar : env_transform.unset_vars)
{
out << "del $" << uvar << "\n";
out << "try:\n del $" << uvar << "\nexcept KeyError:\n pass\n";
}

for (const auto& [skey, svar] : env_transform.set_vars)
Expand Down
62 changes: 62 additions & 0 deletions micromamba/tests/test_activation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import pathlib
import platform
import re
import shutil
import subprocess
import tempfile
Expand Down Expand Up @@ -933,6 +934,67 @@ def test_activate_envs_dirs(
assert any([env_name in p for p in dict_res.values()])


@pytest.mark.skipif(
"xonsh" not in valid_interpreters,
reason="xonsh not available",
)
@pytest.mark.parametrize("alias", ["micromamba", "mamba"])
def test_xonsh_help_and_version(tmp_home, tmp_root_prefix, tmp_path, alias):
umamba = helpers.get_umamba()

s = [f"{umamba} shell init -r {tmp_root_prefix} -s xonsh"]
call_interpreter(s, tmp_path, "xonsh")

def call(s):
return call_interpreter(s, tmp_path, "xonsh", interactive=True)

s = [f"{alias} --help"]
stdout, stderr = call(s)
assert not stderr, f"stderr was not empty: {stderr}"
assert "--help" in stdout
assert "Print this help message and exit" in stdout

s = [f"{alias} --version"]
stdout, stderr = call(s)
assert not stderr, f"stderr was not empty: {stderr}"
assert re.search(r"\d+\.\d+\.\d+", stdout.strip()), f"not a version: {stdout}"


@pytest.mark.skipif(
"xonsh" not in valid_interpreters,
reason="xonsh not available",
)
@pytest.mark.parametrize("alias", ["micromamba", "mamba"])
def test_xonsh_del_nonexistent_env_var(tmp_home, tmp_root_prefix, tmp_path, alias):
umamba = helpers.get_umamba()

s = [f"{umamba} shell init -r {tmp_root_prefix} -s xonsh"]
call_interpreter(s, tmp_path, "xonsh")

def call(s):
return call_interpreter(s, tmp_path, "xonsh", interactive=True)

helpers.create("-n", "test_unset_env", "--offline", "--no-rc", no_dry_run=True)

prefix = tmp_root_prefix / "envs" / "test_unset_env"
state_file = prefix / "conda-meta" / "state"
state_file.write_text(helpers.json.dumps({"env_vars": {"MAMBA_UNSET_TEST": "hello"}}))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also do you think that it is possible to test the case where MAMBA_UNSET_TEST:

  • is set initially to some value (e.g. "hi")
  • is reset by the activation of test_unset_env to "hello"
  • is deleted under this environment
  • is reset to the original value ("hi") after the environment is deactivated?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would be testing restoring environment variables behavior with activation and deactivation, and therefore is a more general test.
I suggest to rather open an issue to check the existence of such tests and add them if not.


# activate → manually delete var → deactivate
s = [
f"{alias} activate test_unset_env",
"del $MAMBA_UNSET_TEST",
f"{alias} deactivate",
]
Comment on lines +983 to +988

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also try to use the -p flag to verify the support of this flag after the fix?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm not sure I get what you mean.
So shell activate was using -p in mamba.xsh while it's supposed to be a positional argument. The fix is to call that command properly in the script, and the test for that is to check there is no warning anymore:
assert "does not contain any filesystem separator" not in stderr


try:
stdout, stderr = call(s)
except subprocess.CalledProcessError:
pytest.fail("deactivate crashed on del of non-existent env var")

assert "does not contain any filesystem separator" not in stderr


@pytest.fixture
def tmp_umamba():
mamba_exe = helpers.get_umamba()
Expand Down
Loading