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
|
/*
* testing-util.c
* This file is part of libpeas
*
* Copyright (C) 2011 - Garrett Regier
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <glib.h>
#include <girepository.h>
#include "libpeas/peas-engine-priv.h"
#include "testing-util.h"
typedef struct {
const gchar *pattern;
gboolean hit;
} LogHook;
static PeasEngine *engine = NULL;
static GPtrArray *log_hooks = NULL;
/* These are warnings and criticals that just have to happen
* for testing purposes and as such we don't want to abort on them.
*
* If the warnings are for specific tests use
* testing_util_push_log_hook() and testing_util_pop_log_hook() which
* will assert that the warning or critical actually happened.
*
* Don't bother putting errors in here as GLib always aborts on errors.
*/
static const gchar *allowed_patterns[] = {
"*lib*loader.so*cannot open shared object file: No such file or directory*"
};
static void
log_handler (const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *message,
gpointer user_data)
{
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 < log_hooks->len; ++i)
{
LogHook *hook = g_ptr_array_index (log_hooks, i);
if (g_pattern_match_simple (hook->pattern, message))
{
hook->hit = TRUE;
return;
}
}
/* Check the allowed_patterns after the log hooks to
* avoid issues when an allowed_pattern would match a hook
*/
for (i = 0; i < G_N_ELEMENTS (allowed_patterns); ++i)
{
if (g_pattern_match_simple (allowed_patterns[i], message))
return;
}
/* Warnings and criticals are not allowed to be unhandled */
if ((log_level & G_LOG_LEVEL_WARNING) != 0)
message = g_strdup_printf ("Unhandled warning: %s: %s", log_domain, message);
else
message = g_strdup_printf ("Unhandled critical: %s: %s", log_domain, message);
/* Use the default log handler directly to avoid recurse complaints */
g_log_default_handler (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR,
message, user_data);
testing_util_pop_log_hooks ();
/* The default handler does not actually abort */
abort ();
}
void
testing_util_init (void)
{
GError *error = NULL;
static gboolean initialized = FALSE;
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);
g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
g_irepository_prepend_search_path (BUILDDIR "/libpeas");
g_setenv ("PEAS_PLUGIN_LOADERS_DIR", BUILDDIR "/loaders", TRUE);
g_irepository_require (g_irepository_get_default (), "Peas", "1.0", 0, &error);
g_assert_no_error (error);
log_hooks = g_ptr_array_new_with_free_func (g_free);
initialized = TRUE;
}
PeasEngine *
testing_util_engine_new (void)
{
/* testing_util_engine_free() checks that the
* engine is freed so only one engine can be created
*/
g_assert (engine == NULL);
testing_util_init ();
/* Must be after requiring typelibs */
engine = peas_engine_new ();
g_object_add_weak_pointer (G_OBJECT (engine), (gpointer *) &engine);
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 (engine != NULL)
{
g_object_unref (engine_);
/* Make sure that at the end of every test the engine is freed */
g_assert (engine == 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;
retval = g_test_run ();
if (log_hooks != NULL)
{
g_assert_cmpuint (log_hooks->len, ==, 0);
g_ptr_array_unref (log_hooks);
}
/* Cannot call this with atexit() because
* gcov does not register that it was called.
*/
peas_engine_shutdown ();
return retval;
}
void
testing_util_push_log_hook (const gchar *pattern)
{
LogHook *hook;
g_return_if_fail (log_hooks != NULL);
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, hook);
}
/* Optional - see testing_util_engine_free() */
void
testing_util_pop_log_hook (void)
{
LogHook *hook;
g_return_if_fail (log_hooks != NULL);
g_return_if_fail (log_hooks->len > 0);
hook = g_ptr_array_index (log_hooks, log_hooks->len - 1);
if (!hook->hit)
testing_util_pop_log_hooks ();
g_ptr_array_remove_index (log_hooks, log_hooks->len - 1);
}
void
testing_util_pop_log_hooks (void)
{
guint i;
LogHook *hook;
GPtrArray *unhit_hooks;
g_return_if_fail (log_hooks != NULL);
if (log_hooks->len == 0)
return;
unhit_hooks = g_ptr_array_new ();
for (i = 0; i < log_hooks->len; ++i)
{
hook = g_ptr_array_index (log_hooks, i);
if (!hook->hit)
g_ptr_array_add (unhit_hooks, hook);
}
if (unhit_hooks->len == 1)
{
gchar *msg;
hook = g_ptr_array_index (unhit_hooks, 0);
msg = g_strdup_printf ("Log hook was not triggered: '%s'",
hook->pattern);
/* Use the default log handler directly to avoid recurse complaints */
g_log_default_handler (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, msg, NULL);
abort ();
}
else if (unhit_hooks->len > 1)
{
/* Use the default log handler directly to avoid recurse complaints */
g_log_default_handler (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR,
"Log hooks were not triggered:\n", NULL);
for (i = 0; i < unhit_hooks->len; ++i)
{
hook = g_ptr_array_index (unhit_hooks, i);
g_print ("\t'%s'\n", hook->pattern);
}
abort ();
}
g_ptr_array_unref (unhit_hooks);
g_ptr_array_set_size (log_hooks, 0);
}
|