[gtk+] tests: Change the way the code does diffs



commit 35c96507528fcdbcc73aabe2e71f249447c0f146
Author: Benjamin Otte <otte redhat com>
Date:   Fri Apr 8 00:50:32 2011 +0200

    tests: Change the way the code does diffs
    
    Use a temp file for intermediate storage and properly unlink that
    tempfile after the diff is done.

 tests/css/Makefile.am              |    1 +
 tests/css/parser/test-css-parser.c |   70 +++++++++++++++++++++---------------
 2 files changed, 42 insertions(+), 29 deletions(-)
---
diff --git a/tests/css/Makefile.am b/tests/css/Makefile.am
new file mode 100644
index 0000000..aeada80
--- /dev/null
+++ b/tests/css/Makefile.am
@@ -0,0 +1 @@
+SUBDIRS = parser
diff --git a/tests/css/parser/test-css-parser.c b/tests/css/parser/test-css-parser.c
index dadcdf3..8c67e3b 100644
--- a/tests/css/parser/test-css-parser.c
+++ b/tests/css/parser/test-css-parser.c
@@ -23,7 +23,7 @@
 #include "config.h"
 
 #include <string.h>
-
+#include <glib/gstdio.h>
 #include <gtk/gtk.h>
 
 static char *
@@ -48,33 +48,49 @@ test_get_reference_file (const char *css_file)
 }
 
 static char *
-test_get_output_file (const char *css_file)
+diff_with_file (const char  *file1,
+                char        *text,
+                gssize       len,
+                GError     **error)
 {
-  GString *file = g_string_new (NULL);
+  const char *command[] = { "diff", "-u", file1, NULL, NULL };
+  char *diff, *tmpfile;
+  int fd;
 
-  if (g_str_has_suffix (css_file, ".css"))
-    g_string_append_len (file, css_file, strlen (css_file) - 4);
-  else
-    g_string_append (file, css_file);
-  
-  g_string_append (file, ".out.css");
+  diff = NULL;
 
-  return g_string_free (file, FALSE);
-}
+  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;
 
-static char *
-diff_files (const char *file1,
-            const char *file2,
-            GError **   error)
-{
-  const char *command[] = { "diff", "-u", file1, file2, NULL };
-  char *diff;
+  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 */
-  if (!g_spawn_sync (NULL, (char **) command, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
-	&diff, NULL, NULL, error)) {
-    return NULL;
-  }
+  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;
 }
@@ -84,7 +100,7 @@ test_css_file (GFile *file)
 {
   GtkCssProvider *provider;
   char *css, *diff;
-  char *css_file, *output_file, *reference_file;
+  char *css_file, *reference_file;
   GError *error = NULL;
 
   css_file = g_file_get_path (file);
@@ -96,20 +112,16 @@ test_css_file (GFile *file)
   g_assert_no_error (error);
 
   css = gtk_css_provider_to_string (provider);
-  output_file = test_get_output_file (css_file);
 
-  g_file_set_contents (output_file, css, -1, &error);
   g_assert_no_error (error);
 
-  g_free (css);
-
   reference_file = test_get_reference_file (css_file);
 
-  diff = diff_files (reference_file, output_file, &error);
+  diff = diff_with_file (reference_file, css, -1, &error);
   g_assert_no_error (error);
 
+  g_free (css);
   g_free (reference_file);
-  g_free (output_file);
 
   if (diff && diff[0])
     {



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