LCOV - code coverage report
Current view: top level - girepository/tests - repository-search-paths.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 100.0 % 74 74
Test Date: 2024-11-26 05:23:01 Functions: 100.0 % 6 6
Branches: - 0 0

             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 char *
      26                 :           3 : test_repository_search_paths_get_expected_libdir_path (void)
      27                 :             : {
      28                 :             : #if defined(G_PLATFORM_WIN32)
      29                 :             :   const char *tests_build_dir = g_getenv ("G_TEST_BUILDDIR");
      30                 :             :   char *expected_rel_path = g_build_filename (tests_build_dir, "lib", "girepository-1.0", NULL);
      31                 :             : #elif defined(__APPLE__)
      32                 :             :   const char *tests_build_dir = g_getenv ("G_TEST_BUILDDIR");
      33                 :             :   char *expected_rel_path = g_build_filename (tests_build_dir, "..", "girepository-1.0", NULL);
      34                 :             : #else /* !G_PLATFORM_WIN32 && !__APPLE__ */
      35                 :           3 :   char *expected_rel_path = g_build_filename (GOBJECT_INTROSPECTION_LIBDIR, "girepository-1.0", NULL);
      36                 :             : #endif
      37                 :           3 :   char *expected_path = g_canonicalize_filename (expected_rel_path, NULL);
      38                 :           3 :   g_clear_pointer (&expected_rel_path, g_free);
      39                 :           3 :   return expected_path;
      40                 :             : }
      41                 :             : 
      42                 :             : static void
      43                 :           1 : test_repository_search_paths_default (void)
      44                 :             : {
      45                 :             :   const char * const *search_paths;
      46                 :             :   size_t n_search_paths;
      47                 :           1 :   GIRepository *repository = NULL;
      48                 :             : 
      49                 :           1 :   repository = gi_repository_new ();
      50                 :             : 
      51                 :           1 :   search_paths = gi_repository_get_search_path (repository, &n_search_paths);
      52                 :           1 :   g_assert_nonnull (search_paths);
      53                 :           1 :   g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 2);
      54                 :             : 
      55                 :           1 :   g_assert_cmpstr (search_paths[0], ==, g_get_tmp_dir ());
      56                 :             : 
      57                 :           1 :   char *expected_path = test_repository_search_paths_get_expected_libdir_path ();
      58                 :           1 :   g_assert_cmpstr (search_paths[1], ==, expected_path);
      59                 :           1 :   g_clear_pointer (&expected_path, g_free);
      60                 :             : 
      61                 :           1 :   g_clear_object (&repository);
      62                 :           1 : }
      63                 :             : 
      64                 :             : static void
      65                 :           1 : test_repository_search_paths_prepend (void)
      66                 :             : {
      67                 :             :   const char * const *search_paths;
      68                 :             :   size_t n_search_paths;
      69                 :           1 :   GIRepository *repository = NULL;
      70                 :             : 
      71                 :           1 :   repository = gi_repository_new ();
      72                 :             : 
      73                 :           1 :   gi_repository_prepend_search_path (repository, g_test_get_dir (G_TEST_BUILT));
      74                 :           1 :   search_paths = gi_repository_get_search_path (repository, &n_search_paths);
      75                 :           1 :   g_assert_nonnull (search_paths);
      76                 :           1 :   g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 3);
      77                 :             : 
      78                 :           1 :   g_assert_cmpstr (search_paths[0], ==, g_test_get_dir (G_TEST_BUILT));
      79                 :           1 :   g_assert_cmpstr (search_paths[1], ==, g_get_tmp_dir ());
      80                 :             : 
      81                 :           1 :   char *expected_path = test_repository_search_paths_get_expected_libdir_path ();
      82                 :           1 :   g_assert_cmpstr (search_paths[2], ==, expected_path);
      83                 :           1 :   g_clear_pointer (&expected_path, g_free);
      84                 :             : 
      85                 :           1 :   gi_repository_prepend_search_path (repository, g_test_get_dir (G_TEST_DIST));
      86                 :           1 :   search_paths = gi_repository_get_search_path (repository, &n_search_paths);
      87                 :           1 :   g_assert_nonnull (search_paths);
      88                 :           1 :   g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 4);
      89                 :             : 
      90                 :           1 :   g_assert_cmpstr (search_paths[0], ==, g_test_get_dir (G_TEST_DIST));
      91                 :           1 :   g_assert_cmpstr (search_paths[1], ==, g_test_get_dir (G_TEST_BUILT));
      92                 :           1 :   g_assert_cmpstr (search_paths[2], ==, g_get_tmp_dir ());
      93                 :             : 
      94                 :           1 :   expected_path = test_repository_search_paths_get_expected_libdir_path ();
      95                 :           1 :   g_assert_cmpstr (search_paths[3], ==, expected_path);
      96                 :           1 :   g_clear_pointer (&expected_path, g_free);
      97                 :             : 
      98                 :           1 :   g_clear_object (&repository);
      99                 :           1 : }
     100                 :             : 
     101                 :             : static void
     102                 :           1 : test_repository_library_paths_default (void)
     103                 :             : {
     104                 :             :   const char * const *library_paths;
     105                 :             :   size_t n_library_paths;
     106                 :           1 :   GIRepository *repository = NULL;
     107                 :             : 
     108                 :           1 :   repository = gi_repository_new ();
     109                 :             : 
     110                 :           1 :   library_paths = gi_repository_get_library_path (repository, &n_library_paths);
     111                 :           1 :   g_assert_nonnull (library_paths);
     112                 :           1 :   g_assert_cmpuint (g_strv_length ((char **) library_paths), ==, 0);
     113                 :             : 
     114                 :           1 :   g_clear_object (&repository);
     115                 :           1 : }
     116                 :             : 
     117                 :             : static void
     118                 :           1 : test_repository_library_paths_prepend (void)
     119                 :             : {
     120                 :             :   const char * const *library_paths;
     121                 :             :   size_t n_library_paths;
     122                 :           1 :   GIRepository *repository = NULL;
     123                 :             : 
     124                 :           1 :   repository = gi_repository_new ();
     125                 :             : 
     126                 :           1 :   gi_repository_prepend_library_path (repository, g_test_get_dir (G_TEST_BUILT));
     127                 :           1 :   library_paths = gi_repository_get_library_path (repository, &n_library_paths);
     128                 :           1 :   g_assert_nonnull (library_paths);
     129                 :           1 :   g_assert_cmpuint (g_strv_length ((char **) library_paths), ==, 1);
     130                 :             : 
     131                 :           1 :   g_assert_cmpstr (library_paths[0], ==, g_test_get_dir (G_TEST_BUILT));
     132                 :             : 
     133                 :           1 :   gi_repository_prepend_library_path (repository, g_test_get_dir (G_TEST_DIST));
     134                 :           1 :   library_paths = gi_repository_get_library_path (repository, &n_library_paths);
     135                 :           1 :   g_assert_nonnull (library_paths);
     136                 :           1 :   g_assert_cmpuint (g_strv_length ((char **) library_paths), ==, 2);
     137                 :             : 
     138                 :           1 :   g_assert_cmpstr (library_paths[0], ==, g_test_get_dir (G_TEST_DIST));
     139                 :           1 :   g_assert_cmpstr (library_paths[1], ==, g_test_get_dir (G_TEST_BUILT));
     140                 :             : 
     141                 :           1 :   g_clear_object (&repository);
     142                 :           1 : }
     143                 :             : 
     144                 :             : int
     145                 :           1 : main (int   argc,
     146                 :             :       char *argv[])
     147                 :             : {
     148                 :           1 :   g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
     149                 :             : 
     150                 :             :   /* Isolate from the system typelibs and GIRs. */
     151                 :           1 :   g_setenv ("GI_TYPELIB_PATH", g_get_tmp_dir (), TRUE);
     152                 :           1 :   g_setenv ("GI_GIR_PATH", g_get_user_cache_dir (), TRUE);
     153                 :             : 
     154                 :           1 :   g_test_add_func ("/repository/search-paths/default", test_repository_search_paths_default);
     155                 :           1 :   g_test_add_func ("/repository/search-paths/prepend", test_repository_search_paths_prepend);
     156                 :           1 :   g_test_add_func ("/repository/library-paths/default", test_repository_library_paths_default);
     157                 :           1 :   g_test_add_func ("/repository/library-paths/prepend", test_repository_library_paths_prepend);
     158                 :             : 
     159                 :           1 :   return g_test_run ();
     160                 :             : }
        

Generated by: LCOV version 2.0-1