Fix exit codes returned by CUnit test runner
Signed-off-by: Jeroen Koekkoek <jeroen@koekkoek.nl>
This commit is contained in:
parent
217def362c
commit
f56eccf6a6
1 changed files with 34 additions and 23 deletions
|
@ -10,11 +10,19 @@
|
||||||
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
|
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
|
||||||
*/
|
*/
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#include <sysexits.h>
|
||||||
|
#else
|
||||||
|
#define EX_USAGE (64)
|
||||||
|
#define EX_SOFTWARE (70)
|
||||||
|
#endif /* _WIN32 */
|
||||||
|
|
||||||
#include <CUnit/Basic.h>
|
#include <CUnit/Basic.h>
|
||||||
#include <CUnit/Automated.h>
|
#include <CUnit/Automated.h>
|
||||||
#include <CUnit/Test.h>
|
#include "CUnit/Test.h"
|
||||||
|
|
||||||
static struct {
|
static struct {
|
||||||
bool print_help;
|
bool print_help;
|
||||||
|
@ -36,7 +44,7 @@ static struct {
|
||||||
"*"
|
"*"
|
||||||
};
|
};
|
||||||
|
|
||||||
int patmatch(const char *pat, const char *str)
|
static int patmatch(const char *pat, const char *str)
|
||||||
{
|
{
|
||||||
while (*pat) {
|
while (*pat) {
|
||||||
if (*pat == '?') {
|
if (*pat == '?') {
|
||||||
|
@ -91,14 +99,19 @@ static void help(const char *name)
|
||||||
printf(" -r FILENAME results file for automated run\n");
|
printf(" -r FILENAME results file for automated run\n");
|
||||||
printf(" -s PATTERN run only tests in suites matching pattern\n");
|
printf(" -s PATTERN run only tests in suites matching pattern\n");
|
||||||
printf(" -t PATTERN run only test matching pattern\n");
|
printf(" -t PATTERN run only test matching pattern\n");
|
||||||
|
printf("\n");
|
||||||
|
printf("Exit codes:\n");
|
||||||
|
printf(" %-2d Successful termination\n", EXIT_SUCCESS);
|
||||||
|
printf(" %-2d One or more failing test cases\n", EXIT_FAILURE);
|
||||||
|
printf(" %-2d Command line usage error\n", EX_USAGE);
|
||||||
|
printf(" %-2d Internal software error\n", EX_SOFTWARE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static CU_ErrorCode parse_options(int argc, char *argv[])
|
static int parse_options(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
CU_ErrorCode err = CUE_SUCCESS;
|
int err = 0;
|
||||||
|
|
||||||
|
for (int i = 1; err == 0 && i < argc; i++) {
|
||||||
for (int i = 1; err == CUE_SUCCESS && i < argc; i++) {
|
|
||||||
switch ((argv[i][0] == '-') ? argv[i][1] : 0) {
|
switch ((argv[i][0] == '-') ? argv[i][1] : 0) {
|
||||||
case 'a':
|
case 'a':
|
||||||
opts.automated = true;
|
opts.automated = true;
|
||||||
|
@ -131,8 +144,7 @@ static CU_ErrorCode parse_options(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
/* FALLS THROUGH */
|
/* FALLS THROUGH */
|
||||||
default:
|
default:
|
||||||
err = (CU_ErrorCode)256;
|
err = 1;
|
||||||
CU_set_error(err); /* Will print as "Undefined Errpr" */
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -140,7 +152,7 @@ static CU_ErrorCode parse_options(int argc, char *argv[])
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static void
|
||||||
add_suite(
|
add_suite(
|
||||||
const char *suite,
|
const char *suite,
|
||||||
CU_InitializeFunc pInitFunc,
|
CU_InitializeFunc pInitFunc,
|
||||||
|
@ -158,7 +170,7 @@ add_suite(
|
||||||
#define ADD_SUITE(suite, init, clean) \
|
#define ADD_SUITE(suite, init, clean) \
|
||||||
add_suite(#suite, init, clean)
|
add_suite(#suite, init, clean)
|
||||||
|
|
||||||
void
|
static void
|
||||||
add_test(
|
add_test(
|
||||||
const char *suite,
|
const char *suite,
|
||||||
const char *test,
|
const char *test,
|
||||||
|
@ -181,19 +193,17 @@ add_test(
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
CU_ErrorCode err;
|
int ret = EXIT_SUCCESS;
|
||||||
|
|
||||||
if ((err = parse_options(argc, argv)) != CUE_SUCCESS) {
|
if (parse_options(argc, argv) != 0) {
|
||||||
usage(argv[0]);
|
usage(argv[0]);
|
||||||
return (int)err;
|
return EX_USAGE;
|
||||||
} else if (opts.print_help) {
|
} else if (opts.print_help) {
|
||||||
help(argv[0]);
|
help(argv[0]);
|
||||||
return CUE_SUCCESS;
|
return ret;
|
||||||
}
|
} else if (CU_initialize_registry() != CUE_SUCCESS) {
|
||||||
|
fprintf(stderr, "CU_initialize_registry: %s\n", CU_get_error_msg());
|
||||||
if ((err = CU_initialize_registry()) != CUE_SUCCESS) {
|
return EX_SOFTWARE;
|
||||||
fprintf(stderr, "CU_initialize_registry: %s\n", CU_get_error_msg());
|
|
||||||
return (int)err;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* CMake will expand the macro below to register all suites and tests. */
|
/* CMake will expand the macro below to register all suites and tests. */
|
||||||
|
@ -220,13 +230,14 @@ int main(int argc, char *argv[])
|
||||||
CU_basic_run_tests();
|
CU_basic_run_tests();
|
||||||
}
|
}
|
||||||
|
|
||||||
err = CU_get_error();
|
if (CU_get_error() != CUE_SUCCESS) {
|
||||||
if (err == CUE_SUCCESS) {
|
ret = EX_SOFTWARE;
|
||||||
err = (CU_ErrorCode)(CU_get_number_of_failures() == 0 ? 0 : 256);
|
} else if (CU_get_number_of_failures() != 0) {
|
||||||
|
ret = EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
CU_cleanup_registry();
|
CU_cleanup_registry();
|
||||||
|
|
||||||
return (int)err;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue