girepository: Use an array to iterate over and return the search paths

We used to store the search paths into a GSList but this is not
efficient for various reasons, so replace this with an array so that we
can replace return just a GStrv in the public API.
This commit is contained in:
Marco Trevisan (Treviño)
2023-12-20 06:29:40 +01:00
parent a8588b803e
commit 2c00c7c924
4 changed files with 99 additions and 79 deletions

View File

@@ -26,32 +26,39 @@
static void
test_repository_search_paths_unset (void)
{
GSList *search_paths;
const char * const *search_paths;
size_t n_search_paths;
search_paths = gi_repository_get_search_path ();
g_assert_null (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)
{
GSList *search_paths;
const char * const *search_paths;
size_t n_search_paths;
search_paths = gi_repository_get_search_path ();
g_assert_null (search_paths);
search_paths = gi_repository_get_search_path (&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 ();
search_paths = gi_repository_get_search_path (&n_search_paths);
g_assert_nonnull (search_paths);
g_assert_cmpuint (g_slist_length (search_paths), ==, 2);
g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 2);
g_assert_cmpstr (g_slist_nth_data (search_paths, 0), ==, g_get_tmp_dir ());
g_assert_cmpstr (search_paths[0], ==, g_get_tmp_dir ());
#ifndef G_PLATFORM_WIN32
char *expected_path = g_build_filename (GOBJECT_INTROSPECTION_LIBDIR, "girepository-1.0", NULL);
g_assert_cmpstr (g_slist_nth_data (search_paths, 1), ==, expected_path);
g_assert_cmpstr (search_paths[1], ==, expected_path);
g_clear_pointer (&expected_path, g_free);
#endif
}
@@ -59,34 +66,35 @@ test_repository_search_paths_default (void)
static void
test_repository_search_paths_prepend (void)
{
GSList *search_paths;
const char * const *search_paths;
size_t n_search_paths;
gi_repository_prepend_search_path (g_test_get_dir (G_TEST_BUILT));
search_paths = gi_repository_get_search_path ();
search_paths = gi_repository_get_search_path (&n_search_paths);
g_assert_nonnull (search_paths);
g_assert_cmpuint (g_slist_length (search_paths), ==, 3);
g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 3);
g_assert_cmpstr (g_slist_nth_data (search_paths, 0), ==, g_test_get_dir (G_TEST_BUILT));
g_assert_cmpstr (g_slist_nth_data (search_paths, 1), ==, g_get_tmp_dir ());
g_assert_cmpstr (search_paths[0], ==, g_test_get_dir (G_TEST_BUILT));
g_assert_cmpstr (search_paths[1], ==, g_get_tmp_dir ());
#ifndef G_PLATFORM_WIN32
char *expected_path = g_build_filename (GOBJECT_INTROSPECTION_LIBDIR, "girepository-1.0", NULL);
g_assert_cmpstr (g_slist_nth_data (search_paths, 2), ==, expected_path);
g_assert_cmpstr (search_paths[2], ==, expected_path);
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 ();
search_paths = gi_repository_get_search_path (&n_search_paths);
g_assert_nonnull (search_paths);
g_assert_cmpuint (g_slist_length (search_paths), ==, 4);
g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 4);
g_assert_cmpstr (g_slist_nth_data (search_paths, 0), ==, g_test_get_dir (G_TEST_DIST));
g_assert_cmpstr (g_slist_nth_data (search_paths, 1), ==, g_test_get_dir (G_TEST_BUILT));
g_assert_cmpstr (g_slist_nth_data (search_paths, 2), ==, g_get_tmp_dir ());
g_assert_cmpstr (search_paths[0], ==, g_test_get_dir (G_TEST_DIST));
g_assert_cmpstr (search_paths[1], ==, g_test_get_dir (G_TEST_BUILT));
g_assert_cmpstr (search_paths[2], ==, g_get_tmp_dir ());
#ifndef G_PLATFORM_WIN32
expected_path = g_build_filename (GOBJECT_INTROSPECTION_LIBDIR, "girepository-1.0", NULL);
g_assert_cmpstr (g_slist_nth_data (search_paths, 3), ==, expected_path);
g_assert_cmpstr (search_paths[3], ==, expected_path);
g_clear_pointer (&expected_path, g_free);
#endif
}

View File

@@ -27,7 +27,7 @@ test_repository_basic (void)
{
GIRepository *repository;
char *gobject_typelib_dir = NULL;
GSList *search_path;
const char * const * search_paths;
GITypelib *typelib = NULL;
char **namespaces = NULL;
const char *expected_namespaces[] = { "GLib", NULL };
@@ -55,9 +55,10 @@ test_repository_basic (void)
g_assert_cmpstrv (versions, ((char *[]){"2.0", NULL}));
g_clear_pointer (&versions, g_strfreev);
search_path = gi_repository_get_search_path ();
g_assert_nonnull (search_path);
g_assert_cmpstr (search_path->data, ==, gobject_typelib_dir);
search_paths = gi_repository_get_search_path (NULL);
g_assert_nonnull (search_paths);
g_assert_cmpuint (g_strv_length ((char **) search_paths), >, 0);
g_assert_cmpstr (search_paths[0], ==, gobject_typelib_dir);
typelib = gi_repository_require (repository, "GLib", "2.0", 0, &local_error);
g_assert_no_error (local_error);