tempdir.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. str_init_strings (NULL);
  35. vfs_init ();
  36. vfs_init_localfs ();
  37. vfs_setup_work_dir ();
  38. }
  39. /* --------------------------------------------------------------------------------------------- */
  40. /* @After */
  41. static void
  42. teardown (void)
  43. {
  44. vfs_shut ();
  45. str_uninit_strings ();
  46. }
  47. /* --------------------------------------------------------------------------------------------- */
  48. /* @Test */
  49. /* *INDENT-OFF* */
  50. START_TEST (test_mc_tmpdir)
  51. /* *INDENT-ON* */
  52. {
  53. /* given */
  54. const char *tmpdir;
  55. const char *env_tmpdir;
  56. /* when */
  57. tmpdir = mc_tmpdir ();
  58. env_tmpdir = g_getenv ("MC_TMPDIR");
  59. /* then */
  60. ck_assert_msg (g_file_test (tmpdir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR),
  61. "\nNo such directory: %s\n", tmpdir);
  62. mctest_assert_str_eq (env_tmpdir, tmpdir);
  63. }
  64. /* *INDENT-OFF* */
  65. END_TEST
  66. /* *INDENT-ON* */
  67. /* --------------------------------------------------------------------------------------------- */
  68. /* @Test */
  69. /* *INDENT-OFF* */
  70. START_TEST (test_mc_mkstemps)
  71. /* *INDENT-ON* */
  72. {
  73. /* given */
  74. vfs_path_t *pname_vpath = NULL;
  75. char *begin_pname;
  76. int fd;
  77. /* when */
  78. fd = mc_mkstemps (&pname_vpath, "mctest-", NULL);
  79. begin_pname = g_build_filename (mc_tmpdir (), "mctest-", (char *) NULL);
  80. /* then */
  81. close (fd);
  82. ck_assert_int_ne (fd, -1);
  83. ck_assert_msg (g_file_test
  84. (vfs_path_as_str (pname_vpath), G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR),
  85. "\nNo such file: %s\n", vfs_path_as_str (pname_vpath));
  86. unlink (vfs_path_as_str (pname_vpath));
  87. ck_assert_msg (strncmp (vfs_path_as_str (pname_vpath), begin_pname, strlen (begin_pname)) == 0,
  88. "\nstart of %s should be equal to %s\n", vfs_path_as_str (pname_vpath),
  89. begin_pname);
  90. vfs_path_free (pname_vpath, TRUE);
  91. }
  92. /* *INDENT-OFF* */
  93. END_TEST
  94. /* *INDENT-ON* */
  95. /* --------------------------------------------------------------------------------------------- */
  96. int
  97. main (void)
  98. {
  99. TCase *tc_core;
  100. tc_core = tcase_create ("Core");
  101. tcase_add_checked_fixture (tc_core, setup, teardown);
  102. /* Add new tests here: *************** */
  103. tcase_add_test (tc_core, test_mc_tmpdir);
  104. tcase_add_test (tc_core, test_mc_mkstemps);
  105. /* *********************************** */
  106. return mctest_run_all (tc_core);
  107. }
  108. /* --------------------------------------------------------------------------------------------- */