girepository: Move search and library paths into GIRepository

Rather than them being set and stored globally, make them members of
`GIRepository`. This helps us move away from the concept of a global
singleton `GIRepository`.

This is slightly complicated by the fact that the library paths are
needed within the module loading code in `GITypelib`, but at that point
the `GITypelib` doesn’t have access to its parent `GIRepository` to call
`gi_repository_get_library_path()`, so we have to cache them in
`typelib->library_paths`.

It also means that it’s no longer possible to retrieve the ‘unset’ paths
from the globals, so the test for that is removed from
`repository-search-paths.c`.

This commit makes some API breaks, but that’s OK because libgirepository
has not been in a stable release yet.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3155
This commit is contained in:
Philip Withnall
2024-01-26 10:21:23 +00:00
parent 5ef1c7e110
commit 80f9153a7f
7 changed files with 134 additions and 101 deletions

View File

@@ -22,35 +22,22 @@
#include "glib.h"
#include "girepository.h"
/* Keep this test first, not to add search paths to other tests */
static void
test_repository_search_paths_unset (void)
{
const char * const *search_paths;
size_t n_search_paths;
search_paths = gi_repository_get_search_path (&n_search_paths);
g_assert_nonnull (search_paths);
g_assert_cmpstrv (search_paths, ((char *[]){NULL}));
g_assert_cmpuint (n_search_paths, ==, 0);
search_paths = gi_repository_get_search_path (NULL);
g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 0);
}
static void
test_repository_search_paths_default (void)
{
const char * const *search_paths;
size_t n_search_paths;
GIRepository *repository = NULL;
search_paths = gi_repository_get_search_path (&n_search_paths);
repository = gi_repository_new ();
search_paths = gi_repository_get_search_path (repository, &n_search_paths);
g_assert_nonnull (search_paths);
/* Init default paths */
g_assert_nonnull (gi_repository_get_default ());
search_paths = gi_repository_get_search_path (&n_search_paths);
search_paths = gi_repository_get_search_path (repository, &n_search_paths);
g_assert_nonnull (search_paths);
g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 2);
@@ -61,6 +48,8 @@ test_repository_search_paths_default (void)
g_assert_cmpstr (search_paths[1], ==, expected_path);
g_clear_pointer (&expected_path, g_free);
#endif
g_clear_object (&repository);
}
static void
@@ -68,9 +57,12 @@ test_repository_search_paths_prepend (void)
{
const char * const *search_paths;
size_t n_search_paths;
GIRepository *repository = NULL;
gi_repository_prepend_search_path (g_test_get_dir (G_TEST_BUILT));
search_paths = gi_repository_get_search_path (&n_search_paths);
repository = gi_repository_new ();
gi_repository_prepend_search_path (repository, g_test_get_dir (G_TEST_BUILT));
search_paths = gi_repository_get_search_path (repository, &n_search_paths);
g_assert_nonnull (search_paths);
g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 3);
@@ -83,8 +75,8 @@ test_repository_search_paths_prepend (void)
g_clear_pointer (&expected_path, g_free);
#endif
gi_repository_prepend_search_path (g_test_get_dir (G_TEST_DIST));
search_paths = gi_repository_get_search_path (&n_search_paths);
gi_repository_prepend_search_path (repository, g_test_get_dir (G_TEST_DIST));
search_paths = gi_repository_get_search_path (repository, &n_search_paths);
g_assert_nonnull (search_paths);
g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 4);
@@ -97,6 +89,8 @@ test_repository_search_paths_prepend (void)
g_assert_cmpstr (search_paths[3], ==, expected_path);
g_clear_pointer (&expected_path, g_free);
#endif
g_clear_object (&repository);
}
static void
@@ -104,10 +98,15 @@ test_repository_library_paths_default (void)
{
const char * const *library_paths;
size_t n_library_paths;
GIRepository *repository = NULL;
library_paths = gi_repository_get_library_path (&n_library_paths);
repository = gi_repository_new ();
library_paths = gi_repository_get_library_path (repository, &n_library_paths);
g_assert_nonnull (library_paths);
g_assert_cmpuint (g_strv_length ((char **) library_paths), ==, 0);
g_clear_object (&repository);
}
static void
@@ -115,21 +114,26 @@ test_repository_library_paths_prepend (void)
{
const char * const *library_paths;
size_t n_library_paths;
GIRepository *repository = NULL;
gi_repository_prepend_library_path (g_test_get_dir (G_TEST_BUILT));
library_paths = gi_repository_get_library_path (&n_library_paths);
repository = gi_repository_new ();
gi_repository_prepend_library_path (repository, g_test_get_dir (G_TEST_BUILT));
library_paths = gi_repository_get_library_path (repository, &n_library_paths);
g_assert_nonnull (library_paths);
g_assert_cmpuint (g_strv_length ((char **) library_paths), ==, 1);
g_assert_cmpstr (library_paths[0], ==, g_test_get_dir (G_TEST_BUILT));
gi_repository_prepend_library_path (g_test_get_dir (G_TEST_DIST));
library_paths = gi_repository_get_library_path (&n_library_paths);
gi_repository_prepend_library_path (repository, g_test_get_dir (G_TEST_DIST));
library_paths = gi_repository_get_library_path (repository, &n_library_paths);
g_assert_nonnull (library_paths);
g_assert_cmpuint (g_strv_length ((char **) library_paths), ==, 2);
g_assert_cmpstr (library_paths[0], ==, g_test_get_dir (G_TEST_DIST));
g_assert_cmpstr (library_paths[1], ==, g_test_get_dir (G_TEST_BUILT));
g_clear_object (&repository);
}
int
@@ -142,7 +146,6 @@ main (int argc,
g_setenv ("GI_TYPELIB_PATH", g_get_tmp_dir (), TRUE);
g_setenv ("GI_GIR_PATH", g_get_user_cache_dir (), TRUE);
g_test_add_func ("/repository/search-paths/unset", test_repository_search_paths_unset);
g_test_add_func ("/repository/search-paths/default", test_repository_search_paths_default);
g_test_add_func ("/repository/search-paths/prepend", test_repository_search_paths_prepend);
g_test_add_func ("/repository/library-paths/default", test_repository_library_paths_default);

View File

@@ -59,7 +59,7 @@ test_repository_basic (RepositoryFixture *fx,
g_assert_cmpstrv (versions, ((char *[]){"2.0", NULL}));
g_clear_pointer (&versions, g_strfreev);
search_paths = gi_repository_get_search_path (NULL);
search_paths = gi_repository_get_search_path (fx->repository, NULL);
g_assert_nonnull (search_paths);
g_assert_cmpuint (g_strv_length ((char **) search_paths), >, 0);
g_assert_cmpstr (search_paths[0], ==, fx->gobject_typelib_dir);

View File

@@ -43,13 +43,13 @@ repository_setup (RepositoryFixture *fx,
GError *local_error = NULL;
TypelibLoadSpec *load_spec = (TypelibLoadSpec *) data;
fx->gobject_typelib_dir = g_test_build_filename (G_TEST_BUILT, "..", "..", "introspection", NULL);
g_test_message ("Using GI_TYPELIB_DIR = %s", fx->gobject_typelib_dir);
gi_repository_prepend_search_path (fx->gobject_typelib_dir);
fx->repository = gi_repository_new ();
g_assert_nonnull (fx->repository);
fx->gobject_typelib_dir = g_test_build_filename (G_TEST_BUILT, "..", "..", "introspection", NULL);
g_test_message ("Using GI_TYPELIB_DIR = %s", fx->gobject_typelib_dir);
gi_repository_prepend_search_path (fx->repository, fx->gobject_typelib_dir);
if (load_spec)
{
typelib = gi_repository_require (fx->repository, load_spec->name, load_spec->version, 0, &local_error);