Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add base flag to info command #3779

Merged
merged 23 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
670491d
add base subcommand
SandrineP Jan 28, 2025
3abf53c
maint: Factor handling of `GetModuleFileNameW` (#3785)
jjerphan Jan 31, 2025
2532535
release libmamba 2.0.6.rc2, micromamba 2.0.6.rc2, libmambapy 2.0.6.rc2
jjerphan Jan 31, 2025
fd4116d
feat: add canonical flag to list command (#3777)
SandrineP Jan 31, 2025
109b47f
ci: Rerun pytest tests on `main` in case of failures (#3769)
jjerphan Feb 4, 2025
d2a444c
fix: Use `libmamba`'s installation instead of `mamba`'s as a fallback…
jjerphan Feb 4, 2025
dac50d8
ci: Fix typo in Windows workflows (#3793)
jjerphan Feb 4, 2025
f201df2
maint: Warn about future removal of `etc/profile.d/mamba.sh` (#3788)
jjerphan Feb 4, 2025
4ef7677
release libmamba 2.0.6.rc3, micromamba 2.0.6.rc3, libmambapy 2.0.6.rc3
jjerphan Feb 4, 2025
1747cd8
release libmamba 2.0.6, micromamba 2.0.6, libmambapy 2.0.6
jjerphan Feb 4, 2025
bc0b7db
feat: add export flag to list command (#3780)
SandrineP Feb 4, 2025
f7d55c8
minor fix
SandrineP Jan 31, 2025
c3f7a42
add base test
SandrineP Feb 3, 2025
ed90b5d
Merge branch 'main' into info_base
SandrineP Feb 6, 2025
d473b05
edit test
SandrineP Feb 6, 2025
8b71bfd
address review commit
SandrineP Feb 17, 2025
3654eea
Merge branch 'main' into info_base
SandrineP Feb 17, 2025
a043994
handle --json case & address review comments
SandrineP Feb 17, 2025
4e56d59
correct handling of --json case and outputs
SandrineP Feb 18, 2025
5e6c092
Merge branch 'main' into info_base
SandrineP Feb 18, 2025
c134eb4
update test
SandrineP Feb 18, 2025
c547bc7
Merge branch 'main' into info_base
SandrineP Feb 18, 2025
fb125ae
address review comment
SandrineP Feb 19, 2025
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
59 changes: 41 additions & 18 deletions libmamba/src/api/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,13 @@ extern "C"

namespace mamba
{
void info(Configuration& config)
{
config.at("use_target_prefix_fallback").set_value(true);
config.at("use_default_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_ALLOW_MISSING_PREFIX | MAMBA_ALLOW_NOT_ENV_PREFIX
);
config.load();

auto channel_context = ChannelContext::make_conda_compatible(config.context());
detail::print_info(config.context(), channel_context, config);

config.operation_teardown();
}

namespace detail
{
struct InfoOptions
{
bool base = false;
};

void info_pretty_print(
std::vector<std::tuple<std::string, nlohmann::json>> items,
const Context::OutputParams& params
Expand Down Expand Up @@ -91,11 +79,26 @@ namespace mamba
Console::instance().json_write(items_map);
}

void print_info(Context& ctx, ChannelContext& channel_context, const Configuration& config)
void print_info(
Context& ctx,
ChannelContext& channel_context,
const Configuration& config,
InfoOptions options
)
{
assert(&ctx == &config.context());

std::vector<std::tuple<std::string, nlohmann::json>> items;

if (options.base)
{
items.push_back({ "base environment", ctx.prefix_params.root_prefix.string() });

info_json_print(items);
info_pretty_print(items, ctx.output_params);
return;
}

items.push_back({ "libmamba version", version() });

if (ctx.command_params.is_mamba_exe && !ctx.command_params.caller_version.empty())
Expand Down Expand Up @@ -189,4 +192,24 @@ namespace mamba
info_pretty_print(items, ctx.output_params);
}
} // detail

void info(Configuration& config)
{
config.at("use_target_prefix_fallback").set_value(true);
config.at("use_default_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_ALLOW_MISSING_PREFIX | MAMBA_ALLOW_NOT_ENV_PREFIX
);
config.load();

detail::InfoOptions options;
options.base = config.at("base").value<bool>();

auto channel_context = ChannelContext::make_conda_compatible(config.context());
detail::print_info(config.context(), channel_context, config, std::move(options));

config.operation_teardown();
}
} // mamba
8 changes: 4 additions & 4 deletions libmamba/src/api/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace mamba

namespace detail
{
struct list_options
struct ListOptions
Copy link
Member

Choose a reason for hiding this comment

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

I agree that the current *_options structs should be PascalCase-named.

{
bool full_name = false;
bool no_pip = false;
Expand Down Expand Up @@ -60,7 +60,7 @@ namespace mamba
}

std::vector<std::string>
get_record_keys(list_options options, const PrefixData::package_map& all_records)
get_record_keys(ListOptions options, const PrefixData::package_map& all_records)
{
std::vector<std::string> keys;

Expand Down Expand Up @@ -121,7 +121,7 @@ namespace mamba
const Context& ctx,
std::string regex,
ChannelContext& channel_context,
list_options options
ListOptions options
)
{
auto sprefix_data = PrefixData::create(
Expand Down Expand Up @@ -297,7 +297,7 @@ namespace mamba
);
config.load();

detail::list_options options;
detail::ListOptions options;
options.full_name = config.at("full_name").value<bool>();
options.no_pip = config.at("no_pip").value<bool>();
options.reverse = config.at("reverse").value<bool>();
Expand Down
6 changes: 6 additions & 0 deletions micromamba/src/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// The full license is in the file LICENSE, distributed with this software.

#include "mamba/api/configuration.hpp"
#include "mamba/api/info.hpp"
#include "mamba/core/context.hpp"

Expand All @@ -22,6 +23,11 @@ set_info_command(CLI::App* subcom, mamba::Configuration& config)
init_info_parser(subcom, config);
static bool print_licenses;

auto& base = config.insert(
mamba::Configurable("base", false).group("cli").description("Display base environment path.")
);
subcom->add_flag("--base", base.get_cli_config<bool>(), base.description());

subcom->add_flag("--licenses", print_licenses, "Print licenses");

subcom->callback(
Expand Down
55 changes: 52 additions & 3 deletions micromamba/tests/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@


@pytest.mark.parametrize("prefix_selection", [None, "prefix", "name"])
def test_base(tmp_home, tmp_root_prefix, prefix_selection):
os.environ["CONDA_PREFIX"] = str(tmp_root_prefix)
def test_base(tmp_home, tmp_root_prefix, prefix_selection, monkeypatch):
monkeypatch.setenv("CONDA_PREFIX", str(tmp_root_prefix))

if prefix_selection == "prefix":
infos = helpers.info("-p", tmp_root_prefix)
Expand Down Expand Up @@ -71,10 +71,59 @@ def test_not_env(tmp_home, tmp_root_prefix, prefix_selection, existing_prefix):
else:
expected_name = name + " (not found)"
location = prefix
print(infos)

assert f"envs directories : {tmp_root_prefix / 'envs'}" in infos
assert f"environment : {expected_name}" in infos
assert f"env location : {location}" in infos
assert f"user config files : {tmp_home / '.mambarc'}" in infos
assert f"base environment : {tmp_root_prefix}" in infos


@pytest.mark.parametrize("base_flag", ["", "--base"])
@pytest.mark.parametrize("json_flag", ["", "--json"])
@pytest.mark.parametrize("prefix_selection", [None, "prefix", "name"])
def test_base_subcommand(
tmp_home, tmp_root_prefix, prefix_selection, base_flag, json_flag, monkeypatch
):
monkeypatch.setenv("CONDA_PREFIX", str(tmp_root_prefix))

if prefix_selection == "prefix":
infos = helpers.info("-p", tmp_root_prefix, base_flag, json_flag)
elif prefix_selection == "name":
infos = helpers.info("-n", "base", base_flag, json_flag)
else:
infos = helpers.info(base_flag, json_flag)

items = [
"libmamba version",
"mamba version",
"curl version",
"libarchive version",
"envs directories",
"package cache",
"environment",
"env location",
"user config files",
"populated config files",
"user config files",
"virtual packages",
"channels",
"platform",
]
if base_flag == "--base":
if json_flag == "--json":
assert all(i not in infos.keys() for i in items)
base_environment_path = infos["base environment"].strip()
else:
assert all(
(f"{i} :" not in infos) | (f"\n{i} :" not in infos) for i in items
) # f"\n{i} :" is to handle the case of the "environment" item
base_environment_path = infos.replace("base environment :", "").strip()
assert os.path.exists(base_environment_path)
assert base_environment_path == str(tmp_root_prefix)
else:
items += ["base environment"]
if json_flag == "--json":
assert all(i in infos.keys() for i in items)
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick: This tests that items is a subset of infos.keys() but I guess this is sufficient

This comment also applies elsewhere.

else:
assert all(f"{i} :" in infos for i in items)
Loading