[glib/wip/gsubprocess-2.36: 2/2] wip: Change API to _new_simple, add tests for I/O disposition



commit 8c8b5575fafc00ee7db7558254cea5e53c5e7d8d
Author: Colin Walters <walters verbum org>
Date:   Mon Oct 22 21:52:13 2012 -0400

    wip: Change API to _new_simple, add tests for I/O disposition

 gio/glib-compile-resources.c |    4 +-
 gio/gsubprocess.c            |   19 ++++++
 gio/tests/gsubprocess.c      |  141 +++++++++++++++++++++++++++++++++++-------
 3 files changed, 140 insertions(+), 24 deletions(-)
---
diff --git a/gio/glib-compile-resources.c b/gio/glib-compile-resources.c
index a6066c5..7db871f 100644
--- a/gio/glib-compile-resources.c
+++ b/gio/glib-compile-resources.c
@@ -315,7 +315,7 @@ end_element (GMarkupParseContext  *context,
 	      g_ptr_array_addv (args, xmllint, "--nonet", "--noblanks", "--output",
 				tmp_file, real_file, NULL);
 	      g_ptr_array_add (args, NULL);
-	      proc = g_subprocess_new (NULL, (const gchar**)args->pdata, NULL, 0, error);
+	      proc = g_subprocess_new_simple ((const gchar**)args->pdata, 0, error);
 	      g_ptr_array_free (args, TRUE);
               g_free (real_file);
 	      real_file = NULL;
@@ -365,7 +365,7 @@ end_element (GMarkupParseContext  *context,
 	      args = g_ptr_array_new ();
 	      g_ptr_array_addv (args, gdk_pixbuf_pixdata, real_file, tmp_file2, NULL);
 	      g_ptr_array_add (args, NULL);
-	      proc = g_subprocess_new (NULL, (const gchar**)args->pdata, NULL, 0, error);
+	      proc = g_subprocess_new_simple ((const gchar**)args->pdata, 0, error);
 	      g_ptr_array_free (args, TRUE);
               g_free (real_file);
 
diff --git a/gio/gsubprocess.c b/gio/gsubprocess.c
index 3bb6764..4d14830 100644
--- a/gio/gsubprocess.c
+++ b/gio/gsubprocess.c
@@ -101,6 +101,9 @@ struct _GSubprocess
   GObject *stdout;
   GObject *stderr;
 
+  GSpawnChildSetupFunc child_setup_func;
+  gpointer child_setup_data;
+
   gboolean reaped_child;
 
   /* These are the streams created if a pipe is requested via flags. */
@@ -203,6 +206,14 @@ g_subprocess_set_property (GObject      *object,
       self->stderr = g_value_dup_object (value);
       break;
 
+    case PROP_CHILD_SETUP_FUNC:
+      self->child_setup_func = g_value_get_pointer (value);
+      break;
+
+    case PROP_CHILD_SETUP_DATA:
+      self->child_setup_data = g_value_get_pointer (value);
+      break;
+
     default:
       g_assert_not_reached ();
     }
@@ -246,6 +257,14 @@ g_subprocess_get_property (GObject    *object,
       g_value_set_object (value, self->stderr);
       break;
 
+    case PROP_CHILD_SETUP_FUNC:
+      g_value_set_pointer (value, self->child_setup_func);
+      break;
+
+    case PROP_CHILD_SETUP_DATA:
+      g_value_set_pointer (value, self->child_setup_data);
+      break;
+
     default:
       g_assert_not_reached ();
     }
diff --git a/gio/tests/gsubprocess.c b/gio/tests/gsubprocess.c
index 7eb7beb..2c3eb5a 100644
--- a/gio/tests/gsubprocess.c
+++ b/gio/tests/gsubprocess.c
@@ -53,7 +53,7 @@ test_noop (void)
   GSubprocess *proc;
 
   args = get_test_subprocess_args ("noop", NULL);
-  proc = g_subprocess_new (NULL, (const char**) args->pdata, NULL, 0, error);
+  proc = g_subprocess_new_simple ((const char**) args->pdata, 0, error);
   g_ptr_array_free (args, TRUE);
   g_assert_no_error (local_error);
 
@@ -72,7 +72,7 @@ test_noop_all_to_null (void)
   GSubprocess *proc;
 
   args = get_test_subprocess_args ("noop", NULL);
-  proc = g_subprocess_new (NULL, (const gchar **) args->pdata, NULL, 0, error);
+  proc = g_subprocess_new_simple ((const gchar **) args->pdata, 0, error);
   g_ptr_array_free (args, TRUE);
   g_assert_no_error (local_error);
 
@@ -91,7 +91,7 @@ test_noop_no_wait (void)
   GSubprocess *proc;
 
   args = get_test_subprocess_args ("noop", NULL);
-  proc = g_subprocess_new (NULL, (const gchar **) args->pdata, NULL, 0, error);
+  proc = g_subprocess_new_simple ((const gchar **) args->pdata, 0, error);
   g_ptr_array_free (args, TRUE);
   g_assert_no_error (local_error);
   
@@ -107,7 +107,7 @@ test_noop_stdin_inherit (void)
   GSubprocess *proc;
 
   args = get_test_subprocess_args ("noop", NULL);
-  proc = g_subprocess_new (NULL, (const gchar **) args->pdata, NULL, 0, error);
+  proc = g_subprocess_new_simple ((const gchar **) args->pdata, 0, error);
   g_ptr_array_free (args, TRUE);
   g_assert_no_error (local_error);
 
@@ -130,7 +130,7 @@ test_search_path (void)
   args = g_ptr_array_new ();
   g_ptr_array_add (args, "true");
   g_ptr_array_add (args, NULL);
-  proc = g_subprocess_new (NULL, (const char **) args->pdata, NULL, G_SUBPROCESS_FLAGS_SEARCH_PATH, error);
+  proc = g_subprocess_new_simple ((const char **) args->pdata, G_SUBPROCESS_FLAGS_SEARCH_PATH, error);
   g_ptr_array_free (args, TRUE);
   g_assert_no_error (local_error);
 
@@ -150,7 +150,7 @@ test_exit1 (void)
   GSubprocess *proc;
 
   args = get_test_subprocess_args ("exit1", NULL);
-  proc = g_subprocess_new (NULL, (const char **) args->pdata, NULL, G_SUBPROCESS_FLAGS_NONE, error);
+  proc = g_subprocess_new_simple ((const char **) args->pdata, G_SUBPROCESS_FLAGS_NONE, error);
   g_ptr_array_free (args, TRUE);
   g_assert_no_error (local_error);
 
@@ -195,7 +195,7 @@ test_echo1 (void)
   gchar *result;
 
   args = get_test_subprocess_args ("echo", "hello", "world!", NULL);
-  proc = g_subprocess_new (NULL, (const gchar **) args->pdata, NULL, G_SUBPROCESS_FLAGS_STDOUT_PIPE, error);
+  proc = g_subprocess_new_simple ((const gchar **) args->pdata, G_SUBPROCESS_FLAGS_STDOUT_PIPE, error);
   g_ptr_array_free (args, TRUE);
   g_assert_no_error (local_error);
 
@@ -222,8 +222,8 @@ test_echo_merged (void)
   gchar *result;
 
   args = get_test_subprocess_args ("echo-stdout-and-stderr", "merge", "this", NULL);
-  proc = g_subprocess_new (NULL, (const gchar **) args->pdata, NULL,
-                           G_SUBPROCESS_FLAGS_STDOUT_PIPE | G_SUBPROCESS_FLAGS_STDERR_MERGE, error);
+  proc = g_subprocess_new_simple ((const gchar **) args->pdata,
+				  G_SUBPROCESS_FLAGS_STDOUT_PIPE | G_SUBPROCESS_FLAGS_STDERR_MERGE, error);
   g_ptr_array_free (args, TRUE);
   g_assert_no_error (local_error);
 
@@ -278,9 +278,9 @@ test_cat_utf8 (void)
   data.loop = g_main_loop_new (NULL, TRUE);
 
   args = get_test_subprocess_args ("cat", NULL);
-  proc = g_subprocess_new (NULL, (const gchar **) args->pdata, NULL,
-                           G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDOUT_PIPE,
-                           error);
+  proc = g_subprocess_new_simple ((const gchar **) args->pdata,
+				  G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDOUT_PIPE,
+				  error);
   g_ptr_array_free (args, TRUE);
   g_assert_no_error (local_error);
 
@@ -344,8 +344,8 @@ test_cat_eof (void)
   gssize s;
 
   /* Spawn 'cat' */
-  cat = g_subprocess_new (NULL, args, NULL, G_SUBPROCESS_FLAGS_SEARCH_PATH |
-                          G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDOUT_PIPE, &error);
+  cat = g_subprocess_new_simple (args, G_SUBPROCESS_FLAGS_SEARCH_PATH |
+				 G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDOUT_PIPE, &error);
   g_assert_no_error (error);
   g_assert (cat);
 
@@ -487,17 +487,17 @@ test_multi_1 (void)
   int splice_flags = G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET;
 
   args = get_test_subprocess_args ("cat", NULL);
-  first = g_subprocess_new (NULL, (const gchar **) args->pdata, NULL,
+  first = g_subprocess_new_simple ((const gchar **) args->pdata,
                             G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDOUT_PIPE,
                             error);
   g_assert_no_error (local_error);
-  second = g_subprocess_new (NULL, (const gchar **) args->pdata, NULL,
-                             G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDOUT_PIPE,
-                             error);
+  second = g_subprocess_new_simple ((const gchar **) args->pdata,
+				    G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDOUT_PIPE,
+				    error);
   g_assert_no_error (local_error);
-  third = g_subprocess_new (NULL, (const gchar **) args->pdata, NULL,
-                            G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDOUT_PIPE,
-                            error);
+  third = g_subprocess_new_simple ((const gchar **) args->pdata,
+				   G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDOUT_PIPE,
+				   error);
   g_assert_no_error (local_error);
 
   g_ptr_array_free (args, TRUE);
@@ -613,7 +613,7 @@ test_terminate (void)
   GMainLoop *loop;
 
   args = get_test_subprocess_args ("sleep-forever", NULL);
-  proc = g_subprocess_new (NULL, (const gchar **) args->pdata, NULL, G_SUBPROCESS_FLAGS_NONE, error);
+  proc = g_subprocess_new_simple ((const gchar **) args->pdata, G_SUBPROCESS_FLAGS_NONE, error);
   g_ptr_array_free (args, TRUE);
   g_assert_no_error (local_error);
 
@@ -629,6 +629,101 @@ test_terminate (void)
   g_object_unref (proc);
 }
 
+static void
+test_stdout_file (void)
+{
+  GError *local_error = NULL;
+  GError **error = &local_error;
+  GSubprocess *proc;
+  GPtrArray *args;
+  GFile *tmpfile;
+  GFileIOStream *iostream;
+  GOutputStream *stdin;
+  const char *test_data = "this is some test data\n";
+  char *tmp_contents;
+
+  tmpfile = g_file_new_tmp ("gsubprocessXXXXXX", &iostream, error);
+  g_assert_no_error (local_error);
+  g_clear_object (&iostream);
+
+  args = get_test_subprocess_args ("cat", NULL);
+  proc = g_initable_new (G_TYPE_SUBPROCESS, NULL, error,
+			 "argv", args->pdata, 
+			 "flags", G_SUBPROCESS_FLAGS_STDIN_PIPE,
+			 "stdout", tmpfile,
+			 NULL);
+  g_ptr_array_free (args, TRUE);
+  g_assert_no_error (local_error);
+
+  stdin = g_subprocess_get_stdin_pipe (proc);
+
+  g_output_stream_write_all (stdin, test_data, strlen (test_data), NULL, NULL, error);
+  g_assert_no_error (local_error);
+
+  g_output_stream_close (stdin, NULL, error);
+  g_assert_no_error (local_error);
+
+  g_subprocess_wait_sync_check (proc, NULL, error);
+
+  g_object_unref (proc);
+
+  g_file_load_contents (tmpfile, NULL, &tmp_contents, NULL, NULL, error);
+  g_assert_no_error (local_error);
+
+  g_assert_cmpstr (test_data, ==, tmp_contents);
+  g_free (tmp_contents);
+
+  (void) g_file_delete (tmpfile, NULL, NULL);
+}
+
+static void
+test_stdout_fd (void)
+{
+  GError *local_error = NULL;
+  GError **error = &local_error;
+  GSubprocess *proc;
+  GPtrArray *args;
+  GFile *tmpfile;
+  GFileIOStream *iostream;
+  GOutputStream *stdin;
+  const char *test_data = "this is some test data\n";
+  char *tmp_contents;
+
+  tmpfile = g_file_new_tmp ("gsubprocessXXXXXX", &iostream, error);
+  g_assert_no_error (local_error);
+
+  args = get_test_subprocess_args ("cat", NULL);
+  proc = g_initable_new (G_TYPE_SUBPROCESS, NULL, error,
+			 "argv", args->pdata, 
+			 "flags", G_SUBPROCESS_FLAGS_STDIN_PIPE,
+			 "stdout", G_FILE_DESCRIPTOR_BASED (g_io_stream_get_output_stream (G_IO_STREAM (iostream))),
+			 NULL);
+  g_ptr_array_free (args, TRUE);
+  g_assert_no_error (local_error);
+
+  g_clear_object (&iostream);
+
+  stdin = g_subprocess_get_stdin_pipe (proc);
+
+  g_output_stream_write_all (stdin, test_data, strlen (test_data), NULL, NULL, error);
+  g_assert_no_error (local_error);
+
+  g_output_stream_close (stdin, NULL, error);
+  g_assert_no_error (local_error);
+
+  g_subprocess_wait_sync_check (proc, NULL, error);
+
+  g_object_unref (proc);
+
+  g_file_load_contents (tmpfile, NULL, &tmp_contents, NULL, NULL, error);
+  g_assert_no_error (local_error);
+
+  g_assert_cmpstr (test_data, ==, tmp_contents);
+  g_free (tmp_contents);
+
+  (void) g_file_delete (tmpfile, NULL, NULL);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -652,6 +747,8 @@ main (int argc, char **argv)
   g_test_add_func ("/gsubprocess/cat-eof", test_cat_eof);
   g_test_add_func ("/gsubprocess/multi1", test_multi_1);
   g_test_add_func ("/gsubprocess/terminate", test_terminate);
+  g_test_add_func ("/gsubprocess/stdout-file", test_stdout_file);
+  g_test_add_func ("/gsubprocess/stdout-fd", test_stdout_fd);
   /* g_test_add_func ("/gsubprocess/argv0", test_argv0); */
 
   return g_test_run ();



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