Add tests for src/execute.c:execute_with_vfs_arg() function.
[midnight-commander.git] / tests / src / execute__execute_with_vfs_arg.c
blob052786ab327814b52908fa0d23f2e094d8d2fefc
1 /*
2 src - tests for execute_with_vfs_arg() function
4 Copyright (C) 2013
5 The Free Software Foundation, Inc.
7 Written by:
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 <config.h>
30 #include <check.h>
32 #include "lib/global.h"
34 #include "execute__common.c"
36 /* --------------------------------------------------------------------------------------------- */
38 /* @DataSource("the_file_is_local_ds") */
39 /* *INDENT-OFF* */
40 static const struct the_file_is_local_ds
42 const char *input_path;
43 } the_file_is_local_ds[] =
46 NULL,
49 "/blabla",
52 /* *INDENT-ON* */
54 /* @Test(dataSource = "the_file_is_local_ds") */
55 /* *INDENT-OFF* */
56 START_TEST (the_file_is_local)
57 /* *INDENT-ON* */
59 /* given */
60 vfs_path_t *filename_vpath;
61 const char *input_path = the_file_is_local_ds[_i].input_path;
62 filename_vpath = vfs_path_from_str (input_path);
64 vfs_file_is_local__return_value = TRUE;
66 /* when */
67 execute_with_vfs_arg ("cmd_for_local_file", filename_vpath);
69 /* then */
70 g_assert_cmpstr (do_execute__lc_shell__captured, ==, "cmd_for_local_file");
71 g_assert_cmpstr (do_execute__command__captured, ==, input_path);
73 ck_assert_int_eq (vfs_file_is_local__vpath__captured->len, 1);
75 const vfs_path_t *tmp_vpath;
77 tmp_vpath = (input_path == NULL) ? vfs_get_raw_current_dir () : filename_vpath;
78 ck_assert_int_eq (vfs_path_cmp
79 (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0), tmp_vpath),
80 0);
82 ck_assert_int_eq (do_execute__flags__captured, EXECUTE_INTERNAL);
83 fail_unless (mc_getlocalcopy__pathname_vpath__captured == NULL,
84 "\nFunction mc_getlocalcopy() shouldn't be called!");
86 vfs_path_free (filename_vpath);
88 /* *INDENT-OFF* */
89 END_TEST
90 /* *INDENT-ON* */
92 /* --------------------------------------------------------------------------------------------- */
94 /* @Test */
95 /* *INDENT-OFF* */
96 START_TEST (the_file_is_remote_but_empty)
97 /* *INDENT-ON* */
99 /* given */
100 vfs_path_t *filename_vpath;
101 filename_vpath = NULL;
103 vfs_file_is_local__return_value = FALSE;
105 /* when */
106 execute_with_vfs_arg ("cmd_for_remote_file", filename_vpath);
108 /* then */
109 g_assert_cmpstr (do_execute__lc_shell__captured, ==, NULL);
110 g_assert_cmpstr (do_execute__command__captured, ==, NULL);
112 ck_assert_int_eq (vfs_file_is_local__vpath__captured->len, 2);
114 ck_assert_int_eq (vfs_path_cmp
115 (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0),
116 vfs_get_raw_current_dir ()), 0);
117 fail_unless (g_ptr_array_index (vfs_file_is_local__vpath__captured, 1) == NULL,
118 "\nParameter for second call to vfs_file_is_local() should be NULL!");
119 fail_unless (mc_getlocalcopy__pathname_vpath__captured == NULL,
120 "\nFunction mc_getlocalcopy() shouldn't be called!");
122 vfs_path_free (filename_vpath);
124 /* *INDENT-OFF* */
125 END_TEST
126 /* *INDENT-ON* */
128 /* --------------------------------------------------------------------------------------------- */
130 /* @Test */
131 /* *INDENT-OFF* */
132 START_TEST (the_file_is_remote_fail_to_create_local_copy)
133 /* *INDENT-ON* */
135 /* given */
136 vfs_path_t *filename_vpath;
138 filename_vpath = vfs_path_from_str ("/ftp://some.host/editme.txt");
140 vfs_file_is_local__return_value = FALSE;
141 mc_getlocalcopy__return_value = NULL;
143 /* when */
144 execute_with_vfs_arg ("cmd_for_remote_file", filename_vpath);
146 /* then */
147 g_assert_cmpstr (do_execute__lc_shell__captured, ==, NULL);
148 g_assert_cmpstr (do_execute__command__captured, ==, NULL);
150 ck_assert_int_eq (vfs_file_is_local__vpath__captured->len, 1);
152 ck_assert_int_eq (vfs_path_cmp
153 (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0), filename_vpath),
156 ck_assert_int_eq (vfs_path_cmp (mc_getlocalcopy__pathname_vpath__captured, filename_vpath), 0);
158 g_assert_cmpstr (message_title__captured, ==, _("Error"));
159 g_assert_cmpstr (message_text__captured, ==,
160 _("Cannot fetch a local copy of /ftp://some.host/editme.txt"));
163 vfs_path_free (filename_vpath);
165 /* *INDENT-OFF* */
166 END_TEST
167 /* *INDENT-ON* */
169 /* --------------------------------------------------------------------------------------------- */
171 /* @Test */
172 /* *INDENT-OFF* */
173 START_TEST (the_file_is_remote)
174 /* *INDENT-ON* */
176 /* given */
177 vfs_path_t *filename_vpath, *local_vpath, *local_vpath_should_be_freeing;
179 filename_vpath = vfs_path_from_str ("/ftp://some.host/editme.txt");
180 local_vpath = vfs_path_from_str ("/tmp/blabla-editme.txt");
181 local_vpath_should_be_freeing = vfs_path_clone (local_vpath);
183 vfs_file_is_local__return_value = FALSE;
184 mc_getlocalcopy__return_value = local_vpath_should_be_freeing;
186 /* when */
187 execute_with_vfs_arg ("cmd_for_remote_file", filename_vpath);
189 /* then */
190 g_assert_cmpstr (do_execute__lc_shell__captured, ==, "cmd_for_remote_file");
191 g_assert_cmpstr (do_execute__command__captured, ==, "/tmp/blabla-editme.txt");
193 ck_assert_int_eq (vfs_file_is_local__vpath__captured->len, 1);
195 ck_assert_int_eq (vfs_path_cmp
196 (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0), filename_vpath),
199 ck_assert_int_eq (vfs_path_cmp (mc_getlocalcopy__pathname_vpath__captured, filename_vpath), 0);
201 ck_assert_int_eq (mc_stat__vpath__captured->len, 2);
203 ck_assert_int_eq (vfs_path_cmp
204 (g_ptr_array_index (mc_stat__vpath__captured, 0), local_vpath), 0);
205 ck_assert_int_eq (vfs_path_cmp
206 (g_ptr_array_index (mc_stat__vpath__captured, 0),
207 g_ptr_array_index (mc_stat__vpath__captured, 1)), 0);
209 ck_assert_int_eq (vfs_path_cmp (mc_ungetlocalcopy__pathname_vpath__captured, filename_vpath),
212 ck_assert_int_eq (vfs_path_cmp (mc_ungetlocalcopy__local_vpath__captured, local_vpath), 0);
214 vfs_path_free (filename_vpath);
215 vfs_path_free (local_vpath);
217 /* *INDENT-OFF* */
218 END_TEST
219 /* *INDENT-ON* */
221 /* --------------------------------------------------------------------------------------------- */
224 main (void)
226 int number_failed;
228 Suite *s = suite_create (TEST_SUITE_NAME);
229 TCase *tc_core = tcase_create ("Core");
230 SRunner *sr;
232 tcase_add_checked_fixture (tc_core, setup, teardown);
234 /* Add new tests here: *************** */
235 tcase_add_loop_test (tc_core, the_file_is_local, 0,
236 sizeof (the_file_is_local_ds) / sizeof (the_file_is_local_ds[0]));
237 tcase_add_test (tc_core, the_file_is_remote_but_empty);
238 tcase_add_test (tc_core, the_file_is_remote_fail_to_create_local_copy);
239 tcase_add_test (tc_core, the_file_is_remote);
240 /* *********************************** */
242 suite_add_tcase (s, tc_core);
243 sr = srunner_create (s);
244 srunner_set_log (sr, "execute__execute_with_vfs_arg.log");
245 srunner_run_all (sr, CK_NORMAL);
246 number_failed = srunner_ntests_failed (sr);
247 srunner_free (sr);
248 return (number_failed == 0) ? 0 : 1;
251 /* --------------------------------------------------------------------------------------------- */