-
Notifications
You must be signed in to change notification settings - Fork 440
maint: Introduce ScopedContextChange for tests
#4334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9204f47
7e9746f
254213f
737a84f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,7 +8,10 @@ | |||||
| #define LIBMAMBATESTS_HPP | ||||||
|
|
||||||
| #include <array> | ||||||
| #include <functional> | ||||||
| #include <string_view> | ||||||
| #include <unordered_set> | ||||||
| #include <vector> | ||||||
|
|
||||||
| #include "mamba/core/context.hpp" | ||||||
| #include "mamba/core/output.hpp" | ||||||
|
|
@@ -87,6 +90,132 @@ namespace mambatests | |||||
| void operator()(const mamba::util::environment_map& env); | ||||||
| }; | ||||||
|
|
||||||
| // RAII helper for C++ tests that temporarily override fields on the shared Context | ||||||
| // singleton (see context()). Tests often need to point at a temp prefix, tweak channels, | ||||||
| // or flip feature flags; without restoration, later tests inherit stale state. | ||||||
| // | ||||||
| // Save the value of each touched field on first use and restore it when the guard is | ||||||
| // destroyed. Repeated calls to the same setter only change the live value — the original | ||||||
| // snapshot is always what gets restored. | ||||||
| // | ||||||
| // Use set_* when the test assigns a known value. Use preserve() when the field will be | ||||||
| // changed later by code under test, by direct assignment (ctx.field = …), or by APIs such | ||||||
| // as Configuration::load() — preserve() only snapshots the current value for restoration. | ||||||
| // | ||||||
| // Example: | ||||||
| // auto& ctx = mambatests::context(); | ||||||
| // mambatests::ScopedContextChange context_change{ ctx }; | ||||||
| // context_change.set_channels({ "conda-forge" }).set_offline(false); | ||||||
| // | ||||||
| // context_change.preserve(&mamba::Context::use_sharded_repodata); | ||||||
| // ctx.use_sharded_repodata = false; // restored to the pre-preserve value at scope end | ||||||
| class ScopedContextChange | ||||||
| { | ||||||
| public: | ||||||
|
|
||||||
| explicit ScopedContextChange(mamba::Context& ctx) | ||||||
| : m_ctx(ctx) | ||||||
| { | ||||||
| } | ||||||
|
|
||||||
| ~ScopedContextChange() | ||||||
| { | ||||||
| for (auto it = m_restorers.rbegin(); it != m_restorers.rend(); ++it) | ||||||
| { | ||||||
| (*it)(); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that if any exception escape this call, abort will be called (because destructors are implicitly Did you consider calling |
||||||
| } | ||||||
| } | ||||||
|
|
||||||
| ScopedContextChange& set_target_prefix(const mamba::fs::u8path& prefix) | ||||||
| { | ||||||
| touch( | ||||||
| &mamba::Context::prefix_params, | ||||||
| [&](auto& params) { params.target_prefix = prefix; } | ||||||
| ); | ||||||
| return *this; | ||||||
| } | ||||||
|
|
||||||
| ScopedContextChange& set_root_prefix(const mamba::fs::u8path& prefix) | ||||||
| { | ||||||
| touch(&mamba::Context::prefix_params, [&](auto& params) { params.root_prefix = prefix; }); | ||||||
| return *this; | ||||||
| } | ||||||
|
|
||||||
| ScopedContextChange& set_envs_dirs(std::vector<mamba::fs::u8path> dirs) | ||||||
| { | ||||||
| touch(&mamba::Context::envs_dirs, [&](auto& field) { field = std::move(dirs); }); | ||||||
| return *this; | ||||||
| } | ||||||
|
|
||||||
| ScopedContextChange& set_pkgs_dirs(std::vector<mamba::fs::u8path> dirs) | ||||||
| { | ||||||
| touch(&mamba::Context::pkgs_dirs, [&](auto& field) { field = std::move(dirs); }); | ||||||
| return *this; | ||||||
| } | ||||||
|
|
||||||
| ScopedContextChange& set_prefix_data_interoperability(bool value) | ||||||
| { | ||||||
| touch(&mamba::Context::prefix_data_interoperability, [&](auto& field) { field = value; }); | ||||||
| return *this; | ||||||
| } | ||||||
|
|
||||||
| ScopedContextChange& set_channels(std::vector<std::string> channels) | ||||||
| { | ||||||
| touch(&mamba::Context::channels, [&](auto& field) { field = std::move(channels); }); | ||||||
| return *this; | ||||||
| } | ||||||
|
|
||||||
| ScopedContextChange& set_use_sharded_repodata(bool value) | ||||||
| { | ||||||
| touch(&mamba::Context::use_sharded_repodata, [&](auto& field) { field = value; }); | ||||||
| return *this; | ||||||
| } | ||||||
|
|
||||||
| ScopedContextChange& set_offline(bool value) | ||||||
| { | ||||||
| touch(&mamba::Context::offline, [&](auto& field) { field = value; }); | ||||||
| return *this; | ||||||
| } | ||||||
|
|
||||||
| ScopedContextChange& set_platform(std::string platform) | ||||||
| { | ||||||
| touch(&mamba::Context::platform, [&](auto& field) { field = std::move(platform); }); | ||||||
| return *this; | ||||||
| } | ||||||
|
|
||||||
| // Snapshot member for restoration without assigning a new value. The member pointer | ||||||
| // syntax (e.g. &mamba::Context::platform) selects which Context field to guard. | ||||||
| template <typename T> | ||||||
| ScopedContextChange& preserve(T mamba::Context::* member) | ||||||
| { | ||||||
| touch(member, [](auto&) {}); | ||||||
| return *this; | ||||||
| } | ||||||
|
|
||||||
| ScopedContextChange(const ScopedContextChange&) = delete; | ||||||
| ScopedContextChange& operator=(const ScopedContextChange&) = delete; | ||||||
| ScopedContextChange(ScopedContextChange&&) = delete; | ||||||
| ScopedContextChange& operator=(ScopedContextChange&&) = delete; | ||||||
|
|
||||||
| private: | ||||||
|
|
||||||
| template <typename T, typename F> | ||||||
| void touch(T mamba::Context::* member, F&& mutator) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Then capture the reference to |
||||||
| { | ||||||
| auto& field = m_ctx.*member; | ||||||
| if (m_saved_fields.insert(static_cast<const void*>(&field)).second) | ||||||
| { | ||||||
| m_restorers.push_back([&field, initial = field]() mutable | ||||||
| { field = std::move(initial); }); | ||||||
| } | ||||||
| mutator(field); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| mamba::Context& m_ctx; | ||||||
| std::vector<std::function<void()>> m_restorers; | ||||||
| std::unordered_set<const void*> m_saved_fields; | ||||||
| }; | ||||||
|
|
||||||
| /****************************************** | ||||||
| * Implementation of EnvironmentCleaner * | ||||||
| ******************************************/ | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.