girepository: Use newer API to create null-terminated ptr array for dependencies

And add a test for this.
This commit is contained in:
Marco Trevisan (Treviño) 2023-12-20 21:02:11 +01:00
parent 2c00c7c924
commit 3ec0192970
2 changed files with 35 additions and 5 deletions

View File

@ -593,8 +593,8 @@ gi_repository_get_dependencies (GIRepository *repository,
transitive_dependencies);
/* Convert to a string array. */
out = g_ptr_array_new_full (g_hash_table_size (transitive_dependencies),
g_free);
out = g_ptr_array_new_null_terminated (g_hash_table_size (transitive_dependencies),
g_free, TRUE);
g_hash_table_iter_init (&iter, transitive_dependencies);
while (g_hash_table_iter_next (&iter, (gpointer) &dependency, NULL))
@ -605,9 +605,6 @@ gi_repository_get_dependencies (GIRepository *repository,
g_hash_table_unref (transitive_dependencies);
/* Add a NULL terminator. */
g_ptr_array_add (out, NULL);
return (gchar **) g_ptr_array_free (out, FALSE);
}

View File

@ -114,6 +114,38 @@ test_repository_info (void)
g_clear_object (&repository);
}
static void
test_repository_dependencies (void)
{
GIRepository *repository;
GITypelib *typelib;
GError *error = NULL;
char *gobject_typelib_dir = NULL;
char **dependencies;
g_test_summary ("Test ensures namespace dependencies are correctly exposed");
gobject_typelib_dir = g_test_build_filename (G_TEST_BUILT, "..", "..", "gobject", NULL);
g_test_message ("Using GI_TYPELIB_DIR = %s", gobject_typelib_dir);
gi_repository_prepend_search_path (gobject_typelib_dir);
g_free (gobject_typelib_dir);
repository = gi_repository_new ();
g_assert_nonnull (repository);
typelib = gi_repository_require (repository, "GObject", "2.0", 0, &error);
g_assert_no_error (error);
g_assert_nonnull (typelib);
dependencies = gi_repository_get_dependencies (repository, "GObject");
g_assert_cmpuint (g_strv_length (dependencies), ==, 1);
g_assert_true (g_strv_contains ((const char **) dependencies, "GLib-2.0"));
g_clear_error (&error);
g_clear_object (&repository);
g_clear_pointer (&dependencies, g_strfreev);
}
int
main (int argc,
char *argv[])
@ -126,6 +158,7 @@ main (int argc,
g_test_add_func ("/repository/basic", test_repository_basic);
g_test_add_func ("/repository/info", test_repository_info);
g_test_add_func ("/repository/dependencies", test_repository_dependencies);
return g_test_run ();
}