Skip to content

Commit

Permalink
Try using a different death test for _WIN32
Browse files Browse the repository at this point in the history
  • Loading branch information
dabrahams committed Aug 12, 2024
1 parent 1fc4026 commit f6aeb92
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ include(GoogleTest)
# calls abort() and outputs a string matching the regular expression
# <expected_output_re>.
function(adobe_contract_checking_add_test base_name)
add_executable("${base_name}" "${base_name}.cpp")
add_executable("${base_name}" "${base_name}.cpp" abort_detection.cpp)
adobe_contract_checking_apply_standard_options("${base_name}")
target_link_libraries(
"${base_name}"
Expand Down
31 changes: 31 additions & 0 deletions test/abort_detection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// When compiled into an executable, this file causes abort() to print
// a special message to stderr and exit with EXIT_FAILURE instead of
// having its usual behavior, allowing our tests to detect that
// abort() was called by checking for the message.
#include <csignal>
#include <cstdio>
#include <cstdlib>

// A signal handler that prints "##ABORTED##" to stderr and exits with
// EXIT_FAILURE.
//
// The printed string is chosen to be unlikely to appear by accident
// in other output.
extern "C" void error_test_handle_abort(int /* unused signum */)
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
(void)std::fprintf(stderr, "##ABORTED##");
// std::_Exit(EXIT_FAILURE);
}

// Abort handler installer.
struct test_override_abort
{

// As a side-effect, installs error_test_handle_abort as a SIGABRT
// handler.
test_override_abort() noexcept { (void)std::signal(SIGABRT, error_test_handle_abort); }
};

// The installation of error_test_handle_abort as an abort handler.
const test_override_abort handler{};
20 changes: 14 additions & 6 deletions test/precondition_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,27 @@ TEST(PreconditionDeathTest, PreconditionFailureAborts)
EXPECT_EXIT(ADOBE_PRECONDITION(false), testing::KilledBySignal(SIGABRT), "precondition");
}

#ifdef _WIN32
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define EXPECT_ABORT(code, expected_output_regex) \
EXPECT_DEATH(code, expected_output_regex ".*##ABORTED##");
#else
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define EXPECT_ABORT(code, expected_output_regex) \
EXPECT_EXIT(code, testing::KilledBySignal(SIGABRT), expected_output_regex ".*##ABORTED##");
#endif
TEST(PreconditionDeathTest, PreconditionFailureOutput)
{
#line 9999
EXPECT_EXIT(ADOBE_PRECONDITION(false),
testing::KilledBySignal(SIGABRT),
"precondition_tests.cpp:9999: Precondition violated [(]false[)]");
#line 9998
EXPECT_ABORT(
ADOBE_PRECONDITION(false), "precondition_tests.cpp:9999: Precondition violated [(]false[)]");
}
TEST(PreconditionDeathTest, PreconditionFailureWithMessageOutput)
{
#line 99991
EXPECT_EXIT(ADOBE_PRECONDITION(false, "expected message"),
testing::KilledBySignal(SIGABRT),
EXPECT_ABORT(ADOBE_PRECONDITION(false, "expected message"),
"precondition_tests.cpp:99991: Precondition violated [(]false[)]. "
"expected message");
}

0 comments on commit f6aeb92

Please sign in to comment.