Skip to content

Commit

Permalink
Change strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Hind-M committed Sep 20, 2024
1 parent c0f69aa commit dc39957
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 194 deletions.
46 changes: 0 additions & 46 deletions .github/workflows/unix_impl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,49 +192,3 @@ jobs:
cd micromamba/test-server
./generate_gpg_keys.sh
./testserver_pkg_signing.sh
mamba_conda_root_prefix_tests:
name: mamba-conda-root-prefix tests
runs-on: ${{ inputs.os }}
if: startsWith(inputs.os, 'ubuntu')
steps:
- name: Checkout mamba repository
uses: actions/checkout@v4
- name: Create environment with conda
uses: mamba-org/setup-micromamba@v1
with:
environment-name: conda_env
create-args: >-
conda
- name: Create conda environment containing mamba
shell: bash -l {0} -euo pipefail -x
run: |
# Get a recent version of mamba to have it tested (mamba 2.x)
conda create -n mamba_in_conda_env mamba=2.0.0rc5 -c conda-forge/label/mamba_dev -y
- name: Run tests using conda and a nested mamba
shell: bash -l {0} -euo pipefail -x
run: |
conda info
source "$(conda info --base)/etc/profile.d/conda.sh" # Initialize Conda
conda activate mamba_in_conda_env
# Testing with conda and mamba having the same root prefix
export MAMBA_ROOT_PREFIX="$(conda info --base)"
mamba create -n mamba_env_test xtensor -c conda-forge -y
mamba env list | grep mamba_env_test || { echo "mamba_env_test not found in env list"; exit 1; }
mamba info | grep "base environment" | grep "$(conda info --base)" || { echo "Expected base environment path not found in mamba info"; exit 1; }
mamba list -n mamba_env_test | grep xtensor || { echo "xtensor not found in mamba_env_test"; exit 1; }
eval "$(mamba shell hook --shell bash)"
mamba activate
# Since no target prefix has been specified, we should fall back on the root prefix
mamba info | grep "environment" | grep "base (active)" || { echo "Expected base environment not active"; exit 1; }
mamba info | grep "env location" | grep "$(conda info --base)" || { echo "Expected environment not active"; exit 1; }
# Activate another env - specified
mamba activate -n mamba_env_test
mamba info | grep "environment" | grep "mamba_env_test (active)" || { echo "Expected mamba_env_test environment not active"; exit 1; }
# Get back to use conda without changing the environment
conda info | grep "shell level" | grep "2" || { echo "Expected conda shell level = 2"; exit 1; }
conda info | grep "active environment" | grep "mamba_env_test" || { echo "Expected conda environment not active"; exit 1; }
# Activate another environment using conda
conda activate mamba_in_conda_env
conda info | grep "shell level" | grep "3" || { echo "Expected conda shell level = 3"; exit 1; }
conda info | grep "active environment" | grep "mamba_in_conda_env" || { echo "Expected nested conda environment not active"; exit 1; }
5 changes: 5 additions & 0 deletions libmamba/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ if(BUILD_STATIC)
add_definitions(-DLIBMAMBA_STATIC_DEPS)
endif()

# Use mamba installation prefix to set root prefix (base)
if(BUILD_SHARED OR BUILD_MAMBA)
add_definitions(-DMAMBA_USE_INSTALL_PREFIX_AS_BASE)
endif()

if(WIN32)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()
Expand Down
6 changes: 0 additions & 6 deletions libmamba/include/mamba/api/configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -955,12 +955,6 @@ namespace mamba
return m_config.at(name);
}

namespace detail
{
auto
get_root_prefix_from_mamba_bin(const fs::u8path& mamba_bin_path) -> expected_t<fs::u8path>;
}

void use_conda_root_prefix(Configuration& config, bool force = false);
}

Expand Down
91 changes: 21 additions & 70 deletions libmamba/src/api/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,58 +627,9 @@ namespace mamba
{
return make_unexpected("`mamba` binary not found.", mamba_error_code::incorrect_usage);
}

auto current_dir(mamba_bin_path);
fs::u8path bin_dir, root_dir;

while (true)
{
fs::u8path parent_dir = current_dir.parent_path();

if (parent_dir == current_dir)
{
// Root reached
break;
}

if (current_dir.filename() == "bin")
{
bin_dir = parent_dir;
}

if (current_dir.filename() == "envs")
{
root_dir = parent_dir;
break; // Exit the loop as we found both directories
}

current_dir = parent_dir;
}

if (!root_dir.empty())
{
return { fs::weakly_canonical(std::move(root_dir)) };
}
else if (!bin_dir.empty())
{
return { fs::weakly_canonical(std::move(bin_dir)) };
}

return { fs::weakly_canonical(mamba_bin_path.parent_path()) };
}

auto validate_mamba_install_prefix(bool is_mamba_exe, expected_t<fs::u8path> candidate)
-> expected_t<fs::u8path>
{
// `is_mamba_exe` is referring to mamba exe as the `micromamba` single executable
if (candidate.has_value() && !candidate.value().empty() && !is_mamba_exe)
{
return { fs::weakly_canonical(std::move(candidate.value())) };
}
else
{
return make_unexpected("Empty root prefix.", mamba_error_code::incorrect_usage);
}
// In linux, the install path would be install_prefix/bin/mamba
// In windows, install_prefix/Scripts/mamba.exe
return { fs::weakly_canonical(mamba_bin_path.parent_path().parent_path()) };
}

auto validate_existing_root_prefix(const fs::u8path& candidate) -> expected_t<fs::u8path>
Expand Down Expand Up @@ -763,8 +714,6 @@ namespace mamba

void root_prefix_hook(Configuration& config, fs::u8path& prefix)
{
const auto& ctx = config.context();

auto& env_name = config.at("env_name");

if (prefix.empty())
Expand All @@ -778,22 +727,24 @@ namespace mamba
}
else
{
validate_existing_root_prefix(default_root_prefix_v1())
.or_else([](const auto& /* error */)
{ return validate_root_prefix(default_root_prefix_v2()); })
.or_else(
[&](const auto& /* error */)
{
// If it's mamba package, and the root prefix is not set,
// set the root prefix as the mamba library installation path
return validate_mamba_install_prefix(
ctx.command_params.is_mamba_exe,
get_root_prefix_from_mamba_bin(util::which("mamba"))
);
}
)
.transform([&](fs::u8path&& p) { prefix = std::move(p); })
.or_else([](mamba_error&& error) { throw std::move(error); });
#ifdef MAMBA_USE_INSTALL_PREFIX_AS_BASE
{
// mamba case
// set the root prefix as the mamba installation path
get_root_prefix_from_mamba_bin(util::which("mamba"))
.transform([&](fs::u8path&& p) { prefix = std::move(p); })
.or_else([](mamba_error&& error) { throw std::move(error); });
}
#else
{
// micromamba case
validate_existing_root_prefix(default_root_prefix_v1())
.or_else([](const auto& /* error */)
{ return validate_root_prefix(default_root_prefix_v2()); })
.transform([&](fs::u8path&& p) { prefix = std::move(p); })
.or_else([](mamba_error&& error) { throw std::move(error); });
}
#endif
}

if (env_name.configured())
Expand Down
72 changes: 0 additions & 72 deletions libmamba/tests/src/core/test_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1274,76 +1274,4 @@ namespace mamba
}
}
} // namespace testing

namespace detail
{
// This is supposed to test the behavior of `get_root_prefix_from_mamba_bin`
// and we should not bother with platform details (even less with CI details)
// (such as having different root directories (in windows and osx))
// Therefore, this test is only run on linux
#ifdef __linux__
TEST_SUITE("get_root_prefix_from_mamba_bin")
{
TEST_CASE("empty_path")
{
CHECK_FALSE(get_root_prefix_from_mamba_bin("").has_value());
}

TEST_CASE("root_dir")
{
fs::u8path test_path = "/home/usr/root_prefix/envs/mamba_env/bin/mamba";
CHECK(get_root_prefix_from_mamba_bin(test_path).has_value());
CHECK_EQ(get_root_prefix_from_mamba_bin(test_path).value(), "/home/usr/root_prefix");
}

TEST_CASE("bin_dir")
{
fs::u8path test_path = "/home/usr/root_prefix/mamba_env/bin/mamba";
CHECK(get_root_prefix_from_mamba_bin(test_path).has_value());
CHECK_EQ(
get_root_prefix_from_mamba_bin(test_path).value(),
"/home/usr/root_prefix/mamba_env"
);
}

TEST_CASE("parent_dir")
{
fs::u8path test_path = "/home/usr/root_prefix/mamba_env/mamba";
CHECK(get_root_prefix_from_mamba_bin(test_path).has_value());
CHECK_EQ(
get_root_prefix_from_mamba_bin(test_path).value(),
"/home/usr/root_prefix/mamba_env"
);
}

TEST_CASE("simple_parent_dir")
{
fs::u8path test_path = "some_folder/mamba";
CHECK(get_root_prefix_from_mamba_bin(test_path).has_value());
CHECK_EQ(get_root_prefix_from_mamba_bin(test_path).value(), "some_folder");
}

TEST_CASE("empty_parent")
{
fs::u8path test_path = "mamba";
CHECK(get_root_prefix_from_mamba_bin(test_path).has_value());
CHECK_EQ(get_root_prefix_from_mamba_bin(test_path).value(), "");
}

TEST_CASE("current_path")
{
fs::u8path test_path = "./mamba";
CHECK(get_root_prefix_from_mamba_bin(test_path).has_value());
CHECK_EQ(get_root_prefix_from_mamba_bin(test_path).value(), mamba::fs::current_path());
}

TEST_CASE("parent_is_root")
{
fs::u8path test_path = "/mamba";
CHECK(get_root_prefix_from_mamba_bin(test_path).has_value());
CHECK_EQ(get_root_prefix_from_mamba_bin(test_path).value(), "/");
}
}
#endif
} // namespace detail
} // namespace mamba

0 comments on commit dc39957

Please sign in to comment.