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
This commit is contained in:
Martchus 2023-04-29 12:54:53 +02:00
parent 93a632e831
commit cb5ca77658
1 changed files with 18 additions and 4 deletions

View File

@ -277,11 +277,25 @@ template <typename T, Traits::DisableIf<std::is_integral<T>> * = 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)); \
} \
}