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.
This commit is contained in:
Charles Barto 2021-12-16 20:20:56 -08:00
parent 42a5f1d387
commit 908ed3498b
2 changed files with 25 additions and 2 deletions

View File

@ -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 *)))
{

View File

@ -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 */