Skip to content

Commit

Permalink
Print stack trace when tests throw exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
hexagonrecursion committed Dec 6, 2024
1 parent e0cd2da commit 43309ef
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,14 @@ if(TESTS)
# Hippomocks library
add_subdirectory(${colobot_SOURCE_DIR}/lib/hippomocks)

include(FetchContent)
FetchContent_Declare(
cpptrace
GIT_REPOSITORY https://github.com/jeremy-rifkin/cpptrace.git
GIT_TAG v0.7.3 # <HASH or TAG>
)
FetchContent_MakeAvailable(cpptrace)

# Tests targets
enable_testing()
include(GoogleTest)
Expand Down
12 changes: 12 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,20 @@ target_link_libraries(Colobot-UnitTests PRIVATE
GTest::GTest
hippomocks
Colobot-Base
cpptrace::cpptrace
)

# Needed for shared library builds on windows: copy cpptrace.dll to the same directory as the
# executable for your_target
if(WIN32)
add_custom_command(
TARGET Colobot-UnitTests POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:cpptrace::cpptrace>
$<TARGET_FILE_DIR:Colobot-UnitTests>
)
endif()

gtest_discover_tests(Colobot-UnitTests
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
DISCOVERY_MODE PRE_TEST
Expand Down
33 changes: 32 additions & 1 deletion test/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,18 @@
#include "common/logger.h"

#include <gtest/gtest.h>
#include <cpptrace/from_current.hpp>

#include <clocale>

// For compatibility with gtest < 1.12.0
#ifndef GTEST_FLAG_GET
#define GTEST_FLAG_GET(name) ::testing::GTEST_FLAG(name)
#endif
#ifndef GTEST_FLAG_SET
#define GTEST_FLAG_SET(name, value) (void)(::testing::GTEST_FLAG(name) = value)
#endif

extern bool g_cbotTestSaveState;

int main(int argc, char* argv[])
Expand All @@ -41,5 +50,27 @@ int main(int argc, char* argv[])
g_cbotTestSaveState = true;
}

return RUN_ALL_TESTS();
if (!GTEST_FLAG_GET(catch_exceptions))
{
// Pass the exception through to the debugger
return RUN_ALL_TESTS();
}

GTEST_FLAG_SET(catch_exceptions, false);
CPPTRACE_TRY
{
return RUN_ALL_TESTS();
}
CPPTRACE_CATCH(const std::exception& e)
{
cpptrace::from_current_exception().print(std::cout);
std::cout << "Exception: " << e.what() << std::endl;
throw;
}
CPPTRACE_CATCH_ALT(...)
{
cpptrace::from_current_exception().print(std::cout);
std::cout << "Not std::exception" << std::endl;
throw;
}
}

0 comments on commit 43309ef

Please sign in to comment.