relative_cd.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* lib/vfs - test vfs_path_t manipulation functions
  2. Copyright (C) 2011-2024
  3. Free Software Foundation, Inc.
  4. Written by:
  5. Slava Zanko <slavazanko@gmail.com>, 2011, 2013
  6. This file is part of the Midnight Commander.
  7. The Midnight Commander is free software: you can redistribute it
  8. and/or modify it under the terms of the GNU General Public License as
  9. published by the Free Software Foundation, either version 3 of the License,
  10. or (at your option) any later version.
  11. The Midnight Commander is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define TEST_SUITE_NAME "/lib/vfs"
  19. #include "tests/mctest.h"
  20. #include <string.h> /* memset() */
  21. #include "lib/strutil.h"
  22. #include "lib/vfs/xdirentry.h"
  23. #include "lib/vfs/path.h"
  24. #include "lib/util.h"
  25. #include "src/vfs/local/local.c"
  26. static struct vfs_s_subclass vfs_test_subclass1;
  27. static struct vfs_class *vfs_test_ops1 = VFS_CLASS (&vfs_test_subclass1);
  28. static int test_chdir (const vfs_path_t * vpath);
  29. /* --------------------------------------------------------------------------------------------- */
  30. /* @CapturedValue */
  31. static vfs_path_t *test_chdir__vpath__captured;
  32. /* @ThenReturnValue */
  33. static int test_chdir__return_value;
  34. /* @Mock */
  35. static int
  36. test_chdir (const vfs_path_t *vpath)
  37. {
  38. test_chdir__vpath__captured = vfs_path_clone (vpath);
  39. return test_chdir__return_value;
  40. }
  41. static void
  42. test_chdir__init (void)
  43. {
  44. test_chdir__vpath__captured = NULL;
  45. }
  46. static void
  47. test_chdir__deinit (void)
  48. {
  49. vfs_path_free (test_chdir__vpath__captured, TRUE);
  50. }
  51. /* --------------------------------------------------------------------------------------------- */
  52. /* @Before */
  53. static void
  54. setup (void)
  55. {
  56. str_init_strings (NULL);
  57. vfs_init ();
  58. vfs_init_localfs ();
  59. vfs_setup_work_dir ();
  60. memset (&vfs_test_subclass1, 0, sizeof (vfs_test_subclass1));
  61. vfs_init_class (vfs_test_ops1, "testfs1", VFSF_NOLINKS | VFSF_REMOTE, "test1");
  62. vfs_test_ops1->chdir = test_chdir;
  63. vfs_register_class (vfs_test_ops1);
  64. vfs_local_ops->chdir = test_chdir;
  65. test_chdir__init ();
  66. }
  67. /* --------------------------------------------------------------------------------------------- */
  68. /* @After */
  69. static void
  70. teardown (void)
  71. {
  72. test_chdir__deinit ();
  73. vfs_shut ();
  74. str_uninit_strings ();
  75. }
  76. /* --------------------------------------------------------------------------------------------- */
  77. /* @DataSource("test_relative_cd_ds") */
  78. /* *INDENT-OFF* */
  79. static const struct test_relative_cd_ds
  80. {
  81. const char *input_string;
  82. const vfs_path_flag_t input_flags;
  83. const char *expected_element_path;
  84. } test_relative_cd_ds[] =
  85. {
  86. { /* 0. */
  87. "/test1://user:pass@some.host:12345/path/to/dir",
  88. VPF_NONE,
  89. "path/to/dir"
  90. },
  91. { /* 1. */
  92. "some-non-exists-dir",
  93. VPF_NO_CANON,
  94. "some-non-exists-dir"
  95. },
  96. };
  97. /* *INDENT-ON* */
  98. /* @Test(dataSource = "test_relative_cd_ds") */
  99. /* *INDENT-OFF* */
  100. START_PARAMETRIZED_TEST (test_relative_cd, test_relative_cd_ds)
  101. /* *INDENT-ON* */
  102. {
  103. /* given */
  104. vfs_path_t *vpath;
  105. int actual_result;
  106. test_chdir__return_value = 0;
  107. vpath = vfs_path_from_str_flags (data->input_string, data->input_flags);
  108. /* when */
  109. actual_result = mc_chdir (vpath);
  110. /* then */
  111. {
  112. const char *element_path;
  113. ck_assert_int_eq (actual_result, 0);
  114. element_path = vfs_path_get_last_path_str (vpath);
  115. mctest_assert_str_eq (element_path, data->expected_element_path);
  116. vfs_path_free (vpath, TRUE);
  117. }
  118. }
  119. /* *INDENT-OFF* */
  120. END_PARAMETRIZED_TEST
  121. /* *INDENT-ON* */
  122. /* --------------------------------------------------------------------------------------------- */
  123. /* Relative to panel_correct_path_to_show() */
  124. /* @Test */
  125. /* *INDENT-OFF* */
  126. START_TEST (test_vpath_to_str_filter)
  127. /* *INDENT-ON* */
  128. {
  129. /* given */
  130. vfs_path_t *vpath, *last_vpath;
  131. char *filtered_path;
  132. const vfs_path_element_t *path_element;
  133. /* when */
  134. vpath = vfs_path_from_str ("/test1://some.host/dir");
  135. path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, -1));
  136. vfs_path_free (vpath, TRUE);
  137. last_vpath = vfs_path_new (TRUE);
  138. vfs_path_add_element (last_vpath, path_element);
  139. filtered_path = vfs_path_to_str_flags (last_vpath, 0,
  140. VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
  141. /* then */
  142. mctest_assert_str_eq (filtered_path, "test1://some.host/dir");
  143. vfs_path_free (last_vpath, TRUE);
  144. g_free (filtered_path);
  145. }
  146. /* *INDENT-OFF* */
  147. END_TEST
  148. /* *INDENT-ON* */
  149. /* --------------------------------------------------------------------------------------------- */
  150. int
  151. main (void)
  152. {
  153. TCase *tc_core;
  154. char *cwd;
  155. tc_core = tcase_create ("Core");
  156. /* writable directory where check creates temporary files */
  157. cwd = my_get_current_dir ();
  158. g_setenv ("TEMP", cwd, TRUE);
  159. g_free (cwd);
  160. tcase_add_checked_fixture (tc_core, setup, teardown);
  161. /* Add new tests here: *************** */
  162. mctest_add_parameterized_test (tc_core, test_relative_cd, test_relative_cd_ds);
  163. tcase_add_test (tc_core, test_vpath_to_str_filter);
  164. /* *********************************** */
  165. return mctest_run_all (tc_core);
  166. }
  167. /* --------------------------------------------------------------------------------------------- */