[gtk] testsuite: Rewrite text diff to use GSubprocess



commit 7420f9c34ac21016d0e1677a1bc7eebebda84ad7
Author: Benjamin Otte <otte redhat com>
Date:   Sat Apr 27 05:06:30 2019 +0200

    testsuite: Rewrite text diff to use GSubprocess
    
    ... instead of g_spawn(). Avoids having to create a temp file, too.

 testsuite/css/parser/test-css-parser.c | 78 ++++++++++++++--------------------
 1 file changed, 33 insertions(+), 45 deletions(-)
---
diff --git a/testsuite/css/parser/test-css-parser.c b/testsuite/css/parser/test-css-parser.c
index eb909583f7..c18c3e0585 100644
--- a/testsuite/css/parser/test-css-parser.c
+++ b/testsuite/css/parser/test-css-parser.c
@@ -72,52 +72,38 @@ test_get_errors_file (const char *css_file)
   return g_string_free (file, FALSE);
 }
 
-static char *
+static GBytes *
 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)
+  GSubprocess *process;
+  GBytes *input, *output;
+
+  process = g_subprocess_new (G_SUBPROCESS_FLAGS_STDIN_PIPE
+                              | G_SUBPROCESS_FLAGS_STDOUT_PIPE,
+                              error,
+                              "diff", "-u", file1, "-", NULL);
+  if (process == NULL)
     return NULL;
 
-  if (write (fd, text, len) != (int) len)
+  input = g_bytes_new_static (text, len >= 0 ? len : strlen (text));
+  if (!g_subprocess_communicate (process,
+                                 input,
+                                 NULL,
+                                 &output,
+                                 NULL,
+                                 error))
     {
-      close (fd);
-      g_set_error (error,
-                   G_FILE_ERROR, G_FILE_ERROR_FAILED,
-                   "Could not write data to temporary file '%s'", tmpfile);
-      goto done;
+      g_object_unref (process);
+      g_bytes_unref (input);
+      return NULL;
     }
-  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:
-  g_unlink (tmpfile);
-  g_free (tmpfile);
-
-  return diff;
+
+  g_bytes_unref (input);
+
+  return output;
 }
 
 static void
@@ -168,9 +154,9 @@ static void
 parse_css_file (GFile *file, gboolean generate)
 {
   GtkCssProvider *provider;
-  char *css, *diff;
-  char *css_file, *reference_file, *errors_file;
+  char *css, *css_file, *reference_file, *errors_file;
   GString *errors;
+  GBytes *diff;
   GError *error = NULL;
 
   css_file = g_file_get_path (file);
@@ -196,12 +182,14 @@ parse_css_file (GFile *file, gboolean generate)
   diff = diff_with_file (reference_file, css, -1, &error);
   g_assert_no_error (error);
 
-  if (diff && diff[0])
+  if (diff && g_bytes_get_size (diff) > 0)
     {
-      g_test_message ("Resulting CSS doesn't match reference:\n%s", diff);
+      g_test_message ("Resulting CSS doesn't match reference:\n%s",
+                      (const char *) g_bytes_get_data (diff, NULL));
       g_test_fail ();
     }
   g_free (reference_file);
+  g_clear_pointer (&diff, g_bytes_unref);
 
   errors_file = test_get_errors_file (css_file);
 
@@ -210,11 +198,13 @@ parse_css_file (GFile *file, gboolean generate)
       diff = diff_with_file (errors_file, errors->str, errors->len, &error);
       g_assert_no_error (error);
 
-      if (diff && diff[0])
+      if (diff && g_bytes_get_size (diff) > 0)
         {
-          g_test_message ("Errors don't match expected errors:\n%s", diff);
+          g_test_message ("Errors don't match expected errors:\n%s",
+                          (const char *) g_bytes_get_data (diff, NULL));
           g_test_fail ();
         }
+      g_clear_pointer (&diff, g_bytes_unref);
     }
   else if (errors->str[0])
     {
@@ -225,8 +215,6 @@ parse_css_file (GFile *file, gboolean generate)
   g_free (errors_file);
   g_string_free (errors, TRUE);
 
-  g_free (diff);
-
 out:
   g_free (css_file);
   g_free (css);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]