root/tests/lib/mc_realpath.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. setup
  2. teardown
  3. START_PARAMETRIZED_TEST
  4. main

   1 /*
   2    lib - realpath
   3 
   4    Copyright (C) 2017-2025
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Andrew Borodin <aborodin@vmail.ru>, 2017
   9 
  10    This file is part of the Midnight Commander.
  11 
  12    The Midnight Commander is free software: you can redistribute it
  13    and/or modify it under the terms of the GNU General Public License as
  14    published by the Free Software Foundation, either version 3 of the License,
  15    or (at your option) any later version.
  16 
  17    The Midnight Commander is distributed in the hope that it will be useful,
  18    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20    GNU General Public License for more details.
  21 
  22    You should have received a copy of the GNU General Public License
  23    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  24  */
  25 
  26 #define TEST_SUITE_NAME "/lib/util"
  27 
  28 #include "tests/mctest.h"
  29 
  30 #include "lib/strutil.h"
  31 #include "lib/vfs/vfs.h"  // VFS_ENCODING_PREFIX, vfs_init(), vfs_shut()
  32 #include "src/vfs/local/local.c"
  33 
  34 #include "lib/util.h"  // mc_realpath()
  35 
  36 /* --------------------------------------------------------------------------------------------- */
  37 
  38 static char resolved_path[PATH_MAX];
  39 
  40 /* --------------------------------------------------------------------------------------------- */
  41 
  42 /* @Before */
  43 static void
  44 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  45 {
  46     str_init_strings (NULL);
  47     vfs_init ();
  48     vfs_init_localfs ();
  49     vfs_setup_work_dir ();
  50 }
  51 
  52 /* @After */
  53 static void
  54 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  55 {
  56     vfs_shut ();
  57     str_uninit_strings ();
  58 }
  59 
  60 /* --------------------------------------------------------------------------------------------- */
  61 
  62 /* @DataSource("data_source") */
  63 static const struct data_source
  64 {
  65     const char *input_string;
  66     const char *expected_string;
  67 } data_source[] = {
  68     // absolute paths
  69     { "/", "/" },
  70     { "/usr/bin", "/usr/bin" },
  71 #ifdef HAVE_CHARSET
  72     { "/" VFS_ENCODING_PREFIX "UTF-8/", "/" },
  73     { "/" VFS_ENCODING_PREFIX "UTF-8/usr/bin", "/usr/bin" },
  74 #else
  75     { "/" VFS_ENCODING_PREFIX "UTF-8/", "/" VFS_ENCODING_PREFIX "UTF-8/" },
  76     { "/" VFS_ENCODING_PREFIX "UTF-8/usr/bin", "/" VFS_ENCODING_PREFIX "UTF-8/usr/bin" },
  77 #endif
  78 
  79     // relative paths are relative to /
  80     { "usr/bin", "/usr/bin" },
  81 #ifdef HAVE_CHARSET
  82     { VFS_ENCODING_PREFIX "UTF-8/", "/" },
  83     { VFS_ENCODING_PREFIX "UTF-8/usr/bin", "/usr/bin" },
  84 #else
  85     { VFS_ENCODING_PREFIX "UTF-8/", VFS_ENCODING_PREFIX "UTF-8/" },
  86     { VFS_ENCODING_PREFIX "UTF-8/usr/bin", VFS_ENCODING_PREFIX "UTF-8/usr/bin" },
  87 #endif
  88 };
  89 
  90 /* @Test(dataSource = "data_source") */
  91 START_PARAMETRIZED_TEST (realpath_test, data_source)
     /* [previous][next][first][last][top][bottom][index][help]  */
  92 {
  93     int ret;
  94 
  95     /* realpath(3) produces a canonicalized absolute pathname using current directory.
  96      * Change the current directory to produce correct pathname. */
  97     ret = chdir ("/");
  98 
  99     // when
 100     if (mc_realpath (data->input_string, resolved_path) == NULL)
 101         resolved_path[0] = '\0';
 102 
 103     // then
 104     mctest_assert_str_eq (resolved_path, data->expected_string);
 105 
 106     (void) ret;
 107 }
 108 END_PARAMETRIZED_TEST
 109 
 110 /* --------------------------------------------------------------------------------------------- */
 111 
 112 int
 113 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 114 {
 115     TCase *tc_core;
 116     char *cwd;
 117 
 118     tc_core = tcase_create ("Core");
 119 
 120     // writable directory where check creates temporary files
 121     cwd = my_get_current_dir ();
 122     g_setenv ("TEMP", cwd, TRUE);
 123     g_free (cwd);
 124 
 125     tcase_add_checked_fixture (tc_core, setup, teardown);
 126 
 127     // Add new tests here: ***************
 128     mctest_add_parameterized_test (tc_core, realpath_test, data_source);
 129     // ***********************************
 130 
 131     return mctest_run_all (tc_core);
 132 }
 133 
 134 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */