From 58eefa1031e14cb402ed0aae85e6bce1ba030a28 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 26 Feb 2015 17:39:20 +0000 Subject: [PATCH] test-shell, test-printf: produce TAP output like the other installable tests Reviewed-by: Philip Withnall Bug: https://bugs.freedesktop.org/show_bug.cgi?id=89846 --- test/internals/printf.c | 17 ++++++++ test/shell-test.c | 103 +++++++++++++++++++++++++++++++++++++----------- 2 files changed, 96 insertions(+), 24 deletions(-) diff --git a/test/internals/printf.c b/test/internals/printf.c index db15151..2006f32 100644 --- a/test/internals/printf.c +++ b/test/internals/printf.c @@ -62,20 +62,32 @@ do_test (int minimum, #define X_TIMES_512 X_TIMES_256 X_TIMES_256 #define X_TIMES_1024 X_TIMES_512 X_TIMES_512 +/* This test outputs TAP syntax: http://testanything.org/ */ int main (int argc, char **argv) { char buf[] = X_TIMES_1024 X_TIMES_1024 X_TIMES_1024 X_TIMES_1024; int i; + int test_num = 0; do_test (1, "%d", 0); + printf ("ok %d\n", ++test_num); + do_test (7, "%d", 1234567); + printf ("ok %d\n", ++test_num); + do_test (3, "%f", 3.5); + printf ("ok %d\n", ++test_num); do_test (0, "%s", ""); + printf ("ok %d\n", ++test_num); + do_test (1024, "%s", X_TIMES_1024); + printf ("ok %d\n", ++test_num); + do_test (1025, "%s", X_TIMES_1024 "Y"); + printf ("ok %d\n", ++test_num); for (i = 4096; i > 0; i--) { @@ -83,6 +95,11 @@ main (int argc, do_test (i, "%s", buf); do_test (i + 3, "%s:%d", buf, 42); } + printf ("ok %d\n", ++test_num); + /* Tell the TAP driver that we have done all the tests we plan to do. + * This is how it can distinguish between an unexpected exit and + * successful completion. */ + printf ("1..%d\n", test_num); return 0; } diff --git a/test/shell-test.c b/test/shell-test.c index 87aa682..23a1bb4 100644 --- a/test/shell-test.c +++ b/test/shell-test.c @@ -9,8 +9,13 @@ #include #include +static int test_num = 0; +static int num_failed = 0; + static dbus_bool_t -test_command_line (const char *arg1, ...) +test_command_line_internal (dbus_bool_t should_work, + const char *arg1, + va_list var_args) { int i, original_argc, shell_argc; char **shell_argv; @@ -18,10 +23,8 @@ test_command_line (const char *arg1, ...) char *command_line, *tmp; DBusString str; DBusList *list = NULL, *node; - va_list var_args; DBusError error; - va_start (var_args, arg1); _dbus_list_append (&list, (char *)arg1); do { @@ -30,7 +33,6 @@ test_command_line (const char *arg1, ...) break; _dbus_list_append (&list, tmp); } while (tmp); - va_end (var_args); original_argc = _dbus_list_get_length (&list); original_argv = dbus_new (char *, original_argc); @@ -46,25 +48,27 @@ test_command_line (const char *arg1, ...) _dbus_list_clear (&list); command_line = _dbus_string_get_data (&str); - printf ("\n\nTesting command line '%s'\n", command_line); + printf ("# Testing command line '%s'\n", command_line); dbus_error_init (&error); if (!_dbus_shell_parse_argv (command_line, &shell_argc, &shell_argv, &error)) { - fprintf (stderr, "Error parsing command line: %s\n", error.message ? error.message : ""); + printf ("# Error%s parsing command line: %s\n", + should_work ? "" : " (as expected)", + error.message ? error.message : ""); dbus_free (original_argv); - return FALSE; + return !should_work; } else { if (shell_argc != original_argc) { - printf ("Number of arguments returned (%d) don't match original (%d)\n", + printf ("# Number of arguments returned (%d) don't match original (%d)\n", shell_argc, original_argc); dbus_free (original_argv); return FALSE; } - printf ("Number of arguments: %d\n", shell_argc); + printf ("# Number of arguments: %d\n", shell_argc); for (i = 0; i < shell_argc; i++) { char *unquoted; @@ -86,28 +90,79 @@ test_command_line (const char *arg1, ...) dbus_free_string_array (shell_argv); } - + + if (!should_work) + { + printf ("# Expected an error\n"); + return FALSE; + } + _dbus_string_free (&str); dbus_free (original_argv); return TRUE; } +static void +test_command_line (const char *arg1, ...) +{ + va_list var_args; + + va_start (var_args, arg1); + + if (test_command_line_internal (TRUE, arg1, var_args)) + { + printf ("ok %d\n", ++test_num); + } + else + { + printf ("not ok %d\n", ++test_num); + num_failed++; + } + + va_end (var_args); +} + +static void +test_command_line_fails (const char *arg1, ...) +{ + va_list var_args; + + va_start (var_args, arg1); + + if (test_command_line_internal (FALSE, arg1, var_args)) + { + printf ("ok %d\n", ++test_num); + } + else + { + printf ("not ok %d\n", ++test_num); + num_failed++; + } + + va_end (var_args); +} + +/* This test outputs TAP syntax: http://testanything.org/ */ int main (int argc, char **argv) { - if (!test_command_line ("command", "-s", "--force-shutdown", "\"a string\"", "123", NULL) - || !test_command_line ("command", "-s", NULL) - || !test_command_line ("/opt/gnome/bin/service-start", NULL) - || !test_command_line ("grep", "-l", "-r", "-i", "'whatever'", "files*.c", NULL) - || !test_command_line ("/home/boston/johnp/devel-local/dbus/test/test-segfault", NULL) - || !test_command_line ("ls", "-l", "-a", "--colors", _dbus_get_tmpdir(), NULL) - || !test_command_line ("rsync-to-server", NULL) - || !test_command_line ("test-segfault", "--no-segfault", NULL) - || !test_command_line ("evolution", "mailto:pepe@cuco.com", NULL) - || !test_command_line ("run", "\"a \n multiline\"", NULL) - || test_command_line ("ls", "\"a wrong string'", NULL) /* invalid command line */ ) - return -1; - - return 0; + test_command_line ("command", "-s", "--force-shutdown", "\"a string\"", "123", NULL); + test_command_line ("command", "-s", NULL); + test_command_line ("/opt/gnome/bin/service-start", NULL); + test_command_line ("grep", "-l", "-r", "-i", "'whatever'", "files*.c", NULL); + test_command_line ("/home/boston/johnp/devel-local/dbus/test/test-segfault", NULL); + test_command_line ("ls", "-l", "-a", "--colors", _dbus_get_tmpdir(), NULL); + test_command_line ("rsync-to-server", NULL); + test_command_line ("test-segfault", "--no-segfault", NULL); + test_command_line ("evolution", "mailto:pepe@cuco.com", NULL); + test_command_line ("run", "\"a \n multiline\"", NULL); + test_command_line_fails ("ls", "\"a wrong string'", NULL); + + /* Tell the TAP driver that we have done all the tests we plan to do. + * This is how it can distinguish between an unexpected exit and + * successful completion. */ + printf ("1..%d\n", test_num); + + return (num_failed != 0); } -- 2.7.4