Skip to content

Commit

Permalink
Implement --md5 and --channel-subdir for non-explicit env export (#2672)
Browse files Browse the repository at this point in the history
Co-authored-by: Johan Mabille <johan.mabille@gmail.com>
  • Loading branch information
jonashaag and JohanMabille committed Sep 1, 2023
1 parent e11fe5a commit 07ce0c4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
21 changes: 16 additions & 5 deletions micromamba/src/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ set_env_command(CLI::App* com, Configuration& config)
init_install_options(create_subcom, config);

static bool explicit_format = false;
static bool no_md5 = false;

static int no_md5 = 0;
static bool no_build = false;
static bool channel_subdir = false;
static bool from_history = false;

auto* export_subcom = com->add_subcommand("export", "Export environment");
Expand All @@ -68,6 +68,7 @@ set_env_command(CLI::App* com, Configuration& config)
export_subcom->add_flag("-e,--explicit", explicit_format, "Use explicit format");
export_subcom->add_flag("--no-md5,!--md5", no_md5, "Disable md5");
export_subcom->add_flag("--no-build,!--build", no_build, "Disable the build string in spec");
export_subcom->add_flag("--channel-subdir", channel_subdir, "Enable channel/subdir in spec");
export_subcom->add_flag(
"--from-history",
from_history,
Expand Down Expand Up @@ -96,7 +97,7 @@ set_env_command(CLI::App* com, Configuration& config)
std::string clean_url, token;
util::split_anaconda_token(record.url, clean_url, token);
std::cout << clean_url;
if (!no_md5)
if (no_md5 != 1)
{
std::cout << "#" << record.md5;
}
Expand All @@ -123,21 +124,31 @@ set_env_command(CLI::App* com, Configuration& config)
continue;
}

const Channel& channel = channel_context.make_channel(v.url);

if (from_history)
{
dependencies << "- " << requested_specs_map[k].str() << "\n";
}
else
{
dependencies << "- " << v.name << "=" << v.version;
dependencies << "- ";
if (channel_subdir)
{
dependencies << channel.name() << "/" << v.subdir << "::";
}
dependencies << v.name << "=" << v.version;
if (!no_build)
{
dependencies << "=" << v.build_string;
}
if (no_md5 == -1)
{
dependencies << "[md5=" << v.md5 << "]";
}
dependencies << "\n";
}

const Channel& channel = channel_context.make_channel(v.url);
channels.insert(channel.name());
}

Expand Down
34 changes: 29 additions & 5 deletions micromamba/tests/test_env.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import shutil
from pathlib import Path

Expand Down Expand Up @@ -59,14 +60,37 @@ def test_register_new_env(tmp_home, tmp_root_prefix):
assert str(env_3_fp) in env_json["envs"]


def test_env_export(tmp_home, tmp_root_prefix):
@pytest.fixture(scope="module")
def export_env():
env_name = "env-create-export"
spec_file = __this_dir__ / "env-create-export.yaml"
helpers.create("-n", env_name, "-f", spec_file)
ret = yaml.safe_load(helpers.run_env("export", "-n", env_name))
assert ret["name"] == env_name
assert set(ret["channels"]) == {"conda-forge"}
assert "micromamba=0.24.0=0" in ret["dependencies"]
return env_name


@pytest.mark.parametrize("channel_subdir_flag", [None, "--channel-subdir"])
@pytest.mark.parametrize("md5_flag", [None, "--md5", "--no-md5"])
@pytest.mark.parametrize("explicit_flag", [None, "--explicit"])
def test_env_export(export_env, explicit_flag, md5_flag, channel_subdir_flag):
flags = filter(None, [explicit_flag, md5_flag, channel_subdir_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:
ret = yaml.safe_load(output)
assert ret["name"] == export_env
assert set(ret["channels"]) == {"conda-forge"}
assert "micromamba=0.24.0=0" in str(ret["dependencies"])
if md5_flag == "--md5":
assert re.search(
r"micromamba=0.24.0=0\[md5=[a-f0-9]{32}\]", str(ret["dependencies"])
)
if channel_subdir_flag:
assert re.search(
r"conda-forge/[a-z0-9-]+::micromamba=0.24.0=0", str(ret["dependencies"])
)


def test_create():
Expand Down

0 comments on commit 07ce0c4

Please sign in to comment.