]> git.ozlabs.org Git - ccan/blobdiff - ccan/tap/tap.c
typesafe_cb: simplify, preserve namespace.
[ccan] / ccan / tap / tap.c
index 4bbd977bbfdd6f3312295afb67a3d9e5d7832206..b7a84ac9900b398a40687e4e2fedd2bef3049518 100644 (file)
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-#define _GNU_SOURCE
+#include "config.h"
 #include <ctype.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include "tap.h"
 
@@ -38,13 +39,14 @@ static unsigned int test_count = 0; /* Number of tests that have been run */
 static unsigned int e_tests = 0; /* Expected number of tests to run */
 static unsigned int failures = 0; /* Number of tests that failed */
 static char *todo_msg = NULL;
-static char *todo_msg_fixed = "libtap malloc issue";
+static const char *todo_msg_fixed = "libtap malloc issue";
 static int todo = 0;
 static int test_died = 0;
+static int test_pid;
 
 /* Encapsulate the pthread code in a conditional.  In the absence of
    libpthread the code does nothing */
-#ifdef HAVE_LIBPTHREAD
+#if HAVE_LIBPTHREAD
 #include <pthread.h>
 static pthread_mutex_t M = PTHREAD_MUTEX_INITIALIZER;
 # define LOCK pthread_mutex_lock(&M)
@@ -63,15 +65,15 @@ _expected_tests(unsigned int tests)
 }
 
 static void
-diagv(char *fmt, va_list ap)
+diagv(const char *fmt, va_list ap)
 {
-       fputs("# ", stderr);
-       vfprintf(stderr, fmt, ap);
-       fputs("\n", stderr);
+       fputs("# ", stdout);
+       vfprintf(stdout, fmt, ap);
+       fputs("\n", stdout);
 }
 
 static void
-_diag(char *fmt, ...)
+_diag(const char *fmt, ...)
 {
        va_list ap;
        va_start(ap, fmt);
@@ -87,8 +89,8 @@ _diag(char *fmt, ...)
  * test_comment -- a comment to print afterwards, may be NULL
  */
 unsigned int
-_gen_result(int ok, const char *func, char *file, unsigned int line, 
-           char *test_name, ...)
+_gen_result(int ok, const char *func, const char *file, unsigned int line,
+           const char *test_name, ...)
 {
        va_list ap;
        char *local_test_name = NULL;
@@ -103,7 +105,8 @@ _gen_result(int ok, const char *func, char *file, unsigned int line,
           expansions on it */
        if(test_name != NULL) {
                va_start(ap, test_name);
-               vasprintf(&local_test_name, test_name, ap);
+               if (vasprintf(&local_test_name, test_name, ap) < 0)
+                       local_test_name = NULL;
                va_end(ap);
 
                /* Make sure the test name contains more than digits
@@ -112,7 +115,8 @@ _gen_result(int ok, const char *func, char *file, unsigned int line,
                if(local_test_name) {
                        name_is_digits = 1;
                        for(c = local_test_name; *c != '\0'; c++) {
-                               if(!isdigit(*c) && !isspace(*c)) {
+                               if(!isdigit((unsigned char)*c)
+                                  && !isspace((unsigned char)*c)) {
                                        name_is_digits = 0;
                                        break;
                                }
@@ -166,13 +170,16 @@ _gen_result(int ok, const char *func, char *file, unsigned int line,
        printf("\n");
 
        if(!ok)
-               _diag("    Failed %stest (%s:%s() at line %d)", 
+               _diag("    Failed %stest (%s:%s() at line %d)",
                      todo ? "(TODO) " : "", file, func, line);
 
        free(local_test_name);
 
        UNLOCK;
 
+       if (!ok && tap_fail_callback)
+               tap_fail_callback();
+
        /* We only care (when testing) that ok is positive, but here we
           specifically only want to return 1 or 0 */
        return ok ? 1 : 0;
@@ -185,6 +192,9 @@ _gen_result(int ok, const char *func, char *file, unsigned int line,
 static void
 _cleanup(void)
 {
+       /* If we forked, don't do cleanup in child! */
+       if (getpid() != test_pid)
+               return;
 
        LOCK;
 
@@ -221,7 +231,7 @@ _cleanup(void)
                _diag("Looks like you planned %d tests but only ran %d.",
                      e_tests, test_count);
                if(failures) {
-                       _diag("Looks like you failed %d tests of %d run.", 
+                       _diag("Looks like you failed %d tests of %d run.",
                              failures, test_count);
                }
                UNLOCK;
@@ -229,7 +239,7 @@ _cleanup(void)
        }
 
        if(failures)
-               _diag("Looks like you failed %d tests of %d.", 
+               _diag("Looks like you failed %d tests of %d.",
                      failures, test_count);
 
        UNLOCK;
@@ -245,12 +255,13 @@ _tap_init(void)
        static int run_once = 0;
 
        if(!run_once) {
+               test_pid = getpid();
                atexit(_cleanup);
 
                /* stdout needs to be unbuffered so that the output appears
-                  in the same place relative to stderr output as it does 
+                  in the same place relative to stderr output as it does
                   with Test::Harness */
-               setbuf(stdout, 0);
+//             setbuf(stdout, 0);
                run_once = 1;
        }
 }
@@ -283,7 +294,7 @@ plan_no_plan(void)
  * Note that the plan is to skip all tests
  */
 void
-plan_skip_all(char *reason)
+plan_skip_all(const char *reason)
 {
 
        LOCK;
@@ -335,7 +346,7 @@ plan_tests(unsigned int tests)
 }
 
 void
-diag(char *fmt, ...)
+diag(const char *fmt, ...)
 {
        va_list ap;
 
@@ -349,7 +360,7 @@ diag(char *fmt, ...)
 }
 
 void
-skip(unsigned int n, char *fmt, ...)
+skip(unsigned int n, const char *fmt, ...)
 {
        va_list ap;
        char *skip_msg;
@@ -357,13 +368,14 @@ skip(unsigned int n, char *fmt, ...)
        LOCK;
 
        va_start(ap, fmt);
-       vasprintf(&skip_msg, fmt, ap);
+       if (vasprintf(&skip_msg, fmt, ap) < 0)
+               skip_msg = NULL;
        va_end(ap);
 
        while(n-- > 0) {
                test_count++;
-               printf("ok %d # skip %s\n", test_count, 
-                      skip_msg != NULL ? 
+               printf("ok %d # skip %s\n", test_count,
+                      skip_msg != NULL ?
                       skip_msg : "libtap():malloc() failed");
        }
 
@@ -373,14 +385,15 @@ skip(unsigned int n, char *fmt, ...)
 }
 
 void
-todo_start(char *fmt, ...)
+todo_start(const char *fmt, ...)
 {
        va_list ap;
 
        LOCK;
 
        va_start(ap, fmt);
-       vasprintf(&todo_msg, fmt, ap);
+       if (vasprintf(&todo_msg, fmt, ap) < 0)
+               todo_msg = NULL;
        va_end(ap);
 
        todo = 1;
@@ -421,7 +434,7 @@ exit_status(void)
                return r;
        }
 
-       /* Return the number of tests that failed + the number of tests 
+       /* Return the number of tests that failed + the number of tests
           that weren't run */
        r = failures + e_tests - test_count;
        UNLOCK;