[glib] gsubprocess: test environment a bit more



commit 2b11af4f1f336f8e7fda26045fe76375176c8a17
Author: Ryan Lortie <desrt desrt ca>
Date:   Tue Mar 4 09:08:24 2014 -0500

    gsubprocess: test environment a bit more
    
    Add a test for GSubprocess to test setting, unsetting and inheritance of
    environment variables.  Use communicate() to give it a bit more of a
    workout as well.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=725651

 gio/tests/gsubprocess-testprog.c |   18 ++++++++++++++++++
 gio/tests/gsubprocess.c          |   35 +++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 0 deletions(-)
---
diff --git a/gio/tests/gsubprocess-testprog.c b/gio/tests/gsubprocess-testprog.c
index fbb0c83..2f00b55 100644
--- a/gio/tests/gsubprocess-testprog.c
+++ b/gio/tests/gsubprocess-testprog.c
@@ -175,6 +175,22 @@ cwd_mode (int argc, char **argv)
   return 0;
 }
 
+static int
+printenv_mode (int argc, char **argv)
+{
+  gint i;
+
+  for (i = 2; i < argc; i++)
+    {
+      const gchar *value = g_getenv (argv[i]);
+
+      if (value != NULL)
+        g_print ("%s=%s\n", argv[i], value);
+    }
+
+  return 0;
+}
+
 int
 main (int argc, char **argv)
 {
@@ -222,6 +238,8 @@ main (int argc, char **argv)
     return env_mode (argc, argv);
   else if (strcmp (mode, "cwd") == 0)
     return cwd_mode (argc, argv);
+  else if (strcmp (mode, "printenv") == 0)
+    return printenv_mode (argc, argv);
   else
     {
       g_printerr ("Unknown MODE %s\n", argv[1]);
diff --git a/gio/tests/gsubprocess.c b/gio/tests/gsubprocess.c
index 199404f..92bddd0 100644
--- a/gio/tests/gsubprocess.c
+++ b/gio/tests/gsubprocess.c
@@ -1203,6 +1203,40 @@ test_pass_fd (void)
 
 #endif
 
+static void
+test_launcher_environment (void)
+{
+  GSubprocessLauncher *launcher;
+  GError *error = NULL;
+  GSubprocess *proc;
+  GPtrArray *args;
+  gchar *out;
+
+  g_setenv ("A", "B", TRUE);
+  g_setenv ("C", "D", TRUE);
+
+  launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE);
+
+  /* unset a variable */
+  g_subprocess_launcher_unsetenv (launcher, "A");
+
+  /* and set a diffferent one */
+  g_subprocess_launcher_setenv (launcher, "E", "F", TRUE);
+
+  args = get_test_subprocess_args ("printenv", "A", "C", "E", NULL);
+  proc = g_subprocess_launcher_spawnv (launcher, (const gchar **) args->pdata, &error);
+  g_assert_no_error (error);
+  g_assert (proc);
+
+  g_subprocess_communicate_utf8 (proc, NULL, NULL, &out, NULL, &error);
+  g_assert_no_error (error);
+
+  g_assert_cmpstr (out, ==, "C=D\nE=F\n");
+  g_free (out);
+
+  g_object_unref (proc);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -1239,6 +1273,7 @@ main (int argc, char **argv)
   g_test_add_func ("/gsubprocess/child-setup", test_child_setup);
   g_test_add_func ("/gsubprocess/pass-fd", test_pass_fd);
 #endif
+  g_test_add_func ("/gsubprocess/launcher-environment", test_launcher_environment);
 
   return g_test_run ();
 }


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