testsuite: Rewrite diff_with_file()
use the modern version using GSubprocess that already exists in node-parser. Also change from one function to two - so tests can diff GBytes and strings, depending on which they prefer.
This commit is contained in:
@@ -111,7 +111,7 @@ load_ui_file (GFile *file, gboolean generate)
|
||||
|
||||
reference_file = test_get_other_file (ui_file, ".nodes");
|
||||
|
||||
diff = diff_with_file (reference_file, output, -1, &error);
|
||||
diff = diff_string_with_file (reference_file, output, -1, &error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
if (diff && diff[0])
|
||||
|
@@ -99,7 +99,7 @@ load_ui_file (GFile *file, gboolean generate)
|
||||
|
||||
reference_file = test_get_reference_file (ui_file);
|
||||
|
||||
diff = diff_with_file (reference_file, output, -1, &error);
|
||||
diff = diff_string_with_file (reference_file, output, -1, &error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
if (diff && diff[0])
|
||||
|
@@ -146,7 +146,7 @@ parse_css_file (GFile *file, gboolean generate)
|
||||
|
||||
reference_file = test_get_reference_file (css_file);
|
||||
|
||||
diff = diff_with_file (reference_file, css, -1, &error);
|
||||
diff = diff_string_with_file (reference_file, css, -1, &error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
if (diff && diff[0])
|
||||
@@ -161,7 +161,7 @@ parse_css_file (GFile *file, gboolean generate)
|
||||
|
||||
if (errors_file)
|
||||
{
|
||||
diff = diff_with_file (errors_file, errors->str, errors->len, &error);
|
||||
diff = diff_string_with_file (errors_file, errors->str, errors->len, &error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
if (diff && diff[0])
|
||||
|
@@ -129,7 +129,7 @@ load_ui_file (GFile *file, gboolean generate)
|
||||
reference_file = test_get_other_file (ui_file, ".nodes");
|
||||
g_assert_nonnull (reference_file);
|
||||
|
||||
diff = diff_with_file (reference_file, output, -1, &error);
|
||||
diff = diff_string_with_file (reference_file, output, -1, &error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
if (diff && diff[0])
|
||||
|
@@ -107,7 +107,7 @@ compose_table_compare (gconstpointer data)
|
||||
table = gtk_compose_table_parse (file, NULL);
|
||||
output = gtk_compose_table_print (table);
|
||||
|
||||
diff = diff_with_file (expected, output, -1, &error);
|
||||
diff = diff_string_with_file (expected, output, -1, &error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
if (diff && diff[0])
|
||||
|
@@ -256,7 +256,7 @@ load_ui_file (GFile *ui_file,
|
||||
|
||||
dir = get_dir_for_file (ref_path);
|
||||
output = generate_focus_chain (window, dir);
|
||||
diff = diff_with_file (ref_path, output, -1, &error);
|
||||
diff = diff_string_with_file (ref_path, output, -1, &error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
if (diff && diff[0])
|
||||
|
@@ -19,6 +19,7 @@
|
||||
|
||||
#include <glib.h>
|
||||
#include <glib/gstdio.h>
|
||||
#include <gio/gio.h>
|
||||
|
||||
#ifdef G_OS_WIN32
|
||||
#include <io.h>
|
||||
@@ -28,66 +29,93 @@
|
||||
|
||||
#include "testsuite/testutils.h"
|
||||
|
||||
/*<private>
|
||||
* diff_bytes_with_file:
|
||||
* @file1: The filename of the original. This is assumed
|
||||
* to be the reference to compare against.
|
||||
* @input: some text contained in a `GBytes` that is
|
||||
* supposed to match the file's contents
|
||||
* @error: A return location for an error if diffing could
|
||||
* not happen
|
||||
*
|
||||
* Diffs generated text with a reference file.
|
||||
*
|
||||
* If the diffing runs into any error, NULL is returned and
|
||||
* `error` is set. If diffing succeeds, the error is not set
|
||||
* and NULL is returned if the file was identical to the
|
||||
* contents of the file or the actual diff is returned if
|
||||
* they were not.
|
||||
*
|
||||
* Returns: NULL on success or failure, the diff on failure
|
||||
*/
|
||||
char *
|
||||
diff_with_file (const char *file1,
|
||||
const char *text,
|
||||
gssize len,
|
||||
GError **error)
|
||||
diff_bytes_with_file (const char *file1,
|
||||
GBytes *input,
|
||||
GError **error)
|
||||
{
|
||||
char *diff_cmd, *diff, *tmpfile;
|
||||
int fd;
|
||||
char *diff_cmd, *diff;
|
||||
|
||||
diff = NULL;
|
||||
|
||||
diff_cmd = g_find_program_in_path ("diff");
|
||||
if (diff_cmd)
|
||||
{
|
||||
const char *command[] = { diff_cmd, "--strip-trailing-cr", "-u", file1, NULL, NULL };
|
||||
GSubprocess *process;
|
||||
GBytes *output;
|
||||
|
||||
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)
|
||||
process = g_subprocess_new (G_SUBPROCESS_FLAGS_STDIN_PIPE
|
||||
| G_SUBPROCESS_FLAGS_STDOUT_PIPE,
|
||||
error,
|
||||
diff_cmd, "--strip-trailing-cr", "-u", file1, "-", NULL);
|
||||
if (process == NULL)
|
||||
return NULL;
|
||||
|
||||
if (write (fd, text, len) != (int) len)
|
||||
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);
|
||||
return NULL;
|
||||
}
|
||||
close (fd);
|
||||
command[3] = tmpfile;
|
||||
|
||||
/* run diff command */
|
||||
g_spawn_sync (NULL,
|
||||
(char **) command,
|
||||
NULL,
|
||||
0,
|
||||
NULL, NULL,
|
||||
&diff,
|
||||
NULL, NULL,
|
||||
error);
|
||||
if (g_subprocess_get_successful (process))
|
||||
{
|
||||
g_clear_pointer (&output, g_bytes_unref);
|
||||
}
|
||||
else if (g_subprocess_get_if_exited (process) && g_subprocess_get_exit_status (process) == 1)
|
||||
{
|
||||
gsize size;
|
||||
|
||||
done:
|
||||
g_unlink (tmpfile);
|
||||
g_free (tmpfile);
|
||||
g_free (diff_cmd);
|
||||
/* this is the condition when the files differ */
|
||||
diff = g_bytes_unref_to_data (output, &size);
|
||||
output = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_clear_pointer (&output, g_bytes_unref);
|
||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
|
||||
"The `diff' process exited with error status %d",
|
||||
g_subprocess_get_exit_status (process));
|
||||
}
|
||||
|
||||
g_object_unref (process);
|
||||
}
|
||||
else
|
||||
{
|
||||
char *buf1;
|
||||
gsize len1;
|
||||
const char *buf2;
|
||||
gsize len1, len2;
|
||||
|
||||
buf2 = g_bytes_get_data (input, &len2);
|
||||
|
||||
if (!g_file_get_contents (file1, &buf1, &len1, error))
|
||||
return NULL;
|
||||
|
||||
if ((len != -1 && len != len1) ||
|
||||
strncmp (text, buf1, len1) != 0)
|
||||
if ((len2 != len1) ||
|
||||
strncmp (buf2, buf1, len1) != 0)
|
||||
diff = g_strdup ("Files differ.\n");
|
||||
|
||||
g_free (buf1);
|
||||
@@ -95,3 +123,24 @@ done:
|
||||
|
||||
return diff;
|
||||
}
|
||||
|
||||
char *
|
||||
diff_string_with_file (const char *file1,
|
||||
const char *text,
|
||||
gssize len,
|
||||
GError **error)
|
||||
{
|
||||
GBytes *bytes;
|
||||
char *result;
|
||||
|
||||
if (len < 0)
|
||||
len = strlen (text);
|
||||
|
||||
bytes = g_bytes_new_static (text, len);
|
||||
|
||||
result = diff_bytes_with_file (file1, bytes, error);
|
||||
|
||||
g_bytes_unref (bytes);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@@ -2,8 +2,11 @@
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
char * diff_with_file (const char *file1,
|
||||
const char *text,
|
||||
gssize len,
|
||||
GError **error);
|
||||
char * diff_string_with_file (const char *file1,
|
||||
const char *text,
|
||||
gssize len,
|
||||
GError **error);
|
||||
|
||||
char * diff_bytes_with_file (const char *file1,
|
||||
GBytes *bytes,
|
||||
GError **error);
|
||||
|
Reference in New Issue
Block a user