mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-10-01 03:16:38 +02:00
Merge branch '3768-test-trap-subprocess-flags' into 'main'
gtestutils: Fix G_TEST_SUBPROCESS_INHERIT_STDIN in g_test_trap_subprocess() Closes #3768 See merge request GNOME/glib!4757
This commit is contained in:
@@ -4177,7 +4177,7 @@ g_test_trap_subprocess_with_envp (const char *test_path,
|
|||||||
flags = G_SPAWN_DO_NOT_REAP_CHILD;
|
flags = G_SPAWN_DO_NOT_REAP_CHILD;
|
||||||
if (test_log_fd != -1)
|
if (test_log_fd != -1)
|
||||||
flags |= G_SPAWN_LEAVE_DESCRIPTORS_OPEN;
|
flags |= G_SPAWN_LEAVE_DESCRIPTORS_OPEN;
|
||||||
if (test_flags & G_TEST_TRAP_INHERIT_STDIN)
|
if (test_flags & G_TEST_SUBPROCESS_INHERIT_STDIN)
|
||||||
flags |= G_SPAWN_CHILD_INHERITS_STDIN;
|
flags |= G_SPAWN_CHILD_INHERITS_STDIN;
|
||||||
|
|
||||||
if (!g_spawn_async_with_pipes (test_initial_cwd,
|
if (!g_spawn_async_with_pipes (test_initial_cwd,
|
||||||
|
@@ -35,6 +35,13 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifdef G_OS_UNIX
|
||||||
|
#define _POSIX_C_SOURCE 200809L /* for F_DUPFD_CLOEXEC */
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <glib-unix.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#define TAP_VERSION G_STRINGIFY (14)
|
#define TAP_VERSION G_STRINGIFY (14)
|
||||||
#define TAP_SUBTEST_PREFIX " "
|
#define TAP_SUBTEST_PREFIX " "
|
||||||
|
|
||||||
@@ -423,6 +430,56 @@ test_subprocess_envp (void)
|
|||||||
g_strfreev (envp);
|
g_strfreev (envp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_subprocess_stdin (void)
|
||||||
|
{
|
||||||
|
#ifdef G_OS_UNIX
|
||||||
|
int old_stdin_fd = -1;
|
||||||
|
int pipe_fd[2] = { -1, -1 };
|
||||||
|
const char *test_string = "*hello there*";
|
||||||
|
GError *local_error = NULL;
|
||||||
|
|
||||||
|
if (g_test_subprocess ())
|
||||||
|
{
|
||||||
|
char buf[100];
|
||||||
|
ssize_t n_read;
|
||||||
|
|
||||||
|
g_assert_no_errno (n_read = read (STDIN_FILENO, buf, sizeof (buf)));
|
||||||
|
g_assert_cmpint (n_read, >, 0);
|
||||||
|
|
||||||
|
g_print ("Read: %.*s\n", (int) n_read, buf);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Temporarily override this process’ stdin with a pipe. */
|
||||||
|
old_stdin_fd = fcntl (STDIN_FILENO, F_DUPFD_CLOEXEC, 0);
|
||||||
|
g_assert_cmpint (old_stdin_fd, >=, 0);
|
||||||
|
|
||||||
|
g_unix_open_pipe (pipe_fd, O_CLOEXEC, &local_error);
|
||||||
|
g_assert_no_error (local_error);
|
||||||
|
|
||||||
|
g_assert_no_errno (dup2 (pipe_fd[0], STDIN_FILENO));
|
||||||
|
|
||||||
|
/* Write something into it for the subprocess to read. */
|
||||||
|
g_assert_no_errno (write (pipe_fd[1], test_string, strlen (test_string) + 1));
|
||||||
|
|
||||||
|
/* Run the subprocess. */
|
||||||
|
g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDIN);
|
||||||
|
g_test_trap_assert_passed ();
|
||||||
|
g_test_trap_assert_stdout (test_string);
|
||||||
|
|
||||||
|
/* Restore the old stdin */
|
||||||
|
g_assert_no_errno (dup2 (old_stdin_fd, STDIN_FILENO));
|
||||||
|
|
||||||
|
g_assert_no_errno (close (old_stdin_fd));
|
||||||
|
g_assert_no_errno (close (pipe_fd[0]));
|
||||||
|
g_assert_no_errno (close (pipe_fd[1]));
|
||||||
|
#else
|
||||||
|
g_test_skip ("Testing stdin for subprocesses can only be done on Unix at the moment");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
/* run a test with fixture setup and teardown */
|
/* run a test with fixture setup and teardown */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
guint seed;
|
guint seed;
|
||||||
@@ -2916,6 +2973,7 @@ main (int argc,
|
|||||||
g_test_add_func ("/trap_subprocess/no-such-test", test_subprocess_no_such_test);
|
g_test_add_func ("/trap_subprocess/no-such-test", test_subprocess_no_such_test);
|
||||||
g_test_add_func ("/trap_subprocess/timeout", test_subprocess_timeout);
|
g_test_add_func ("/trap_subprocess/timeout", test_subprocess_timeout);
|
||||||
g_test_add_func ("/trap_subprocess/envp", test_subprocess_envp);
|
g_test_add_func ("/trap_subprocess/envp", test_subprocess_envp);
|
||||||
|
g_test_add_func ("/trap_subprocess/stdin", test_subprocess_stdin);
|
||||||
|
|
||||||
g_test_add_func ("/trap_subprocess/patterns", test_subprocess_patterns);
|
g_test_add_func ("/trap_subprocess/patterns", test_subprocess_patterns);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user