feat: add base flag to info command (#3779)

Signed-off-by: Julien Jerphanion <git@jjerphan.xyz>
Co-authored-by: Julien Jerphanion <git@jjerphan.xyz>
Co-authored-by: Klaim <Klaim@users.noreply.github.com>
Co-authored-by: Johan Mabille <johan.mabille@gmail.com>
Co-authored-by: Hind Montassif <hind.montassif@gmail.com>
This commit is contained in:
Sandrine Pataut 2025-02-19 12:37:42 +01:00 committed by GitHub
parent 746360cb15
commit 2bea5a283b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 103 additions and 25 deletions

View File

@ -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
@ -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())
@ -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

View File

@ -20,7 +20,7 @@ namespace mamba
namespace detail
{
struct list_options
struct ListOptions
{
bool full_name = false;
bool no_pip = false;
@ -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;
@ -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(
@ -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>();

View File

@ -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"
@ -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(

View File

@ -6,8 +6,8 @@ from . import helpers
@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)
@ -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)
else:
assert all(f"{i} :" in infos for i in items)