[glib/wip/smcv/defer-test-cleanup: 1/2] testutils: Defer global cleanup until we really exit




commit c651ea0453c9818255d28d3e4b0909c408631d25
Author: Simon McVittie <smcv collabora com>
Date:   Thu Jan 6 15:41:08 2022 +0000

    testutils: Defer global cleanup until we really exit
    
    Some test suites try to call g_test_build_filename() after g_test_run()
    has returned. In the installed-tests use-case where G_TEST_BUILDDIR and
    G_TEST_SRCDIR are unset, that call uses test_argv0_dirname, which
    is freed in test_cleanup(). Defer test_cleanup() using atexit() so it
    isn't freed until after we return from main().
    
    Resolves: https://gitlab.gnome.org/GNOME/glib/-/issues/2563
    Signed-off-by: Simon McVittie <smcv collabora com>

 glib/gtestutils.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
---
diff --git a/glib/gtestutils.c b/glib/gtestutils.c
index a061da799..0a91f9322 100644
--- a/glib/gtestutils.c
+++ b/glib/gtestutils.c
@@ -890,10 +890,10 @@ static gboolean    test_debug_log = FALSE;
 static gboolean    test_tap_log = TRUE;  /* default to TAP as of GLib 2.62; see #1619; the non-TAP output 
mode is deprecated */
 static gboolean    test_nonfatal_assertions = FALSE;
 static DestroyEntry *test_destroy_queue = NULL;
-static char       *test_argv0 = NULL;
-static char       *test_argv0_dirname;
-static const char *test_disted_files_dir;
-static const char *test_built_files_dir;
+static char       *test_argv0 = NULL;           /* points into global argv */
+static char       *test_argv0_dirname = NULL;   /* owned by GLib */
+static const char *test_disted_files_dir;       /* points into test_argv0_dirname or an environment variable 
*/
+static const char *test_built_files_dir;        /* points into test_argv0_dirname or an environment variable 
*/
 static char       *test_initial_cwd = NULL;
 static gboolean    test_in_forked_child = FALSE;
 static gboolean    test_in_subprocess = FALSE;
@@ -2219,6 +2219,13 @@ g_test_run (void)
   int ret;
   GTestSuite *suite;
 
+  if (atexit (test_cleanup) != 0)
+    {
+      int errsv = errno;
+      g_error ("Unable to register test cleanup to be run at exit: %s",
+               g_strerror (errsv));
+    }
+
   suite = g_test_get_root ();
   if (g_test_run_suite (suite) != 0)
     {
@@ -2255,7 +2262,6 @@ g_test_run (void)
 
 out:
   g_test_suite_free (suite);
-  test_cleanup ();
   return ret;
 }
 


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