(root)/
glib-2.79.0/
glib/
tests/
utils-isolated.c
       1  /* Copyright (C) 2022 Marco Trevisan
       2   *
       3   * SPDX-License-Identifier: LGPL-2.1-or-later
       4   *
       5   * This library is free software; you can redistribute it and/or
       6   * modify it under the terms of the GNU Lesser General Public
       7   * License as published by the Free Software Foundation; either
       8   * version 2.1 of the License, or (at your option) any later version.
       9   *
      10   * This library is distributed in the hope that it will be useful,
      11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13   * Lesser General Public License for more details.
      14   *
      15   * You should have received a copy of the GNU Lesser General
      16   * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      17   *
      18   * Author: Marco Trevisan <marco.trevisan@canonical.com>
      19   */
      20  
      21  #include "config.h"
      22  
      23  #include <glib/glib.h>
      24  
      25  /* Test that all of the well-known directories returned by GLib
      26   * are returned as children of test_tmpdir when running with
      27   * %G_TEST_OPTION_ISOLATE_DIRS. This ensures that tests should
      28   * not interfere with each other in `/tmp` while running.
      29   */
      30  
      31  const char *test_tmpdir;
      32  
      33  static void
      34  test_tmp_dir (void)
      35  {
      36    g_assert_cmpstr (g_get_tmp_dir (), ==, test_tmpdir);
      37  }
      38  
      39  static void
      40  test_home_dir (void)
      41  {
      42    g_assert_true (g_str_has_prefix (g_get_home_dir (), test_tmpdir));
      43  }
      44  
      45  static void
      46  test_user_cache_dir (void)
      47  {
      48    g_assert_true (g_str_has_prefix (g_get_user_cache_dir (), test_tmpdir));
      49  }
      50  
      51  static void
      52  test_system_config_dirs (void)
      53  {
      54    const char *const *dir;
      55  
      56    for (dir = g_get_system_config_dirs (); *dir != NULL; dir++)
      57      g_assert_true (g_str_has_prefix (*dir, test_tmpdir));
      58  }
      59  
      60  static void
      61  test_user_config_dir (void)
      62  {
      63    g_assert_true (g_str_has_prefix (g_get_user_config_dir (), test_tmpdir));
      64  }
      65  
      66  static void
      67  test_system_data_dirs (void)
      68  {
      69    const char *const *dir;
      70  
      71    for (dir = g_get_system_data_dirs (); *dir != NULL; dir++)
      72      g_assert_true (g_str_has_prefix (*dir, test_tmpdir));
      73  }
      74  
      75  static void
      76  test_user_data_dir (void)
      77  {
      78    g_assert_true (g_str_has_prefix (g_get_user_data_dir (), test_tmpdir));
      79  }
      80  
      81  static void
      82  test_user_state_dir (void)
      83  {
      84    g_assert_true (g_str_has_prefix (g_get_user_state_dir (), test_tmpdir));
      85  }
      86  
      87  static void
      88  test_user_runtime_dir (void)
      89  {
      90    g_assert_true (g_str_has_prefix (g_get_user_runtime_dir (), test_tmpdir));
      91  }
      92  
      93  
      94  int
      95  main (int   argc,
      96        char *argv[])
      97  {
      98    g_setenv ("LC_ALL", "C", TRUE);
      99    g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
     100  
     101    test_tmpdir = g_getenv ("G_TEST_TMPDIR");
     102    g_assert_nonnull (test_tmpdir);
     103  
     104    g_test_add_func ("/utils-isolated/tmp-dir", test_tmp_dir);
     105    g_test_add_func ("/utils-isolated/home-dir", test_home_dir);
     106    g_test_add_func ("/utils-isolated/user-cache-dir", test_user_cache_dir);
     107    g_test_add_func ("/utils-isolated/system-config-dirs", test_system_config_dirs);
     108    g_test_add_func ("/utils-isolated/user-config-dir", test_user_config_dir);
     109    g_test_add_func ("/utils-isolated/system-data-dirs", test_system_data_dirs);
     110    g_test_add_func ("/utils-isolated/user-data-dir", test_user_data_dir);
     111    g_test_add_func ("/utils-isolated/user-state-dir", test_user_state_dir);
     112    g_test_add_func ("/utils-isolated/user-runtime-dir", test_user_runtime_dir);
     113    return g_test_run ();
     114  }