tempdir.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. lib/vfs - manipulations with temp files and dirs
  3. Copyright (C) 2012-2024
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2012, 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. #ifndef HAVE_CHARSET
  22. #define HAVE_CHARSET 1
  23. #endif
  24. #include "lib/charsets.h"
  25. #include "lib/strutil.h"
  26. #include "lib/vfs/xdirentry.h"
  27. #include "lib/vfs/path.h"
  28. #include "src/vfs/local/local.c"
  29. /* --------------------------------------------------------------------------------------------- */
  30. /* @Before */
  31. static void
  32. setup (void)
  33. {
  34. /* Ensure that tests behave consistently irrespectively of the environment */
  35. g_unsetenv ("MC_TMPDIR");
  36. str_init_strings (NULL);
  37. vfs_init ();
  38. vfs_init_localfs ();
  39. vfs_setup_work_dir ();
  40. }
  41. /* --------------------------------------------------------------------------------------------- */
  42. /* @After */
  43. static void
  44. teardown (void)
  45. {
  46. vfs_shut ();
  47. str_uninit_strings ();
  48. }
  49. /* --------------------------------------------------------------------------------------------- */
  50. /* @Test */
  51. /* *INDENT-OFF* */
  52. START_TEST (test_mc_tmpdir)
  53. /* *INDENT-ON* */
  54. {
  55. /* given */
  56. const char *tmpdir;
  57. const char *env_tmpdir;
  58. /* when */
  59. tmpdir = mc_tmpdir ();
  60. env_tmpdir = g_getenv ("MC_TMPDIR");
  61. /* then */
  62. ck_assert_msg (g_file_test (tmpdir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR),
  63. "\nNo such directory: %s\n", tmpdir);
  64. mctest_assert_str_eq (env_tmpdir, tmpdir);
  65. }
  66. /* *INDENT-OFF* */
  67. END_TEST
  68. /* *INDENT-ON* */
  69. /* --------------------------------------------------------------------------------------------- */
  70. /* @Test */
  71. /* *INDENT-OFF* */
  72. START_TEST (test_mc_mkstemps)
  73. /* *INDENT-ON* */
  74. {
  75. /* given */
  76. vfs_path_t *pname_vpath = NULL;
  77. char *begin_pname;
  78. int fd;
  79. /* when */
  80. fd = mc_mkstemps (&pname_vpath, "mctest-", NULL);
  81. begin_pname = g_build_filename (mc_tmpdir (), "mctest-", (char *) NULL);
  82. /* then */
  83. close (fd);
  84. ck_assert_int_ne (fd, -1);
  85. ck_assert_msg (g_file_test
  86. (vfs_path_as_str (pname_vpath), G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR),
  87. "\nNo such file: %s\n", vfs_path_as_str (pname_vpath));
  88. unlink (vfs_path_as_str (pname_vpath));
  89. ck_assert_msg (strncmp (vfs_path_as_str (pname_vpath), begin_pname, strlen (begin_pname)) == 0,
  90. "\nstart of %s should be equal to %s\n", vfs_path_as_str (pname_vpath),
  91. begin_pname);
  92. vfs_path_free (pname_vpath, TRUE);
  93. }
  94. /* *INDENT-OFF* */
  95. END_TEST
  96. /* *INDENT-ON* */
  97. /* --------------------------------------------------------------------------------------------- */
  98. int
  99. main (void)
  100. {
  101. TCase *tc_core;
  102. tc_core = tcase_create ("Core");
  103. tcase_add_checked_fixture (tc_core, setup, teardown);
  104. /* Add new tests here: *************** */
  105. tcase_add_test (tc_core, test_mc_tmpdir);
  106. tcase_add_test (tc_core, test_mc_mkstemps);
  107. /* *********************************** */
  108. return mctest_run_all (tc_core);
  109. }
  110. /* --------------------------------------------------------------------------------------------- */