2 src - tests for execute_with_vfs_arg() function
4 Copyright (C) 2013-2024
5 Free Software Foundation, Inc.
8 Slava Zanko <slavazanko@gmail.com>, 2013
10 This file is part of the Midnight Commander.
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.
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.
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/>.
26 #define TEST_SUITE_NAME "/src"
28 #include "tests/mctest.h"
30 #include "execute__common.c"
32 /* --------------------------------------------------------------------------------------------- */
34 /* @DataSource("the_file_is_local_ds") */
36 static const struct the_file_is_local_ds
38 const char *input_path
;
39 } the_file_is_local_ds
[] =
50 /* @Test(dataSource = "the_file_is_local_ds") */
52 START_PARAMETRIZED_TEST (the_file_is_local
, the_file_is_local_ds
)
56 vfs_path_t
*filename_vpath
;
57 filename_vpath
= vfs_path_from_str (data
->input_path
);
59 vfs_file_is_local__return_value
= TRUE
;
62 execute_with_vfs_arg ("cmd_for_local_file", filename_vpath
);
65 mctest_assert_str_eq (do_execute__lc_shell__captured
, "cmd_for_local_file");
66 mctest_assert_str_eq (do_execute__command__captured
, data
->input_path
);
68 ck_assert_int_eq (vfs_file_is_local__vpath__captured
->len
, 1);
70 const vfs_path_t
*tmp_vpath
;
72 tmp_vpath
= (data
->input_path
== NULL
) ? vfs_get_raw_current_dir () : filename_vpath
;
73 mctest_assert_true (vfs_path_equal
74 (g_ptr_array_index (vfs_file_is_local__vpath__captured
, 0), tmp_vpath
));
76 ck_assert_int_eq (do_execute__flags__captured
, EXECUTE_INTERNAL
);
77 ck_assert_msg (mc_getlocalcopy__pathname_vpath__captured
== NULL
,
78 "\nFunction mc_getlocalcopy() shouldn't be called!");
80 vfs_path_free (filename_vpath
, TRUE
);
86 /* --------------------------------------------------------------------------------------------- */
90 START_TEST (the_file_is_remote_but_empty
)
94 vfs_path_t
*filename_vpath
;
95 filename_vpath
= NULL
;
97 vfs_file_is_local__return_value
= FALSE
;
100 execute_with_vfs_arg ("cmd_for_remote_file", filename_vpath
);
103 mctest_assert_str_eq (do_execute__lc_shell__captured
, NULL
);
104 mctest_assert_str_eq (do_execute__command__captured
, NULL
);
106 ck_assert_int_eq (vfs_file_is_local__vpath__captured
->len
, 2);
108 mctest_assert_true (vfs_path_equal
109 (g_ptr_array_index (vfs_file_is_local__vpath__captured
, 0),
110 vfs_get_raw_current_dir ()));
111 ck_assert_msg (g_ptr_array_index (vfs_file_is_local__vpath__captured
, 1) == NULL
,
112 "\nParameter for second call to vfs_file_is_local() should be NULL!");
113 ck_assert_msg (mc_getlocalcopy__pathname_vpath__captured
== NULL
,
114 "\nFunction mc_getlocalcopy() shouldn't be called!");
116 vfs_path_free (filename_vpath
, TRUE
);
122 /* --------------------------------------------------------------------------------------------- */
126 START_TEST (the_file_is_remote_fail_to_create_local_copy
)
130 vfs_path_t
*filename_vpath
;
132 filename_vpath
= vfs_path_from_str ("/ftp://some.host/editme.txt");
134 vfs_file_is_local__return_value
= FALSE
;
135 mc_getlocalcopy__return_value
= NULL
;
138 execute_with_vfs_arg ("cmd_for_remote_file", filename_vpath
);
141 mctest_assert_str_eq (do_execute__lc_shell__captured
, NULL
);
142 mctest_assert_str_eq (do_execute__command__captured
, NULL
);
144 ck_assert_int_eq (vfs_file_is_local__vpath__captured
->len
, 1);
146 mctest_assert_true (vfs_path_equal
147 (g_ptr_array_index (vfs_file_is_local__vpath__captured
, 0),
150 mctest_assert_true (vfs_path_equal (mc_getlocalcopy__pathname_vpath__captured
, filename_vpath
));
152 mctest_assert_str_eq (message_title__captured
, _("Error"));
153 mctest_assert_str_eq (message_text__captured
,
154 _("Cannot fetch a local copy of /ftp://some.host/editme.txt"));
157 vfs_path_free (filename_vpath
, TRUE
);
163 /* --------------------------------------------------------------------------------------------- */
167 START_TEST (the_file_is_remote
)
171 vfs_path_t
*filename_vpath
, *local_vpath
, *local_vpath_should_be_freeing
;
173 filename_vpath
= vfs_path_from_str ("/ftp://some.host/editme.txt");
174 local_vpath
= vfs_path_from_str ("/tmp/blabla-editme.txt");
175 local_vpath_should_be_freeing
= vfs_path_clone (local_vpath
);
177 vfs_file_is_local__return_value
= FALSE
;
178 mc_getlocalcopy__return_value
= local_vpath_should_be_freeing
;
181 execute_with_vfs_arg ("cmd_for_remote_file", filename_vpath
);
184 mctest_assert_str_eq (do_execute__lc_shell__captured
, "cmd_for_remote_file");
185 mctest_assert_str_eq (do_execute__command__captured
, "/tmp/blabla-editme.txt");
187 ck_assert_int_eq (vfs_file_is_local__vpath__captured
->len
, 1);
189 mctest_assert_true (vfs_path_equal
190 (g_ptr_array_index (vfs_file_is_local__vpath__captured
, 0),
193 mctest_assert_true (vfs_path_equal (mc_getlocalcopy__pathname_vpath__captured
, filename_vpath
));
195 ck_assert_int_eq (mc_stat__vpath__captured
->len
, 2);
197 mctest_assert_true (vfs_path_equal
198 (g_ptr_array_index (mc_stat__vpath__captured
, 0), local_vpath
));
199 mctest_assert_true (vfs_path_equal
200 (g_ptr_array_index (mc_stat__vpath__captured
, 0),
201 g_ptr_array_index (mc_stat__vpath__captured
, 1)));
203 mctest_assert_true (vfs_path_equal
204 (mc_ungetlocalcopy__pathname_vpath__captured
, filename_vpath
));
206 mctest_assert_true (vfs_path_equal (mc_ungetlocalcopy__local_vpath__captured
, local_vpath
));
208 vfs_path_free (filename_vpath
, TRUE
);
209 vfs_path_free (local_vpath
, TRUE
);
215 /* --------------------------------------------------------------------------------------------- */
222 tc_core
= tcase_create ("Core");
224 tcase_add_checked_fixture (tc_core
, setup
, teardown
);
226 /* Add new tests here: *************** */
227 mctest_add_parameterized_test (tc_core
, the_file_is_local
, the_file_is_local_ds
);
228 tcase_add_test (tc_core
, the_file_is_remote_but_empty
);
229 tcase_add_test (tc_core
, the_file_is_remote_fail_to_create_local_copy
);
230 tcase_add_test (tc_core
, the_file_is_remote
);
231 /* *********************************** */
233 return mctest_run_all (tc_core
);
236 /* --------------------------------------------------------------------------------------------- */