Ticket #3020: segfault on creating new file in external editor
[midnight-commander.git] / tests / src / execute__execute_with_vfs_arg.c
blobd14dec6de7260c4078d4eadf7aa945b13fbcc36f
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 "tests/mctest.h"
30 #include "execute__common.c"
32 /* --------------------------------------------------------------------------------------------- */
34 /* @DataSource("the_file_is_local_ds") */
35 /* *INDENT-OFF* */
36 static const struct the_file_is_local_ds
38 const char *input_path;
39 } the_file_is_local_ds[] =
42 NULL,
45 "/blabla",
48 /* *INDENT-ON* */
50 /* @Test(dataSource = "the_file_is_local_ds") */
51 /* *INDENT-OFF* */
52 START_PARAMETRIZED_TEST (the_file_is_local, the_file_is_local_ds)
53 /* *INDENT-ON* */
55 /* given */
56 vfs_path_t *filename_vpath;
57 filename_vpath = vfs_path_from_str (data->input_path);
59 vfs_file_is_local__return_value = TRUE;
61 /* when */
62 execute_with_vfs_arg ("cmd_for_local_file", filename_vpath);
64 /* then */
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 mctest_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_int_eq (vfs_path_equal
74 (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0),
75 tmp_vpath), TRUE);
77 mctest_assert_int_eq (do_execute__flags__captured, EXECUTE_INTERNAL);
78 fail_unless (mc_getlocalcopy__pathname_vpath__captured == NULL,
79 "\nFunction mc_getlocalcopy() shouldn't be called!");
81 vfs_path_free (filename_vpath);
83 /* *INDENT-OFF* */
84 END_PARAMETRIZED_TEST
85 /* *INDENT-ON* */
87 /* --------------------------------------------------------------------------------------------- */
89 /* @Test */
90 /* *INDENT-OFF* */
91 START_TEST (the_file_is_remote_but_empty)
92 /* *INDENT-ON* */
94 /* given */
95 vfs_path_t *filename_vpath;
96 filename_vpath = NULL;
98 vfs_file_is_local__return_value = FALSE;
100 /* when */
101 execute_with_vfs_arg ("cmd_for_remote_file", filename_vpath);
103 /* then */
104 mctest_assert_str_eq (do_execute__lc_shell__captured, NULL);
105 mctest_assert_str_eq (do_execute__command__captured, NULL);
107 mctest_assert_int_eq (vfs_file_is_local__vpath__captured->len, 2);
109 mctest_assert_int_eq (vfs_path_equal
110 (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0),
111 vfs_get_raw_current_dir ()), TRUE);
112 fail_unless (g_ptr_array_index (vfs_file_is_local__vpath__captured, 1) == NULL,
113 "\nParameter for second call to vfs_file_is_local() should be NULL!");
114 fail_unless (mc_getlocalcopy__pathname_vpath__captured == NULL,
115 "\nFunction mc_getlocalcopy() shouldn't be called!");
117 vfs_path_free (filename_vpath);
119 /* *INDENT-OFF* */
120 END_TEST
121 /* *INDENT-ON* */
123 /* --------------------------------------------------------------------------------------------- */
125 /* @Test */
126 /* *INDENT-OFF* */
127 START_TEST (the_file_is_remote_fail_to_create_local_copy)
128 /* *INDENT-ON* */
130 /* given */
131 vfs_path_t *filename_vpath;
133 filename_vpath = vfs_path_from_str ("/ftp://some.host/editme.txt");
135 vfs_file_is_local__return_value = FALSE;
136 mc_getlocalcopy__return_value = NULL;
138 /* when */
139 execute_with_vfs_arg ("cmd_for_remote_file", filename_vpath);
141 /* then */
142 mctest_assert_str_eq (do_execute__lc_shell__captured, NULL);
143 mctest_assert_str_eq (do_execute__command__captured, NULL);
145 mctest_assert_int_eq (vfs_file_is_local__vpath__captured->len, 1);
147 mctest_assert_int_eq (vfs_path_equal
148 (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0),
149 filename_vpath), TRUE);
151 mctest_assert_int_eq (vfs_path_equal
152 (mc_getlocalcopy__pathname_vpath__captured, filename_vpath), TRUE);
154 mctest_assert_str_eq (message_title__captured, _("Error"));
155 mctest_assert_str_eq (message_text__captured,
156 _("Cannot fetch a local copy of /ftp://some.host/editme.txt"));
159 vfs_path_free (filename_vpath);
161 /* *INDENT-OFF* */
162 END_TEST
163 /* *INDENT-ON* */
165 /* --------------------------------------------------------------------------------------------- */
167 /* @Test */
168 /* *INDENT-OFF* */
169 START_TEST (the_file_is_remote)
170 /* *INDENT-ON* */
172 /* given */
173 vfs_path_t *filename_vpath, *local_vpath, *local_vpath_should_be_freeing;
175 filename_vpath = vfs_path_from_str ("/ftp://some.host/editme.txt");
176 local_vpath = vfs_path_from_str ("/tmp/blabla-editme.txt");
177 local_vpath_should_be_freeing = vfs_path_clone (local_vpath);
179 vfs_file_is_local__return_value = FALSE;
180 mc_getlocalcopy__return_value = local_vpath_should_be_freeing;
182 /* when */
183 execute_with_vfs_arg ("cmd_for_remote_file", filename_vpath);
185 /* then */
186 mctest_assert_str_eq (do_execute__lc_shell__captured, "cmd_for_remote_file");
187 mctest_assert_str_eq (do_execute__command__captured, "/tmp/blabla-editme.txt");
189 mctest_assert_int_eq (vfs_file_is_local__vpath__captured->len, 1);
191 mctest_assert_int_eq (vfs_path_equal
192 (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0),
193 filename_vpath), TRUE);
195 mctest_assert_int_eq (vfs_path_equal
196 (mc_getlocalcopy__pathname_vpath__captured, filename_vpath), TRUE);
198 mctest_assert_int_eq (mc_stat__vpath__captured->len, 2);
200 mctest_assert_int_eq (vfs_path_equal
201 (g_ptr_array_index (mc_stat__vpath__captured, 0), local_vpath), TRUE);
202 mctest_assert_int_eq (vfs_path_equal
203 (g_ptr_array_index (mc_stat__vpath__captured, 0),
204 g_ptr_array_index (mc_stat__vpath__captured, 1)), TRUE);
206 mctest_assert_int_eq (vfs_path_equal
207 (mc_ungetlocalcopy__pathname_vpath__captured, filename_vpath), TRUE);
209 mctest_assert_int_eq (vfs_path_equal (mc_ungetlocalcopy__local_vpath__captured, local_vpath),
210 TRUE);
212 vfs_path_free (filename_vpath);
213 vfs_path_free (local_vpath);
215 /* *INDENT-OFF* */
216 END_TEST
217 /* *INDENT-ON* */
219 /* --------------------------------------------------------------------------------------------- */
222 main (void)
224 int number_failed;
226 Suite *s = suite_create (TEST_SUITE_NAME);
227 TCase *tc_core = tcase_create ("Core");
228 SRunner *sr;
230 tcase_add_checked_fixture (tc_core, setup, teardown);
232 /* Add new tests here: *************** */
233 mctest_add_parameterized_test (tc_core, the_file_is_local, the_file_is_local_ds);
234 tcase_add_test (tc_core, the_file_is_remote_but_empty);
235 tcase_add_test (tc_core, the_file_is_remote_fail_to_create_local_copy);
236 tcase_add_test (tc_core, the_file_is_remote);
237 /* *********************************** */
239 suite_add_tcase (s, tc_core);
240 sr = srunner_create (s);
241 srunner_set_log (sr, "execute__execute_with_vfs_arg.log");
242 srunner_run_all (sr, CK_NORMAL);
243 number_failed = srunner_ntests_failed (sr);
244 srunner_free (sr);
245 return (number_failed == 0) ? 0 : 1;
248 /* --------------------------------------------------------------------------------------------- */