From ac6c50d6d917b5ce2f27aadddb8f6953206cd3ce Mon Sep 17 00:00:00 2001
From: Ryan Gonzalez <ryan.gonzalez@collabora.com>
Date: Tue, 11 Mar 2025 16:09:28 -0500
Subject: [PATCH] test-system-info-cli: Switch to shared functions for JSON &
 diff code

The last two commits extracted out the code for pretty printing JSON and
running diff on string changes, so switch over to those versions now.

Signed-off-by: Ryan Gonzalez <ryan.gonzalez@collabora.com>
---
 tests/system-info-cli.c | 81 ++---------------------------------------
 1 file changed, 4 insertions(+), 77 deletions(-)

diff --git a/tests/system-info-cli.c b/tests/system-info-cli.c
index a42076fb1..44ff386db 100644
--- a/tests/system-info-cli.c
+++ b/tests/system-info-cli.c
@@ -43,6 +43,7 @@
 #include <json-glib/json-glib.h>
 
 #include "test-utils.h"
+#include "test-json-utils.h"
 #include "fake-home.h"
 
 #if defined(__i386__) || defined(__x86_64__)
@@ -737,34 +738,6 @@ 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 ("test-system-info-cli",
-                                  "Unable to redirect stdout to stderr",
-                                  1);
-}
-
-static gchar *
-pretty_print_json (const char *unformatted)
-{
-  g_autoptr(JsonParser) parser = NULL;
-  JsonNode *node = NULL;  /* not owned */
-  g_autoptr(JsonGenerator) generator = NULL;
-  g_autoptr(GError) error = NULL;
-
-  parser = json_parser_new ();
-  json_parser_load_from_data (parser, unformatted, -1, &error);
-  g_assert_no_error (error);
-  node = json_parser_get_root (parser);
-  g_assert_nonnull (node);
-  generator = json_generator_new ();
-  json_generator_set_root (generator, node);
-  json_generator_set_pretty (generator, TRUE);
-  return json_generator_to_data (generator, NULL);
-}
-
 static void
 json_parsing (Fixture *f,
               gconstpointer context)
@@ -782,7 +755,6 @@ json_parsing (Fixture *f,
       g_auto(GStrv) envp = NULL;
       g_autoptr(GError) error = NULL;
       const gchar *argv[] = { "steam-runtime-system-info", "--verbose", NULL };
-      gsize len;
 
       g_test_message ("%s: input=%s output=%s", test->description, test->input_name, test->output_name);
 
@@ -791,10 +763,7 @@ json_parsing (Fixture *f,
       output_json = g_build_filename (f->srcdir, "json-report", multiarch_tuples[0],
                                       test->output_name, NULL);
 
-      g_file_get_contents (output_json, &unformatted, &len, &error);
-      g_assert_no_error (error);
-      g_assert_cmpuint (len, ==, strlen (unformatted));
-      expectation = pretty_print_json (unformatted);
+      expectation = _srt_json_pretty_from_file (output_json);
       g_clear_pointer (&unformatted, g_free);
 
       envp = g_get_environ ();
@@ -814,51 +783,9 @@ json_parsing (Fixture *f,
       g_assert_true (result);
       g_assert_cmpint (exit_status, ==, 0);
       g_assert_nonnull (unformatted);
-      output = pretty_print_json (unformatted);
-
-      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);
-        }
+      output = _srt_json_pretty (unformatted);
 
-      g_assert_cmpstr (output, ==, expectation);
+      _srt_assert_streq_diff (expectation, output);
     }
 }
 
-- 
GitLab