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 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
/* foundry-test-manager.c
*
* Copyright 2025 Christian Hergert <chergert@redhat.com>
*
* This library 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.
*
* This library 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 General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "config.h"
#include <libpeas.h>
#include "foundry-debug.h"
#include "foundry-model-manager.h"
#include "foundry-test.h"
#include "foundry-test-manager.h"
#include "foundry-test-provider-private.h"
#include "foundry-test-suite-private.h"
#include "foundry-service-private.h"
#include "foundry-util-private.h"
/**
* FoundryTestManager:
*
* Manages plugins providing test providers and their tests.
*/
struct _FoundryTestManager
{
FoundryService parent_instance;
PeasExtensionSet *addins;
};
struct _FoundryTestManagerClass
{
FoundryServiceClass parent_class;
};
static void list_model_iface_init (GListModelInterface *iface);
G_DEFINE_FINAL_TYPE_WITH_CODE (FoundryTestManager, foundry_test_manager, FOUNDRY_TYPE_SERVICE,
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
static void
foundry_test_manager_provider_added (PeasExtensionSet *set,
PeasPluginInfo *plugin_info,
GObject *addin,
gpointer user_data)
{
FoundryTestManager *self = user_data;
g_assert (PEAS_IS_EXTENSION_SET (set));
g_assert (PEAS_IS_PLUGIN_INFO (plugin_info));
g_assert (FOUNDRY_IS_TEST_PROVIDER (addin));
g_assert (FOUNDRY_IS_TEST_MANAGER (self));
g_debug ("Adding FoundryTestProvider of type %s", G_OBJECT_TYPE_NAME (addin));
dex_future_disown (foundry_test_provider_load (FOUNDRY_TEST_PROVIDER (addin)));
}
static void
foundry_test_manager_provider_removed (PeasExtensionSet *set,
PeasPluginInfo *plugin_info,
GObject *addin,
gpointer user_data)
{
FoundryTestManager *self = user_data;
g_assert (PEAS_IS_EXTENSION_SET (set));
g_assert (PEAS_IS_PLUGIN_INFO (plugin_info));
g_assert (FOUNDRY_IS_TEST_PROVIDER (addin));
g_assert (FOUNDRY_IS_TEST_MANAGER (self));
g_debug ("Removing FoundryTestProvider of type %s", G_OBJECT_TYPE_NAME (addin));
dex_future_disown (foundry_test_provider_unload (FOUNDRY_TEST_PROVIDER (addin)));
}
static DexFuture *
foundry_test_manager_start_fiber (gpointer user_data)
{
FoundryTestManager *self = user_data;
g_autoptr(GPtrArray) futures = NULL;
guint n_items;
g_assert (FOUNDRY_IS_MAIN_THREAD ());
g_assert (FOUNDRY_IS_TEST_MANAGER (self));
g_assert (PEAS_IS_EXTENSION_SET (self->addins));
g_signal_connect_object (self->addins,
"extension-added",
G_CALLBACK (foundry_test_manager_provider_added),
self,
0);
g_signal_connect_object (self->addins,
"extension-removed",
G_CALLBACK (foundry_test_manager_provider_removed),
self,
0);
g_signal_connect_object (self->addins,
"items-changed",
G_CALLBACK (g_list_model_items_changed),
self,
G_CONNECT_SWAPPED);
n_items = g_list_model_get_n_items (G_LIST_MODEL (self->addins));
futures = g_ptr_array_new_with_free_func (dex_unref);
for (guint i = 0; i < n_items; i++)
{
g_autoptr(FoundryTestProvider) provider = g_list_model_get_item (G_LIST_MODEL (self->addins), i);
g_ptr_array_add (futures, foundry_test_provider_load (provider));
}
g_list_model_items_changed (G_LIST_MODEL (self), 0, 0, n_items);
if (futures->len > 0)
dex_await (foundry_future_all (futures), NULL);
return dex_future_new_true ();
}
static DexFuture *
foundry_test_manager_start (FoundryService *service)
{
FoundryTestManager *self = (FoundryTestManager *)service;
g_assert (FOUNDRY_IS_MAIN_THREAD ());
g_assert (FOUNDRY_IS_TEST_MANAGER (self));
g_assert (PEAS_IS_EXTENSION_SET (self->addins));
return dex_scheduler_spawn (NULL, 0,
foundry_test_manager_start_fiber,
g_object_ref (self),
g_object_unref);
}
static DexFuture *
foundry_test_manager_stop (FoundryService *service)
{
FoundryTestManager *self = (FoundryTestManager *)service;
g_autoptr(GPtrArray) futures = NULL;
guint n_items;
g_assert (FOUNDRY_IS_MAIN_THREAD ());
g_assert (FOUNDRY_IS_SERVICE (service));
g_signal_handlers_disconnect_by_func (self->addins,
G_CALLBACK (foundry_test_manager_provider_added),
self);
g_signal_handlers_disconnect_by_func (self->addins,
G_CALLBACK (foundry_test_manager_provider_removed),
self);
n_items = g_list_model_get_n_items (G_LIST_MODEL (self->addins));
futures = g_ptr_array_new_with_free_func (dex_unref);
for (guint i = 0; i < n_items; i++)
{
g_autoptr(FoundryTestProvider) provider = g_list_model_get_item (G_LIST_MODEL (self->addins), i);
g_ptr_array_add (futures, foundry_test_provider_unload (provider));
}
g_list_model_items_changed (G_LIST_MODEL (self), 0, n_items, 0);
g_clear_object (&self->addins);
if (futures->len > 0)
return foundry_future_all (futures);
return dex_future_new_true ();
}
static void
foundry_test_manager_constructed (GObject *object)
{
FoundryTestManager *self = (FoundryTestManager *)object;
g_autoptr(FoundryContext) context = NULL;
G_OBJECT_CLASS (foundry_test_manager_parent_class)->constructed (object);
context = foundry_contextual_dup_context (FOUNDRY_CONTEXTUAL (self));
self->addins = peas_extension_set_new (NULL,
FOUNDRY_TYPE_TEST_PROVIDER,
"context", context,
NULL);
}
static void
foundry_test_manager_finalize (GObject *object)
{
FoundryTestManager *self = (FoundryTestManager *)object;
g_clear_object (&self->addins);
G_OBJECT_CLASS (foundry_test_manager_parent_class)->finalize (object);
}
static void
foundry_test_manager_class_init (FoundryTestManagerClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
FoundryServiceClass *service_class = FOUNDRY_SERVICE_CLASS (klass);
object_class->constructed = foundry_test_manager_constructed;
object_class->finalize = foundry_test_manager_finalize;
service_class->start = foundry_test_manager_start;
service_class->stop = foundry_test_manager_stop;
}
static void
foundry_test_manager_init (FoundryTestManager *self)
{
}
static GType
foundry_test_manager_get_item_type (GListModel *model)
{
return FOUNDRY_TYPE_TEST_PROVIDER;
}
static guint
foundry_test_manager_get_n_items (GListModel *model)
{
FoundryTestManager *self = FOUNDRY_TEST_MANAGER (model);
if (self->addins == NULL)
return 0;
return g_list_model_get_n_items (G_LIST_MODEL (self->addins));
}
static gpointer
foundry_test_manager_get_item (GListModel *model,
guint position)
{
FoundryTestManager *self = FOUNDRY_TEST_MANAGER (model);
if (self->addins == NULL)
return NULL;
return g_list_model_get_item (G_LIST_MODEL (self->addins), position);
}
static void
list_model_iface_init (GListModelInterface *iface)
{
iface->get_item_type = foundry_test_manager_get_item_type;
iface->get_n_items = foundry_test_manager_get_n_items;
iface->get_item = foundry_test_manager_get_item;
}
/**
* foundry_test_manager_list_tests:
* @self: a [class@Foundry.TestManager]
*
* Queries all [class@Foundry.TestProvider] for available unit tests.
*
* The resulting module may not be fully populated by all providers
* by time it resolves. You may await the completion of all providers
* by awaiting [func@Foundry.list_model_await] for the completion
* of all providers.
*
* This allows the consumer to get a dynamically populating list model
* for user interfaces without delay.
*
* Returns: (transfer full): a [class@Dex.Future] that resolves to a
* [iface@Gio.ListModel] of [class@Foundry.Test]
*/
DexFuture *
foundry_test_manager_list_tests (FoundryTestManager *self)
{
g_autoptr(GPtrArray) futures = NULL;
guint n_items;
dex_return_error_if_fail (FOUNDRY_IS_TEST_MANAGER (self));
if (self->addins == NULL)
return foundry_future_new_not_supported ();
futures = g_ptr_array_new_with_free_func (dex_unref);
n_items = g_list_model_get_n_items (G_LIST_MODEL (self->addins));
for (guint i = 0; i < n_items; i++)
{
g_autoptr(FoundryTestProvider) provider = g_list_model_get_item (G_LIST_MODEL (self->addins), i);
g_ptr_array_add (futures, foundry_test_provider_list_tests (provider));
}
return _foundry_flatten_list_model_new_from_futures (futures);
}
static DexFuture *
foundry_test_manager_filter_test (DexFuture *completed,
gpointer data)
{
const char *id = data;
g_autoptr(GListModel) tests = NULL;
guint n_items;
g_assert (DEX_IS_FUTURE (completed));
g_assert (id != NULL);
if (!(tests = dex_await_object (dex_ref (completed), NULL)))
return NULL;
n_items = g_list_model_get_n_items (tests);
for (guint i = 0; i < n_items; i++)
{
g_autoptr(FoundryTest) test = g_list_model_get_item (tests, i);
g_autofree char *test_id = foundry_test_dup_id (test);
if (foundry_str_equal0 (test_id, id))
return dex_future_new_take_object (g_steal_pointer (&test));
}
return dex_future_new_reject (G_IO_ERROR,
G_IO_ERROR_NOT_FOUND,
"No such test `%s`", id);
}
static DexFuture *
foundry_test_manager_finish_cb (DexFuture *completed,
gpointer data)
{
g_autoptr(GListModel) tests = dex_await_object (dex_ref (completed), NULL);
return dex_future_then (foundry_list_model_await (tests),
foundry_future_return_object,
g_object_ref (tests),
g_object_unref);
}
/**
* foundry_test_manager_find_test:
* @self: a [class@Foundry.TestManager]
* @test_id: the identifier of the test
*
* Returns: (transfer full): a [class@Dex.Future] that resolves to a
* [class@Foundry.Test] or rejects with error.
*/
DexFuture *
foundry_test_manager_find_test (FoundryTestManager *self,
const char *test_id)
{
dex_return_error_if_fail (FOUNDRY_IS_TEST_MANAGER (self));
dex_return_error_if_fail (test_id != NULL);
return dex_future_then (dex_future_then (foundry_test_manager_list_tests (self),
foundry_test_manager_finish_cb,
NULL, NULL),
foundry_test_manager_filter_test,
g_strdup (test_id),
g_free);
}
static void
add_to_suite (GListStore *suites,
GHashTable *name_to_suite,
const char *name,
FoundryTest *test)
{
FoundryTestSuite *suite;
g_assert (G_IS_LIST_STORE (suites));
g_assert (name_to_suite != NULL);
g_assert (name != NULL);
g_assert (FOUNDRY_IS_TEST (test));
if (!(suite = g_hash_table_lookup (name_to_suite, name)))
{
suite = _foundry_test_suite_new (name);
g_hash_table_replace (name_to_suite, g_strdup (name), suite);
g_list_store_append (suites, suite);
}
_foundry_test_suite_add (suite, test);
}
static DexFuture *
foundry_test_manager_list_suites_fiber (gpointer user_data)
{
FoundryTestManager *self = user_data;
g_autoptr(GListModel) tests = NULL;
g_autoptr(GListStore) suites = NULL;
g_autoptr(GHashTable) name_to_suite = NULL;
g_autoptr(GError) error = NULL;
guint n_tests;
g_assert (FOUNDRY_IS_TEST_MANAGER (self));
if (!(tests = dex_await_object (foundry_test_manager_list_tests (self), &error)))
return dex_future_new_for_error (g_steal_pointer (&error));
dex_await (foundry_list_model_await (tests), NULL);
name_to_suite = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
suites = g_list_store_new (FOUNDRY_TYPE_TEST_SUITE);
n_tests = g_list_model_get_n_items (tests);
for (guint i = 0; i < n_tests; i++)
{
g_autoptr(FoundryTest) test = g_list_model_get_item (tests, i);
g_auto(GStrv) suite_names = foundry_test_dup_suites (test);
if (suite_names == NULL || suite_names[0] == NULL)
{
add_to_suite (suites, name_to_suite, "", test);
continue;
}
for (guint j = 0; suite_names[j]; j++)
add_to_suite (suites, name_to_suite, suite_names[j], test);
}
return dex_future_new_take_object (g_steal_pointer (&suites));
}
/**
* foundry_test_manager_list_suites:
* @self: a [class@Foundry.TestManager]
*
* Loads available tests and groups them within a [class@Foundry.TestSuite].
*
* Returns: (transfer full): a [class@Dex.Future] that resolves to a
* [iface@Gio.ListModel] of [class@Foundry.TestSuite].
*
* Since: 1.1
*/
DexFuture *
foundry_test_manager_list_suites (FoundryTestManager *self)
{
dex_return_error_if_fail (FOUNDRY_IS_TEST_MANAGER (self));
return dex_scheduler_spawn (NULL, 0,
foundry_test_manager_list_suites_fiber,
g_object_ref (self),
g_object_unref);
}
|