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-2020
   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 <string.h>             /* memset() */
  30 
  31 #include "lib/strutil.h"
  32 #include "lib/vfs/xdirentry.h"
  33 #include "lib/vfs/path.h"
  34 
  35 #include "src/vfs/local/local.c"
  36 
  37 
  38 static struct vfs_s_subclass vfs_test_subclass1;
  39 static struct vfs_class *vfs_test_ops1 = VFS_CLASS (&vfs_test_subclass1);
  40 
  41 static int test_chdir (const vfs_path_t * vpath);
  42 
  43 /* --------------------------------------------------------------------------------------------- */
  44 
  45 /* @CapturedValue */
  46 static vfs_path_t *test_chdir__vpath__captured;
  47 /* @ThenReturnValue */
  48 static int test_chdir__return_value;
  49 
  50 /* @Mock */
  51 static int
  52 test_chdir (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
  53 {
  54     test_chdir__vpath__captured = vfs_path_clone (vpath);
  55     return test_chdir__return_value;
  56 }
  57 
  58 static void
  59 test_chdir__init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  60 {
  61     test_chdir__vpath__captured = NULL;
  62 }
  63 
  64 static void
  65 test_chdir__deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  66 {
  67     vfs_path_free (test_chdir__vpath__captured);
  68 }
  69 
  70 /* --------------------------------------------------------------------------------------------- */
  71 
  72 /* @Before */
  73 static void
  74 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  75 {
  76     mc_global.timer = mc_timer_new ();
  77     str_init_strings (NULL);
  78 
  79     vfs_init ();
  80     vfs_init_localfs ();
  81     vfs_setup_work_dir ();
  82 
  83     memset (&vfs_test_subclass1, 0, sizeof (vfs_test_subclass1));
  84     vfs_init_class (vfs_test_ops1, "testfs1", VFSF_NOLINKS | VFSF_REMOTE, "test1");
  85     vfs_test_ops1->chdir = test_chdir;
  86     vfs_register_class (vfs_test_ops1);
  87 
  88     mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  89 
  90     vfs_local_ops->chdir = test_chdir;
  91 
  92     test_chdir__init ();
  93 }
  94 
  95 /* --------------------------------------------------------------------------------------------- */
  96 
  97 /* @After */
  98 static void
  99 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 100 {
 101     test_chdir__deinit ();
 102 
 103     vfs_shut ();
 104     str_uninit_strings ();
 105     mc_timer_destroy (mc_global.timer);
 106 }
 107 
 108 /* --------------------------------------------------------------------------------------------- */
 109 /* @DataSource("test_relative_cd_ds") */
 110 /* *INDENT-OFF* */
 111 static const struct test_relative_cd_ds
 112 {
 113     const char *input_string;
 114     const vfs_path_flag_t input_flags;
 115     const char *expected_element_path;
 116 } test_relative_cd_ds[] =
 117 {
 118     { /* 0. */
 119         "/test1://user:pass@some.host:12345/path/to/dir",
 120         VPF_NONE,
 121         "path/to/dir"
 122     },
 123     { /* 1. */
 124         "some-non-exists-dir",
 125         VPF_NO_CANON,
 126         "some-non-exists-dir"
 127     },
 128 };
 129 /* *INDENT-ON* */
 130 
 131 /* @Test(dataSource = "test_relative_cd_ds") */
 132 /* *INDENT-OFF* */
 133 START_PARAMETRIZED_TEST (test_relative_cd, test_relative_cd_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 134 /* *INDENT-ON* */
 135 {
 136     /* given */
 137     vfs_path_t *vpath;
 138     int actual_result;
 139 
 140     test_chdir__return_value = 0;
 141 
 142     vpath = vfs_path_from_str_flags (data->input_string, data->input_flags);
 143 
 144     /* when */
 145     actual_result = mc_chdir (vpath);
 146 
 147     /* then */
 148     {
 149         const vfs_path_element_t *element;
 150 
 151         mctest_assert_int_eq (actual_result, 0);
 152         element = vfs_path_get_by_index (vpath, -1);
 153         mctest_assert_str_eq (element->path, data->expected_element_path);
 154         vfs_path_free (vpath);
 155     }
 156 }
 157 /* *INDENT-OFF* */
 158 END_PARAMETRIZED_TEST
 159 /* *INDENT-ON* */
 160 
 161 /* --------------------------------------------------------------------------------------------- */
 162 
 163 /* Relative to panel_correct_path_to_show()  */
 164 
 165 /* @Test */
 166 /* *INDENT-OFF* */
 167 START_TEST (test_vpath_to_str_filter)
     /* [previous][next][first][last][top][bottom][index][help]  */
 168 /* *INDENT-ON* */
 169 {
 170     /* given */
 171     vfs_path_t *vpath, *last_vpath;
 172     char *filtered_path;
 173     const vfs_path_element_t *path_element;
 174 
 175     /* when */
 176     vpath = vfs_path_from_str ("/test1://some.host/dir");
 177     path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, -1));
 178     vfs_path_free (vpath);
 179 
 180     last_vpath = vfs_path_new ();
 181     last_vpath->relative = TRUE;
 182 
 183     vfs_path_add_element (last_vpath, path_element);
 184 
 185     filtered_path = vfs_path_to_str_flags (last_vpath, 0,
 186                                            VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
 187 
 188     /* then */
 189     mctest_assert_str_eq (filtered_path, "test1://some.host/dir");
 190 
 191     vfs_path_free (last_vpath);
 192     g_free (filtered_path);
 193 }
 194 /* *INDENT-OFF* */
 195 END_TEST
 196 /* *INDENT-ON* */
 197 
 198 /* --------------------------------------------------------------------------------------------- */
 199 
 200 int
 201 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 202 {
 203     int number_failed;
 204     char *cwd;
 205 
 206     Suite *s = suite_create (TEST_SUITE_NAME);
 207     TCase *tc_core = tcase_create ("Core");
 208     SRunner *sr;
 209 
 210     /* writable directory where check creates temporary files */
 211     cwd = g_get_current_dir ();
 212     g_setenv ("TEMP", cwd, TRUE);
 213     g_free (cwd);
 214 
 215     tcase_add_checked_fixture (tc_core, setup, teardown);
 216 
 217     /* Add new tests here: *************** */
 218     mctest_add_parameterized_test (tc_core, test_relative_cd, test_relative_cd_ds);
 219     tcase_add_test (tc_core, test_vpath_to_str_filter);
 220     /* *********************************** */
 221 
 222     suite_add_tcase (s, tc_core);
 223     sr = srunner_create (s);
 224     srunner_set_log (sr, "relative_cd.log");
 225     srunner_run_all (sr, CK_ENV);
 226     number_failed = srunner_ntests_failed (sr);
 227     srunner_free (sr);
 228     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 229 }
 230 
 231 /* --------------------------------------------------------------------------------------------- */

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