mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-01 23:13:40 +02:00
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
This commit is contained in:
@@ -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]);
|
||||
|
@@ -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 ();
|
||||
}
|
||||
|
Reference in New Issue
Block a user