[phodav: 1/2] tests: launch virtual-dir-server using GSubprocess




commit 560ab5ca4f836d82bddbbe66ea0f7c6b4cab6b3b
Author: Jakub Janků <jjanku redhat com>
Date:   Wed May 19 23:31:19 2021 +0200

    tests: launch virtual-dir-server using GSubprocess
    
    Use g_subprocess_new to spawn the test server.
    This way the tests can also be used on Windows.
    
    If the parent process with the tests aborts, we don't want to
    leave the test server running. Use G_SUBPROCESS_FLAGS_STDIN_PIPE
    to setup a pipe to the test server and add an io channel watch
    in the server. If the channel becomes ready for reading, quit.
    
    Fixes: https://gitlab.gnome.org/GNOME/phodav/-/issues/14
    
    Signed-off-by: Jakub Janků <jjanku redhat com>

 tests/meson.build          |  8 +++-----
 tests/virtual-dir-server.c | 27 +++++++++++++++++++++++++++
 tests/virtual-dir.c        | 24 ++++++++++++------------
 3 files changed, 42 insertions(+), 17 deletions(-)
---
diff --git a/tests/meson.build b/tests/meson.build
index aeb48e3..2d2f401 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -1,8 +1,6 @@
-tests_sources = []
-
-if host_machine.system() != 'windows'
-  tests_sources += 'virtual-dir.c'
-endif
+tests_sources = [
+  'virtual-dir.c',
+]
 
 executable('virtual-dir-server',
            sources : 'virtual-dir-server.c',
diff --git a/tests/virtual-dir-server.c b/tests/virtual-dir-server.c
index 0da2895..2678e74 100644
--- a/tests/virtual-dir-server.c
+++ b/tests/virtual-dir-server.c
@@ -57,6 +57,16 @@ delete_file_recursive (GFile *file)
   g_file_delete (file, NULL, NULL);
 }
 
+static gboolean
+stdin_watch (GIOChannel *source, GIOCondition condition, gpointer data)
+{
+  g_debug ("stdin condition %d, quitting", condition);
+  GMainLoop *loop = data;
+  g_main_loop_quit (loop);
+
+  return G_SOURCE_REMOVE;
+}
+
 /* This is basically a very stripped-down version of chezdav
  * for the purpose of testing PhodavVirtualDir.
  *
@@ -65,6 +75,11 @@ delete_file_recursive (GFile *file)
 int
 main (int argc, char *argv[])
 {
+  gboolean quit_on_stdin = FALSE;
+  if (argc >= 2 && !g_strcmp0 (argv[1], "--quit-on-stdin")) {
+    quit_on_stdin = TRUE;
+  }
+
   GFile *root_dir = g_file_new_for_path ("./phodav-virtual-root");
   GFile *real_dir = g_file_get_child (root_dir, "real");
 
@@ -99,6 +114,18 @@ main (int argc, char *argv[])
     }
 
   GMainLoop *loop = g_main_loop_new (NULL, FALSE);
+
+  if (quit_on_stdin) {
+    GIOChannel *stdin_channel;
+#ifdef G_OS_WIN32
+    stdin_channel = g_io_channel_win32_new_fd (0);
+#else
+    stdin_channel = g_io_channel_unix_new (0);
+#endif
+    g_io_add_watch (stdin_channel, G_IO_IN | G_IO_HUP | G_IO_ERR, stdin_watch, loop);
+    g_io_channel_unref (stdin_channel);
+  }
+
   g_main_loop_run (loop);
   g_main_loop_unref (loop);
   g_object_unref (phodav);
diff --git a/tests/virtual-dir.c b/tests/virtual-dir.c
index 4d5d9e9..7273b49 100644
--- a/tests/virtual-dir.c
+++ b/tests/virtual-dir.c
@@ -15,9 +15,6 @@
  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 #include <stdlib.h>
-#include <unistd.h>
-#include <signal.h>
-#include <sys/prctl.h>
 #include <glib.h>
 
 #include "libphodav/phodav.h"
@@ -97,15 +94,17 @@ get_test_path (const TestCase *test)
 int
 main (int argc, char *argv[])
 {
-  pid_t child_pid = fork ();
-  g_assert_cmpint (child_pid, !=, -1);
-  if (child_pid == 0)
-    {
-      prctl(PR_SET_PDEATHSIG, SIGTERM);
-      execl ("tests/virtual-dir-server", "virtual-dir-server", NULL);
-      g_printerr ("Error launching virtual-dir-server\n");
-      return 1;
-    }
+  GError *error = NULL;
+  GSubprocess *server_subproc;
+  server_subproc = g_subprocess_new (
+    G_SUBPROCESS_FLAGS_STDIN_PIPE, &error,
+    "tests/virtual-dir-server", "--quit-on-stdin", NULL);
+
+  if (error) {
+    g_printerr ("Failed to launch virtual-dir-server: %s\n", error->message);
+    g_error_free (error);
+    return 1;
+  }
 
   g_test_init (&argc, &argv, NULL);
 
@@ -146,5 +145,6 @@ main (int argc, char *argv[])
 
   gint res = g_test_run ();
   g_object_unref (session);
+  g_object_unref (server_subproc);
   return res;
 }


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