[gtk/wip/otte/json: 61/85] testutils: Rewrite to use GSubprocess
- From: Benjamin Otte <otte src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/wip/otte/json: 61/85] testutils: Rewrite to use GSubprocess
- Date: Thu, 9 Dec 2021 14:53:29 +0000 (UTC)
commit eede73304d66a2e3419c4b5621754e0e6d45eef7
Author: Benjamin Otte <otte redhat com>
Date: Sat Dec 4 20:04:53 2021 +0100
testutils: Rewrite to use GSubprocess
That way, we also properly catch all errors and don't let tests pass
when diff(1) fails.
Oops.
testsuite/testutils.c | 90 ++++++++++++++++++++++++++++++---------------------
1 file changed, 53 insertions(+), 37 deletions(-)
---
diff --git a/testsuite/testutils.c b/testsuite/testutils.c
index d1776da7b5..57b6889e6d 100644
--- a/testsuite/testutils.c
+++ b/testsuite/testutils.c
@@ -17,14 +17,7 @@
* Authors: Matthias Clasen <mclasen redhat com>
*/
-#include <glib.h>
-#include <glib/gstdio.h>
-
-#ifdef G_OS_WIN32
-#include <io.h>
-#else
-#include <unistd.h>
-#endif
+#include <gio/gio.h>
#include "testsuite/testutils.h"
@@ -34,46 +27,69 @@ diff_with_file (const char *file1,
gssize len,
GError **error)
{
- const char *command[] = { "diff", "-u", file1, NULL, NULL };
- char *diff, *tmpfile;
- int fd;
+ char *diff = NULL;
- diff = NULL;
+ g_return_val_if_fail (file1 != NULL, NULL);
+ g_return_val_if_fail (text != NULL, NULL);
if (g_find_program_in_path ("diff"))
{
+ GSubprocess *process;
+ GBytes *input, *output, *error_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
+ | G_SUBPROCESS_FLAGS_STDERR_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);
+ if (!g_subprocess_communicate (process,
+ input,
+ NULL,
+ &output,
+ &error_output,
+ 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);
+ g_bytes_unref (input);
+
+ if (error_output)
+ {
+ const char *error_text = g_bytes_get_data (error_output, NULL);
+ if (error_text && error_text[0])
+ {
+ g_test_message ("%s", error_text);
+ }
+
+ g_bytes_unref (error_output);
+ }
+
+ if (!g_subprocess_get_successful (process) &&
+ /* this is the condition when the files differ */
+ !(g_subprocess_get_if_exited (process) && g_subprocess_get_exit_status (process) == 1))
+ {
+ 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));
+ }
+ else
+ {
+ diff = g_strdup (g_bytes_get_data (output, NULL));
+ }
+
+ g_object_unref (process);
+ g_clear_pointer (&output, g_bytes_unref);
+
+ return diff;
}
else
{
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]