[glib/wip/pwithnall/more-macos-test-fixes] tests: Fix incorrect basename comparison in gsubprocess test




commit 087272777b2325d38a0becd40e8223d768cba163
Author: Philip Withnall <pwithnall endlessos org>
Date:   Mon Jun 27 15:00:39 2022 +0100

    tests: Fix incorrect basename comparison in gsubprocess test
    
    This was causing intermittent failures on macOS, depending on whether
    the tmpdir ended with a `/` or `/some-dir`. `g_strrstr()` is not the
    right function to use to extract a basename from a path, for this
    reason.
    
    When it failed, the macOS test was failing with:
    ```
    ok 16 /gsubprocess/env
    Bail out! GLib-GIO:ERROR:../gio/tests/gsubprocess.c:1507:test_cwd: assertion failed (basename == 
tmp_lineend_basename): ("/T\n" == "/\n")
    ```
    
    The test now passes reliably, which means that it can be removed from
    the list of expected failures on macOS.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>
    
    Helps: #1392

 gio/tests/gsubprocess.c | 33 +++++++++++++++++++--------------
 gio/tests/meson.build   |  5 +----
 2 files changed, 20 insertions(+), 18 deletions(-)
---
diff --git a/gio/tests/gsubprocess.c b/gio/tests/gsubprocess.c
index fc5d4624ed..036c2afadf 100644
--- a/gio/tests/gsubprocess.c
+++ b/gio/tests/gsubprocess.c
@@ -1477,35 +1477,40 @@ static void
 test_cwd (void)
 {
   GError *local_error = NULL;
-  GError **error = &local_error;
   GSubprocessLauncher *launcher;
   GSubprocess *proc;
   GPtrArray *args;
   GInputStream *stdout_stream;
   gchar *result;
-  const char *basename;
-  gchar *tmp_lineend;
-  const gchar *tmp_lineend_basename;
+  gsize result_len;
+  const gchar *tmpdir = g_get_tmp_dir ();
+  gchar *tmpdir_basename = NULL, *result_basename = NULL;
 
   args = get_test_subprocess_args ("cwd", NULL);
   launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE);
   g_subprocess_launcher_set_flags (launcher, G_SUBPROCESS_FLAGS_STDOUT_PIPE);
-  g_subprocess_launcher_set_cwd (launcher, g_get_tmp_dir ());
-  tmp_lineend = g_strdup_printf ("%s%s", g_get_tmp_dir (), LINEEND);
-  tmp_lineend_basename = g_strrstr (tmp_lineend, G_DIR_SEPARATOR_S);
+  g_subprocess_launcher_set_cwd (launcher, tmpdir);
 
-  proc = g_subprocess_launcher_spawnv (launcher, (const char * const *)args->pdata, error);
+  proc = g_subprocess_launcher_spawnv (launcher, (const char * const *)args->pdata, &local_error);
   g_ptr_array_free (args, TRUE);
   g_assert_no_error (local_error);
 
   stdout_stream = g_subprocess_get_stdout_pipe (proc);
 
-  result = splice_to_string (stdout_stream, error);
-
-  basename = g_strrstr (result, G_DIR_SEPARATOR_S);
-  g_assert_nonnull (basename);
-  g_assert_cmpstr (basename, ==, tmp_lineend_basename);
-  g_free (tmp_lineend);
+  result = splice_to_string (stdout_stream, &local_error);
+  g_assert_no_error (local_error);
+  result_len = strlen (result);
+
+  /* The result should end with a line ending */
+  g_assert_cmpstr (result + result_len - strlen (LINEEND), ==, LINEEND);
+
+  /* Not sure if the testprog guarantees to return an absolute path for the cwd,
+   * so only compare the basenames. */
+  tmpdir_basename = g_path_get_basename (tmpdir);
+  result_basename = g_path_get_basename (g_strstrip (result));
+  g_assert_cmpstr (tmpdir_basename, ==, result_basename);
+  g_free (tmpdir_basename);
+  g_free (result_basename);
 
   g_free (result);
   g_object_unref (proc);
diff --git a/gio/tests/meson.build b/gio/tests/meson.build
index ce102a6587..1cb5e30ee2 100644
--- a/gio/tests/meson.build
+++ b/gio/tests/meson.build
@@ -70,10 +70,7 @@ gio_tests = {
     # FIXME: https://gitlab.gnome.org/GNOME/glib/-/issues/1392
     'should_fail' : host_system == 'darwin',
   },
-  'gsubprocess' : {
-    # FIXME: https://gitlab.gnome.org/GNOME/glib/-/issues/1392
-    'should_fail' : host_system == 'darwin',
-  },
+  'gsubprocess' : {},
   'g-file' : {},
   'g-file-info' : {
     # FIXME: https://gitlab.gnome.org/GNOME/glib/-/issues/1392


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