]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
test-dbus: Produce machine-readable TAP output
authorSimon McVittie <smcv@collabora.com>
Tue, 14 Nov 2017 17:20:40 +0000 (17:20 +0000)
committerSimon McVittie <smcv@collabora.com>
Wed, 15 Nov 2017 12:12:40 +0000 (12:12 +0000)
See http://testanything.org/ for more information on TAP.

Reviewed-by: Philip Withnall <withnall@endlessm.com>
Signed-off-by: Simon McVittie <smcv@collabora.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=103601

dbus/dbus-test-main.c
dbus/dbus-test-tap.c
dbus/dbus-test-tap.h
dbus/dbus-test.c
dbus/dbus-test.h
test/Makefile.am

index f8088f8232c40775a88c118a68073c9a9cbe2134..4624fd5c927243ecbb4bd9d5c3b745d41e621df7 100644 (file)
@@ -25,6 +25,7 @@
 #include <config.h>
 #include "dbus-types.h"
 #include "dbus-test.h"
+#include "dbus-test-tap.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -61,8 +62,7 @@ main (int    argc,
     specific_test = argv[2];
   else
     specific_test = NULL;
-  
-  dbus_internal_do_not_use_run_tests (test_data_dir, specific_test);
-  
-  return 0;
+
+  _dbus_run_tests (test_data_dir, specific_test);
+  return _dbus_test_done_testing ();
 }
index a6f99b5470621729f3474ef10625327f5455554d..710e5eb87006771f83c81e5d398470240a44f2e5 100644 (file)
@@ -40,6 +40,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+static unsigned int failures = 0;
+static unsigned int tap_test_counter = 0;
+
 /*
  * Output TAP indicating a fatal error, and exit unsuccessfully.
  */
@@ -74,4 +77,129 @@ _dbus_test_diag (const char *format,
   printf ("\n");
 }
 
+/*
+ * Output TAP indicating that all tests have been skipped, and exit
+ * successfully.
+ *
+ * This is only valid if _dbus_test_ok(), _dbus_test_not_ok(),
+ * etc. have not yet been called.
+ */
+void
+_dbus_test_skip_all (const char *format,
+    ...)
+{
+  va_list ap;
+
+  _dbus_assert (tap_test_counter == 0);
+
+  printf ("1..0 # SKIP - ");
+  va_start (ap, format);
+  vprintf (format, ap);
+  va_end (ap);
+  printf ("\n");
+  fflush (stdout);
+  exit (0);
+}
+
+/*
+ * Output TAP indicating that a test has passed, and increment the
+ * test counter.
+ */
+void
+_dbus_test_ok (const char *format,
+    ...)
+{
+  va_list ap;
+
+  printf ("ok %u - ", ++tap_test_counter);
+  va_start (ap, format);
+  vprintf (format, ap);
+  va_end (ap);
+  printf ("\n");
+  fflush (stdout);
+}
+
+/*
+ * Output TAP indicating that a test has failed (in a way that is not
+ * fatal to the test executable), and increment the test counter.
+ */
+void
+_dbus_test_not_ok (const char *format,
+    ...)
+{
+  va_list ap;
+
+  printf ("not ok %u - ", ++tap_test_counter);
+  va_start (ap, format);
+  vprintf (format, ap);
+  va_end (ap);
+  printf ("\n");
+  failures++;
+  fflush (stdout);
+}
+
+/*
+ * Output TAP indicating that a test has been skipped (in a way that is
+ * not fatal to the test executable), and increment the test counter.
+ */
+void
+_dbus_test_skip (const char *format,
+    ...)
+{
+  va_list ap;
+
+  printf ("ok %u # SKIP ", ++tap_test_counter);
+  va_start (ap, format);
+  vprintf (format, ap);
+  va_end (ap);
+  printf ("\n");
+  fflush (stdout);
+}
+
+/*
+ * Shut down libdbus, check that exactly previously_allocated memory
+ * blocks are allocated, and output TAP indicating a test pass or failure.
+ *
+ * Return TRUE if no leaks were detected.
+ */
+void
+_dbus_test_check_memleaks (const char *test_name)
+{
+  dbus_shutdown ();
+
+  if (_dbus_get_malloc_blocks_outstanding () == 0)
+    {
+      printf ("ok %u - %s did not leak memory\n", ++tap_test_counter,
+          test_name);
+    }
+  else
+    {
+      printf ("not ok %u - %s leaked %d blocks\n",
+          ++tap_test_counter, test_name,
+          _dbus_get_malloc_blocks_outstanding ());
+      failures++;
+    }
+}
+
+/*
+ * Output TAP indicating that testing has finished and no more tests
+ * are going to be run. Return the Unix-style exit code.
+ */
+int
+_dbus_test_done_testing (void)
+{
+  if (failures == 0)
+    _dbus_test_diag ("%u tests passed", tap_test_counter);
+  else
+    _dbus_test_diag ("%u/%u tests failed", failures, tap_test_counter);
+
+  printf ("1..%u\n", tap_test_counter);
+  fflush (stdout);
+
+  if (failures == 0)
+    return 0;
+
+  return 1;
+}
+
 #endif
index 706475bddaf0c4a0a1f810fafc5959fe18e1b5e4..ea1169212b3918f743d38a96e173aaf9f7d5c87e 100644 (file)
@@ -39,6 +39,26 @@ DBUS_PRIVATE_EXPORT
 void _dbus_test_diag (const char *format,
     ...) _DBUS_GNUC_PRINTF (1, 2);
 
+DBUS_PRIVATE_EXPORT
+void _dbus_test_skip_all (const char *format,
+    ...) _DBUS_GNUC_NORETURN _DBUS_GNUC_PRINTF (1, 2);
+
+DBUS_PRIVATE_EXPORT
+void _dbus_test_ok (const char *format,
+    ...) _DBUS_GNUC_PRINTF (1, 2);
+DBUS_PRIVATE_EXPORT
+void _dbus_test_not_ok (const char *format,
+    ...) _DBUS_GNUC_PRINTF (1, 2);
+DBUS_PRIVATE_EXPORT
+void _dbus_test_skip (const char *format,
+    ...) _DBUS_GNUC_PRINTF (1, 2);
+
+DBUS_PRIVATE_EXPORT
+void _dbus_test_check_memleaks (const char *test_name);
+
+DBUS_PRIVATE_EXPORT
+int _dbus_test_done_testing (void);
+
 #endif
 
 #endif
index 8ae91d65732e0bf959d1c7ae1e7bfe2bd1814ee8..ae0b791c488d6b2ad7c2e5cb4a6c1bdf437aee11 100644 (file)
 #include <stdlib.h>
 
 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
-static void
-check_memleaks (void)
-{
-  dbus_shutdown ();
-
-  _dbus_test_diag ("%s: checking for memleaks", "test-dbus");
-  if (_dbus_get_malloc_blocks_outstanding () != 0)
-    _dbus_test_fatal ("%d dbus_malloc blocks were not freed",
-                      _dbus_get_malloc_blocks_outstanding ());
-}
-
 typedef dbus_bool_t (*TestFunc)(void);
 typedef dbus_bool_t (*TestDataFunc)(const char *data);
 
 static void
 run_test (const char             *test_name,
-         const char             *specific_test,
-         TestFunc                test)
+          const char             *specific_test,
+          TestFunc                test)
 {
-  if (!specific_test || strcmp (specific_test, test_name) == 0)
+  if (specific_test != NULL && strcmp (specific_test, test_name) != 0)
     {
-      _dbus_test_diag ("%s: running %s tests", "test-dbus", test_name);
-      if (!test ())
-        _dbus_test_fatal ("%s test failed", test_name);
-
-      check_memleaks ();
+      _dbus_test_skip ("%s - Only intending to run %s", test_name, specific_test);
+      return;
     }
+
+  _dbus_test_diag ("%s: running %s tests", "test-dbus", test_name);
+
+  if (test ())
+    _dbus_test_ok ("%s", test_name);
+  else
+    _dbus_test_not_ok ("%s", test_name);
+
+  _dbus_test_check_memleaks (test_name);
 }
 
 static void
 run_data_test (const char             *test_name,
-              const char             *specific_test,
-              TestDataFunc            test,
-              const char             *test_data_dir)
+               const char             *specific_test,
+               TestDataFunc            test,
+               const char             *test_data_dir)
 {
-  if (!specific_test || strcmp (specific_test, test_name) == 0)
+  if (specific_test != NULL && strcmp (specific_test, test_name) != 0)
     {
-      _dbus_test_diag ("%s: running %s tests", "test-dbus", test_name);
-      if (!test (test_data_dir))
-        _dbus_test_fatal ("%s test failed", test_name);
-
-      check_memleaks ();
+      _dbus_test_skip ("%s - Only intending to run %s", test_name, specific_test);
+      return;
     }
+
+  _dbus_test_diag ("%s: running %s tests", "test-dbus", test_name);
+
+  if (test (test_data_dir))
+    _dbus_test_ok ("%s", test_name);
+  else
+    _dbus_test_not_ok ("%s", test_name);
+
+  _dbus_test_check_memleaks (test_name);
 }
 
 /**
@@ -86,7 +87,8 @@ run_data_test (const char             *test_name,
  * @param specific_test run specific test or #NULL to run all tests
  */
 void
-dbus_internal_do_not_use_run_tests (const char *test_data_dir, const char *specific_test)
+_dbus_run_tests (const char   *test_data_dir,
+                 const char   *specific_test)
 {
   if (!_dbus_threads_init_debug ())
     _dbus_test_fatal ("debug threads init failed");
@@ -152,8 +154,6 @@ dbus_internal_do_not_use_run_tests (const char *test_data_dir, const char *speci
   run_data_test ("sha", specific_test, _dbus_sha_test, test_data_dir);
   
   run_data_test ("auth", specific_test, _dbus_auth_test, test_data_dir);
-
-  _dbus_test_diag ("%s: completed successfully", "test-dbus");
 }
 
 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
index 71876a1021e6f0a2be98221e6487dc3c35a8f017..113aded4296bd2140f37b17719646dd7c5534c80 100644 (file)
@@ -96,8 +96,9 @@ dbus_bool_t _dbus_object_tree_test       (void);
 
 dbus_bool_t _dbus_credentials_test       (const char *test_data_dir);
 
-void        dbus_internal_do_not_use_run_tests         (const char          *test_data_dir,
-                                                       const char          *specific_test);
+void        _dbus_run_tests                            (const char          *test_data_dir,
+                                                        const char          *specific_test);
+
 dbus_bool_t dbus_internal_do_not_use_try_message_file  (const DBusString    *filename,
                                                         DBusValidity         expected_validity);
 dbus_bool_t dbus_internal_do_not_use_try_message_data  (const DBusString    *data,
index bc5bc1852456564c38f8744022b19221cc204544..2721a4a9025b961ea8478e149562ae19ac55ed06 100644 (file)
@@ -70,28 +70,24 @@ TEST_BINARIES = \
 
 ## These are conceptually part of directories that come earlier in SUBDIRS
 ## order, but we don't want to run them til we arrive in this directory,
-## since they depend on stuff from this directory. We wrap them in a
+## since they depend on stuff from this directory. We wrap some of them in a
 ## simple shell script to get TAP output.
 
 wrap_bus_tests = test-bus.sh
-wrap_dbus_tests = test-dbus.sh
 
 if DBUS_UNIX
 wrap_bus_tests += test-bus-launch-helper.sh
 wrap_bus_tests += test-bus-system.sh
 endif
 
-TESTS += $(wrap_bus_tests) $(wrap_dbus_tests)
-CLEANFILES += $(wrap_bus_tests) $(wrap_dbus_tests)
+TESTS += $(wrap_bus_tests)
+TESTS += ../dbus/test-dbus$(EXEEXT)
+CLEANFILES += $(wrap_bus_tests)
 
 $(wrap_bus_tests): test-bus%.sh: ../bus/test-bus%$(EXEEXT) tap-test.sh.in Makefile
        sed -e 's![@]RUN[@]!$<!' \
                < $(srcdir)/tap-test.sh.in > $@
 
-$(wrap_dbus_tests): test-dbus%.sh: ../dbus/test-dbus%$(EXEEXT) tap-test.sh.in Makefile
-       sed -e 's![@]RUN[@]!$<!' \
-               < $(srcdir)/tap-test.sh.in > $@
-
 else !DBUS_ENABLE_EMBEDDED_TESTS
 
 TEST_BINARIES=