From c8cabd433a582a259368c60c59bfcaad6ff6e038 Mon Sep 17 00:00:00 2001 From: Michel Hermier Date: Wed, 15 Dec 2021 08:58:40 +0100 Subject: [PATCH] LibTest: Handle Crash printing in a private method --- Userland/Libraries/LibTest/CrashTest.cpp | 39 ++++++++++++------------ Userland/Libraries/LibTest/CrashTest.h | 2 ++ 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/Userland/Libraries/LibTest/CrashTest.cpp b/Userland/Libraries/LibTest/CrashTest.cpp index bcb7b83f1a..aff77990bf 100644 --- a/Userland/Libraries/LibTest/CrashTest.cpp +++ b/Userland/Libraries/LibTest/CrashTest.cpp @@ -27,26 +27,8 @@ bool Crash::run(RunType run_type) { outln("\x1B[33mTesting\x1B[0m: \"{}\"", m_type); - auto run_crash_and_print_if_error = [this]() -> bool { - auto failure = m_crash_function(); - - // If we got here something went wrong - out("\x1B[31mFAIL\x1B[0m: "); - switch (failure) { - case Failure::DidNotCrash: - outln("Did not crash!"); - break; - case Failure::UnexpectedError: - outln("Unexpected error!"); - break; - default: - VERIFY_NOT_REACHED(); - } - return false; - }; - if (run_type == RunType::UsingCurrentProcess) { - return run_crash_and_print_if_error(); + return do_report(m_crash_function()); } else { // Run the test in a child process so that we do not crash the crash program :^) pid_t pid = fork(); @@ -58,7 +40,7 @@ bool Crash::run(RunType run_type) if (prctl(PR_SET_DUMPABLE, 0, 0) < 0) perror("prctl(PR_SET_DUMPABLE)"); #endif - run_crash_and_print_if_error(); + return do_report(m_crash_function()); exit(0); } @@ -72,4 +54,21 @@ bool Crash::run(RunType run_type) } } +bool Crash::do_report(Failure failure) +{ + // If we got here something went wrong + out("\x1B[31mFAIL\x1B[0m: "); + switch (failure) { + case Failure::DidNotCrash: + outln("Did not crash!"); + break; + case Failure::UnexpectedError: + outln("Unexpected error!"); + break; + default: + VERIFY_NOT_REACHED(); + } + return false; +} + } diff --git a/Userland/Libraries/LibTest/CrashTest.h b/Userland/Libraries/LibTest/CrashTest.h index f7dbf1f217..46e5e858e0 100644 --- a/Userland/Libraries/LibTest/CrashTest.h +++ b/Userland/Libraries/LibTest/CrashTest.h @@ -30,6 +30,8 @@ public: bool run(RunType run_type = RunType::UsingChildProcess); private: + bool do_report(Failure failure); + String m_type; Function m_crash_function; };