tempdir.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. lib/vfs - manipulations with temp files and dirs
  3. Copyright (C) 2012, 2013
  4. The 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. struct vfs_s_subclass test_subclass1, test_subclass2, test_subclass3;
  30. struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
  31. /* --------------------------------------------------------------------------------------------- */
  32. /* @Before */
  33. static void
  34. setup (void)
  35. {
  36. str_init_strings (NULL);
  37. vfs_init ();
  38. 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. fail_unless (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-", NULL);
  82. /* then */
  83. close (fd);
  84. mctest_assert_int_ne (fd, -1);
  85. fail_unless (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. fail_unless (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);
  93. }
  94. /* *INDENT-OFF* */
  95. END_TEST
  96. /* *INDENT-ON* */
  97. /* --------------------------------------------------------------------------------------------- */
  98. int
  99. main (void)
  100. {
  101. int number_failed;
  102. Suite *s = suite_create (TEST_SUITE_NAME);
  103. TCase *tc_core = tcase_create ("Core");
  104. SRunner *sr;
  105. tcase_add_checked_fixture (tc_core, setup, teardown);
  106. /* Add new tests here: *************** */
  107. tcase_add_test (tc_core, test_mc_tmpdir);
  108. tcase_add_test (tc_core, test_mc_mkstemps);
  109. /* *********************************** */
  110. suite_add_tcase (s, tc_core);
  111. sr = srunner_create (s);
  112. srunner_set_log (sr, "tempdir.log");
  113. srunner_run_all (sr, CK_NORMAL);
  114. number_failed = srunner_ntests_failed (sr);
  115. srunner_free (sr);
  116. return (number_failed == 0) ? 0 : 1;
  117. }
  118. /* --------------------------------------------------------------------------------------------- */