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-2024
   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 <http://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 /* *INDENT-OFF* */
  84 static const struct test_cd_ds
  85 {
  86     const char *input_initial_path;
  87     const char *input_cd_path;
  88     const vfs_flags_t input_class_flags;
  89 
  90     const char *expected_cd_path;
  91 } test_cd_ds[] =
  92 {
  93     { /* 0. */
  94         "/",
  95         "/dev/some.file/test://",
  96         VFSF_NOLINKS,
  97         "/dev/some.file/test://"
  98     },
  99     { /* 1. */
 100         "/",
 101         "/dev/some.file/test://bla-bla",
 102         VFSF_NOLINKS,
 103         "/dev/some.file/test://bla-bla"
 104     },
 105     { /* 2. */
 106         "/dev/some.file/test://bla-bla",
 107         "..",
 108         VFSF_NOLINKS,
 109         "/dev/some.file/test://"
 110     },
 111     { /* 3. */
 112         "/dev/some.file/test://",
 113         "..",
 114         VFSF_NOLINKS,
 115         "/dev"
 116     },
 117     { /* 4. */
 118         "/dev",
 119         "..",
 120         VFSF_NOLINKS,
 121         "/"
 122     },
 123     { /* 5. */
 124         "/",
 125         "..",
 126         VFSF_NOLINKS,
 127         "/"
 128     },
 129     { /* 6. */
 130         "/",
 131         "/test://user:pass@host.net/path",
 132         VFSF_NOLINKS | VFSF_REMOTE,
 133         "/test://user:pass@host.net/path"
 134     },
 135     { /* 7. */
 136         "/test://user:pass@host.net/path",
 137         "..",
 138         VFSF_NOLINKS | VFSF_REMOTE,
 139         "/test://user:pass@host.net/"
 140     },
 141     { /* 8. */
 142         "/test://user:pass@host.net/",
 143         "..",
 144         VFSF_NOLINKS | VFSF_REMOTE,
 145         "/"
 146     },
 147 };
 148 /* *INDENT-ON* */
 149 
 150 /* @Test(dataSource = "test_cd_ds") */
 151 /* *INDENT-OFF* */
 152 START_PARAMETRIZED_TEST (test_cd, test_cd_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 153 /* *INDENT-ON* */
 154 {
 155     /* given */
 156     vfs_path_t *vpath;
 157 
 158     vfs_test_ops->flags = data->input_class_flags;
 159     vfs_register_class (vfs_test_ops);
 160 
 161     vfs_set_raw_current_dir (vfs_path_from_str (data->input_initial_path));
 162 
 163     vpath = vfs_path_from_str (data->input_cd_path);
 164 
 165     /* when */
 166     mc_chdir (vpath);
 167 
 168     /* then */
 169     {
 170         char *actual_cd_path;
 171 
 172         actual_cd_path = vfs_get_cwd ();
 173         mctest_assert_str_eq (actual_cd_path, data->expected_cd_path);
 174         g_free (actual_cd_path);
 175     }
 176     vfs_path_free (vpath, TRUE);
 177 
 178     vfs_unregister_class (vfs_test_ops);
 179 }
 180 /* *INDENT-OFF* */
 181 END_PARAMETRIZED_TEST
 182 /* *INDENT-ON* */
 183 
 184 /* --------------------------------------------------------------------------------------------- */
 185 
 186 int
 187 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 188 {
 189     TCase *tc_core;
 190     char *cwd;
 191 
 192     tc_core = tcase_create ("Core");
 193 
 194     /* writable directory where check creates temporary files */
 195     cwd = my_get_current_dir ();
 196     g_setenv ("TEMP", cwd, TRUE);
 197     g_free (cwd);
 198 
 199     tcase_add_checked_fixture (tc_core, setup, teardown);
 200 
 201     /* Add new tests here: *************** */
 202     mctest_add_parameterized_test (tc_core, test_cd, test_cd_ds);
 203     /* *********************************** */
 204 
 205     return mctest_run_all (tc_core);
 206 }
 207 
 208 /* --------------------------------------------------------------------------------------------- */

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