Skip to content

Commit

Permalink
Add envrionment cleaner test fixtures (#2973)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv authored Nov 14, 2023
1 parent b7b87f7 commit 4fe976d
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 55 deletions.
79 changes: 77 additions & 2 deletions libmamba/tests/include/mambatests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,23 @@
#ifndef LIBMAMBATESTS_HPP
#define LIBMAMBATESTS_HPP

#include <array>
#include <string_view>

#include "mamba/core/context.hpp"
#include "mamba/core/execution.hpp"
#include "mamba/core/output.hpp"
#include "mamba/fs/filesystem.hpp"
#include "mamba/util/environment.hpp"
#include "mamba/util/string.hpp"

namespace mambatests
{

#ifndef MAMBA_TEST_DATA_DIR
#error "MAMBA_TEST_DATA_DIR must be defined pointing to test data"
#endif
inline static const mamba::fs::u8path test_data_dir = MAMBA_TEST_DATA_DIR;

struct Singletons
{
// mamba::MainExecutor main_executor; // FIXME: reactivate once the tests are not indirectly
Expand All @@ -35,7 +46,71 @@ namespace mambatests
return singletons().context;
}

}
class EnvironmentCleaner
{
public:

EnvironmentCleaner();

template <typename... Func>
EnvironmentCleaner(Func&&... cleaner);

~EnvironmentCleaner();

private:

mamba::util::environment_map m_env;
};

class CleanMambaEnv
{
public:

inline static constexpr auto prefixes = std::array<std::string_view, 4>{
"CONDA",
"_CONDA",
"MAMBA",
"_MAMBA",
};

void operator()(const mamba::util::environment_map& env);
};

/******************************************
* Implementation of EnvironmentCleaner *
******************************************/

inline EnvironmentCleaner::EnvironmentCleaner()
: m_env(mamba::util::get_env_map())
{
}

template <typename... Func>
EnvironmentCleaner::EnvironmentCleaner(Func&&... cleaner)
: EnvironmentCleaner()
{
((cleaner(const_cast<const mamba::util::environment_map&>(m_env))), ...);
}

inline EnvironmentCleaner::~EnvironmentCleaner()
{
mamba::util::set_env_map(m_env);
}

/*************************************
* Implementation of CleanMambaEnv *
*************************************/

inline void CleanMambaEnv::operator()(const mamba::util::environment_map& env)
{
for (const auto& [key, val] : env)
{
if (mamba::util::starts_with_any(key, prefixes))
{
mamba::util::unset_env(key);
}
}
}

}
#endif
14 changes: 7 additions & 7 deletions libmamba/tests/src/core/test_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "mamba/util/string.hpp"

#include "mambatests.hpp"
#include "test_data.hpp"

namespace mamba
{
Expand Down Expand Up @@ -111,6 +110,7 @@ namespace mamba
std::string m_channel_alias_bu;
std::string m_ssl_verify;
std::map<std::string, std::string> m_proxy_servers;
mambatests::EnvironmentCleaner restore = { mambatests::CleanMambaEnv() };
};

TEST_SUITE("Configuration")
Expand Down Expand Up @@ -164,7 +164,7 @@ namespace mamba
TEST_CASE_FIXTURE(Configuration, "parse_condarc")
{
std::vector<fs::u8path> possible_rc_paths = {
test_data_dir / "config/.condarc",
mambatests::test_data_dir / "config/.condarc",
};

config.set_rc_values(possible_rc_paths, RCConfigLevel::kTargetPrefix);
Expand Down Expand Up @@ -1095,13 +1095,13 @@ namespace mamba
{
using namespace detail;

fs::u8path p = test_data_dir / "config/.condarc";
fs::u8path p = mambatests::test_data_dir / "config/.condarc";

std::vector<fs::u8path> wrong_paths = {
test_data_dir / "config",
test_data_dir / "conf",
test_data_dir / "config/condarc",
test_data_dir / "history/conda-meta/history",
mambatests::test_data_dir / "config",
mambatests::test_data_dir / "conf",
mambatests::test_data_dir / "config/condarc",
mambatests::test_data_dir / "history/conda-meta/history",
};

CHECK(is_config_file(p));
Expand Down
3 changes: 1 addition & 2 deletions libmamba/tests/src/core/test_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "mamba/util/build.hpp"

#include "mambatests.hpp"
#include "test_data.hpp"

namespace mamba
{
Expand Down Expand Up @@ -606,7 +605,7 @@ namespace mamba
{
TEST_CASE("parse_last_modified_etag")
{
fs::u8path cache_folder = fs::u8path{ test_data_dir / "repodata_json_cache" };
fs::u8path cache_folder = fs::u8path{ mambatests::test_data_dir / "repodata_json_cache" };
auto mq = MSubdirMetadata::read(cache_folder / "test_1.json");
CHECK(mq.has_value());
auto j = mq.value();
Expand Down
18 changes: 13 additions & 5 deletions libmamba/tests/src/core/test_env_file_reading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "mamba/util/build.hpp"

#include "mambatests.hpp"
#include "test_data.hpp"

namespace mamba
{
Expand Down Expand Up @@ -48,13 +47,19 @@ namespace mamba
{
const auto& context = mambatests::context();
using V = std::vector<std::string>;
auto res = detail::read_yaml_file(test_data_dir / "env_file/env_1.yaml", context.platform);
auto res = detail::read_yaml_file(
mambatests::test_data_dir / "env_file/env_1.yaml",
context.platform
);
CHECK_EQ(res.name, "env_1");
CHECK_EQ(res.channels, V({ "conda-forge", "bioconda" }));
CHECK_EQ(res.dependencies, V({ "test1", "test2", "test3" }));
CHECK_FALSE(res.others_pkg_mgrs_specs.size());

auto res2 = detail::read_yaml_file(test_data_dir / "env_file/env_2.yaml", context.platform);
auto res2 = detail::read_yaml_file(
mambatests::test_data_dir / "env_file/env_2.yaml",
context.platform
);
CHECK_EQ(res2.name, "env_2");
CHECK_EQ(res2.channels, V({ "conda-forge", "bioconda" }));
#ifdef __linux__
Expand All @@ -71,7 +76,10 @@ namespace mamba
{
const auto& context = mambatests::context();
using V = std::vector<std::string>;
auto res = detail::read_yaml_file(test_data_dir / "env_file/env_3.yaml", context.platform);
auto res = detail::read_yaml_file(
mambatests::test_data_dir / "env_file/env_3.yaml",
context.platform
);
CHECK_EQ(res.name, "env_3");
CHECK_EQ(res.channels, V({ "conda-forge", "bioconda" }));
CHECK_EQ(res.dependencies, V({ "test1", "test2", "test3", "pip" }));
Expand All @@ -80,7 +88,7 @@ namespace mamba
auto o = res.others_pkg_mgrs_specs[0];
CHECK_EQ(o.pkg_mgr, "pip");
CHECK_EQ(o.deps, V({ "pytest", "numpy" }));
CHECK_EQ(o.cwd, fs::absolute(test_data_dir / "env_file"));
CHECK_EQ(o.cwd, fs::absolute(mambatests::test_data_dir / "env_file"));
}
}

Expand Down
19 changes: 10 additions & 9 deletions libmamba/tests/src/core/test_env_lockfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "mamba/core/transaction.hpp"

#include "mambatests.hpp"
#include "test_data.hpp"

namespace mamba
{
Expand Down Expand Up @@ -45,7 +44,7 @@ namespace mamba
TEST_CASE("invalid_version_fails")
{
ChannelContext channel_context{ mambatests::context() };
const fs::u8path invalid_version_lockfile_path{ test_data_dir
const fs::u8path invalid_version_lockfile_path{ mambatests::test_data_dir
/ "env_lockfile/bad_version-lock.yaml" };
const auto maybe_lockfile = read_environment_lockfile(
channel_context,
Expand All @@ -61,7 +60,8 @@ namespace mamba
TEST_CASE("valid_no_package_succeed")
{
ChannelContext channel_context{ mambatests::context() };
const fs::u8path lockfile_path{ test_data_dir / "env_lockfile/good_no_package-lock.yaml" };
const fs::u8path lockfile_path{ mambatests::test_data_dir
/ "env_lockfile/good_no_package-lock.yaml" };
const auto maybe_lockfile = read_environment_lockfile(channel_context, lockfile_path);
REQUIRE_MESSAGE(maybe_lockfile, maybe_lockfile.error().what());
const auto lockfile = maybe_lockfile.value();
Expand All @@ -71,7 +71,8 @@ namespace mamba
TEST_CASE("invalid_package_fails")
{
ChannelContext channel_context{ mambatests::context() };
const fs::u8path lockfile_path{ test_data_dir / "env_lockfile/bad_package-lock.yaml" };
const fs::u8path lockfile_path{ mambatests::test_data_dir
/ "env_lockfile/bad_package-lock.yaml" };
const auto maybe_lockfile = read_environment_lockfile(channel_context, lockfile_path);
REQUIRE_FALSE(maybe_lockfile);
const auto error = maybe_lockfile.error();
Expand All @@ -83,7 +84,7 @@ namespace mamba
TEST_CASE("valid_one_package_succeed")
{
ChannelContext channel_context{ mambatests::context() };
const fs::u8path lockfile_path{ test_data_dir
const fs::u8path lockfile_path{ mambatests::test_data_dir
/ "env_lockfile/good_one_package-lock.yaml" };
const auto maybe_lockfile = read_environment_lockfile(channel_context, lockfile_path);
REQUIRE_MESSAGE(maybe_lockfile, maybe_lockfile.error().what());
Expand All @@ -95,7 +96,7 @@ namespace mamba
{
ChannelContext channel_context{ mambatests::context() };
const fs::u8path lockfile_path{
test_data_dir / "env_lockfile/good_one_package_missing_category-lock.yaml"
mambatests::test_data_dir / "env_lockfile/good_one_package_missing_category-lock.yaml"
};
const auto maybe_lockfile = read_environment_lockfile(channel_context, lockfile_path);
REQUIRE_MESSAGE(maybe_lockfile, maybe_lockfile.error().what());
Expand All @@ -106,7 +107,7 @@ namespace mamba
TEST_CASE("valid_multiple_packages_succeed")
{
ChannelContext channel_context{ mambatests::context() };
const fs::u8path lockfile_path{ test_data_dir
const fs::u8path lockfile_path{ mambatests::test_data_dir
/ "env_lockfile/good_multiple_packages-lock.yaml" };
const auto maybe_lockfile = read_environment_lockfile(channel_context, lockfile_path);
REQUIRE_MESSAGE(maybe_lockfile, maybe_lockfile.error().what());
Expand All @@ -117,7 +118,7 @@ namespace mamba
TEST_CASE("get_specific_packages")
{
ChannelContext channel_context{ mambatests::context() };
const fs::u8path lockfile_path{ test_data_dir
const fs::u8path lockfile_path{ mambatests::test_data_dir
/ "env_lockfile/good_multiple_packages-lock.yaml" };
const auto lockfile = read_environment_lockfile(channel_context, lockfile_path).value();
CHECK(lockfile.get_packages_for("", "", "").empty());
Expand All @@ -136,7 +137,7 @@ namespace mamba
TEST_CASE("create_transaction_with_categories")
{
auto& ctx = mambatests::context();
const fs::u8path lockfile_path{ test_data_dir
const fs::u8path lockfile_path{ mambatests::test_data_dir
/ "env_lockfile/good_multiple_categories-lock.yaml" };
ChannelContext channel_context{ ctx };
MPool pool{ channel_context };
Expand Down
8 changes: 4 additions & 4 deletions libmamba/tests/src/core/test_history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "mamba/core/channel.hpp"
#include "mamba/core/history.hpp"

#include "test_data.hpp"
#include "mambatests.hpp"

namespace mamba
{
Expand All @@ -31,10 +31,10 @@ namespace mamba
TEST_CASE("parse")
{
static const auto history_file_path = fs::absolute(
test_data_dir / "history/parse/conda-meta/history"
mambatests::test_data_dir / "history/parse/conda-meta/history"
);
static const auto aux_file_path = fs::absolute(
test_data_dir / "history/parse/conda-meta/aux_file"
mambatests::test_data_dir / "history/parse/conda-meta/aux_file"
);

// Backup history file and restore it at the end of the test, whatever the output.
Expand All @@ -55,7 +55,7 @@ namespace mamba
ChannelContext channel_context{ mambatests::context() };

// Gather history from current history file.
History history_instance(test_data_dir / "history/parse", channel_context);
History history_instance(mambatests::test_data_dir / "history/parse", channel_context);
std::vector<History::UserRequest> user_reqs = history_instance.get_user_requests();

// Extract raw history file content into buffer.
Expand Down
5 changes: 2 additions & 3 deletions libmamba/tests/src/core/test_validate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "mamba/util/string.hpp"

#include "mambatests.hpp"
#include "test_data.hpp"

namespace mamba::validation
{
Expand Down Expand Up @@ -347,7 +346,7 @@ namespace mamba::validation

protected:

fs::u8path root1_pgp = test_data_dir / "validation/1.sv0.6.root.json";
fs::u8path root1_pgp = mambatests::test_data_dir / "validation/1.sv0.6.root.json";
json root1_json, root1_pgp_json;

secrets_type secrets;
Expand Down Expand Up @@ -1633,7 +1632,7 @@ namespace mamba::validation

protected:

fs::u8path root1 = test_data_dir / "validation/root.json";
fs::u8path root1 = mambatests::test_data_dir / "validation/root.json";
json root1_json;

std::unique_ptr<TemporaryDirectory> channel_dir;
Expand Down
23 changes: 0 additions & 23 deletions libmamba/tests/src/test_data.hpp

This file was deleted.

Loading

0 comments on commit 4fe976d

Please sign in to comment.