Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

file_helper.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
  3. */
  4. #include "includes.h"
  5. using spdlog::details::file_helper;
  6. using spdlog::details::log_msg;
  7. static const std::string target_filename = "logs/file_helper_test.txt";
  8. static void write_with_helper(file_helper &helper, size_t howmany)
  9. {
  10. log_msg msg;
  11. fmt::memory_buffer formatted;
  12. fmt::format_to(formatted, "{}", std::string(howmany, '1'));
  13. helper.write(formatted);
  14. helper.flush();
  15. }
  16. TEST_CASE("file_helper_filename", "[file_helper::filename()]]")
  17. {
  18. prepare_logdir();
  19. file_helper helper;
  20. helper.open(target_filename);
  21. REQUIRE(helper.filename() == target_filename);
  22. }
  23. TEST_CASE("file_helper_size", "[file_helper::size()]]")
  24. {
  25. prepare_logdir();
  26. size_t expected_size = 123;
  27. {
  28. file_helper helper;
  29. helper.open(target_filename);
  30. write_with_helper(helper, expected_size);
  31. REQUIRE(static_cast<size_t>(helper.size()) == expected_size);
  32. }
  33. REQUIRE(get_filesize(target_filename) == expected_size);
  34. }
  35. TEST_CASE("file_helper_exists", "[file_helper::file_exists()]]")
  36. {
  37. prepare_logdir();
  38. REQUIRE(!file_helper::file_exists(target_filename));
  39. file_helper helper;
  40. helper.open(target_filename);
  41. REQUIRE(file_helper::file_exists(target_filename));
  42. }
  43. TEST_CASE("file_helper_reopen", "[file_helper::reopen()]]")
  44. {
  45. prepare_logdir();
  46. file_helper helper;
  47. helper.open(target_filename);
  48. write_with_helper(helper, 12);
  49. REQUIRE(helper.size() == 12);
  50. helper.reopen(true);
  51. REQUIRE(helper.size() == 0);
  52. }
  53. TEST_CASE("file_helper_reopen2", "[file_helper::reopen(false)]]")
  54. {
  55. prepare_logdir();
  56. size_t expected_size = 14;
  57. file_helper helper;
  58. helper.open(target_filename);
  59. write_with_helper(helper, expected_size);
  60. REQUIRE(helper.size() == expected_size);
  61. helper.reopen(false);
  62. REQUIRE(helper.size() == expected_size);
  63. }
  64. static void test_split_ext(const char *fname, const char *expect_base, const char *expect_ext)
  65. {
  66. spdlog::filename_t filename(fname);
  67. spdlog::filename_t expected_base(expect_base);
  68. spdlog::filename_t expected_ext(expect_ext);
  69. #ifdef _WIN32 // replace folder sep
  70. std::replace(filename.begin(), filename.end(), '/', '\\');
  71. std::replace(expected_base.begin(), expected_base.end(), '/', '\\');
  72. #endif
  73. spdlog::filename_t basename, ext;
  74. std::tie(basename, ext) = file_helper::split_by_extenstion(filename);
  75. REQUIRE(basename == expected_base);
  76. REQUIRE(ext == expected_ext);
  77. }
  78. TEST_CASE("file_helper_split_by_extenstion", "[file_helper::split_by_extenstion()]]")
  79. {
  80. test_split_ext("mylog.txt", "mylog", ".txt");
  81. test_split_ext(".mylog.txt", ".mylog", ".txt");
  82. test_split_ext(".mylog", ".mylog", "");
  83. test_split_ext("/aaa/bb.d/mylog", "/aaa/bb.d/mylog", "");
  84. test_split_ext("/aaa/bb.d/mylog.txt", "/aaa/bb.d/mylog", ".txt");
  85. test_split_ext("aaa/bbb/ccc/mylog.txt", "aaa/bbb/ccc/mylog", ".txt");
  86. test_split_ext("aaa/bbb/ccc/mylog.", "aaa/bbb/ccc/mylog.", "");
  87. test_split_ext("aaa/bbb/ccc/.mylog.txt", "aaa/bbb/ccc/.mylog", ".txt");
  88. test_split_ext("/aaa/bbb/ccc/mylog.txt", "/aaa/bbb/ccc/mylog", ".txt");
  89. test_split_ext("/aaa/bbb/ccc/.mylog", "/aaa/bbb/ccc/.mylog", "");
  90. test_split_ext("../mylog.txt", "../mylog", ".txt");
  91. test_split_ext(".././mylog.txt", ".././mylog", ".txt");
  92. test_split_ext(".././mylog.txt/xxx", ".././mylog.txt/xxx", "");
  93. test_split_ext("/mylog.txt", "/mylog", ".txt");
  94. test_split_ext("//mylog.txt", "//mylog", ".txt");
  95. test_split_ext("", "", "");
  96. test_split_ext(".", ".", "");
  97. test_split_ext("..txt", ".", ".txt");
  98. }