Skip to content

maint: Introduce ScopedContextChange for tests#4334

Merged
jjerphan merged 4 commits into
mamba-org:mainfrom
jjerphan:maint/test-ScopedContextChange
Jun 24, 2026
Merged

maint: Introduce ScopedContextChange for tests#4334
jjerphan merged 4 commits into
mamba-org:mainfrom
jjerphan:maint/test-ScopedContextChange

Conversation

@jjerphan

Copy link
Copy Markdown
Member

Description

Factor the pattern observed with on_scope_exit under one abstraction for consistent usages.

See #4328 (comment).

Type of Change

  • Bugfix
  • Feature / enhancement
  • CI / Documentation
  • Maintenance

Checklist

  • My code follows the general style and conventions of the codebase, ensuring consistency
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have run pre-commit run --all locally in the source folder and confirmed that there are no linter errors.
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing tests pass locally with my changes

Signed-off-by: Julien Jerphanion <git@jjerphan.xyz>
@github-actions github-actions Bot added the release::maintenance For PRs related to maintenance label Jun 23, 2026
jjerphan added 2 commits June 23, 2026 17:03
Signed-off-by: Julien Jerphanion <git@jjerphan.xyz>
Signed-off-by: Julien Jerphanion <git@jjerphan.xyz>
@jjerphan jjerphan marked this pull request as ready for review June 23, 2026 15:23
Comment thread libmamba/tests/include/mambatests.hpp Outdated
Signed-off-by: Julien Jerphanion <git@jjerphan.xyz>

Co-authored-by: Johan Mabille <johan.mabille@gmail.com>
@jjerphan jjerphan merged commit be47751 into mamba-org:main Jun 24, 2026
33 of 34 checks passed

@Klaim Klaim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if replacing all this by explicitly using a non-singleton context in these tests would be superior. I remember adding mambatests.hpp because I didn't have much choice but ideally


~ScopedContextChange()
{
for (auto it = m_restorers.rbegin(); it != m_restorers.rend(); ++it)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (auto it = m_restorers.rbegin(); it != m_restorers.rend(); ++it)
for (auto& restorer : [restorers_reversed](std::ranges::reverse_view(m_restorers)))

{
for (auto it = m_restorers.rbegin(); it != m_restorers.rend(); ++it)
{
(*it)();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 noexcept -> exception escaping calls terminate -> no terminate handler leads to abort())

Did you consider calling safe_invoke instead?

private:

template <typename T, typename F>
void touch(T mamba::Context::* member, F&& mutator)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void touch(T mamba::Context::* member, F&& mutator)
void touch(T& member, F&& mutator)

Then capture the reference to member, with the field pointer being &member.

m_restorers.push_back([&field, initial = field]() mutable
{ field = std::move(initial); });
}
mutator(field);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mutator(field);
std::invoke(std::forward<F>(mutator), field);

@jjerphan jjerphan deleted the maint/test-ScopedContextChange branch June 24, 2026 13:22
@jjerphan jjerphan mentioned this pull request Jun 24, 2026
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release::maintenance For PRs related to maintenance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants