Skip to content

Commit

Permalink
fix: Export 'channels' as part of environments' export (#3587)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathbunnyru authored Nov 18, 2024
1 parent 333abd0 commit ccbf795
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
16 changes: 6 additions & 10 deletions micromamba/src/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,14 @@ set_env_command(CLI::App* com, Configuration& config)

std::cout << "{\n";

if (!channels.empty())
std::cout << " \"channels\": [\n";
for (auto channel_it = channels.begin(); channel_it != channels.end(); ++channel_it)
{
std::cout << " \"channels\": [\n";
for (auto channel_it = channels.begin(); channel_it != channels.end();
++channel_it)
{
auto last_channel = std::next(channel_it) == channels.end();
std::cout << " \"" << *channel_it << "\"" << (last_channel ? "" : ",")
<< "\n";
}
std::cout << " ],\n";
auto last_channel = std::next(channel_it) == channels.end();
std::cout << " \"" << *channel_it << "\"" << (last_channel ? "" : ",") << "\n";
}
std::cout << " ],\n";

std::cout << " \"dependencies\": [\n" << dependencies.str() << " ],\n";

std::cout << " \"name\": \"" << get_env_name(ctx, ctx.prefix_params.target_prefix)
Expand Down
32 changes: 24 additions & 8 deletions micromamba/tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@ def test_register_new_env(tmp_home, tmp_root_prefix):
assert str(env_3_fp) in env_json["envs"]


@pytest.fixture(scope="module")
def empty_env():
env_name = "env-empty"
helpers.create("-n", env_name)
return env_name


@pytest.mark.parametrize("json_flag", [None, "--json"])
def test_env_export_empty(json_flag, empty_env):
flags = filter(None, [json_flag])
output = helpers.run_env("export", "-n", empty_env, *flags)

# json is already parsed
ret = output if json_flag else yaml.safe_load(output)
assert ret["name"] == empty_env
assert empty_env in ret["prefix"]
assert not ret["channels"]


@pytest.fixture(scope="module")
def export_env():
env_name = "env-create-export"
Expand Down Expand Up @@ -90,27 +109,24 @@ def test_env_export_from_history(json_flag, export_env):
@pytest.mark.parametrize("no_build_flag", [None, "--no-build", "--no-builds"])
@pytest.mark.parametrize("json_flag", [None, "--json"])
def test_env_export(
export_env, json_flag, no_build_flag, explicit_flag, md5_flag, channel_subdir_flag
channel_subdir_flag, md5_flag, explicit_flag, no_build_flag, json_flag, export_env
):
if explicit_flag and json_flag:
# `--explicit` has precedence over `--json`, which is tested bellow.
# But we need to omit here to avoid `helpers.run_env` to parse the output as JSON and fail.
json_flag = None

flags = filter(None, [no_build_flag, json_flag, explicit_flag, md5_flag, channel_subdir_flag])
flags = filter(None, [channel_subdir_flag, md5_flag, explicit_flag, no_build_flag, json_flag])
output = helpers.run_env("export", "-n", export_env, *flags)
if explicit_flag:
assert "/micromamba-0.24.0-0." in output
if md5_flag != "--no-md5":
assert re.search("#[a-f0-9]{32}$", output.replace("\r", ""))
else:
if json_flag:
# Already parsed
ret = output
else:
ret = yaml.safe_load(output)
# json is already parsed
ret = output if json_flag else yaml.safe_load(output)
assert ret["name"] == export_env
assert "env-create-export" in ret["prefix"]
assert export_env in ret["prefix"]
assert set(ret["channels"]) == {"conda-forge"}
micromamba_spec_prefix = "micromamba=0.24.0" if no_build_flag else "micromamba=0.24.0=0"
assert micromamba_spec_prefix in str(ret["dependencies"])
Expand Down

0 comments on commit ccbf795

Please sign in to comment.