root/tests/src/execute__execute_with_vfs_arg.c

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

DEFINITIONS

This source file includes following definitions.
  1. START_PARAMETRIZED_TEST
  2. START_TEST
  3. START_TEST
  4. START_TEST
  5. main

   1 /*
   2    src - tests for execute_with_vfs_arg() function
   3 
   4    Copyright (C) 2013-2025
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Slava Zanko <slavazanko@gmail.com>, 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 "/src"
  27 
  28 #include "tests/mctest.h"
  29 
  30 #include "execute__common.c"
  31 
  32 /* --------------------------------------------------------------------------------------------- */
  33 
  34 /* @DataSource("the_file_is_local_ds") */
  35 static const struct the_file_is_local_ds
  36 {
  37     const char *input_path;
  38 } the_file_is_local_ds[] = {
  39     {
  40         NULL,
  41     },
  42     {
  43         "/blabla",
  44     },
  45 };
  46 
  47 /* @Test(dataSource = "the_file_is_local_ds") */
  48 START_PARAMETRIZED_TEST (the_file_is_local, the_file_is_local_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
  49 {
  50     // given
  51     vfs_path_t *filename_vpath;
  52     filename_vpath = vfs_path_from_str (data->input_path);
  53 
  54     vfs_file_is_local__return_value = TRUE;
  55 
  56     // when
  57     execute_with_vfs_arg ("cmd_for_local_file", filename_vpath);
  58 
  59     // then
  60     mctest_assert_str_eq (do_execute__lc_shell__captured, "cmd_for_local_file");
  61     mctest_assert_str_eq (do_execute__command__captured, data->input_path);
  62 
  63     ck_assert_int_eq (vfs_file_is_local__vpath__captured->len, 1);
  64     {
  65         const vfs_path_t *tmp_vpath;
  66 
  67         tmp_vpath = (data->input_path == NULL) ? vfs_get_raw_current_dir () : filename_vpath;
  68         mctest_assert_true (
  69             vfs_path_equal (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0), tmp_vpath));
  70     }
  71     ck_assert_int_eq (do_execute__flags__captured, EXECUTE_INTERNAL);
  72     ck_assert_msg (mc_getlocalcopy__pathname_vpath__captured == NULL,
  73                    "\nFunction mc_getlocalcopy() shouldn't be called!");
  74 
  75     vfs_path_free (filename_vpath, TRUE);
  76 }
  77 END_PARAMETRIZED_TEST
  78 
  79 /* --------------------------------------------------------------------------------------------- */
  80 
  81 /* @Test */
  82 START_TEST (the_file_is_remote_but_empty)
     /* [previous][next][first][last][top][bottom][index][help]  */
  83 {
  84     // given
  85     vfs_path_t *filename_vpath;
  86     filename_vpath = NULL;
  87 
  88     vfs_file_is_local__return_value = FALSE;
  89 
  90     // when
  91     execute_with_vfs_arg ("cmd_for_remote_file", filename_vpath);
  92 
  93     // then
  94     mctest_assert_str_eq (do_execute__lc_shell__captured, NULL);
  95     mctest_assert_str_eq (do_execute__command__captured, NULL);
  96 
  97     ck_assert_int_eq (vfs_file_is_local__vpath__captured->len, 2);
  98 
  99     mctest_assert_true (vfs_path_equal (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0),
 100                                         vfs_get_raw_current_dir ()));
 101     ck_assert_msg (g_ptr_array_index (vfs_file_is_local__vpath__captured, 1) == NULL,
 102                    "\nParameter for second call to vfs_file_is_local() should be NULL!");
 103     ck_assert_msg (mc_getlocalcopy__pathname_vpath__captured == NULL,
 104                    "\nFunction mc_getlocalcopy() shouldn't be called!");
 105 
 106     vfs_path_free (filename_vpath, TRUE);
 107 }
 108 END_TEST
 109 
 110 /* --------------------------------------------------------------------------------------------- */
 111 
 112 /* @Test */
 113 START_TEST (the_file_is_remote_fail_to_create_local_copy)
     /* [previous][next][first][last][top][bottom][index][help]  */
 114 {
 115     // given
 116     vfs_path_t *filename_vpath;
 117 
 118     filename_vpath = vfs_path_from_str ("/ftp://some.host/editme.txt");
 119 
 120     vfs_file_is_local__return_value = FALSE;
 121     mc_getlocalcopy__return_value = NULL;
 122 
 123     // when
 124     execute_with_vfs_arg ("cmd_for_remote_file", filename_vpath);
 125 
 126     // then
 127     mctest_assert_str_eq (do_execute__lc_shell__captured, NULL);
 128     mctest_assert_str_eq (do_execute__command__captured, NULL);
 129 
 130     ck_assert_int_eq (vfs_file_is_local__vpath__captured->len, 1);
 131 
 132     mctest_assert_true (
 133         vfs_path_equal (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0), filename_vpath));
 134 
 135     mctest_assert_true (vfs_path_equal (mc_getlocalcopy__pathname_vpath__captured, filename_vpath));
 136 
 137     mctest_assert_str_eq (message_title__captured, _ ("Error"));
 138     mctest_assert_str_eq (message_text__captured,
 139                           _ ("Cannot fetch a local copy of /ftp://some.host/editme.txt"));
 140 
 141     vfs_path_free (filename_vpath, TRUE);
 142 }
 143 END_TEST
 144 
 145 /* --------------------------------------------------------------------------------------------- */
 146 
 147 /* @Test */
 148 START_TEST (the_file_is_remote)
     /* [previous][next][first][last][top][bottom][index][help]  */
 149 {
 150     // given
 151     vfs_path_t *filename_vpath, *local_vpath, *local_vpath_should_be_freeing;
 152 
 153     filename_vpath = vfs_path_from_str ("/ftp://some.host/editme.txt");
 154     local_vpath = vfs_path_from_str ("/tmp/blabla-editme.txt");
 155     local_vpath_should_be_freeing = vfs_path_clone (local_vpath);
 156 
 157     vfs_file_is_local__return_value = FALSE;
 158     mc_getlocalcopy__return_value = local_vpath_should_be_freeing;
 159 
 160     // when
 161     execute_with_vfs_arg ("cmd_for_remote_file", filename_vpath);
 162 
 163     // then
 164     mctest_assert_str_eq (do_execute__lc_shell__captured, "cmd_for_remote_file");
 165     mctest_assert_str_eq (do_execute__command__captured, "/tmp/blabla-editme.txt");
 166 
 167     ck_assert_int_eq (vfs_file_is_local__vpath__captured->len, 1);
 168 
 169     mctest_assert_true (
 170         vfs_path_equal (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0), filename_vpath));
 171 
 172     mctest_assert_true (vfs_path_equal (mc_getlocalcopy__pathname_vpath__captured, filename_vpath));
 173 
 174     ck_assert_int_eq (mc_stat__vpath__captured->len, 2);
 175 
 176     mctest_assert_true (
 177         vfs_path_equal (g_ptr_array_index (mc_stat__vpath__captured, 0), local_vpath));
 178     mctest_assert_true (vfs_path_equal (g_ptr_array_index (mc_stat__vpath__captured, 0),
 179                                         g_ptr_array_index (mc_stat__vpath__captured, 1)));
 180 
 181     mctest_assert_true (
 182         vfs_path_equal (mc_ungetlocalcopy__pathname_vpath__captured, filename_vpath));
 183 
 184     mctest_assert_true (vfs_path_equal (mc_ungetlocalcopy__local_vpath__captured, local_vpath));
 185 
 186     vfs_path_free (filename_vpath, TRUE);
 187     vfs_path_free (local_vpath, TRUE);
 188 }
 189 END_TEST
 190 
 191 /* --------------------------------------------------------------------------------------------- */
 192 
 193 int
 194 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 195 {
 196     TCase *tc_core;
 197 
 198     tc_core = tcase_create ("Core");
 199 
 200     tcase_add_checked_fixture (tc_core, setup, teardown);
 201 
 202     // Add new tests here: ***************
 203     mctest_add_parameterized_test (tc_core, the_file_is_local, the_file_is_local_ds);
 204     tcase_add_test (tc_core, the_file_is_remote_but_empty);
 205     tcase_add_test (tc_core, the_file_is_remote_fail_to_create_local_copy);
 206     tcase_add_test (tc_core, the_file_is_remote);
 207     // ***********************************
 208 
 209     return mctest_run_all (tc_core);
 210 }
 211 
 212 /* --------------------------------------------------------------------------------------------- */

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