root/tests/lib/vfs/current_dir.c

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

DEFINITIONS

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

   1 /*
   2    lib/vfs - manipulate with current directory
   3 
   4    Copyright (C) 2011-2025
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Slava Zanko <slavazanko@gmail.com>, 2011, 2013
   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/vfs"
  27 
  28 #include "tests/mctest.h"
  29 
  30 #include <string.h>  // memset()
  31 
  32 #include "lib/global.h"
  33 #include "lib/strutil.h"
  34 #include "lib/vfs/xdirentry.h"
  35 #include "lib/util.h"
  36 
  37 #include "src/vfs/local/local.c"
  38 
  39 static struct vfs_s_subclass vfs_test_subclass;
  40 static struct vfs_class *vfs_test_ops = VFS_CLASS (&vfs_test_subclass);
  41 
  42 /* --------------------------------------------------------------------------------------------- */
  43 
  44 /* @Mock */
  45 static int
  46 test_chdir (const vfs_path_t *vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
  47 {
  48     (void) vpath;
  49 
  50     return 0;
  51 }
  52 
  53 /* --------------------------------------------------------------------------------------------- */
  54 
  55 /* @Before */
  56 static void
  57 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  58 {
  59     str_init_strings (NULL);
  60 
  61     vfs_init ();
  62     vfs_init_localfs ();
  63     vfs_setup_work_dir ();
  64 
  65     memset (&vfs_test_subclass, 0, sizeof (vfs_test_subclass));
  66     vfs_init_class (vfs_test_ops, "testfs", VFSF_UNKNOWN, "test");
  67     vfs_test_ops->chdir = test_chdir;
  68 }
  69 
  70 /* --------------------------------------------------------------------------------------------- */
  71 
  72 /* @After */
  73 static void
  74 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  75 {
  76     vfs_shut ();
  77     str_uninit_strings ();
  78 }
  79 
  80 /* --------------------------------------------------------------------------------------------- */
  81 
  82 /* @DataSource("test_cd_ds") */
  83 static const struct test_cd_ds
  84 {
  85     const char *input_initial_path;
  86     const char *input_cd_path;
  87     const vfs_flags_t input_class_flags;
  88 
  89     const char *expected_cd_path;
  90 } test_cd_ds[] = {
  91     {
  92         // 0.
  93         "/",
  94         "/dev/some.file/test://",
  95         VFSF_NOLINKS,
  96         "/dev/some.file/test://",
  97     },
  98     {
  99         // 1.
 100         "/",
 101         "/dev/some.file/test://bla-bla",
 102         VFSF_NOLINKS,
 103         "/dev/some.file/test://bla-bla",
 104     },
 105     {
 106         // 2.
 107         "/dev/some.file/test://bla-bla",
 108         "..",
 109         VFSF_NOLINKS,
 110         "/dev/some.file/test://",
 111     },
 112     {
 113         // 3.
 114         "/dev/some.file/test://",
 115         "..",
 116         VFSF_NOLINKS,
 117         "/dev",
 118     },
 119     {
 120         // 4.
 121         "/dev",
 122         "..",
 123         VFSF_NOLINKS,
 124         "/",
 125     },
 126     {
 127         // 5.
 128         "/",
 129         "..",
 130         VFSF_NOLINKS,
 131         "/",
 132     },
 133     {
 134         // 6.
 135         "/",
 136         "/test://user:pass@host.net/path",
 137         VFSF_NOLINKS | VFSF_REMOTE,
 138         "/test://user:pass@host.net/path",
 139     },
 140     {
 141         // 7.
 142         "/test://user:pass@host.net/path",
 143         "..",
 144         VFSF_NOLINKS | VFSF_REMOTE,
 145         "/test://user:pass@host.net/",
 146     },
 147     {
 148         // 8.
 149         "/test://user:pass@host.net/",
 150         "..",
 151         VFSF_NOLINKS | VFSF_REMOTE,
 152         "/",
 153     },
 154 };
 155 
 156 /* @Test(dataSource = "test_cd_ds") */
 157 START_PARAMETRIZED_TEST (test_cd, test_cd_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 158 {
 159     // given
 160     vfs_path_t *vpath;
 161 
 162     vfs_test_ops->flags = data->input_class_flags;
 163     vfs_register_class (vfs_test_ops);
 164 
 165     vfs_set_raw_current_dir (vfs_path_from_str (data->input_initial_path));
 166 
 167     vpath = vfs_path_from_str (data->input_cd_path);
 168 
 169     // when
 170     mc_chdir (vpath);
 171 
 172     // then
 173     {
 174         char *actual_cd_path;
 175 
 176         actual_cd_path = vfs_get_cwd ();
 177         mctest_assert_str_eq (actual_cd_path, data->expected_cd_path);
 178         g_free (actual_cd_path);
 179     }
 180     vfs_path_free (vpath, TRUE);
 181 
 182     vfs_unregister_class (vfs_test_ops);
 183 }
 184 END_PARAMETRIZED_TEST
 185 
 186 /* --------------------------------------------------------------------------------------------- */
 187 
 188 int
 189 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 190 {
 191     TCase *tc_core;
 192     char *cwd;
 193 
 194     tc_core = tcase_create ("Core");
 195 
 196     // writable directory where check creates temporary files
 197     cwd = my_get_current_dir ();
 198     g_setenv ("TEMP", cwd, TRUE);
 199     g_free (cwd);
 200 
 201     tcase_add_checked_fixture (tc_core, setup, teardown);
 202 
 203     // Add new tests here: ***************
 204     mctest_add_parameterized_test (tc_core, test_cd, test_cd_ds);
 205     // ***********************************
 206 
 207     return mctest_run_all (tc_core);
 208 }
 209 
 210 /* --------------------------------------------------------------------------------------------- */

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