Skip to content
Snippets Groups Projects
Commit ad877aae authored by Simon McVittie's avatar Simon McVittie
Browse files

tests: Write out JSON files so we can diff them nicely


Comparing two giant strings of JSON in g_assert_cmpstr() output is not
straightforward, so let's try to write them out to files and run diff
on them.

The AUTOPKGTEST_ARTIFACTS variable set by Debian's autopkgtest
framework is as good a mechanism as any for signalling where would be a
good place; or if that's not set, use a temporary directory.

Signed-off-by: default avatarSimon McVittie <smcv@collabora.com>
parent 7531ff96
No related branches found
No related tags found
1 merge request!492tests: Improve ability to run tests against LD_LIBRARY_PATH Steam Runtime
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <steam-runtime-tools/glib-backports-internal.h> #include <steam-runtime-tools/glib-backports-internal.h>
#include <steam-runtime-tools/json-glib-backports-internal.h> #include <steam-runtime-tools/json-glib-backports-internal.h>
#include <steam-runtime-tools/utils-internal.h>
#include <glib.h> #include <glib.h>
#include <glib/gstdio.h> #include <glib/gstdio.h>
...@@ -685,6 +686,13 @@ static const JsonTest json_test[] = ...@@ -685,6 +686,13 @@ static const JsonTest json_test[] =
}, },
}; };
static void
stdout_to_stderr_child_setup (gpointer nil)
{
if (dup2 (STDERR_FILENO, STDOUT_FILENO) != STDOUT_FILENO)
_srt_async_signal_safe_error ("Unable to redirect stdout to stderr", 1);
}
static void static void
json_parsing (Fixture *f, json_parsing (Fixture *f,
gconstpointer context) gconstpointer context)
...@@ -741,6 +749,49 @@ json_parsing (Fixture *f, ...@@ -741,6 +749,49 @@ json_parsing (Fixture *f,
g_assert_true (result); g_assert_true (result);
g_assert_cmpint (exit_status, ==, 0); g_assert_cmpint (exit_status, ==, 0);
g_assert_nonnull (output); g_assert_nonnull (output);
if (g_strcmp0 (output, expectation) != 0)
{
const char *artifacts = g_getenv ("AUTOPKGTEST_ARTIFACTS");
g_autofree gchar *tmpdir = NULL;
g_autofree gchar *expected_path = NULL;
g_autofree gchar *output_path = NULL;
const gchar *diff_argv[] = { "diff", "-u", "<expected>", "<output>", NULL };
if (artifacts == NULL)
{
tmpdir = g_dir_make_tmp ("srt-tests-XXXXXX", &error);
g_assert_no_error (error);
artifacts = tmpdir;
}
expected_path = g_build_filename (artifacts, "expected.json", NULL);
g_file_set_contents (expected_path, expectation, -1, &error);
g_assert_no_error (error);
output_path = g_build_filename (artifacts, "output.json", NULL);
g_file_set_contents (output_path, output, -1, &error);
g_assert_no_error (error);
g_assert_cmpstr (diff_argv[2], ==, "<expected>");
diff_argv[2] = expected_path;
g_assert_cmpstr (diff_argv[3], ==, "<output>");
diff_argv[3] = output_path;
g_assert_null (diff_argv[4]);
/* Ignore error from calling diff, if any: we're running it
* for its side-effect of producing output on our stderr. */
g_spawn_sync (NULL, (gchar **) diff_argv, NULL, G_SPAWN_SEARCH_PATH,
stdout_to_stderr_child_setup, NULL,
NULL, NULL, NULL, NULL);
g_test_message ("Output for comparison: %s %s",
expected_path, output_path);
if (tmpdir != NULL)
_srt_rm_rf (tmpdir);
}
g_assert_cmpstr (output, ==, expectation); g_assert_cmpstr (output, ==, expectation);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment