File copy/move: make ETA accurate.
[midnight-commander.git] / tests / lib / mc_realpath.c
blob2af5406a6e440ed38f0bc0ff24ba98be27eabd49
1 /*
2 lib - realpath
4 Copyright (C) 2017-2024
5 Free Software Foundation, Inc.
7 Written by:
8 Andrew Borodin <aborodin@vmail.ru>, 2017
10 This file is part of the Midnight Commander.
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.
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.
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/>.
26 #define TEST_SUITE_NAME "/lib/util"
28 #include "tests/mctest.h"
30 #include "lib/strutil.h"
31 #include "lib/vfs/vfs.h" /* VFS_ENCODING_PREFIX, vfs_init(), vfs_shut() */
32 #include "src/vfs/local/local.c"
34 #include "lib/util.h" /* mc_realpath() */
36 /* --------------------------------------------------------------------------------------------- */
38 static char resolved_path[PATH_MAX];
40 /* --------------------------------------------------------------------------------------------- */
42 /* @Before */
43 static void
44 setup (void)
46 str_init_strings (NULL);
47 vfs_init ();
48 vfs_init_localfs ();
49 vfs_setup_work_dir ();
52 /* @After */
53 static void
54 teardown (void)
56 vfs_shut ();
57 str_uninit_strings ();
60 /* --------------------------------------------------------------------------------------------- */
62 /* @DataSource("data_source") */
63 /* *INDENT-OFF* */
64 static const struct data_source
66 const char *input_string;
67 const char *expected_string;
68 } data_source[] =
70 /* absolute paths */
71 { "/", "/"},
72 { "/usr/bin", "/usr/bin" },
73 #ifdef HAVE_CHARSET
74 { "/" VFS_ENCODING_PREFIX "UTF-8/", "/" },
75 { "/" VFS_ENCODING_PREFIX "UTF-8/usr/bin", "/usr/bin" },
76 #else
77 { "/" VFS_ENCODING_PREFIX "UTF-8/", "/" VFS_ENCODING_PREFIX "UTF-8/" },
78 { "/" VFS_ENCODING_PREFIX "UTF-8/usr/bin", "/" VFS_ENCODING_PREFIX "UTF-8/usr/bin" },
79 #endif
81 /* relative paths are relative to / */
82 { "usr/bin", "/usr/bin" },
83 #ifdef HAVE_CHARSET
84 { VFS_ENCODING_PREFIX "UTF-8/", "/" },
85 { VFS_ENCODING_PREFIX "UTF-8/usr/bin", "/usr/bin" }
86 #else
87 { VFS_ENCODING_PREFIX "UTF-8/", VFS_ENCODING_PREFIX "UTF-8/" },
88 { VFS_ENCODING_PREFIX "UTF-8/usr/bin", VFS_ENCODING_PREFIX "UTF-8/usr/bin" }
89 #endif
91 /* *INDENT-ON* */
93 /* @Test(dataSource = "data_source") */
94 /* *INDENT-OFF* */
95 START_PARAMETRIZED_TEST (realpath_test, data_source)
96 /* *INDENT-ON* */
98 int ret;
100 /* realpath(3) produces a canonicalized absolute pathname using current directory.
101 * Change the current directory to produce correct pathname. */
102 ret = chdir ("/");
104 /* when */
105 if (mc_realpath (data->input_string, resolved_path) == NULL)
106 resolved_path[0] = '\0';
108 /* then */
109 mctest_assert_str_eq (resolved_path, data->expected_string);
111 (void) ret;
113 /* *INDENT-OFF* */
114 END_PARAMETRIZED_TEST
115 /* *INDENT-ON* */
117 /* --------------------------------------------------------------------------------------------- */
120 main (void)
122 TCase *tc_core;
123 char *cwd;
125 tc_core = tcase_create ("Core");
127 /* writable directory where check creates temporary files */
128 cwd = my_get_current_dir ();
129 g_setenv ("TEMP", cwd, TRUE);
130 g_free (cwd);
132 tcase_add_checked_fixture (tc_core, setup, teardown);
134 /* Add new tests here: *************** */
135 mctest_add_parameterized_test (tc_core, realpath_test, data_source);
136 /* *********************************** */
138 return mctest_run_all (tc_core);
141 /* --------------------------------------------------------------------------------------------- */