1 /*
2 lib/vfs - test vfs_setup_cwd() functionality
3
4 Copyright (C) 2013-2025
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 <https://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/util.h"
34 #include "lib/vfs/xdirentry.h"
35 #include "src/vfs/local/local.c"
36
37 /* --------------------------------------------------------------------------------------------- */
38
39 /* @Mock */
40 char *
41 my_get_current_dir (void)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
42 {
43 return g_strdup ("/some/path");
44 }
45
46 /* --------------------------------------------------------------------------------------------- */
47
48 static gboolean mc_stat__is_2nd_call_different = FALSE;
49 static gboolean mc_stat__call_count = 0;
50
51 /* @Mock */
52 int
53 mc_stat (const vfs_path_t *vpath, struct stat *my_stat)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
54 {
55 (void) vpath;
56
57 if (mc_stat__call_count++ > 1 && mc_stat__is_2nd_call_different)
58 {
59 my_stat->st_ino = 2;
60 my_stat->st_dev = 22;
61 }
62 else
63 {
64 my_stat->st_ino = 1;
65 my_stat->st_dev = 11;
66 }
67 if (mc_stat__call_count > 2)
68 {
69 mc_stat__call_count = 0;
70 }
71 return 0;
72 }
73
74 /* --------------------------------------------------------------------------------------------- */
75
76 /* @Before */
77 static void
78 setup (void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
79 {
80 str_init_strings (NULL);
81
82 vfs_init ();
83 vfs_init_localfs ();
84 vfs_setup_work_dir ();
85 }
86
87 /* --------------------------------------------------------------------------------------------- */
88
89 /* @After */
90 static void
91 teardown (void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
92 {
93 vfs_shut ();
94 str_uninit_strings ();
95 }
96
97 /* --------------------------------------------------------------------------------------------- */
98
99 /* @DataSource("test_vfs_setup_cwd_symlink_ds") */
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 { TRUE, "/some/path" }, // 0.
106 { FALSE, "/some/path2" }, // 1.
107 };
108
109 /* @Test */
110 START_PARAMETRIZED_TEST (test_vfs_setup_cwd_symlink, test_vfs_setup_cwd_symlink_ds)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
111 {
112 // given
113 vfs_set_raw_current_dir (NULL);
114 mc_stat__is_2nd_call_different = data->is_2nd_call_different;
115 mc_stat__call_count = 0;
116 setenv ("PWD", "/some/path2", 1);
117
118 // when
119 vfs_setup_cwd ();
120
121 // then
122 mctest_assert_str_eq (vfs_get_current_dir (), data->expected_result);
123 }
124 END_PARAMETRIZED_TEST
125
126 /* --------------------------------------------------------------------------------------------- */
127
128 int
129 main (void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
130 {
131 TCase *tc_core;
132
133 tc_core = tcase_create ("Core");
134
135 tcase_add_checked_fixture (tc_core, setup, teardown);
136
137 // Add new tests here: ***************
138 mctest_add_parameterized_test (tc_core, test_vfs_setup_cwd_symlink,
139 test_vfs_setup_cwd_symlink_ds);
140 // ***********************************
141
142 return mctest_run_all (tc_core);
143 }
144
145 /* --------------------------------------------------------------------------------------------- */