vfs_setup_cwd.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. lib/vfs - test vfs_setup_cwd() functionality
  3. Copyright (C) 2013-2025
  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 <https://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. static const struct test_vfs_setup_cwd_symlink_ds
  78. {
  79. gboolean is_2nd_call_different;
  80. const char *expected_result;
  81. } test_vfs_setup_cwd_symlink_ds[] = {
  82. { TRUE, "/some/path" }, // 0.
  83. { FALSE, "/some/path2" }, // 1.
  84. };
  85. /* @Test */
  86. START_PARAMETRIZED_TEST (test_vfs_setup_cwd_symlink, test_vfs_setup_cwd_symlink_ds)
  87. {
  88. // given
  89. vfs_set_raw_current_dir (NULL);
  90. mc_stat__is_2nd_call_different = data->is_2nd_call_different;
  91. mc_stat__call_count = 0;
  92. setenv ("PWD", "/some/path2", 1);
  93. // when
  94. vfs_setup_cwd ();
  95. // then
  96. mctest_assert_str_eq (vfs_get_current_dir (), data->expected_result);
  97. }
  98. END_PARAMETRIZED_TEST
  99. /* --------------------------------------------------------------------------------------------- */
  100. int
  101. main (void)
  102. {
  103. TCase *tc_core;
  104. tc_core = tcase_create ("Core");
  105. tcase_add_checked_fixture (tc_core, setup, teardown);
  106. // Add new tests here: ***************
  107. mctest_add_parameterized_test (tc_core, test_vfs_setup_cwd_symlink,
  108. test_vfs_setup_cwd_symlink_ds);
  109. // ***********************************
  110. return mctest_run_all (tc_core);
  111. }
  112. /* --------------------------------------------------------------------------------------------- */