(root)/
glib-2.79.0/
girepository/
tests/
repository-search-paths.c
       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  /* Keep this test first, not to add search paths to other tests */
      26  static void
      27  test_repository_search_paths_unset (void)
      28  {
      29    const char * const *search_paths;
      30    size_t n_search_paths;
      31  
      32    search_paths = gi_repository_get_search_path (&n_search_paths);
      33    g_assert_nonnull (search_paths);
      34    g_assert_cmpstrv (search_paths, ((char *[]){NULL}));
      35    g_assert_cmpuint (n_search_paths, ==, 0);
      36  
      37    search_paths = gi_repository_get_search_path (NULL);
      38    g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 0);
      39  }
      40  
      41  static void
      42  test_repository_search_paths_default (void)
      43  {
      44    const char * const *search_paths;
      45    size_t n_search_paths;
      46  
      47    search_paths = gi_repository_get_search_path (&n_search_paths);
      48    g_assert_nonnull (search_paths);
      49  
      50    /* Init default paths */
      51    g_assert_nonnull (gi_repository_get_default ());
      52  
      53    search_paths = gi_repository_get_search_path (&n_search_paths);
      54    g_assert_nonnull (search_paths);
      55    g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 2);
      56  
      57    g_assert_cmpstr (search_paths[0], ==, g_get_tmp_dir ());
      58  
      59  #ifndef G_PLATFORM_WIN32
      60    char *expected_path = g_build_filename (GOBJECT_INTROSPECTION_LIBDIR, "girepository-1.0", NULL);
      61    g_assert_cmpstr (search_paths[1], ==, expected_path);
      62    g_clear_pointer (&expected_path, g_free);
      63  #endif
      64  }
      65  
      66  static void
      67  test_repository_search_paths_prepend (void)
      68  {
      69    const char * const *search_paths;
      70    size_t n_search_paths;
      71  
      72    gi_repository_prepend_search_path (g_test_get_dir (G_TEST_BUILT));
      73    search_paths = gi_repository_get_search_path (&n_search_paths);
      74    g_assert_nonnull (search_paths);
      75    g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 3);
      76  
      77    g_assert_cmpstr (search_paths[0], ==, g_test_get_dir (G_TEST_BUILT));
      78    g_assert_cmpstr (search_paths[1], ==, g_get_tmp_dir ());
      79  
      80  #ifndef G_PLATFORM_WIN32
      81    char *expected_path = g_build_filename (GOBJECT_INTROSPECTION_LIBDIR, "girepository-1.0", NULL);
      82    g_assert_cmpstr (search_paths[2], ==, expected_path);
      83    g_clear_pointer (&expected_path, g_free);
      84  #endif
      85  
      86    gi_repository_prepend_search_path (g_test_get_dir (G_TEST_DIST));
      87    search_paths = gi_repository_get_search_path (&n_search_paths);
      88    g_assert_nonnull (search_paths);
      89    g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 4);
      90  
      91    g_assert_cmpstr (search_paths[0], ==, g_test_get_dir (G_TEST_DIST));
      92    g_assert_cmpstr (search_paths[1], ==, g_test_get_dir (G_TEST_BUILT));
      93    g_assert_cmpstr (search_paths[2], ==, g_get_tmp_dir ());
      94  
      95  #ifndef G_PLATFORM_WIN32
      96    expected_path = g_build_filename (GOBJECT_INTROSPECTION_LIBDIR, "girepository-1.0", NULL);
      97    g_assert_cmpstr (search_paths[3], ==, expected_path);
      98    g_clear_pointer (&expected_path, g_free);
      99  #endif
     100  }
     101  
     102  int
     103  main (int   argc,
     104        char *argv[])
     105  {
     106    g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
     107  
     108    /* Isolate from the system typelibs and GIRs. */
     109    g_setenv ("GI_TYPELIB_PATH", g_get_tmp_dir (), TRUE);
     110    g_setenv ("GI_GIR_PATH", g_get_user_cache_dir (), TRUE);
     111  
     112    g_test_add_func ("/repository-search-paths/unset", test_repository_search_paths_unset);
     113    g_test_add_func ("/repository-search-paths/default", test_repository_search_paths_default);
     114    g_test_add_func ("/repository-search-paths/prepend", test_repository_search_paths_prepend);
     115  
     116    return g_test_run ();
     117  }