Always add `root_prefix/envs` in `envs_dirs` (#3692)

This commit is contained in:
Hind-M 2024-12-18 12:25:10 +01:00 committed by GitHub
parent 7f3f481b47
commit 9c61b5f71c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 49 additions and 11 deletions

View File

@ -965,13 +965,17 @@ namespace mamba
}
}
std::vector<fs::u8path> fallback_envs_dirs_hook(const Context& context)
void envs_dirs_hook(const Context& context, std::vector<fs::u8path>& dirs)
{
return { context.prefix_params.root_prefix / "envs" };
}
// Check that "root_prefix/envs" is already in the dirs,
// and append if not - to match `conda`
fs::u8path default_env_dir = context.prefix_params.root_prefix / "envs";
if (std::find(dirs.begin(), dirs.end(), default_env_dir) == dirs.end())
{
dirs.push_back(default_env_dir);
}
void envs_dirs_hook(std::vector<fs::u8path>& dirs)
{
// Check that the values exist as directories
for (auto& d : dirs)
{
d = fs::weakly_canonical(util::expand_home(d.string())).string();
@ -1297,10 +1301,10 @@ namespace mamba
.set_rc_configurable(RCConfigLevel::kHomeDir)
.set_env_var_names({ "CONDA_ENVS_DIRS" })
.needs({ "root_prefix" })
.set_fallback_value_hook<decltype(m_context.envs_dirs)>(
[this] { return detail::fallback_envs_dirs_hook(m_context); }
.set_post_merge_hook<decltype(m_context.envs_dirs)>(
[this](decltype(m_context.envs_dirs)& value)
{ return detail::envs_dirs_hook(m_context, value); }
)
.set_post_merge_hook(detail::envs_dirs_hook)
.description("Possible locations of named environments"));
insert(Configurable("pkgs_dirs", &m_context.pkgs_dirs)

View File

@ -93,6 +93,14 @@ namespace mamba
return util::shrink_home(config.valid_sources()[position].string());
}
const std::string get_root_prefix_envs_dir()
{
return util::path_concat(
std::string(config.at("root_prefix").value<mamba::fs::u8path>()),
"envs"
);
}
std::unique_ptr<TemporaryFile> tempfile_ptr = std::make_unique<TemporaryFile>(
"mambarc",
".yaml"
@ -538,6 +546,33 @@ namespace mamba
- https://repo.mamba.pm/conda-forge)"));
}
TEST_CASE_METHOD(Configuration, "envs_dirs")
{
// Load default config
config.load();
// `envs_dirs` should be set to `root_prefix / envs`
const auto& envs_dirs = config.at("envs_dirs").value<std::vector<fs::u8path>>();
REQUIRE(envs_dirs.size() == 1);
REQUIRE(envs_dirs[0] == get_root_prefix_envs_dir());
}
TEST_CASE_METHOD(Configuration, "envs_dirs_with_additional_rc")
{
std::string cache1 = util::path_concat(util::user_home_dir(), "foo_envs_dirs");
std::string rc1 = "envs_dirs:\n - " + cache1;
load_test_config(rc1);
// `envs_dirs` should be set to the configured value `cache1`
// and `root_prefix / envs`
REQUIRE(
config.dump()
== "envs_dirs:\n - " + cache1 + "\n - " + get_root_prefix_envs_dir()
);
}
TEST_CASE_METHOD(Configuration, "pkgs_dirs")
{
std::string cache1 = util::path_concat(util::user_home_dir(), "foo");

View File

@ -40,9 +40,6 @@ init_channel_parser(CLI::App* subcom, mamba::Configuration& config);
void
load_channel_options(mamba::Context& ctx);
void
channels_hook(std::vector<std::string>& channels);
void
override_channels_hook(mamba::Configuration& config, bool& override_channels);

View File

@ -31,6 +31,7 @@ def test_env(tmp_home, tmp_root_prefix, tmp_env_name, tmp_prefix, prefix_selecti
else:
infos = helpers.info()
assert f"envs directories : {tmp_root_prefix / 'envs' }" in infos
assert f"environment : {tmp_env_name} (active)" in infos
assert f"env location : {tmp_prefix}" in infos
assert f"user config files : {tmp_home / '.mambarc' }" in infos
@ -72,6 +73,7 @@ def test_not_env(tmp_home, tmp_root_prefix, prefix_selection, existing_prefix):
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