catchorg / Catch2

A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)
https://discord.gg/4CWS9zD
Boost Software License 1.0
18.51k stars 3.04k forks source link

Handle active Sections for fatal errors #2855

Closed hbirler closed 5 months ago

hbirler commented 5 months ago

Closes #1210

When a signal is caught, the destructors of Sections will not be called. Thus, we must call sectionEndedEarly manually for those Sections.

Example test case:

TEST_CASE("broken") {
   SECTION("section") {
      /// Use illegal cpu instruction
      __asm__ __volatile__("ud2" : : : "memory");
   }
}
hbirler commented 5 months ago

I am not familiar with the internals of Catch2, so there might be a nicer way of doing this. For example, I am not sure whether it is possible to properly construct SectionEndInfo endInfo with correct prevAssertions and durationInSeconds

hbirler commented 5 months ago

Additionally, it might be safe to do:

auto nl = CATCH_MOVE(m_activeSections.back()->nameAndLocation());

instead of a copy, but I took the "safe" route (allocating memory might also not be the best idea within a signal handler).

codecov[bot] commented 5 months ago

Codecov Report

Attention: Patch coverage is 0% with 5 lines in your changes are missing coverage. Please review.

Project coverage is 91.04%. Comparing base (029fe3b) to head (fe428d6). Report is 5 commits behind head on devel.

:exclamation: Current head fe428d6 differs from pull request most recent head 0b013eb. Consider uploading reports for the commit 0b013eb to get more accurate results

Additional details and impacted files ```diff @@ Coverage Diff @@ ## devel #2855 +/- ## ========================================== - Coverage 91.09% 91.04% -0.05% ========================================== Files 197 198 +1 Lines 8382 8391 +9 ========================================== + Hits 7635 7639 +4 - Misses 747 752 +5 ```
hbirler commented 5 months ago

The function RunContext::handleFatalErrorCondition seems to not be covered at all by the existing tests, and I wouldn't know how to test signal handlers reliably within the existing framework.

horenmar commented 5 months ago

Thanks, this has been around for a while.