vfs_setup_cwd.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. lib/vfs - test vfs_setup_cwd() functionality
  3. Copyright (C) 2013-2024
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 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 <http://www.gnu.org/licenses/>.
  18. */
  19. #define TEST_SUITE_NAME "/lib/vfs"
  20. #include "tests/mctest.h"
  21. #include <stdlib.h>
  22. #include "lib/strutil.h"
  23. #include "lib/util.h"
  24. #include "lib/vfs/xdirentry.h"
  25. #include "src/vfs/local/local.c"
  26. /* --------------------------------------------------------------------------------------------- */
  27. /* @Mock */
  28. char *
  29. my_get_current_dir (void)
  30. {
  31. return g_strdup ("/some/path");
  32. }
  33. /* --------------------------------------------------------------------------------------------- */
  34. static gboolean mc_stat__is_2nd_call_different = FALSE;
  35. static gboolean mc_stat__call_count = 0;
  36. /* @Mock */
  37. int
  38. mc_stat (const vfs_path_t *vpath, struct stat *my_stat)
  39. {
  40. (void) vpath;
  41. if (mc_stat__call_count++ > 1 && mc_stat__is_2nd_call_different)
  42. {
  43. my_stat->st_ino = 2;
  44. my_stat->st_dev = 22;
  45. }
  46. else
  47. {
  48. my_stat->st_ino = 1;
  49. my_stat->st_dev = 11;
  50. }
  51. if (mc_stat__call_count > 2)
  52. {
  53. mc_stat__call_count = 0;
  54. }
  55. return 0;
  56. }
  57. /* --------------------------------------------------------------------------------------------- */
  58. /* @Before */
  59. static void
  60. setup (void)
  61. {
  62. str_init_strings (NULL);
  63. vfs_init ();
  64. vfs_init_localfs ();
  65. vfs_setup_work_dir ();
  66. }
  67. /* --------------------------------------------------------------------------------------------- */
  68. /* @After */
  69. static void
  70. teardown (void)
  71. {
  72. vfs_shut ();
  73. str_uninit_strings ();
  74. }
  75. /* --------------------------------------------------------------------------------------------- */
  76. /* @DataSource("test_vfs_setup_cwd_symlink_ds") */
  77. /* *INDENT-OFF* */
  78. static const struct test_vfs_setup_cwd_symlink_ds
  79. {
  80. gboolean is_2nd_call_different;
  81. const char *expected_result;
  82. } test_vfs_setup_cwd_symlink_ds[] =
  83. {
  84. { /* 0. */
  85. TRUE,
  86. "/some/path"
  87. },
  88. { /* 1. */
  89. FALSE,
  90. "/some/path2"
  91. },
  92. };
  93. /* *INDENT-ON* */
  94. /* @Test */
  95. /* *INDENT-OFF* */
  96. START_PARAMETRIZED_TEST (test_vfs_setup_cwd_symlink, test_vfs_setup_cwd_symlink_ds)
  97. /* *INDENT-ON* */
  98. {
  99. /* given */
  100. vfs_set_raw_current_dir (NULL);
  101. mc_stat__is_2nd_call_different = data->is_2nd_call_different;
  102. mc_stat__call_count = 0;
  103. setenv ("PWD", "/some/path2", 1);
  104. /* when */
  105. vfs_setup_cwd ();
  106. /* then */
  107. mctest_assert_str_eq (vfs_get_current_dir (), data->expected_result);
  108. }
  109. /* *INDENT-OFF* */
  110. END_PARAMETRIZED_TEST
  111. /* *INDENT-ON* */
  112. /* --------------------------------------------------------------------------------------------- */
  113. int
  114. main (void)
  115. {
  116. TCase *tc_core;
  117. tc_core = tcase_create ("Core");
  118. tcase_add_checked_fixture (tc_core, setup, teardown);
  119. /* Add new tests here: *************** */
  120. mctest_add_parameterized_test (tc_core, test_vfs_setup_cwd_symlink,
  121. test_vfs_setup_cwd_symlink_ds);
  122. /* *********************************** */
  123. return mctest_run_all (tc_core);
  124. }
  125. /* --------------------------------------------------------------------------------------------- */