vfs_setup_cwd.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. lib/vfs - test vfs_setup_cwd() functionality
  3. Copyright (C) 2013-2019
  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/vfs/xdirentry.h"
  24. #include "src/vfs/local/local.c"
  25. /* --------------------------------------------------------------------------------------------- */
  26. /* @Mock */
  27. char *
  28. g_get_current_dir (void)
  29. {
  30. return g_strdup ("/some/path");
  31. }
  32. /* --------------------------------------------------------------------------------------------- */
  33. static gboolean mc_stat__is_2nd_call_different = FALSE;
  34. static gboolean mc_stat__call_count = 0;
  35. /* @Mock */
  36. int
  37. mc_stat (const vfs_path_t * vpath, struct stat *my_stat)
  38. {
  39. (void) vpath;
  40. if (mc_stat__call_count++ > 1 && mc_stat__is_2nd_call_different)
  41. {
  42. my_stat->st_ino = 2;
  43. my_stat->st_dev = 22;
  44. }
  45. else
  46. {
  47. my_stat->st_ino = 1;
  48. my_stat->st_dev = 11;
  49. }
  50. if (mc_stat__call_count > 2)
  51. {
  52. mc_stat__call_count = 0;
  53. }
  54. return 0;
  55. }
  56. /* --------------------------------------------------------------------------------------------- */
  57. /* @Before */
  58. static void
  59. setup (void)
  60. {
  61. str_init_strings (NULL);
  62. vfs_init ();
  63. vfs_init_localfs ();
  64. vfs_setup_work_dir ();
  65. }
  66. /* --------------------------------------------------------------------------------------------- */
  67. /* @After */
  68. static void
  69. teardown (void)
  70. {
  71. vfs_shut ();
  72. str_uninit_strings ();
  73. }
  74. /* --------------------------------------------------------------------------------------------- */
  75. /* @DataSource("test_vfs_setup_cwd_symlink_ds") */
  76. /* *INDENT-OFF* */
  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. {
  83. { /* 0. */
  84. TRUE,
  85. "/some/path"
  86. },
  87. { /* 1. */
  88. FALSE,
  89. "/some/path2"
  90. },
  91. };
  92. /* *INDENT-ON* */
  93. /* @Test */
  94. /* *INDENT-OFF* */
  95. START_PARAMETRIZED_TEST (test_vfs_setup_cwd_symlink, test_vfs_setup_cwd_symlink_ds)
  96. /* *INDENT-ON* */
  97. {
  98. /* given */
  99. vfs_set_raw_current_dir (NULL);
  100. mc_stat__is_2nd_call_different = data->is_2nd_call_different;
  101. mc_stat__call_count = 0;
  102. setenv ("PWD", "/some/path2", 1);
  103. /* when */
  104. vfs_setup_cwd ();
  105. /* then */
  106. mctest_assert_str_eq (vfs_path_as_str (vfs_get_raw_current_dir ()), data->expected_result);
  107. }
  108. /* *INDENT-OFF* */
  109. END_PARAMETRIZED_TEST
  110. /* *INDENT-ON* */
  111. /* --------------------------------------------------------------------------------------------- */
  112. int
  113. main (void)
  114. {
  115. int number_failed;
  116. Suite *s = suite_create (TEST_SUITE_NAME);
  117. TCase *tc_core = tcase_create ("Core");
  118. SRunner *sr;
  119. tcase_add_checked_fixture (tc_core, setup, teardown);
  120. /* Add new tests here: *************** */
  121. mctest_add_parameterized_test (tc_core, test_vfs_setup_cwd_symlink,
  122. test_vfs_setup_cwd_symlink_ds);
  123. /* *********************************** */
  124. suite_add_tcase (s, tc_core);
  125. sr = srunner_create (s);
  126. srunner_set_log (sr, "vfs_setup_cwd.log");
  127. srunner_run_all (sr, CK_ENV);
  128. number_failed = srunner_ntests_failed (sr);
  129. srunner_free (sr);
  130. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  131. }
  132. /* --------------------------------------------------------------------------------------------- */