execute__execute_with_vfs_arg.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. src - tests for execute_with_vfs_arg() function
  3. Copyright (C) 2013-2024
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2013
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define TEST_SUITE_NAME "/src"
  20. #include "tests/mctest.h"
  21. #include "execute__common.c"
  22. /* --------------------------------------------------------------------------------------------- */
  23. /* @DataSource("the_file_is_local_ds") */
  24. /* *INDENT-OFF* */
  25. static const struct the_file_is_local_ds
  26. {
  27. const char *input_path;
  28. } the_file_is_local_ds[] =
  29. {
  30. {
  31. NULL,
  32. },
  33. {
  34. "/blabla",
  35. },
  36. };
  37. /* *INDENT-ON* */
  38. /* @Test(dataSource = "the_file_is_local_ds") */
  39. /* *INDENT-OFF* */
  40. START_PARAMETRIZED_TEST (the_file_is_local, the_file_is_local_ds)
  41. /* *INDENT-ON* */
  42. {
  43. /* given */
  44. vfs_path_t *filename_vpath;
  45. filename_vpath = vfs_path_from_str (data->input_path);
  46. vfs_file_is_local__return_value = TRUE;
  47. /* when */
  48. execute_with_vfs_arg ("cmd_for_local_file", filename_vpath);
  49. /* then */
  50. mctest_assert_str_eq (do_execute__lc_shell__captured, "cmd_for_local_file");
  51. mctest_assert_str_eq (do_execute__command__captured, data->input_path);
  52. ck_assert_int_eq (vfs_file_is_local__vpath__captured->len, 1);
  53. {
  54. const vfs_path_t *tmp_vpath;
  55. tmp_vpath = (data->input_path == NULL) ? vfs_get_raw_current_dir () : filename_vpath;
  56. mctest_assert_true (vfs_path_equal
  57. (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0), tmp_vpath));
  58. }
  59. ck_assert_int_eq (do_execute__flags__captured, EXECUTE_INTERNAL);
  60. ck_assert_msg (mc_getlocalcopy__pathname_vpath__captured == NULL,
  61. "\nFunction mc_getlocalcopy() shouldn't be called!");
  62. vfs_path_free (filename_vpath, TRUE);
  63. }
  64. /* *INDENT-OFF* */
  65. END_PARAMETRIZED_TEST
  66. /* *INDENT-ON* */
  67. /* --------------------------------------------------------------------------------------------- */
  68. /* @Test */
  69. /* *INDENT-OFF* */
  70. START_TEST (the_file_is_remote_but_empty)
  71. /* *INDENT-ON* */
  72. {
  73. /* given */
  74. vfs_path_t *filename_vpath;
  75. filename_vpath = NULL;
  76. vfs_file_is_local__return_value = FALSE;
  77. /* when */
  78. execute_with_vfs_arg ("cmd_for_remote_file", filename_vpath);
  79. /* then */
  80. mctest_assert_str_eq (do_execute__lc_shell__captured, NULL);
  81. mctest_assert_str_eq (do_execute__command__captured, NULL);
  82. ck_assert_int_eq (vfs_file_is_local__vpath__captured->len, 2);
  83. mctest_assert_true (vfs_path_equal
  84. (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0),
  85. vfs_get_raw_current_dir ()));
  86. ck_assert_msg (g_ptr_array_index (vfs_file_is_local__vpath__captured, 1) == NULL,
  87. "\nParameter for second call to vfs_file_is_local() should be NULL!");
  88. ck_assert_msg (mc_getlocalcopy__pathname_vpath__captured == NULL,
  89. "\nFunction mc_getlocalcopy() shouldn't be called!");
  90. vfs_path_free (filename_vpath, TRUE);
  91. }
  92. /* *INDENT-OFF* */
  93. END_TEST
  94. /* *INDENT-ON* */
  95. /* --------------------------------------------------------------------------------------------- */
  96. /* @Test */
  97. /* *INDENT-OFF* */
  98. START_TEST (the_file_is_remote_fail_to_create_local_copy)
  99. /* *INDENT-ON* */
  100. {
  101. /* given */
  102. vfs_path_t *filename_vpath;
  103. filename_vpath = vfs_path_from_str ("/ftp://some.host/editme.txt");
  104. vfs_file_is_local__return_value = FALSE;
  105. mc_getlocalcopy__return_value = NULL;
  106. /* when */
  107. execute_with_vfs_arg ("cmd_for_remote_file", filename_vpath);
  108. /* then */
  109. mctest_assert_str_eq (do_execute__lc_shell__captured, NULL);
  110. mctest_assert_str_eq (do_execute__command__captured, NULL);
  111. ck_assert_int_eq (vfs_file_is_local__vpath__captured->len, 1);
  112. mctest_assert_true (vfs_path_equal
  113. (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0),
  114. filename_vpath));
  115. mctest_assert_true (vfs_path_equal (mc_getlocalcopy__pathname_vpath__captured, filename_vpath));
  116. mctest_assert_str_eq (message_title__captured, _("Error"));
  117. mctest_assert_str_eq (message_text__captured,
  118. _("Cannot fetch a local copy of /ftp://some.host/editme.txt"));
  119. vfs_path_free (filename_vpath, TRUE);
  120. }
  121. /* *INDENT-OFF* */
  122. END_TEST
  123. /* *INDENT-ON* */
  124. /* --------------------------------------------------------------------------------------------- */
  125. /* @Test */
  126. /* *INDENT-OFF* */
  127. START_TEST (the_file_is_remote)
  128. /* *INDENT-ON* */
  129. {
  130. /* given */
  131. vfs_path_t *filename_vpath, *local_vpath, *local_vpath_should_be_freeing;
  132. filename_vpath = vfs_path_from_str ("/ftp://some.host/editme.txt");
  133. local_vpath = vfs_path_from_str ("/tmp/blabla-editme.txt");
  134. local_vpath_should_be_freeing = vfs_path_clone (local_vpath);
  135. vfs_file_is_local__return_value = FALSE;
  136. mc_getlocalcopy__return_value = local_vpath_should_be_freeing;
  137. /* when */
  138. execute_with_vfs_arg ("cmd_for_remote_file", filename_vpath);
  139. /* then */
  140. mctest_assert_str_eq (do_execute__lc_shell__captured, "cmd_for_remote_file");
  141. mctest_assert_str_eq (do_execute__command__captured, "/tmp/blabla-editme.txt");
  142. ck_assert_int_eq (vfs_file_is_local__vpath__captured->len, 1);
  143. mctest_assert_true (vfs_path_equal
  144. (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0),
  145. filename_vpath));
  146. mctest_assert_true (vfs_path_equal (mc_getlocalcopy__pathname_vpath__captured, filename_vpath));
  147. ck_assert_int_eq (mc_stat__vpath__captured->len, 2);
  148. mctest_assert_true (vfs_path_equal
  149. (g_ptr_array_index (mc_stat__vpath__captured, 0), local_vpath));
  150. mctest_assert_true (vfs_path_equal
  151. (g_ptr_array_index (mc_stat__vpath__captured, 0),
  152. g_ptr_array_index (mc_stat__vpath__captured, 1)));
  153. mctest_assert_true (vfs_path_equal
  154. (mc_ungetlocalcopy__pathname_vpath__captured, filename_vpath));
  155. mctest_assert_true (vfs_path_equal (mc_ungetlocalcopy__local_vpath__captured, local_vpath));
  156. vfs_path_free (filename_vpath, TRUE);
  157. vfs_path_free (local_vpath, TRUE);
  158. }
  159. /* *INDENT-OFF* */
  160. END_TEST
  161. /* *INDENT-ON* */
  162. /* --------------------------------------------------------------------------------------------- */
  163. int
  164. main (void)
  165. {
  166. TCase *tc_core;
  167. tc_core = tcase_create ("Core");
  168. tcase_add_checked_fixture (tc_core, setup, teardown);
  169. /* Add new tests here: *************** */
  170. mctest_add_parameterized_test (tc_core, the_file_is_local, the_file_is_local_ds);
  171. tcase_add_test (tc_core, the_file_is_remote_but_empty);
  172. tcase_add_test (tc_core, the_file_is_remote_fail_to_create_local_copy);
  173. tcase_add_test (tc_core, the_file_is_remote);
  174. /* *********************************** */
  175. return mctest_run_all (tc_core);
  176. }
  177. /* --------------------------------------------------------------------------------------------- */