1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
/*
* testing-util.c
* This file is part of libpeas
*
* Copyright (C) 2011 - Garrett Regier
*
* libpeas is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* libpeas is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "config.h"
#include <stdlib.h>
#include <glib.h>
#if GLIB_CHECK_VERSION(2, 85, 0)
# include <girepository/girepository.h>
#else
# include <girepository.h>
#endif
#include "libpeas/peas-engine-priv.h"
#include "testing-util.h"
typedef struct {
const char *pattern;
gboolean hit;
} LogHook;
typedef struct {
GPtrArray *hooks;
GPtrArray *hits;
} LogHooks;
static void engine_private_notify (gpointer value);
static void unhandled_private_notify (gpointer value);
static void log_hooks_private_notify (gpointer value);
static gboolean initialized = FALSE;
static GLogLevelFlags fatal_flags = 0;
static gpointer dead_engine = NULL;
#define DEAD_ENGINE ((gpointer) &dead_engine)
static GPrivate engine_key = G_PRIVATE_INIT (engine_private_notify);
static GPrivate unhandled_key = G_PRIVATE_INIT (unhandled_private_notify);
static GPrivate log_hooks_key = G_PRIVATE_INIT (log_hooks_private_notify);
static void
engine_private_notify (gpointer value)
{
if (value != NULL)
g_error ("A PeasEngine was not freed!");
}
static void
unhandled_private_notify (gpointer value)
{
if (value != NULL)
g_error ("Log hooks not popped!");
}
static void
log_hooks_private_notify (gpointer value)
{
LogHooks *log_hooks = value;
if (log_hooks != NULL)
{
g_assert_cmpuint (log_hooks->hooks->len, ==, 0);
g_ptr_array_unref (log_hooks->hooks);
g_assert_cmpuint (log_hooks->hits->len, ==, 0);
g_ptr_array_unref (log_hooks->hits);
g_free (log_hooks);
}
}
static LogHooks *
get_log_hooks (void)
{
LogHooks *log_hooks = g_private_get (&log_hooks_key);
if (log_hooks != NULL)
return log_hooks;
g_assert_true (initialized);
log_hooks = g_new (LogHooks, 1);
log_hooks->hooks = g_ptr_array_new_with_free_func (g_free);
log_hooks->hits = g_ptr_array_new_with_free_func (g_free);
g_private_set (&log_hooks_key, log_hooks);
return log_hooks;
}
static void
log_handler (const char *log_domain,
GLogLevelFlags log_level,
const char *message,
gpointer user_data)
{
LogHooks *log_hooks = get_log_hooks ();
GPtrArray *hooks = log_hooks->hooks;
guint i;
/* We always want to log debug, info and message logs */
if ((log_level & G_LOG_LEVEL_DEBUG) != 0 ||
(log_level & G_LOG_LEVEL_INFO) != 0 ||
(log_level & G_LOG_LEVEL_MESSAGE) != 0)
{
g_log_default_handler (log_domain, log_level, message, user_data);
return;
}
/* Don't bother trying to match errors as GLib always aborts on them */
if ((log_level & G_LOG_LEVEL_ERROR) != 0)
{
g_log_default_handler (log_domain, log_level, message, user_data);
/* Call abort() as GLib may call G_BREAKPOINT() instead */
abort ();
}
for (i = 0; i < hooks->len; ++i)
{
LogHook *hook = g_ptr_array_index (hooks, i);
char *msg;
if (!g_pattern_match_simple (hook->pattern, message))
continue;
msg = g_strdup_printf ("%s-%s: %s", log_domain,
(log_level & G_LOG_LEVEL_WARNING) != 0 ?
"WARNING" : "CRITICAL",
message);
g_ptr_array_add (log_hooks->hits, msg);
hook->hit = TRUE;
return;
}
/* Checked in testing_util_pop_log_hooks() */
g_private_set (&unhandled_key, (gpointer) TRUE);
/* Use the default log handler directly to avoid recurse complaints */
g_log_default_handler (log_domain, log_level, message, user_data);
/* Support for the standard G_DEBUG flags */
if (((log_level & G_LOG_LEVEL_WARNING) != 0 &&
(fatal_flags & G_LOG_LEVEL_WARNING) != 0) ||
((log_level & G_LOG_LEVEL_CRITICAL) != 0 &&
(fatal_flags & G_LOG_LEVEL_CRITICAL) != 0))
{
G_BREAKPOINT ();
}
}
void
testing_util_envars (void)
{
/* Allow test runners to set this to 1 */
g_setenv ("G_ENABLE_DIAGNOSTIC", "0", FALSE);
/* Prevent GDBus from being used by GIO internally */
g_setenv ("GIO_USE_VFS", "local", TRUE);
/* We never want to save the settings */
g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
/* Prevent python from generating compiled files, they break distcheck */
g_setenv ("PYTHONDONTWRITEBYTECODE", "yes", TRUE);
g_setenv ("PEAS_PLUGIN_LOADERS_DIR", BUILDDIR "/loaders", TRUE);
}
void
testing_util_init (void)
{
GError *error = NULL;
GIRepository *repository;
const GDebugKey glib_debug_keys[] = {
{ "fatal-criticals", G_LOG_LEVEL_CRITICAL },
{ "fatal-warnings", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING }
};
if (initialized)
return;
/* Don't abort on warnings or criticals */
g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
g_log_set_default_handler (log_handler, NULL);
/* Force a breakpoint when the standard GLib debug flags
* are used. This is not supplied automatically because
* GLib's test utilities change the default.
*/
fatal_flags = g_parse_debug_string (g_getenv ("G_DEBUG"), glib_debug_keys,
G_N_ELEMENTS (glib_debug_keys));
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
#if GLIB_CHECK_VERSION(2, 85, 0)
repository = gi_repository_dup_default ();
gi_repository_require_private (repository,
#else
repository = g_object_ref (g_irepository_get_default ());
g_irepository_require_private (repository,
#endif
BUILDDIR "/libpeas",
"Peas", API_VERSION_S, 0, &error);
G_GNUC_END_IGNORE_DEPRECATIONS
g_assert_no_error (error);
g_clear_object (&repository);
initialized = TRUE;
}
static void
engine_weak_notify (gpointer unused,
PeasEngine *engine)
{
/* Cannot use NULL because testing_util_engine_free() must be called */
g_private_set (&engine_key, DEAD_ENGINE);
}
PeasEngine *
testing_util_engine_new_full (gboolean nonglobal_loaders)
{
PeasEngine *engine;
g_assert_true (initialized);
/* testing_util_engine_free() checks that the
* engine is freed so only one engine can be created
*/
g_assert_true (g_private_get (&engine_key) == NULL);
/* Must be after requiring typelibs */
if (!nonglobal_loaders)
engine = peas_engine_new ();
else
engine = peas_engine_new_with_nonglobal_loaders ();
g_private_set (&engine_key, engine);
g_object_weak_ref (G_OBJECT (engine),
(GWeakNotify) engine_weak_notify,
NULL);
/* The plugins that two-deps depends on must be added
* to the engine before it. This is used to verify that
* the engine will order the plugin list correctly.
*/
peas_engine_add_search_path (engine,
BUILDDIR "/tests/plugins/builtin",
SRCDIR "/tests/plugins/builtin");
peas_engine_add_search_path (engine,
BUILDDIR "/tests/plugins/loadable",
SRCDIR "/tests/plugins/loadable");
peas_engine_add_search_path (engine,
BUILDDIR "/tests/plugins",
SRCDIR "/tests/plugins");
return engine;
}
void
testing_util_engine_free (PeasEngine *engine)
{
/* In case a test needs to free the engine */
if (g_private_get (&engine_key) != DEAD_ENGINE)
{
g_object_unref (engine);
/* Make sure that at the end of every test the engine is freed */
g_assert_true (g_private_get (&engine_key) == DEAD_ENGINE);
}
g_private_set (&engine_key, NULL);
/* Pop the log hooks so the test cases don't have to */
testing_util_pop_log_hooks ();
}
int
testing_util_run_tests (void)
{
int retval;
g_assert_true (initialized);
retval = g_test_run ();
/* Cleanup various data early otherwise some
* tools, like gcov, will not process it correctly
*/
g_private_replace (&engine_key, NULL);
g_private_replace (&unhandled_key, NULL);
g_private_replace (&log_hooks_key, NULL);
_peas_engine_shutdown ();
return retval;
}
void
testing_util_push_log_hook (const char *pattern)
{
LogHooks *log_hooks = get_log_hooks ();
LogHook *hook;
g_return_if_fail (pattern != NULL && *pattern != '\0');
hook = g_new (LogHook, 1);
hook->pattern = pattern;
hook->hit = FALSE;
g_ptr_array_add (log_hooks->hooks, hook);
}
/* Optional - see testing_util_engine_free() */
void
testing_util_pop_log_hook (void)
{
LogHooks *log_hooks = get_log_hooks ();
GPtrArray *hooks = log_hooks->hooks;
LogHook *hook;
g_return_if_fail (hooks->len > 0);
hook = g_ptr_array_index (hooks, hooks->len - 1);
if (!hook->hit)
testing_util_pop_log_hooks ();
g_ptr_array_remove_index (hooks, hooks->len - 1);
}
void
testing_util_pop_log_hooks (void)
{
LogHooks *log_hooks = get_log_hooks ();
GPtrArray *hooks = log_hooks->hooks;
GPtrArray *hits = log_hooks->hits;
gboolean unhandled = g_private_get (&unhandled_key) != NULL;
guint i;
LogHook *hook;
GPtrArray *unhit_hooks;
GString *msg;
if (hooks->len == 0)
return;
unhit_hooks = g_ptr_array_new ();
for (i = 0; i < hooks->len; ++i)
{
hook = g_ptr_array_index (hooks, i);
if (!hook->hit)
g_ptr_array_add (unhit_hooks, hook);
}
if (unhit_hooks->len == 0 && !unhandled)
{
g_ptr_array_unref (unhit_hooks);
g_ptr_array_set_size (hooks, 0);
g_ptr_array_set_size (hits, 0);
return;
}
msg = g_string_new ("");
if (unhit_hooks->len != 0)
{
g_string_append (msg, "Log hooks were not triggered:");
if (unhit_hooks->len == 1)
{
hook = g_ptr_array_index (unhit_hooks, 0);
g_string_append_printf (msg, " '%s'", hook->pattern);
}
else
{
for (i = 0; i < unhit_hooks->len; ++i)
{
hook = g_ptr_array_index (unhit_hooks, i);
g_string_append_printf (msg, "\n\t'%s'", hook->pattern);
}
}
}
if (hits->len != 0)
{
if (unhit_hooks->len != 0)
g_string_append (msg, "\n\n");
g_string_append (msg, "Log messages filtered:");
for (i = 0; i < hits->len; ++i)
{
const char *hit = g_ptr_array_index (hits, i);
g_string_append_printf (msg, "\n\t%s", hit);
}
}
/* Use the default log handler directly to avoid recurse complaints */
g_log_default_handler (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, msg->str, NULL);
abort ();
}
|