root/tests/lib/vfs/vfs_setup_cwd.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. g_get_current_dir
  2. mc_stat
  3. setup
  4. teardown
  5. START_PARAMETRIZED_TEST
  6. main

   1 /*
   2    lib/vfs - test vfs_setup_cwd() functionality
   3 
   4    Copyright (C) 2013-2024
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Slava Zanko <slavazanko@gmail.com>, 2013
   9 
  10    This file is part of the Midnight Commander.
  11 
  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.
  16 
  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.
  21 
  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/>.
  24  */
  25 
  26 #define TEST_SUITE_NAME "/lib/vfs"
  27 
  28 #include "tests/mctest.h"
  29 
  30 #include <stdlib.h>
  31 
  32 #include "lib/strutil.h"
  33 #include "lib/vfs/xdirentry.h"
  34 #include "src/vfs/local/local.c"
  35 
  36 /* --------------------------------------------------------------------------------------------- */
  37 
  38 /* @Mock */
  39 char *
  40 g_get_current_dir (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  41 {
  42     return g_strdup ("/some/path");
  43 }
  44 
  45 /* --------------------------------------------------------------------------------------------- */
  46 
  47 static gboolean mc_stat__is_2nd_call_different = FALSE;
  48 static gboolean mc_stat__call_count = 0;
  49 
  50 /* @Mock */
  51 int
  52 mc_stat (const vfs_path_t * vpath, struct stat *my_stat)
     /* [previous][next][first][last][top][bottom][index][help]  */
  53 {
  54     (void) vpath;
  55 
  56     if (mc_stat__call_count++ > 1 && mc_stat__is_2nd_call_different)
  57     {
  58         my_stat->st_ino = 2;
  59         my_stat->st_dev = 22;
  60     }
  61     else
  62     {
  63         my_stat->st_ino = 1;
  64         my_stat->st_dev = 11;
  65     }
  66     if (mc_stat__call_count > 2)
  67     {
  68         mc_stat__call_count = 0;
  69     }
  70     return 0;
  71 }
  72 
  73 /* --------------------------------------------------------------------------------------------- */
  74 
  75 /* @Before */
  76 static void
  77 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  78 {
  79     str_init_strings (NULL);
  80 
  81     vfs_init ();
  82     vfs_init_localfs ();
  83     vfs_setup_work_dir ();
  84 }
  85 
  86 /* --------------------------------------------------------------------------------------------- */
  87 
  88 /* @After */
  89 static void
  90 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  91 {
  92     vfs_shut ();
  93     str_uninit_strings ();
  94 }
  95 
  96 /* --------------------------------------------------------------------------------------------- */
  97 
  98 /* @DataSource("test_vfs_setup_cwd_symlink_ds") */
  99 /* *INDENT-OFF* */
 100 static const struct test_vfs_setup_cwd_symlink_ds
 101 {
 102     gboolean is_2nd_call_different;
 103     const char *expected_result;
 104 } test_vfs_setup_cwd_symlink_ds[] =
 105 {
 106     { /* 0. */
 107         TRUE,
 108         "/some/path"
 109     },
 110     { /* 1. */
 111         FALSE,
 112         "/some/path2"
 113     },
 114 };
 115 /* *INDENT-ON* */
 116 
 117 /* @Test */
 118 /* *INDENT-OFF* */
 119 START_PARAMETRIZED_TEST (test_vfs_setup_cwd_symlink, test_vfs_setup_cwd_symlink_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 120 /* *INDENT-ON* */
 121 {
 122     /* given */
 123     vfs_set_raw_current_dir (NULL);
 124     mc_stat__is_2nd_call_different = data->is_2nd_call_different;
 125     mc_stat__call_count = 0;
 126     setenv ("PWD", "/some/path2", 1);
 127 
 128     /* when */
 129     vfs_setup_cwd ();
 130 
 131     /* then */
 132     mctest_assert_str_eq (vfs_get_current_dir (), data->expected_result);
 133 }
 134 /* *INDENT-OFF* */
 135 END_PARAMETRIZED_TEST
 136 /* *INDENT-ON* */
 137 
 138 /* --------------------------------------------------------------------------------------------- */
 139 
 140 int
 141 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 142 {
 143     TCase *tc_core;
 144 
 145     tc_core = tcase_create ("Core");
 146 
 147     tcase_add_checked_fixture (tc_core, setup, teardown);
 148 
 149     /* Add new tests here: *************** */
 150     mctest_add_parameterized_test (tc_core, test_vfs_setup_cwd_symlink,
 151                                    test_vfs_setup_cwd_symlink_ds);
 152     /* *********************************** */
 153 
 154     return mctest_run_all (tc_core);
 155 }
 156 
 157 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */