[glib/wip/gsubprocess-2.36] wip: Add some more redirection tests



commit 29616cc3b7785dfbe24904294f3b9adc16819ea8
Author: Colin Walters <walters verbum org>
Date:   Mon Oct 22 22:23:24 2012 -0400

    wip: Add some more redirection tests

 gio/tests/gsubprocess.c |   59 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 59 insertions(+), 0 deletions(-)
---
diff --git a/gio/tests/gsubprocess.c b/gio/tests/gsubprocess.c
index 2c3eb5a..33e517d 100644
--- a/gio/tests/gsubprocess.c
+++ b/gio/tests/gsubprocess.c
@@ -724,6 +724,64 @@ test_stdout_fd (void)
   (void) g_file_delete (tmpfile, NULL, NULL);
 }
 
+static void
+child_setup (gpointer user_data)
+{
+  dup2 (GPOINTER_TO_INT (user_data), 1);
+}
+
+static void
+test_child_setup (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;
+  int fd;
+
+  tmpfile = g_file_new_tmp ("gsubprocessXXXXXX", &iostream, error);
+  g_assert_no_error (local_error);
+
+  fd = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (g_io_stream_get_output_stream (G_IO_STREAM (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,
+			 "child-setup-func", child_setup,
+			 "child-setup-data", GINT_TO_POINTER (fd),
+			 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)
 {
@@ -749,6 +807,7 @@ main (int argc, char **argv)
   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/child-setup", test_child_setup);
   /* 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]