From 3ec30d2e9049b58ddd4ab1d1be294b73b9273bf2 Mon Sep 17 00:00:00 2001 From: brarcher Date: Thu, 7 May 2015 03:16:23 +0000 Subject: [PATCH] Change behavior of calls for fork() on non-fork() platforms It was requested by users of Windows (e.g. non-fork() supporting platforms) that calls to set CK_FORK mode be ignored if fork() is not supported. This is to avoid the need to detect the current platform and conditionally compile unit test code. git-svn-id: svn+ssh://svn.code.sf.net/p/check/code/trunk@1202 64e312b2-a51f-0410-8e61-82d0ca0eb02a --- NEWS | 6 ++++++ src/check.c | 4 +--- src/check.h.in | 15 ++++++++++++++- src/check_run.c | 12 +++++++----- tests/check_nofork.c | 27 +++++++++++++++++++++++++++ tests/test_check_nofork.sh | 4 +++- 6 files changed, 58 insertions(+), 10 deletions(-) diff --git a/NEWS b/NEWS index 91e2056..b6aac95 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,12 @@ In Development: # Mentioning Check 0.9.14 for now, to fix distcheck target until next release +* If Check is compiled without support for fork(), the behavior of + functions which require fork() to be useful have been changed. + Functions that attempt to set CK_FORK mode are no-ops, + check_fork() returns in failure, and check_waitpid_and_exit() + exits in failure. + * Add space around operators in assert messages for readability. Bug #102. diff --git a/src/check.c b/src/check.c index d288e6d..20eec90 100644 --- a/src/check.c +++ b/src/check.c @@ -265,9 +265,7 @@ void tcase_set_timeout(TCase * tc, double timeout) #else (void)tc; (void)timeout; - eprintf - ("This version does not support timeouts, as fork is not supported", - __FILE__, __LINE__); + /* Ignoring, as Check is not compiled with fork support. */ #endif /* HAVE_FORK */ } diff --git a/src/check.h.in b/src/check.h.in index 4adcb11..3d1459f 100644 --- a/src/check.h.in +++ b/src/check.h.in @@ -343,6 +343,9 @@ CK_DLL_EXP void CK_EXPORT tcase_add_checked_fixture(TCase * tc, SFun setup, * the environment variable CK_DEFAULT_TIMEOUT is defined and no timeout * is set, the value in the environment variable is used. * + * If Check is compile without fork() support this call is ignored, + * as timeouts are not possible. + * * @param tc test case to assign timeout to * @param timeout to use, in seconds. If the value contains a decimal * portion, but no high resolution timer is available, @@ -1206,6 +1209,9 @@ CK_DLL_EXP enum fork_status CK_EXPORT srunner_fork_status(SRunner * sr); * If set to CK_FORK or CK_NOFORK, the environment variable * if defined is ignored. * + * If Check is compiled without support for fork(), attempting + * to set the status to CK_FORK is ignored. + * * @param sr suite runner to assign the fork status to * @param fstat fork status to assign * @@ -1224,9 +1230,13 @@ CK_DLL_EXP void CK_EXPORT srunner_set_fork_status(SRunner * sr, * the process group will be killed; using this wrapper will prevent * orphan processes. * + * If Check is compiled without fork() support this call simply + * return -1 and does nothing. + * * @return On success, the PID of the child process is returned in * the parent, and 0 is returned in the child. On failure, - * an error will be printed and exit() invoked. + * a value of -1 is returned to the parent process and no + * child process is created. * * @since 0.9.3 */ @@ -1240,6 +1250,9 @@ CK_DLL_EXP pid_t CK_EXPORT check_fork(void); * exited without error, exit(EXIT_SUCCESS) is invoked; otherwise * exit(EXIT_FAILURE) is invoked. * + * If Check is compiled without support for fork(), this invokes + * exit(EXIT_FAILURE). + * * @param pid process to wait for, created by check_fork() * * @since 0.9.3 diff --git a/src/check_run.c b/src/check_run.c index 94486bc..911ccfe 100644 --- a/src/check_run.c +++ b/src/check_run.c @@ -685,7 +685,7 @@ enum fork_status srunner_fork_status(SRunner * sr) #if defined(HAVE_FORK) && HAVE_FORK==1 return CK_FORK; #else /* HAVE_FORK */ - eprintf("This version does not support fork", __FILE__, __LINE__); + /* Ignoring, as Check is not compiled with fork support. */ return CK_NOFORK; #endif /* HAVE_FORK */ } @@ -700,7 +700,8 @@ void srunner_set_fork_status(SRunner * sr, enum fork_status fstat) /* If fork() is unavailable, do not allow a fork mode to be set */ if(fstat != CK_NOFORK) { - eprintf("This version does not support fork", __FILE__, __LINE__); + /* Overriding, as Check is not compiled with fork support. */ + fstat = CK_NOFORK; } #endif /* ! HAVE_FORK */ sr->fstat = fstat; @@ -760,8 +761,8 @@ pid_t check_fork(void) } return pid; #else /* HAVE_FORK */ - eprintf("This version does not support fork", __FILE__, __LINE__); - return 0; + /* Ignoring, as Check is not compiled with fork support. */ + return -1; #endif /* HAVE_FORK */ } @@ -785,6 +786,7 @@ void check_waitpid_and_exit(pid_t pid CK_ATTRIBUTE_UNUSED) } exit(EXIT_SUCCESS); #else /* HAVE_FORK */ - eprintf("This version does not support fork", __FILE__, __LINE__); + /* Ignoring, as Check is not compiled with fork support. */ + exit(EXIT_FAILURE); #endif /* HAVE_FORK */ } diff --git a/tests/check_nofork.c b/tests/check_nofork.c index f7310d7..92d7b2b 100644 --- a/tests/check_nofork.c +++ b/tests/check_nofork.c @@ -20,6 +20,14 @@ START_TEST(test_nofork_exit) } END_TEST +#if !defined(HAVE_FORK) || HAVE_FORK == 0 +START_TEST(test_check_fork) +{ + ck_assert_int_eq(-1, check_fork()); +} +END_TEST +#endif + int main(void) { s = suite_create("NoFork"); @@ -33,5 +41,24 @@ int main(void) srunner_run_all(sr, CK_MINIMAL); srunner_free(sr); +#if !defined(HAVE_FORK) || HAVE_FORK == 0 + s = suite_create("NoForkSupport"); + tc = tcase_create("NoFork"); + sr = srunner_create(s); + + /* The following should not fail, but should be ignored */ + srunner_set_fork_status(sr, CK_FORK); + if(srunner_fork_status(sr) != CK_NOFORK) + { + fprintf(stderr, "Call to srunner_set_fork_status() was not ignored\n"); + exit(1); + } + + suite_add_tcase(s, tc); + tcase_add_test(tc, test_check_fork); + srunner_run_all(sr, CK_MINIMAL); + srunner_free(sr); +#endif + return 0; } diff --git a/tests/test_check_nofork.sh b/tests/test_check_nofork.sh index fff318e..fda9cd9 100755 --- a/tests/test_check_nofork.sh +++ b/tests/test_check_nofork.sh @@ -7,7 +7,9 @@ expected="Running suite(s): NoFork 0%: Checks: 1, Failures: 1, Errors: 0" else expected="Running suite(s): NoFork -0%: Checks: 1, Failures: 1, Errors: 0" +0%: Checks: 1, Failures: 1, Errors: 0 +Running suite(s): NoForkSupport +100%: Checks: 1, Failures: 0, Errors: 0" fi actual=`./check_nofork${EXEEXT} | tr -d "\r"` -- 2.49.0