From c7e181d8f88304fbc6cd5895794a6a81cf768bf6 Mon Sep 17 00:00:00 2001 From: qgymib Date: Tue, 20 Dec 2022 09:13:56 +0800 Subject: [PATCH] exit code is explicit --- CHANGELOG.md | 1 + include/cutest.h | 2 +- src/cutest.c | 21 +++++++++++++++------ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea80fcd9..7c9da309 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ### BREAKING CHANGES 1. Remove x32 / x64 assertion methods 2. Hide some function that user should not use +3. Exit code is explicit: 0 is success, otherwise failure ### Fixed 1. Fix: crash on log if no hook passed diff --git a/include/cutest.h b/include/cutest.h index 0f318468..2096a056 100644 --- a/include/cutest.h +++ b/include/cutest.h @@ -40,7 +40,7 @@ /** * @brief Development version. */ -#define CUTEST_VERSION_PREREL 6 +#define CUTEST_VERSION_PREREL 7 #ifdef __cplusplus extern "C" { diff --git a/src/cutest.c b/src/cutest.c index 3c9f2712..6cfce44e 100644 --- a/src/cutest.c +++ b/src/cutest.c @@ -2216,7 +2216,15 @@ static void _test_prepare(void) g_test_ctx.counter.repeat.repeat = 1; } -static int _test_setup(int argc, char* argv[], const cutest_hook_t* hook) +/** + * @brief Setup test context + * @param[in] argc The number of command line argument. + * @param[in] argv Command line argument list. + * @param[in] hook Global test hook. + * @param[out] b_exit Whether need to exit. + * @return 0 if success, otherwise failure. + */ +static int _test_setup(int argc, char* argv[], const cutest_hook_t* hook, int* b_exit) { (void)argc; @@ -2287,7 +2295,8 @@ static int _test_setup(int argc, char* argv[], const cutest_hook_t* hook) break; case help: _print_encoded(_get_logfile(), s_test_help_encoded); - return -1; + *b_exit = 1; + return 0; default: break; } @@ -2572,12 +2581,12 @@ int cutest_register_case(cutest_case_t* data) int cutest_run_tests(int argc, char* argv[], const cutest_hook_t* hook) { - int ret; + int ret = 0; /* Parser parameter */ - if (_test_setup(argc, argv, hook) < 0) + int b_exit = 0; + if ((ret = _test_setup(argc, argv, hook, &b_exit)) < 0 || b_exit) { - ret = 0; goto fin; } @@ -2589,7 +2598,7 @@ int cutest_run_tests(int argc, char* argv[], const cutest_hook_t* hook) _test_hook_after_all_test(); _test_cleanup(); - return ret; + return ret != 0 ? EXIT_FAILURE : EXIT_SUCCESS; } void cutest_unwrap_assert_fail(const char *expr, const char *file, int line, const char *func)