Branch data Line data Source code
1 : : /*
2 : : * Copyright 2023 Canonical Ltd.
3 : : *
4 : : * SPDX-License-Identifier: LGPL-2.1-or-later
5 : : *
6 : : * This library is free software; you can redistribute it and/or
7 : : * modify it under the terms of the GNU Lesser General Public
8 : : * License as published by the Free Software Foundation; either
9 : : * version 2.1 of the License, or (at your option) any later version.
10 : : *
11 : : * This library is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : : * Lesser General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU Lesser General
17 : : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 : : *
19 : : * Author: Marco Trevisan <marco.trevisan@canonical.com>
20 : : */
21 : :
22 : : #include "glib.h"
23 : : #include "girepository.h"
24 : :
25 : : static void
26 : 1 : test_repository_search_paths_default (void)
27 : : {
28 : : const char * const *search_paths;
29 : : size_t n_search_paths;
30 : 1 : GIRepository *repository = NULL;
31 : :
32 : 1 : repository = gi_repository_new ();
33 : :
34 : 1 : search_paths = gi_repository_get_search_path (repository, &n_search_paths);
35 : 1 : g_assert_nonnull (search_paths);
36 : 1 : g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 2);
37 : :
38 : 1 : g_assert_cmpstr (search_paths[0], ==, g_get_tmp_dir ());
39 : :
40 : : #ifndef G_PLATFORM_WIN32
41 : 1 : char *expected_path = g_build_filename (GOBJECT_INTROSPECTION_LIBDIR, "girepository-1.0", NULL);
42 : 1 : g_assert_cmpstr (search_paths[1], ==, expected_path);
43 : 1 : g_clear_pointer (&expected_path, g_free);
44 : : #endif
45 : :
46 : 1 : g_clear_object (&repository);
47 : 1 : }
48 : :
49 : : static void
50 : 1 : test_repository_search_paths_prepend (void)
51 : : {
52 : : const char * const *search_paths;
53 : : size_t n_search_paths;
54 : 1 : GIRepository *repository = NULL;
55 : :
56 : 1 : repository = gi_repository_new ();
57 : :
58 : 1 : gi_repository_prepend_search_path (repository, g_test_get_dir (G_TEST_BUILT));
59 : 1 : search_paths = gi_repository_get_search_path (repository, &n_search_paths);
60 : 1 : g_assert_nonnull (search_paths);
61 : 1 : g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 3);
62 : :
63 : 1 : g_assert_cmpstr (search_paths[0], ==, g_test_get_dir (G_TEST_BUILT));
64 : 1 : g_assert_cmpstr (search_paths[1], ==, g_get_tmp_dir ());
65 : :
66 : : #ifndef G_PLATFORM_WIN32
67 : 1 : char *expected_path = g_build_filename (GOBJECT_INTROSPECTION_LIBDIR, "girepository-1.0", NULL);
68 : 1 : g_assert_cmpstr (search_paths[2], ==, expected_path);
69 : 1 : g_clear_pointer (&expected_path, g_free);
70 : : #endif
71 : :
72 : 1 : gi_repository_prepend_search_path (repository, g_test_get_dir (G_TEST_DIST));
73 : 1 : search_paths = gi_repository_get_search_path (repository, &n_search_paths);
74 : 1 : g_assert_nonnull (search_paths);
75 : 1 : g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 4);
76 : :
77 : 1 : g_assert_cmpstr (search_paths[0], ==, g_test_get_dir (G_TEST_DIST));
78 : 1 : g_assert_cmpstr (search_paths[1], ==, g_test_get_dir (G_TEST_BUILT));
79 : 1 : g_assert_cmpstr (search_paths[2], ==, g_get_tmp_dir ());
80 : :
81 : : #ifndef G_PLATFORM_WIN32
82 : 1 : expected_path = g_build_filename (GOBJECT_INTROSPECTION_LIBDIR, "girepository-1.0", NULL);
83 : 1 : g_assert_cmpstr (search_paths[3], ==, expected_path);
84 : 1 : g_clear_pointer (&expected_path, g_free);
85 : : #endif
86 : :
87 : 1 : g_clear_object (&repository);
88 : 1 : }
89 : :
90 : : static void
91 : 1 : test_repository_library_paths_default (void)
92 : : {
93 : : const char * const *library_paths;
94 : : size_t n_library_paths;
95 : 1 : GIRepository *repository = NULL;
96 : :
97 : 1 : repository = gi_repository_new ();
98 : :
99 : 1 : library_paths = gi_repository_get_library_path (repository, &n_library_paths);
100 : 1 : g_assert_nonnull (library_paths);
101 : 1 : g_assert_cmpuint (g_strv_length ((char **) library_paths), ==, 0);
102 : :
103 : 1 : g_clear_object (&repository);
104 : 1 : }
105 : :
106 : : static void
107 : 1 : test_repository_library_paths_prepend (void)
108 : : {
109 : : const char * const *library_paths;
110 : : size_t n_library_paths;
111 : 1 : GIRepository *repository = NULL;
112 : :
113 : 1 : repository = gi_repository_new ();
114 : :
115 : 1 : gi_repository_prepend_library_path (repository, g_test_get_dir (G_TEST_BUILT));
116 : 1 : library_paths = gi_repository_get_library_path (repository, &n_library_paths);
117 : 1 : g_assert_nonnull (library_paths);
118 : 1 : g_assert_cmpuint (g_strv_length ((char **) library_paths), ==, 1);
119 : :
120 : 1 : g_assert_cmpstr (library_paths[0], ==, g_test_get_dir (G_TEST_BUILT));
121 : :
122 : 1 : gi_repository_prepend_library_path (repository, g_test_get_dir (G_TEST_DIST));
123 : 1 : library_paths = gi_repository_get_library_path (repository, &n_library_paths);
124 : 1 : g_assert_nonnull (library_paths);
125 : 1 : g_assert_cmpuint (g_strv_length ((char **) library_paths), ==, 2);
126 : :
127 : 1 : g_assert_cmpstr (library_paths[0], ==, g_test_get_dir (G_TEST_DIST));
128 : 1 : g_assert_cmpstr (library_paths[1], ==, g_test_get_dir (G_TEST_BUILT));
129 : :
130 : 1 : g_clear_object (&repository);
131 : 1 : }
132 : :
133 : : int
134 : 1 : main (int argc,
135 : : char *argv[])
136 : : {
137 : 1 : g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
138 : :
139 : : /* Isolate from the system typelibs and GIRs. */
140 : 1 : g_setenv ("GI_TYPELIB_PATH", g_get_tmp_dir (), TRUE);
141 : 1 : g_setenv ("GI_GIR_PATH", g_get_user_cache_dir (), TRUE);
142 : :
143 : 1 : g_test_add_func ("/repository/search-paths/default", test_repository_search_paths_default);
144 : 1 : g_test_add_func ("/repository/search-paths/prepend", test_repository_search_paths_prepend);
145 : 1 : g_test_add_func ("/repository/library-paths/default", test_repository_library_paths_default);
146 : 1 : g_test_add_func ("/repository/library-paths/prepend", test_repository_library_paths_prepend);
147 : :
148 : 1 : return g_test_run ();
149 : : }
|