current_dir.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. lib/vfs - manipulate with current directory
  3. Copyright (C) 2011-2025
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2011, 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 <https://www.gnu.org/licenses/>.
  18. */
  19. #define TEST_SUITE_NAME "/lib/vfs"
  20. #include "tests/mctest.h"
  21. #include <string.h> // memset()
  22. #include "lib/global.h"
  23. #include "lib/strutil.h"
  24. #include "lib/vfs/xdirentry.h"
  25. #include "lib/util.h"
  26. #include "src/vfs/local/local.c"
  27. static struct vfs_s_subclass vfs_test_subclass;
  28. static struct vfs_class *vfs_test_ops = VFS_CLASS (&vfs_test_subclass);
  29. /* --------------------------------------------------------------------------------------------- */
  30. /* @Mock */
  31. static int
  32. test_chdir (const vfs_path_t *vpath)
  33. {
  34. (void) vpath;
  35. return 0;
  36. }
  37. /* --------------------------------------------------------------------------------------------- */
  38. /* @Before */
  39. static void
  40. setup (void)
  41. {
  42. str_init_strings (NULL);
  43. vfs_init ();
  44. vfs_init_localfs ();
  45. vfs_setup_work_dir ();
  46. memset (&vfs_test_subclass, 0, sizeof (vfs_test_subclass));
  47. vfs_init_class (vfs_test_ops, "testfs", VFSF_UNKNOWN, "test");
  48. vfs_test_ops->chdir = test_chdir;
  49. }
  50. /* --------------------------------------------------------------------------------------------- */
  51. /* @After */
  52. static void
  53. teardown (void)
  54. {
  55. vfs_shut ();
  56. str_uninit_strings ();
  57. }
  58. /* --------------------------------------------------------------------------------------------- */
  59. /* @DataSource("test_cd_ds") */
  60. static const struct test_cd_ds
  61. {
  62. const char *input_initial_path;
  63. const char *input_cd_path;
  64. const vfs_flags_t input_class_flags;
  65. const char *expected_cd_path;
  66. } test_cd_ds[] = {
  67. {
  68. // 0.
  69. "/",
  70. "/dev/some.file/test://",
  71. VFSF_NOLINKS,
  72. "/dev/some.file/test://",
  73. },
  74. {
  75. // 1.
  76. "/",
  77. "/dev/some.file/test://bla-bla",
  78. VFSF_NOLINKS,
  79. "/dev/some.file/test://bla-bla",
  80. },
  81. {
  82. // 2.
  83. "/dev/some.file/test://bla-bla",
  84. "..",
  85. VFSF_NOLINKS,
  86. "/dev/some.file/test://",
  87. },
  88. {
  89. // 3.
  90. "/dev/some.file/test://",
  91. "..",
  92. VFSF_NOLINKS,
  93. "/dev",
  94. },
  95. {
  96. // 4.
  97. "/dev",
  98. "..",
  99. VFSF_NOLINKS,
  100. "/",
  101. },
  102. {
  103. // 5.
  104. "/",
  105. "..",
  106. VFSF_NOLINKS,
  107. "/",
  108. },
  109. {
  110. // 6.
  111. "/",
  112. "/test://user:pass@host.net/path",
  113. VFSF_NOLINKS | VFSF_REMOTE,
  114. "/test://user:pass@host.net/path",
  115. },
  116. {
  117. // 7.
  118. "/test://user:pass@host.net/path",
  119. "..",
  120. VFSF_NOLINKS | VFSF_REMOTE,
  121. "/test://user:pass@host.net/",
  122. },
  123. {
  124. // 8.
  125. "/test://user:pass@host.net/",
  126. "..",
  127. VFSF_NOLINKS | VFSF_REMOTE,
  128. "/",
  129. },
  130. };
  131. /* @Test(dataSource = "test_cd_ds") */
  132. START_PARAMETRIZED_TEST (test_cd, test_cd_ds)
  133. {
  134. // given
  135. vfs_path_t *vpath;
  136. vfs_test_ops->flags = data->input_class_flags;
  137. vfs_register_class (vfs_test_ops);
  138. vfs_set_raw_current_dir (vfs_path_from_str (data->input_initial_path));
  139. vpath = vfs_path_from_str (data->input_cd_path);
  140. // when
  141. mc_chdir (vpath);
  142. // then
  143. {
  144. char *actual_cd_path;
  145. actual_cd_path = vfs_get_cwd ();
  146. mctest_assert_str_eq (actual_cd_path, data->expected_cd_path);
  147. g_free (actual_cd_path);
  148. }
  149. vfs_path_free (vpath, TRUE);
  150. vfs_unregister_class (vfs_test_ops);
  151. }
  152. END_PARAMETRIZED_TEST
  153. /* --------------------------------------------------------------------------------------------- */
  154. int
  155. main (void)
  156. {
  157. TCase *tc_core;
  158. char *cwd;
  159. tc_core = tcase_create ("Core");
  160. // writable directory where check creates temporary files
  161. cwd = my_get_current_dir ();
  162. g_setenv ("TEMP", cwd, TRUE);
  163. g_free (cwd);
  164. tcase_add_checked_fixture (tc_core, setup, teardown);
  165. // Add new tests here: ***************
  166. mctest_add_parameterized_test (tc_core, test_cd, test_cd_ds);
  167. // ***********************************
  168. return mctest_run_all (tc_core);
  169. }
  170. /* --------------------------------------------------------------------------------------------- */