root/tests/lib/vfs/relative_cd.c

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

DEFINITIONS

This source file includes following definitions.
  1. test_chdir
  2. test_chdir__init
  3. test_chdir__deinit
  4. setup
  5. teardown
  6. START_PARAMETRIZED_TEST
  7. START_TEST
  8. main

   1 /* lib/vfs - test vfs_path_t manipulation functions
   2 
   3    Copyright (C) 2011-2019
   4    Free Software Foundation, Inc.
   5 
   6    Written by:
   7    Slava Zanko <slavazanko@gmail.com>, 2011, 2013
   8 
   9    This file is part of the Midnight Commander.
  10 
  11    The Midnight Commander is free software: you can redistribute it
  12    and/or modify it under the terms of the GNU General Public License as
  13    published by the Free Software Foundation, either version 3 of the License,
  14    or (at your option) any later version.
  15 
  16    The Midnight Commander is distributed in the hope that it will be useful,
  17    but WITHOUT ANY WARRANTY; without even the implied warranty of
  18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19    GNU General Public License for more details.
  20 
  21    You should have received a copy of the GNU General Public License
  22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  23  */
  24 
  25 #define TEST_SUITE_NAME "/lib/vfs"
  26 
  27 #include "tests/mctest.h"
  28 
  29 #include "lib/strutil.h"
  30 #include "lib/vfs/xdirentry.h"
  31 #include "lib/vfs/path.h"
  32 
  33 #include "src/vfs/local/local.c"
  34 
  35 
  36 static struct vfs_class vfs_test_ops1;
  37 
  38 static int test_chdir (const vfs_path_t * vpath);
  39 
  40 /* --------------------------------------------------------------------------------------------- */
  41 
  42 /* @CapturedValue */
  43 static vfs_path_t *test_chdir__vpath__captured;
  44 /* @ThenReturnValue */
  45 static int test_chdir__return_value;
  46 
  47 /* @Mock */
  48 static int
  49 test_chdir (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
  50 {
  51     test_chdir__vpath__captured = vfs_path_clone (vpath);
  52     return test_chdir__return_value;
  53 }
  54 
  55 static void
  56 test_chdir__init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  57 {
  58     test_chdir__vpath__captured = NULL;
  59 }
  60 
  61 static void
  62 test_chdir__deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  63 {
  64     vfs_path_free (test_chdir__vpath__captured);
  65 }
  66 
  67 /* --------------------------------------------------------------------------------------------- */
  68 
  69 /* @Before */
  70 static void
  71 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  72 {
  73     str_init_strings (NULL);
  74 
  75     vfs_init ();
  76     init_localfs ();
  77     vfs_setup_work_dir ();
  78 
  79     vfs_init_class (&vfs_test_ops1, "testfs1", VFS_NOLINKS | VFS_REMOTE, "test1");
  80     vfs_test_ops1.chdir = test_chdir;
  81     vfs_register_class (&vfs_test_ops1);
  82 
  83     mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  84 
  85     vfs_local_ops.chdir = test_chdir;
  86 
  87     test_chdir__init ();
  88 }
  89 
  90 /* --------------------------------------------------------------------------------------------- */
  91 
  92 /* @After */
  93 static void
  94 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  95 {
  96     test_chdir__deinit ();
  97 
  98     vfs_shut ();
  99     str_uninit_strings ();
 100 }
 101 
 102 /* --------------------------------------------------------------------------------------------- */
 103 /* @DataSource("test_relative_cd_ds") */
 104 /* *INDENT-OFF* */
 105 static const struct test_relative_cd_ds
 106 {
 107     const char *input_string;
 108     const vfs_path_flag_t input_flags;
 109     const char *expected_element_path;
 110 } test_relative_cd_ds[] =
 111 {
 112     { /* 0. */
 113         "/test1://user:pass@some.host:12345/path/to/dir",
 114         VPF_NONE,
 115         "path/to/dir"
 116     },
 117     { /* 1. */
 118         "some-non-exists-dir",
 119         VPF_NO_CANON,
 120         "some-non-exists-dir"
 121     },
 122 };
 123 /* *INDENT-ON* */
 124 
 125 /* @Test(dataSource = "test_relative_cd_ds") */
 126 /* *INDENT-OFF* */
 127 START_PARAMETRIZED_TEST (test_relative_cd, test_relative_cd_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 128 /* *INDENT-ON* */
 129 {
 130     /* given */
 131     vfs_path_t *vpath;
 132     int actual_result;
 133 
 134     test_chdir__return_value = 0;
 135 
 136     vpath = vfs_path_from_str_flags (data->input_string, data->input_flags);
 137 
 138     /* when */
 139     actual_result = mc_chdir (vpath);
 140 
 141     /* then */
 142     {
 143         const vfs_path_element_t *element;
 144 
 145         mctest_assert_int_eq (actual_result, 0);
 146         element = vfs_path_get_by_index (vpath, -1);
 147         mctest_assert_str_eq (element->path, data->expected_element_path);
 148         vfs_path_free (vpath);
 149     }
 150 }
 151 /* *INDENT-OFF* */
 152 END_PARAMETRIZED_TEST
 153 /* *INDENT-ON* */
 154 
 155 /* --------------------------------------------------------------------------------------------- */
 156 
 157 /* Relative to panel_correct_path_to_show()  */
 158 
 159 /* @Test */
 160 /* *INDENT-OFF* */
 161 START_TEST (test_vpath_to_str_filter)
     /* [previous][next][first][last][top][bottom][index][help]  */
 162 /* *INDENT-ON* */
 163 {
 164     /* given */
 165     vfs_path_t *vpath, *last_vpath;
 166     char *filtered_path;
 167     const vfs_path_element_t *path_element;
 168 
 169     /* when */
 170     vpath = vfs_path_from_str ("/test1://some.host/dir");
 171     path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, -1));
 172     vfs_path_free (vpath);
 173 
 174     last_vpath = vfs_path_new ();
 175     last_vpath->relative = TRUE;
 176 
 177     vfs_path_add_element (last_vpath, path_element);
 178 
 179     filtered_path = vfs_path_to_str_flags (last_vpath, 0,
 180                                            VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
 181 
 182     /* then */
 183     mctest_assert_str_eq (filtered_path, "test1://some.host/dir");
 184 
 185     vfs_path_free (last_vpath);
 186     g_free (filtered_path);
 187 }
 188 /* *INDENT-OFF* */
 189 END_TEST
 190 /* *INDENT-ON* */
 191 
 192 /* --------------------------------------------------------------------------------------------- */
 193 
 194 int
 195 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 196 {
 197     int number_failed;
 198     char *cwd;
 199 
 200     Suite *s = suite_create (TEST_SUITE_NAME);
 201     TCase *tc_core = tcase_create ("Core");
 202     SRunner *sr;
 203 
 204     /* writable directory where check creates temporary files */
 205     cwd = g_get_current_dir ();
 206     g_setenv ("TEMP", cwd, TRUE);
 207     g_free (cwd);
 208 
 209     tcase_add_checked_fixture (tc_core, setup, teardown);
 210 
 211     /* Add new tests here: *************** */
 212     mctest_add_parameterized_test (tc_core, test_relative_cd, test_relative_cd_ds);
 213     tcase_add_test (tc_core, test_vpath_to_str_filter);
 214     /* *********************************** */
 215 
 216     suite_add_tcase (s, tc_core);
 217     sr = srunner_create (s);
 218     srunner_set_log (sr, "relative_cd.log");
 219     srunner_run_all (sr, CK_ENV);
 220     number_failed = srunner_ntests_failed (sr);
 221     srunner_free (sr);
 222     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 223 }
 224 
 225 /* --------------------------------------------------------------------------------------------- */

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