[glib: 1/4] Prevent gtest tests from popping up dialog boxes




commit 908ed3498bb911dee53a221549d8a4a7bdf1bea3
Author: Charles Barto <barto charlie gmail com>
Date:   Thu Dec 16 20:20:56 2021 -0800

    Prevent gtest tests from popping up dialog boxes
    
    Many UCRT (and msvcrt/msvcxx) functions open dialog boxes
    by default for .... some reason. This is a problem because a test runner
    waiting on a process to exit won't see it exit unless someone actually
    clicks away the box, which won't happen on a CI machine.
    
    Additionally g_abort unconditionally raises a debugging exception,
    which, if uncaught, will cause windows error reporting to pop a dialog
    
    Resolve the first problem by calling platform specific (but documented)
    functions to change the CRT's behavior in g_test_init
    
    Resolve the second by only throwing a debug exception if we're under
    debugging, and just calling abort() otherwise.
    
    This reduces the number of popups triggerd by `meson test` from
    over 10 to about three on my machine, mostly in the spawn test code.

 glib/gtestutils.c | 18 ++++++++++++++++++
 glib/gutils.c     |  9 +++++++--
 2 files changed, 25 insertions(+), 2 deletions(-)
---
diff --git a/glib/gtestutils.c b/glib/gtestutils.c
index 578270394..ebf696c6d 100644
--- a/glib/gtestutils.c
+++ b/glib/gtestutils.c
@@ -38,6 +38,9 @@
 #include <io.h>
 #include <windows.h>
 #endif
+#ifdef G_PLATFORM_WIN32
+#include <crtdbg.h>
+#endif
 #include <errno.h>
 #include <signal.h>
 #ifdef HAVE_SYS_SELECT_H
@@ -1597,6 +1600,21 @@ void
   mutable_test_config_vars.test_undefined = FALSE;
 #endif
 
+#ifdef G_PLATFORM_WIN32
+  // don't open a window for errors (like the "abort() was called one")
+  _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
+  _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
+  // while gtest tests tend to use g_assert and friends
+  // if they do use the C standard assert macro we want to
+  // output a message to stderr, not open a popup window
+  _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
+  _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
+  // in release mode abort() will pop up a windows error
+  // reporting dialog, let's prevent that.
+  _set_abort_behavior(0, _CALL_REPORTFAULT);
+#endif
+
+
   va_start (args, argv);
   while ((option = va_arg (args, char *)))
     {
diff --git a/glib/gutils.c b/glib/gutils.c
index 2c47e023a..2789fe157 100644
--- a/glib/gutils.c
+++ b/glib/gutils.c
@@ -3160,8 +3160,13 @@ g_check_setuid (void)
 void
 g_abort (void)
 {
-  /* One call to break the debugger */
-  DebugBreak ();
+  /* One call to break the debugger 
+   * We check if a debugger is actually attached to
+   * avoid a windows error reporting popup window
+   * when run in a test harness / on CI 
+   */
+  if(IsDebuggerPresent())
+    DebugBreak ();
   /* One call in case CRT changes its abort() behaviour */
   abort ();
   /* And one call to bind them all and terminate the program for sure */


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