diff --git a/libmamba/tests/include/mambatests.hpp b/libmamba/tests/include/mambatests.hpp index 7e9b62fc1e..bf4818e884 100644 --- a/libmamba/tests/include/mambatests.hpp +++ b/libmamba/tests/include/mambatests.hpp @@ -7,12 +7,23 @@ #ifndef LIBMAMBATESTS_HPP #define LIBMAMBATESTS_HPP +#include +#include + #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 @@ -35,7 +46,71 @@ namespace mambatests return singletons().context; } -} + class EnvironmentCleaner + { + public: + + EnvironmentCleaner(); + + template + EnvironmentCleaner(Func&&... cleaner); + ~EnvironmentCleaner(); + + private: + + mamba::util::environment_map m_env; + }; + + class CleanMambaEnv + { + public: + + inline static constexpr auto prefixes = std::array{ + "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 + EnvironmentCleaner::EnvironmentCleaner(Func&&... cleaner) + : EnvironmentCleaner() + { + ((cleaner(const_cast(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 diff --git a/libmamba/tests/src/core/test_configuration.cpp b/libmamba/tests/src/core/test_configuration.cpp index 747ebf34ed..1e48614337 100644 --- a/libmamba/tests/src/core/test_configuration.cpp +++ b/libmamba/tests/src/core/test_configuration.cpp @@ -14,7 +14,6 @@ #include "mamba/util/string.hpp" #include "mambatests.hpp" -#include "test_data.hpp" namespace mamba { @@ -111,6 +110,7 @@ namespace mamba std::string m_channel_alias_bu; std::string m_ssl_verify; std::map m_proxy_servers; + mambatests::EnvironmentCleaner restore = { mambatests::CleanMambaEnv() }; }; TEST_SUITE("Configuration") @@ -164,7 +164,7 @@ namespace mamba TEST_CASE_FIXTURE(Configuration, "parse_condarc") { std::vector possible_rc_paths = { - test_data_dir / "config/.condarc", + mambatests::test_data_dir / "config/.condarc", }; config.set_rc_values(possible_rc_paths, RCConfigLevel::kTargetPrefix); @@ -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 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)); diff --git a/libmamba/tests/src/core/test_cpp.cpp b/libmamba/tests/src/core/test_cpp.cpp index 8d6acb8409..86c43d7169 100644 --- a/libmamba/tests/src/core/test_cpp.cpp +++ b/libmamba/tests/src/core/test_cpp.cpp @@ -21,7 +21,6 @@ #include "mamba/util/build.hpp" #include "mambatests.hpp" -#include "test_data.hpp" namespace mamba { @@ -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(); diff --git a/libmamba/tests/src/core/test_env_file_reading.cpp b/libmamba/tests/src/core/test_env_file_reading.cpp index 34bd76200a..6f3cea599a 100644 --- a/libmamba/tests/src/core/test_env_file_reading.cpp +++ b/libmamba/tests/src/core/test_env_file_reading.cpp @@ -10,7 +10,6 @@ #include "mamba/util/build.hpp" #include "mambatests.hpp" -#include "test_data.hpp" namespace mamba { @@ -48,13 +47,19 @@ namespace mamba { const auto& context = mambatests::context(); using V = std::vector; - 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__ @@ -71,7 +76,10 @@ namespace mamba { const auto& context = mambatests::context(); using V = std::vector; - 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" })); @@ -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")); } } diff --git a/libmamba/tests/src/core/test_env_lockfile.cpp b/libmamba/tests/src/core/test_env_lockfile.cpp index be0e998b50..afe593d9df 100644 --- a/libmamba/tests/src/core/test_env_lockfile.cpp +++ b/libmamba/tests/src/core/test_env_lockfile.cpp @@ -13,7 +13,6 @@ #include "mamba/core/transaction.hpp" #include "mambatests.hpp" -#include "test_data.hpp" namespace mamba { @@ -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, @@ -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(); @@ -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(); @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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 }; diff --git a/libmamba/tests/src/core/test_history.cpp b/libmamba/tests/src/core/test_history.cpp index 557696ea3f..0f57fc37c8 100644 --- a/libmamba/tests/src/core/test_history.cpp +++ b/libmamba/tests/src/core/test_history.cpp @@ -22,7 +22,7 @@ #include "mamba/core/channel.hpp" #include "mamba/core/history.hpp" -#include "test_data.hpp" +#include "mambatests.hpp" namespace mamba { @@ -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. @@ -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 user_reqs = history_instance.get_user_requests(); // Extract raw history file content into buffer. diff --git a/libmamba/tests/src/core/test_validate.cpp b/libmamba/tests/src/core/test_validate.cpp index 547b327d55..a29780ba6b 100644 --- a/libmamba/tests/src/core/test_validate.cpp +++ b/libmamba/tests/src/core/test_validate.cpp @@ -18,7 +18,6 @@ #include "mamba/util/string.hpp" #include "mambatests.hpp" -#include "test_data.hpp" namespace mamba::validation { @@ -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; @@ -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 channel_dir; diff --git a/libmamba/tests/src/test_data.hpp b/libmamba/tests/src/test_data.hpp deleted file mode 100644 index 3ed7a77881..0000000000 --- a/libmamba/tests/src/test_data.hpp +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) 2022, QuantStack and Mamba Contributors -// -// Distributed under the terms of the BSD 3-Clause License. -// -// The full license is in the file LICENSE, distributed with this software. - -#ifndef MAMBA_CORE_URL_HPP -#define MAMBA_CORE_URL_HPP - -#include "mamba/core/fsutil.hpp" -#include "mamba/fs/filesystem.hpp" - -namespace mamba -{ - -#ifndef MAMBA_TEST_DATA_DIR -#error "MAMBA_TEST_DATA_DIR must be defined pointing to test data" -#endif - inline static const fs::u8path test_data_dir = MAMBA_TEST_DATA_DIR; - -} - -#endif diff --git a/libmamba/tests/src/util/test_environment.cpp b/libmamba/tests/src/util/test_environment.cpp index 36b00b7849..35f92a9d4b 100644 --- a/libmamba/tests/src/util/test_environment.cpp +++ b/libmamba/tests/src/util/test_environment.cpp @@ -8,18 +8,24 @@ #include "mamba/util/environment.hpp" +#include "mambatests.hpp" + using namespace mamba::util; TEST_SUITE("util::environment") { TEST_CASE("get_env") { + const auto restore = mambatests::EnvironmentCleaner(); + CHECK_FALSE(get_env("VAR_THAT_DOES_NOT_EXIST_XYZ").has_value()); CHECK(get_env("PATH").has_value()); } TEST_CASE("set_env") { + const auto restore = mambatests::EnvironmentCleaner(); + SUBCASE("ASCII") { const auto key = std::string(u8"VAR_THAT_DOES_NOT_EXIST_XYZ"); @@ -45,6 +51,8 @@ TEST_SUITE("util::environment") TEST_CASE("unset_env") { + const auto restore = mambatests::EnvironmentCleaner(); + const auto key = std::string(u8"VAR_THAT_DOES_NOT_EXIST_ABC_😀"); CHECK_FALSE(get_env(key).has_value()); unset_env(key); @@ -57,6 +65,8 @@ TEST_SUITE("util::environment") TEST_CASE("get_env_map") { + const auto restore = mambatests::EnvironmentCleaner(); + auto env = mamba::util::get_env_map(); CHECK_GT(env.size(), 0); CHECK_EQ(env.count("VAR_THAT_MUST_NOT_EXIST_XYZ"), 0); @@ -71,6 +81,8 @@ TEST_SUITE("util::environment") TEST_CASE("update_env_map") { + const auto restore = mambatests::EnvironmentCleaner(); + const auto key_inexistant = std::string(u8"CONDA😀"); const auto key_unchanged = std::string(u8"MAMBA😀"); const auto key_changed = std::string(u8"PIXI😀"); @@ -94,6 +106,8 @@ TEST_SUITE("util::environment") TEST_CASE("set_env_map") { + const auto restore = mambatests::EnvironmentCleaner(); + const auto key_inexistant = std::string(u8"CONDA🤗"); const auto key_unchanged = std::string(u8"MAMBA🤗"); const auto key_changed = std::string(u8"PIXI🤗");