Skip to content

Commit

Permalink
feat: add export flag to list command (#3780)
Browse files Browse the repository at this point in the history
  • Loading branch information
SandrineP authored Feb 4, 2025
1 parent 09f87f1 commit b5e197f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
22 changes: 21 additions & 1 deletion libmamba/src/api/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace mamba
bool explicit_ = false;
bool md5 = false;
bool canonical = false;
bool export_ = false;
};

struct formatted_pkg
Expand Down Expand Up @@ -218,7 +219,13 @@ namespace mamba
{
if (options.canonical)
{
LOG_WARNING << "Option --canonical ignored because of --explicit";
LOG_WARNING
<< "Option --canonical ignored because --explicit was also provided.";
}
if (options.export_)
{
LOG_WARNING
<< "Option --export ignored because --explicit was also provided.";
}
for (auto p : packages)
{
Expand All @@ -234,12 +241,24 @@ namespace mamba
}
else if (options.canonical)
{
if (options.export_)
{
LOG_WARNING
<< "Option --export ignored because --canonical was also provided.";
}
for (auto p : packages)
{
std::cout << p.channel << "/" << p.platform << "::" << p.name << "-"
<< p.version << "-" << p.build_string << std::endl;
}
}
else if (options.export_)
{
for (auto p : packages)
{
std::cout << p.name << "=" << p.version << "=" << p.build_string << std::endl;
}
}
else
{
auto requested_specs = prefix_data.history().get_requested_specs_map();
Expand Down Expand Up @@ -285,6 +304,7 @@ namespace mamba
options.explicit_ = config.at("explicit").value<bool>();
options.md5 = config.at("md5").value<bool>();
options.canonical = config.at("canonical").value<bool>();
options.export_ = config.at("export").value<bool>();

auto channel_context = ChannelContext::make_conda_compatible(config.context());
detail::list_packages(config.context(), regex, channel_context, std::move(options));
Expand Down
12 changes: 11 additions & 1 deletion micromamba/src/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,19 @@ init_list_parser(CLI::App* subcom, Configuration& config)
auto& canonical = config.insert(
Configurable("canonical", false)
.group("cli")
.description("Output canonical names of packages only. Ignored if --explicit.")
.description("Output canonical names of packages only. Ignored if --explicit is also provided."
)
);
subcom->add_flag("-c,--canonical", canonical.get_cli_config<bool>(), canonical.description());

auto& export_ = config.insert(
Configurable("export", false)
.group("cli")
.description(
"Output explicit, machine-readable requirement strings instead of human-readable lists of packages. Ignored if --explicit or --canonical is also provided."
)
);
subcom->add_flag("-e,--export", export_.get_cli_config<bool>(), export_.description());
}

void
Expand Down
29 changes: 20 additions & 9 deletions micromamba/tests/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def test_list_no_json(
@pytest.mark.parametrize("explicit_flag", ["", "--explicit"])
@pytest.mark.parametrize("md5_flag", ["", "--md5"])
@pytest.mark.parametrize("canonical_flag", ["", "-c", "--canonical"])
@pytest.mark.parametrize("export_flag", ["", "-e", "--export"])
@pytest.mark.parametrize("env_selector", ["", "name", "prefix"])
@pytest.mark.parametrize("shared_pkgs_dirs", [True], indirect=True)
def test_list_subcommands(
Expand All @@ -93,28 +94,38 @@ def test_list_subcommands(
explicit_flag,
md5_flag,
canonical_flag,
export_flag,
):
if env_selector == "prefix":
res = helpers.umamba_list("-p", tmp_xtensor_env, explicit_flag, md5_flag, canonical_flag)
res = helpers.umamba_list(
"-p", tmp_xtensor_env, explicit_flag, md5_flag, canonical_flag, export_flag
)
elif env_selector == "name":
res = helpers.umamba_list("-n", tmp_env_name, explicit_flag, md5_flag, canonical_flag)
res = helpers.umamba_list(
"-n", tmp_env_name, explicit_flag, md5_flag, canonical_flag, export_flag
)
else:
res = helpers.umamba_list(explicit_flag, md5_flag, canonical_flag)
res = helpers.umamba_list(explicit_flag, md5_flag, canonical_flag, export_flag)

outputs_list = res.strip().split("\n")[2:]
outputs_list = [i for i in outputs_list if i != "" and not i.startswith("Warning")]
items = ["conda-forge/", "::"]
if explicit_flag == "--explicit":
for output in outputs_list:
assert "/conda-forge/" in output
if md5_flag == "--md5":
assert "#" in output
else:
assert "#" not in output
else:
if canonical_flag == "--canonical":
items = ["conda-forge/", "::"]
for output in outputs_list:
assert all(i in output for i in items)
assert " " not in output
elif canonical_flag in ["-c", "--canonical"]:
for output in outputs_list:
assert all(i in output for i in items)
assert " " not in output
elif export_flag in ["-e", "--export"]:
items += [" "]
for output in outputs_list:
assert all(i not in output for i in items)
assert len(output.split("=")) == 3


@pytest.mark.parametrize("quiet_flag", ["", "-q", "--quiet"])
Expand Down

0 comments on commit b5e197f

Please sign in to comment.