aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorjade <lix@jade.fyi>2024-09-26 17:06:01 +0000
committerGerrit Code Review <gerrit@localhost>2024-09-26 17:06:01 +0000
commitb6038e988d989f3306e1c7cce8796a685cd8b0d0 (patch)
tree9e8a09281a0e442a2c7b1a179357dbcc411e1346 /tests
parentca9256a789b413b71f424405c8a0d7d37ca36696 (diff)
parent19e0ce2c03d8e0baa16998b086665664c420c1df (diff)
Merge "main: log stack traces for std::terminate" into main
Diffstat (limited to 'tests')
-rw-r--r--tests/unit/libmain/crash.cc56
-rw-r--r--tests/unit/meson.build7
2 files changed, 62 insertions, 1 deletions
diff --git a/tests/unit/libmain/crash.cc b/tests/unit/libmain/crash.cc
new file mode 100644
index 000000000..883dc39bd
--- /dev/null
+++ b/tests/unit/libmain/crash.cc
@@ -0,0 +1,56 @@
+#include <gtest/gtest.h>
+#include "crash-handler.hh"
+
+namespace nix {
+
+class OopsException : public std::exception
+{
+ const char * msg;
+
+public:
+ OopsException(const char * msg) : msg(msg) {}
+ const char * what() const noexcept override
+ {
+ return msg;
+ }
+};
+
+void causeCrashForTesting(std::function<void()> fixture)
+{
+ registerCrashHandler();
+ std::cerr << "time to crash\n";
+ try {
+ fixture();
+ } catch (...) {
+ std::terminate();
+ }
+}
+
+TEST(CrashHandler, exceptionName)
+{
+ ASSERT_DEATH(
+ causeCrashForTesting([]() { throw OopsException{"lol oops"}; }),
+ "time to crash\nLix crashed.*OopsException: lol oops"
+ );
+}
+
+TEST(CrashHandler, unknownTerminate)
+{
+ ASSERT_DEATH(
+ causeCrashForTesting([]() { std::terminate(); }),
+ "time to crash\nLix crashed.*std::terminate\\(\\) called without exception"
+ );
+}
+
+TEST(CrashHandler, nonStdException)
+{
+ ASSERT_DEATH(
+ causeCrashForTesting([]() {
+ // NOLINTNEXTLINE(hicpp-exception-baseclass): intentional
+ throw 4;
+ }),
+ "time to crash\nLix crashed.*Unknown exception! Spooky\\."
+ );
+}
+
+}
diff --git a/tests/unit/meson.build b/tests/unit/meson.build
index 5baf10de2..3d3930731 100644
--- a/tests/unit/meson.build
+++ b/tests/unit/meson.build
@@ -264,9 +264,14 @@ test(
protocol : 'gtest',
)
+libmain_tests_sources = files(
+ 'libmain/crash.cc',
+ 'libmain/progress-bar.cc',
+)
+
libmain_tester = executable(
'liblixmain-tests',
- files('libmain/progress-bar.cc'),
+ libmain_tests_sources,
dependencies : [
liblixmain,
liblixexpr,