2007-04-27 Jonathan Chambers <joncham@gmail.com>
[mono.git] / eglib / test / spawn.c
1 #include <config.h>
2 #include <glib.h>
3 #include <string.h>
4 #include <stdio.h>
5 #ifdef HAVE_UNISTD_H
6 #include <unistd.h>
7 #endif
8 #include "test.h"
9
10 #ifdef G_OS_WIN32
11 #include <io.h>
12 #define read _read
13 #define close _close
14 #endif
15
16 RESULT
17 test_spawn_sync ()
18 {
19         gchar *out;
20         gchar *err;
21         gint status = -1;
22         GError *error = NULL;
23
24         if (!g_spawn_command_line_sync ("ls", &out, &err, &status, &error))
25                 return FAILED ("Error executing 'ls'");
26
27         if (status != 0)
28                 return FAILED ("Status is %d", status);
29
30         if (out == NULL || strlen (out) == 0)
31                 return FAILED ("Didn't get any output from ls!?");
32
33         g_free (out);
34         g_free (err);
35         return OK;
36 }
37
38 RESULT
39 test_spawn_async ()
40 {
41         /*
42 gboolean
43 g_spawn_async_with_pipes (const gchar *working_directory,
44                         gchar **argv,
45                         gchar **envp,
46                         GSpawnFlags flags,
47                         GSpawnChildSetupFunc child_setup,
48                         gpointer user_data,
49                         GPid *child_pid,
50                         gint *standard_input,
51                         gint *standard_output,
52                         gint *standard_error,
53                         GError **error) */
54         char *argv [15];
55         int stdout_fd = -1;
56         char buffer [512];
57         pid_t child_pid = 0;
58
59         memset (argv, 0, 15 * sizeof (char *));
60         argv [0] = "ls";
61         if (!g_spawn_async_with_pipes (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &child_pid, NULL, &stdout_fd, NULL, NULL))
62                 return FAILED ("1 Failed to run ls");
63         if (child_pid == 0)
64                 return FAILED ("2 child pid not returned");
65         if (stdout_fd == -1)
66                 return FAILED ("3 out fd is -1");
67
68         while (read (stdout_fd, buffer, 512) > 0);
69         close (stdout_fd);
70
71         return OK;
72 }
73
74 static Test spawn_tests [] = {
75         {"g_shell_spawn_sync", test_spawn_sync},
76         {"g_shell_spawn_async_with_pipes", test_spawn_async},
77         {NULL, NULL}
78 };
79
80 DEFINE_TEST_GROUP_INIT(spawn_tests_init, spawn_tests)
81
82