From d984352b5edb9e0f5c7d07956f0562288865ff37 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Wed, 3 Sep 2014 15:37:02 -0400 Subject: test-layout: Better error reporting Run diff over the output to give a meaningful error if the comparison fails. --- tests/test-layout.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 8 deletions(-) diff --git a/tests/test-layout.c b/tests/test-layout.c index 25b81147..a56cdae8 100644 --- a/tests/test-layout.c +++ b/tests/test-layout.c @@ -21,10 +21,60 @@ #include #include +#include #include #include +static char * +diff_with_file (const char *file1, + char *text, + gssize len, + GError **error) +{ + const char *command[] = { "diff", "-u", file1, NULL, NULL }; + char *diff, *tmpfile; + int fd; + + diff = NULL; + + if (len < 0) + len = strlen (text); + + /* write the text buffer to a temporary file */ + fd = g_file_open_tmp (NULL, &tmpfile, error); + if (fd < 0) + return NULL; + + if (write (fd, text, len) != (int) len) + { + close (fd); + g_set_error (error, + G_FILE_ERROR, G_FILE_ERROR_FAILED, + "Could not write data to temporary file '%s'", tmpfile); + goto done; + } + close (fd); + command[3] = tmpfile; + + /* run diff command */ + g_spawn_sync (NULL, + (char **) command, + NULL, + G_SPAWN_SEARCH_PATH, + NULL, NULL, + &diff, + NULL, NULL, + error); + +done: + unlink (tmpfile); + g_free (tmpfile); + + return diff; +} + + static PangoContext *context; static void @@ -416,23 +466,27 @@ test_layout (gconstpointer d) { const gchar *filename = d; gchar *expected_file; - gchar *expected; GError *error = NULL; - GString *string; + GString *dump; + gchar *diff; expected_file = get_expected_filename (filename); - string = g_string_sized_new (0); + dump = g_string_sized_new (0); - test_file (filename, string); + test_file (filename, dump); - g_file_get_contents (expected_file, &expected, NULL, &error); + diff = diff_with_file (expected_file, dump->str, dump->len, &error); g_assert_no_error (error); - g_assert_cmpstr (string->str, ==, expected); - g_free (expected); - g_string_free (string, TRUE); + if (diff && diff[0]) + { + g_printerr ("Contents don't match expected contents:\n%s", diff); + g_test_fail (); + g_free (diff); + } + g_string_free (dump, TRUE); g_free (expected_file); } -- cgit v1.2.1