Add tests for src/execute.c:execute_with_vfs_arg() function.
authorSlava Zanko <slavazanko@gmail.com>
Wed, 9 Jan 2013 21:31:51 +0000 (10 00:31 +0300)
committerSlava Zanko <slavazanko@gmail.com>
Sat, 16 Feb 2013 14:39:33 +0000 (16 17:39 +0300)
Signed-off-by: Slava Zanko <slavazanko@gmail.com>
src/execute.c
tests/src/Makefile.am
tests/src/execute__common.c [new file with mode: 0644]
tests/src/execute__execute_with_vfs_arg.c [new file with mode: 0644]

index 30fc0f8..3b2fd36 100644 (file)
@@ -61,6 +61,9 @@ int pause_after_run = pause_on_dumb_terminals;
 /*** file scope variables ************************************************************************/
 
 /*** file scope functions ************************************************************************/
+
+void do_execute (const char *shell, const char *command, int flags);
+
 /* --------------------------------------------------------------------------------------------- */
 
 static void
@@ -129,6 +132,40 @@ do_possible_cd (const vfs_path_t * new_dir_vpath)
 /* --------------------------------------------------------------------------------------------- */
 
 static void
+do_suspend_cmd (void)
+{
+    pre_exec ();
+
+    if (mc_global.tty.console_flag != '\0' && !mc_global.tty.use_subshell)
+        handle_console (CONSOLE_RESTORE);
+
+#ifdef SIGTSTP
+    {
+        struct sigaction sigtstp_action;
+
+        /* Make sure that the SIGTSTP below will suspend us directly,
+           without calling ncurses' SIGTSTP handler; we *don't* want
+           ncurses to redraw the screen immediately after the SIGCONT */
+        sigaction (SIGTSTP, &startup_handler, &sigtstp_action);
+
+        kill (getpid (), SIGTSTP);
+
+        /* Restore previous SIGTSTP action */
+        sigaction (SIGTSTP, &sigtstp_action, NULL);
+    }
+#endif /* SIGTSTP */
+
+    if (mc_global.tty.console_flag != '\0' && !mc_global.tty.use_subshell)
+        handle_console (CONSOLE_SAVE);
+
+    edition_post_exec ();
+}
+
+/* --------------------------------------------------------------------------------------------- */
+/*** public functions ****************************************************************************/
+/* --------------------------------------------------------------------------------------------- */
+
+void
 do_execute (const char *shell, const char *command, int flags)
 {
 #ifdef ENABLE_SUBSHELL
@@ -221,40 +258,6 @@ do_execute (const char *shell, const char *command, int flags)
 
 /* --------------------------------------------------------------------------------------------- */
 
-static void
-do_suspend_cmd (void)
-{
-    pre_exec ();
-
-    if (mc_global.tty.console_flag != '\0' && !mc_global.tty.use_subshell)
-        handle_console (CONSOLE_RESTORE);
-
-#ifdef SIGTSTP
-    {
-        struct sigaction sigtstp_action;
-
-        /* Make sure that the SIGTSTP below will suspend us directly,
-           without calling ncurses' SIGTSTP handler; we *don't* want
-           ncurses to redraw the screen immediately after the SIGCONT */
-        sigaction (SIGTSTP, &startup_handler, &sigtstp_action);
-
-        kill (getpid (), SIGTSTP);
-
-        /* Restore previous SIGTSTP action */
-        sigaction (SIGTSTP, &sigtstp_action, NULL);
-    }
-#endif /* SIGTSTP */
-
-    if (mc_global.tty.console_flag != '\0' && !mc_global.tty.use_subshell)
-        handle_console (CONSOLE_SAVE);
-
-    edition_post_exec ();
-}
-
-/* --------------------------------------------------------------------------------------------- */
-/*** public functions ****************************************************************************/
-/* --------------------------------------------------------------------------------------------- */
-
 /** Set up the terminal before executing a program */
 
 void
@@ -460,14 +463,15 @@ execute_with_vfs_arg (const char *command, const vfs_path_t * filename_vpath)
     vfs_path_t *localcopy_vpath;
 
     /* Simplest case, this file is local */
-    if (filename_vpath == NULL || vfs_file_is_local (filename_vpath))
+    if ((filename_vpath == NULL && vfs_file_is_local (vfs_get_raw_current_dir ()))
+        || vfs_file_is_local (filename_vpath))
     {
         do_execute (command, vfs_path_get_last_path_str (filename_vpath), EXECUTE_INTERNAL);
         return;
     }
 
     /* FIXME: Creation of new files on VFS is not supported */
-    if (vfs_path_len (filename_vpath) == 0)
+    if (filename_vpath == NULL)
         return;
 
     localcopy_vpath = mc_getlocalcopy (filename_vpath);
dissimilarity index 100%
index c2c82a4..c5b4f9f 100644 (file)
@@ -1 +1,28 @@
-SUBDIRS = filemanager
+SUBDIRS = . filemanager
+
+AM_CPPFLAGS = \
+       $(GLIB_CFLAGS) \
+       -I$(top_srcdir) \
+       -I$(top_srcdir)/lib/vfs \
+       @CHECK_CFLAGS@
+
+AM_LDFLAGS = @TESTS_LDFLAGS@
+
+LIBS=@CHECK_LIBS@  \
+       $(top_builddir)/src/libinternal.la \
+       $(top_builddir)/lib/libmc.la
+
+if ENABLE_VFS_SMB
+# this is a hack for linking with own samba library in simple way
+LIBS += $(top_builddir)/src/vfs/smbfs/helpers/libsamba.a
+endif
+
+EXTRA_DIST = execute__common.c
+
+TESTS = \
+       execute__execute_with_vfs_arg
+
+check_PROGRAMS = $(TESTS)
+
+execute__execute_with_vfs_arg_SOURCES = \
+       execute__execute_with_vfs_arg.c
diff --git a/tests/src/execute__common.c b/tests/src/execute__common.c
new file mode 100644 (file)
index 0000000..97d12f0
--- /dev/null
@@ -0,0 +1,270 @@
+/*
+   Common code for testing functions in src/execute.c file.
+
+   Copyright (C) 2013
+
+   The Free Software Foundation, Inc.
+
+   Written by:
+   Slava Zanko <slavazanko@gmail.com>, 2013
+
+   This file is part of the Midnight Commander.
+
+   The Midnight Commander is free software: you can redistribute it
+   and/or modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation, either version 3 of the License,
+   or (at your option) any later version.
+
+   The Midnight Commander is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "lib/widget.h"
+#include "lib/strutil.h"
+#include "lib/vfs/path.h"
+#include "src/vfs/local/local.c"
+
+#include "src/execute.h"
+
+/* --------------------------------------------------------------------------------------------- */
+
+/* @CapturedValue */
+static GPtrArray *vfs_file_is_local__vpath__captured;
+
+/* @ThenReturnValue */
+static gboolean vfs_file_is_local__return_value;
+
+/* @Mock */
+gboolean
+vfs_file_is_local (const vfs_path_t * vpath)
+{
+    g_ptr_array_add (vfs_file_is_local__vpath__captured, vfs_path_clone (vpath));
+    return vfs_file_is_local__return_value;
+}
+
+static void
+vfs_file_is_local__init ()
+{
+    vfs_file_is_local__vpath__captured = g_ptr_array_new ();
+}
+
+static void
+vfs_file_is_local__deinit ()
+{
+    g_ptr_array_foreach (vfs_file_is_local__vpath__captured, (GFunc) vfs_path_free, NULL);
+    g_ptr_array_free (vfs_file_is_local__vpath__captured, TRUE);
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+void do_execute (const char *lc_shell, const char *command, int flags);
+
+/* @CapturedValue */
+static char *do_execute__lc_shell__captured;
+/* @CapturedValue */
+static char *do_execute__command__captured;
+/* @CapturedValue */
+static int do_execute__flags__captured;
+
+/* @Mock */
+void
+do_execute (const char *lc_shell, const char *command, int flags)
+{
+    do_execute__lc_shell__captured = g_strdup (lc_shell);
+    do_execute__command__captured = g_strdup (command);
+    do_execute__flags__captured = flags;
+}
+
+static void
+do_execute__init ()
+{
+    do_execute__command__captured = NULL;
+    do_execute__lc_shell__captured = NULL;
+}
+
+static void
+do_execute__deinit ()
+{
+    g_free (do_execute__lc_shell__captured);
+    g_free (do_execute__command__captured);
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+/* @CapturedValue */
+static vfs_path_t *mc_getlocalcopy__pathname_vpath__captured;
+/* @ThenReturnValue */
+static vfs_path_t *mc_getlocalcopy__return_value;
+
+/* @Mock */
+vfs_path_t *
+mc_getlocalcopy (const vfs_path_t * pathname_vpath)
+{
+    mc_getlocalcopy__pathname_vpath__captured = vfs_path_clone (pathname_vpath);
+    return mc_getlocalcopy__return_value;
+}
+
+static void
+mc_getlocalcopy__init ()
+{
+    mc_getlocalcopy__pathname_vpath__captured = NULL;
+    mc_getlocalcopy__return_value = NULL;
+}
+
+static void
+mc_getlocalcopy__deinit ()
+{
+    vfs_path_free (mc_getlocalcopy__pathname_vpath__captured);
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+
+/* @CapturedValue */
+static int message_flags__captured;
+
+/* @CapturedValue */
+static char *message_title__captured;
+
+/* @CapturedValue */
+static char *message_text__captured;
+
+/* @Mock */
+void
+message (int flags, const char *title, const char *text, ...)
+{
+    va_list ap;
+
+    message_flags__captured = flags;
+
+    message_title__captured = (title == MSG_ERROR) ? g_strdup (_("Error")) : g_strdup (title);
+
+    va_start (ap, text);
+    message_text__captured = g_strdup_vprintf (text, ap);
+    va_end (ap);
+
+}
+
+static void
+message__init ()
+{
+    message_flags__captured = 0;
+    message_title__captured = NULL;
+    message_text__captured = NULL;
+}
+
+static void
+message__deinit ()
+{
+    g_free (message_title__captured);
+    g_free (message_text__captured);
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+/* @CapturedValue */
+static GPtrArray *mc_stat__vpath__captured = NULL;
+/* @ThenReturnValue */
+static int mc_stat__return_value = 0;
+
+/* @Mock */
+int
+mc_stat (const vfs_path_t * vpath, struct stat *stat_ignored)
+{
+    (void) stat_ignored;
+    if (mc_stat__vpath__captured != NULL)
+        g_ptr_array_add (mc_stat__vpath__captured, vfs_path_clone (vpath));
+    return mc_stat__return_value;
+}
+
+
+static void
+mc_stat__init ()
+{
+    mc_stat__vpath__captured = g_ptr_array_new ();
+}
+
+static void
+mc_stat__deinit ()
+{
+    g_ptr_array_foreach (mc_stat__vpath__captured, (GFunc) vfs_path_free, NULL);
+    g_ptr_array_free (mc_stat__vpath__captured, TRUE);
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+/* @CapturedValue */
+static vfs_path_t *mc_ungetlocalcopy__pathname_vpath__captured;
+/* @CapturedValue */
+static vfs_path_t *mc_ungetlocalcopy__local_vpath__captured;
+/* @ThenReturnValue */
+static int mc_ungetlocalcopy__return_value = 0;
+
+/* @Mock */
+int
+mc_ungetlocalcopy (const vfs_path_t * pathname_vpath, const vfs_path_t * local_vpath,
+                   gboolean has_changed_ignored)
+{
+    (void) has_changed_ignored;
+
+    mc_ungetlocalcopy__pathname_vpath__captured = vfs_path_clone (pathname_vpath);
+    mc_ungetlocalcopy__local_vpath__captured = vfs_path_clone (local_vpath);
+    return mc_ungetlocalcopy__return_value;
+}
+
+static void
+mc_ungetlocalcopy__init ()
+{
+    mc_ungetlocalcopy__pathname_vpath__captured = NULL;
+    mc_ungetlocalcopy__local_vpath__captured = NULL;
+}
+
+static void
+mc_ungetlocalcopy__deinit ()
+{
+    vfs_path_free (mc_ungetlocalcopy__pathname_vpath__captured);
+    vfs_path_free (mc_ungetlocalcopy__local_vpath__captured);
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+/* @Before */
+static void
+setup (void)
+{
+    str_init_strings (NULL);
+    vfs_init ();
+    init_localfs ();
+    vfs_setup_work_dir ();
+
+    vfs_file_is_local__init ();
+    do_execute__init ();
+    mc_getlocalcopy__init ();
+    message__init ();
+    mc_stat__init ();
+    mc_ungetlocalcopy__init ();
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+/* @After */
+static void
+teardown (void)
+{
+    mc_ungetlocalcopy__deinit ();
+    mc_stat__deinit ();
+    message__deinit ();
+    mc_getlocalcopy__deinit ();
+    do_execute__deinit ();
+    vfs_file_is_local__deinit ();
+
+    vfs_shut ();
+    str_uninit_strings ();
+}
+
+/* --------------------------------------------------------------------------------------------- */
diff --git a/tests/src/execute__execute_with_vfs_arg.c b/tests/src/execute__execute_with_vfs_arg.c
new file mode 100644 (file)
index 0000000..052786a
--- /dev/null
@@ -0,0 +1,251 @@
+/*
+   src - tests for execute_with_vfs_arg() function
+
+   Copyright (C) 2013
+   The Free Software Foundation, Inc.
+
+   Written by:
+   Slava Zanko <slavazanko@gmail.com>, 2013
+
+   This file is part of the Midnight Commander.
+
+   The Midnight Commander is free software: you can redistribute it
+   and/or modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation, either version 3 of the License,
+   or (at your option) any later version.
+
+   The Midnight Commander is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define TEST_SUITE_NAME "/src"
+
+#include <config.h>
+
+#include <check.h>
+
+#include "lib/global.h"
+
+#include "execute__common.c"
+
+/* --------------------------------------------------------------------------------------------- */
+
+/* @DataSource("the_file_is_local_ds") */
+/* *INDENT-OFF* */
+static const struct the_file_is_local_ds
+{
+    const char *input_path;
+} the_file_is_local_ds[] =
+{
+    {
+        NULL,
+    },
+    {
+        "/blabla",
+    },
+};
+/* *INDENT-ON* */
+
+/* @Test(dataSource = "the_file_is_local_ds") */
+/* *INDENT-OFF* */
+START_TEST (the_file_is_local)
+/* *INDENT-ON* */
+{
+    /* given */
+    vfs_path_t *filename_vpath;
+    const char *input_path = the_file_is_local_ds[_i].input_path;
+    filename_vpath = vfs_path_from_str (input_path);
+
+    vfs_file_is_local__return_value = TRUE;
+
+    /* when */
+    execute_with_vfs_arg ("cmd_for_local_file", filename_vpath);
+
+    /* then */
+    g_assert_cmpstr (do_execute__lc_shell__captured, ==, "cmd_for_local_file");
+    g_assert_cmpstr (do_execute__command__captured, ==, input_path);
+
+    ck_assert_int_eq (vfs_file_is_local__vpath__captured->len, 1);
+    {
+        const vfs_path_t *tmp_vpath;
+
+        tmp_vpath = (input_path == NULL) ? vfs_get_raw_current_dir () : filename_vpath;
+        ck_assert_int_eq (vfs_path_cmp
+                          (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0), tmp_vpath),
+                          0);
+    }
+    ck_assert_int_eq (do_execute__flags__captured, EXECUTE_INTERNAL);
+    fail_unless (mc_getlocalcopy__pathname_vpath__captured == NULL,
+                 "\nFunction mc_getlocalcopy() shouldn't be called!");
+
+    vfs_path_free (filename_vpath);
+}
+/* *INDENT-OFF* */
+END_TEST
+/* *INDENT-ON* */
+
+/* --------------------------------------------------------------------------------------------- */
+
+/* @Test */
+/* *INDENT-OFF* */
+START_TEST (the_file_is_remote_but_empty)
+/* *INDENT-ON* */
+{
+    /* given */
+    vfs_path_t *filename_vpath;
+    filename_vpath = NULL;
+
+    vfs_file_is_local__return_value = FALSE;
+
+    /* when */
+    execute_with_vfs_arg ("cmd_for_remote_file", filename_vpath);
+
+    /* then */
+    g_assert_cmpstr (do_execute__lc_shell__captured, ==, NULL);
+    g_assert_cmpstr (do_execute__command__captured, ==, NULL);
+
+    ck_assert_int_eq (vfs_file_is_local__vpath__captured->len, 2);
+
+    ck_assert_int_eq (vfs_path_cmp
+                      (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0),
+                       vfs_get_raw_current_dir ()), 0);
+    fail_unless (g_ptr_array_index (vfs_file_is_local__vpath__captured, 1) == NULL,
+                 "\nParameter for second call to vfs_file_is_local() should be NULL!");
+    fail_unless (mc_getlocalcopy__pathname_vpath__captured == NULL,
+                 "\nFunction mc_getlocalcopy() shouldn't be called!");
+
+    vfs_path_free (filename_vpath);
+}
+/* *INDENT-OFF* */
+END_TEST
+/* *INDENT-ON* */
+
+/* --------------------------------------------------------------------------------------------- */
+
+/* @Test */
+/* *INDENT-OFF* */
+START_TEST (the_file_is_remote_fail_to_create_local_copy)
+/* *INDENT-ON* */
+{
+    /* given */
+    vfs_path_t *filename_vpath;
+
+    filename_vpath = vfs_path_from_str ("/ftp://some.host/editme.txt");
+
+    vfs_file_is_local__return_value = FALSE;
+    mc_getlocalcopy__return_value = NULL;
+
+    /* when */
+    execute_with_vfs_arg ("cmd_for_remote_file", filename_vpath);
+
+    /* then */
+    g_assert_cmpstr (do_execute__lc_shell__captured, ==, NULL);
+    g_assert_cmpstr (do_execute__command__captured, ==, NULL);
+
+    ck_assert_int_eq (vfs_file_is_local__vpath__captured->len, 1);
+
+    ck_assert_int_eq (vfs_path_cmp
+                      (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0), filename_vpath),
+                      0);
+
+    ck_assert_int_eq (vfs_path_cmp (mc_getlocalcopy__pathname_vpath__captured, filename_vpath), 0);
+
+    g_assert_cmpstr (message_title__captured, ==, _("Error"));
+    g_assert_cmpstr (message_text__captured, ==,
+                     _("Cannot fetch a local copy of /ftp://some.host/editme.txt"));
+
+
+    vfs_path_free (filename_vpath);
+}
+/* *INDENT-OFF* */
+END_TEST
+/* *INDENT-ON* */
+
+/* --------------------------------------------------------------------------------------------- */
+
+/* @Test */
+/* *INDENT-OFF* */
+START_TEST (the_file_is_remote)
+/* *INDENT-ON* */
+{
+    /* given */
+    vfs_path_t *filename_vpath, *local_vpath, *local_vpath_should_be_freeing;
+
+    filename_vpath = vfs_path_from_str ("/ftp://some.host/editme.txt");
+    local_vpath = vfs_path_from_str ("/tmp/blabla-editme.txt");
+    local_vpath_should_be_freeing = vfs_path_clone (local_vpath);
+
+    vfs_file_is_local__return_value = FALSE;
+    mc_getlocalcopy__return_value = local_vpath_should_be_freeing;
+
+    /* when */
+    execute_with_vfs_arg ("cmd_for_remote_file", filename_vpath);
+
+    /* then */
+    g_assert_cmpstr (do_execute__lc_shell__captured, ==, "cmd_for_remote_file");
+    g_assert_cmpstr (do_execute__command__captured, ==, "/tmp/blabla-editme.txt");
+
+    ck_assert_int_eq (vfs_file_is_local__vpath__captured->len, 1);
+
+    ck_assert_int_eq (vfs_path_cmp
+                      (g_ptr_array_index (vfs_file_is_local__vpath__captured, 0), filename_vpath),
+                      0);
+
+    ck_assert_int_eq (vfs_path_cmp (mc_getlocalcopy__pathname_vpath__captured, filename_vpath), 0);
+
+    ck_assert_int_eq (mc_stat__vpath__captured->len, 2);
+
+    ck_assert_int_eq (vfs_path_cmp
+                      (g_ptr_array_index (mc_stat__vpath__captured, 0), local_vpath), 0);
+    ck_assert_int_eq (vfs_path_cmp
+                      (g_ptr_array_index (mc_stat__vpath__captured, 0),
+                       g_ptr_array_index (mc_stat__vpath__captured, 1)), 0);
+
+    ck_assert_int_eq (vfs_path_cmp (mc_ungetlocalcopy__pathname_vpath__captured, filename_vpath),
+                      0);
+
+    ck_assert_int_eq (vfs_path_cmp (mc_ungetlocalcopy__local_vpath__captured, local_vpath), 0);
+
+    vfs_path_free (filename_vpath);
+    vfs_path_free (local_vpath);
+}
+/* *INDENT-OFF* */
+END_TEST
+/* *INDENT-ON* */
+
+/* --------------------------------------------------------------------------------------------- */
+
+int
+main (void)
+{
+    int number_failed;
+
+    Suite *s = suite_create (TEST_SUITE_NAME);
+    TCase *tc_core = tcase_create ("Core");
+    SRunner *sr;
+
+    tcase_add_checked_fixture (tc_core, setup, teardown);
+
+    /* Add new tests here: *************** */
+    tcase_add_loop_test (tc_core, the_file_is_local, 0,
+                         sizeof (the_file_is_local_ds) / sizeof (the_file_is_local_ds[0]));
+    tcase_add_test (tc_core, the_file_is_remote_but_empty);
+    tcase_add_test (tc_core, the_file_is_remote_fail_to_create_local_copy);
+    tcase_add_test (tc_core, the_file_is_remote);
+    /* *********************************** */
+
+    suite_add_tcase (s, tc_core);
+    sr = srunner_create (s);
+    srunner_set_log (sr, "execute__execute_with_vfs_arg.log");
+    srunner_run_all (sr, CK_NORMAL);
+    number_failed = srunner_ntests_failed (sr);
+    srunner_free (sr);
+    return (number_failed == 0) ? 0 : 1;
+}
+
+/* --------------------------------------------------------------------------------------------- */