From cb5ca7765843e2205ed92556c65718331401a392 Mon Sep 17 00:00:00 2001 From: Martchus Date: Sat, 29 Apr 2023 12:54:53 +0200 Subject: [PATCH] Improve test helper for launching an app * Add helper that allows to specify the exit code (in case a non-zero exit code is actually expected) * Use the proper macros to inspect the status returned by `waitpid()` to print/check the exit status correctly --- tests/testutils.h | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/tests/testutils.h b/tests/testutils.h index c1bc57b..9c87bf8 100644 --- a/tests/testutils.h +++ b/tests/testutils.h @@ -277,11 +277,25 @@ template > * = nullptr> const * * \remarks Requires cppunit. */ -#define TESTUTILS_ASSERT_EXEC(args) \ +#define TESTUTILS_ASSERT_EXEC(args) TESTUTILS_ASSERT_EXEC_EXIT_STATUS(args, 0) + +/*! + * \brief Asserts the execution of the application with the specified CLI \a args and the specified \a expectedExitStatus. + * + * The application is executed via TestApplication::execApp(). Output is stored in the std::string variables stdout + * and stderr. + * + * \remarks Requires cppunit. + */ +#define TESTUTILS_ASSERT_EXEC_EXIT_STATUS(args, expectedExitStatus) \ { \ - const auto returnCode = execApp(args, stdout, stderr); \ - if (returnCode != 0) { \ - CPPUNIT_FAIL(::CppUtilities::argsToString("app failed with return code ", returnCode, "\nstdout: ", stdout, "\nstderr: ", stderr)); \ + const auto status = execApp(args, stdout, stderr); \ + if (!WIFEXITED(status)) { \ + CPPUNIT_FAIL(::CppUtilities::argsToString("app did not terminate normally\nstdout: ", stdout, "\nstderr: ", stderr)); \ + } \ + if (const auto exitStatus = WEXITSTATUS(status); exitStatus != expectedExitStatus) { \ + CPPUNIT_FAIL(::CppUtilities::argsToString( \ + "app exited with status ", exitStatus, " (expected ", expectedExitStatus, ")\nstdout: ", stdout, "\nstderr: ", stderr)); \ } \ }