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

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