execute__common.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. Common code for testing functions in src/execute.c file.
  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. #include "lib/widget.h"
  20. #include "lib/strutil.h"
  21. #include "lib/vfs/vfs.h"
  22. #include "src/vfs/local/local.c"
  23. #include "src/execute.h"
  24. /* --------------------------------------------------------------------------------------------- */
  25. /* @CapturedValue */
  26. static GPtrArray *vfs_file_is_local__vpath__captured;
  27. /* @ThenReturnValue */
  28. static gboolean vfs_file_is_local__return_value;
  29. /* @Mock */
  30. gboolean
  31. vfs_file_is_local (const vfs_path_t *vpath)
  32. {
  33. g_ptr_array_add (vfs_file_is_local__vpath__captured, vfs_path_clone (vpath));
  34. return vfs_file_is_local__return_value;
  35. }
  36. static void
  37. vpath_captured_free (gpointer data)
  38. {
  39. vfs_path_free ((vfs_path_t *) data, TRUE);
  40. }
  41. static void
  42. vfs_file_is_local__init (void)
  43. {
  44. vfs_file_is_local__vpath__captured = g_ptr_array_new_with_free_func (vpath_captured_free);
  45. }
  46. static void
  47. vfs_file_is_local__deinit (void)
  48. {
  49. g_ptr_array_free (vfs_file_is_local__vpath__captured, TRUE);
  50. }
  51. /* --------------------------------------------------------------------------------------------- */
  52. void do_execute (const char *lc_shell, const char *command, int flags);
  53. /* @CapturedValue */
  54. static char *do_execute__lc_shell__captured;
  55. /* @CapturedValue */
  56. static char *do_execute__command__captured;
  57. /* @CapturedValue */
  58. static int do_execute__flags__captured;
  59. /* @Mock */
  60. void
  61. do_execute (const char *lc_shell, const char *command, int flags)
  62. {
  63. do_execute__lc_shell__captured = g_strdup (lc_shell);
  64. do_execute__command__captured = g_strdup (command);
  65. do_execute__flags__captured = flags;
  66. }
  67. static void
  68. do_execute__init (void)
  69. {
  70. do_execute__command__captured = NULL;
  71. do_execute__lc_shell__captured = NULL;
  72. }
  73. static void
  74. do_execute__deinit (void)
  75. {
  76. g_free (do_execute__lc_shell__captured);
  77. g_free (do_execute__command__captured);
  78. }
  79. /* --------------------------------------------------------------------------------------------- */
  80. /* @CapturedValue */
  81. static vfs_path_t *mc_getlocalcopy__pathname_vpath__captured;
  82. /* @ThenReturnValue */
  83. static vfs_path_t *mc_getlocalcopy__return_value;
  84. /* @Mock */
  85. vfs_path_t *
  86. mc_getlocalcopy (const vfs_path_t *pathname_vpath)
  87. {
  88. mc_getlocalcopy__pathname_vpath__captured = vfs_path_clone (pathname_vpath);
  89. return mc_getlocalcopy__return_value;
  90. }
  91. static void
  92. mc_getlocalcopy__init (void)
  93. {
  94. mc_getlocalcopy__pathname_vpath__captured = NULL;
  95. mc_getlocalcopy__return_value = NULL;
  96. }
  97. static void
  98. mc_getlocalcopy__deinit (void)
  99. {
  100. vfs_path_free (mc_getlocalcopy__pathname_vpath__captured, TRUE);
  101. }
  102. /* --------------------------------------------------------------------------------------------- */
  103. /* @CapturedValue */
  104. static int message_flags__captured;
  105. /* @CapturedValue */
  106. static char *message_title__captured;
  107. /* @CapturedValue */
  108. static char *message_text__captured;
  109. /* @Mock */
  110. void
  111. message (int flags, const char *title, const char *text, ...)
  112. {
  113. va_list ap;
  114. message_flags__captured = flags;
  115. message_title__captured = (title == MSG_ERROR) ? g_strdup (_("Error")) : g_strdup (title);
  116. va_start (ap, text);
  117. message_text__captured = g_strdup_vprintf (text, ap);
  118. va_end (ap);
  119. }
  120. static void
  121. message__init (void)
  122. {
  123. message_flags__captured = 0;
  124. message_title__captured = NULL;
  125. message_text__captured = NULL;
  126. }
  127. static void
  128. message__deinit (void)
  129. {
  130. g_free (message_title__captured);
  131. g_free (message_text__captured);
  132. }
  133. /* --------------------------------------------------------------------------------------------- */
  134. /* @CapturedValue */
  135. static GPtrArray *mc_stat__vpath__captured = NULL;
  136. /* @ThenReturnValue */
  137. static int mc_stat__return_value = 0;
  138. /* @Mock */
  139. int
  140. mc_stat (const vfs_path_t *vpath, struct stat *stat_ignored)
  141. {
  142. (void) stat_ignored;
  143. if (mc_stat__vpath__captured != NULL)
  144. g_ptr_array_add (mc_stat__vpath__captured, vfs_path_clone (vpath));
  145. return mc_stat__return_value;
  146. }
  147. static void
  148. mc_stat__init (void)
  149. {
  150. mc_stat__vpath__captured = g_ptr_array_new_with_free_func (vpath_captured_free);
  151. }
  152. static void
  153. mc_stat__deinit (void)
  154. {
  155. g_ptr_array_free (mc_stat__vpath__captured, TRUE);
  156. mc_stat__vpath__captured = NULL;
  157. }
  158. /* --------------------------------------------------------------------------------------------- */
  159. /* @CapturedValue */
  160. static vfs_path_t *mc_ungetlocalcopy__pathname_vpath__captured;
  161. /* @CapturedValue */
  162. static vfs_path_t *mc_ungetlocalcopy__local_vpath__captured;
  163. /* @ThenReturnValue */
  164. static int mc_ungetlocalcopy__return_value = 0;
  165. /* @Mock */
  166. int
  167. mc_ungetlocalcopy (const vfs_path_t *pathname_vpath, const vfs_path_t *local_vpath,
  168. gboolean has_changed_ignored)
  169. {
  170. (void) has_changed_ignored;
  171. mc_ungetlocalcopy__pathname_vpath__captured = vfs_path_clone (pathname_vpath);
  172. mc_ungetlocalcopy__local_vpath__captured = vfs_path_clone (local_vpath);
  173. return mc_ungetlocalcopy__return_value;
  174. }
  175. static void
  176. mc_ungetlocalcopy__init (void)
  177. {
  178. mc_ungetlocalcopy__pathname_vpath__captured = NULL;
  179. mc_ungetlocalcopy__local_vpath__captured = NULL;
  180. }
  181. static void
  182. mc_ungetlocalcopy__deinit (void)
  183. {
  184. vfs_path_free (mc_ungetlocalcopy__pathname_vpath__captured, TRUE);
  185. vfs_path_free (mc_ungetlocalcopy__local_vpath__captured, TRUE);
  186. }
  187. /* --------------------------------------------------------------------------------------------- */
  188. /* @Before */
  189. static void
  190. setup (void)
  191. {
  192. str_init_strings (NULL);
  193. vfs_init ();
  194. vfs_init_localfs ();
  195. vfs_setup_work_dir ();
  196. vfs_file_is_local__init ();
  197. do_execute__init ();
  198. mc_getlocalcopy__init ();
  199. message__init ();
  200. mc_stat__init ();
  201. mc_ungetlocalcopy__init ();
  202. }
  203. /* --------------------------------------------------------------------------------------------- */
  204. /* @After */
  205. static void
  206. teardown (void)
  207. {
  208. mc_ungetlocalcopy__deinit ();
  209. mc_stat__deinit ();
  210. message__deinit ();
  211. mc_getlocalcopy__deinit ();
  212. do_execute__deinit ();
  213. vfs_file_is_local__deinit ();
  214. vfs_shut ();
  215. str_uninit_strings ();
  216. }
  217. /* --------------------------------------------------------------------------------------------- */