spdlog/tests/test_file_helper.cpp

170 lines
7.1 KiB
C++
Raw Normal View History

2016-04-20 16:57:49 +08:00
/*
* This content is released under the MIT License as specified in
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
2018-03-09 21:26:33 +08:00
*/
2016-04-20 16:57:49 +08:00
#include "includes.h"
2021-01-07 07:55:57 +08:00
#define TEST_FILENAME "test_logs/file_helper_test.txt"
2018-02-25 19:12:34 +08:00
using spdlog::details::file_helper;
2016-04-20 16:57:49 +08:00
static void write_with_helper(file_helper &helper, size_t howmany) {
spdlog::memory_buf_t formatted;
spdlog::fmt_lib::format_to(std::back_inserter(formatted), "{}", std::string(howmany, '1'));
2018-06-24 06:32:39 +08:00
helper.write(formatted);
2016-10-13 04:08:44 +08:00
helper.flush();
2016-04-20 16:57:49 +08:00
}
TEST_CASE("file_helper_filename", "[file_helper::filename()]") {
2016-04-20 16:57:49 +08:00
prepare_logdir();
file_helper helper;
2021-01-07 07:55:57 +08:00
spdlog::filename_t target_filename = SPDLOG_FILENAME_T(TEST_FILENAME);
2016-04-20 16:57:49 +08:00
helper.open(target_filename);
REQUIRE(helper.filename() == target_filename);
}
TEST_CASE("file_helper_size", "[file_helper::size()]") {
2016-04-20 16:57:49 +08:00
prepare_logdir();
2021-01-07 07:55:57 +08:00
spdlog::filename_t target_filename = SPDLOG_FILENAME_T(TEST_FILENAME);
2016-07-15 23:41:59 +08:00
size_t expected_size = 123;
2016-04-20 16:57:49 +08:00
{
file_helper helper;
2016-04-20 16:57:49 +08:00
helper.open(target_filename);
write_with_helper(helper, expected_size);
2016-07-15 23:41:59 +08:00
REQUIRE(static_cast<size_t>(helper.size()) == expected_size);
2016-04-20 16:57:49 +08:00
}
2021-01-07 07:55:57 +08:00
REQUIRE(get_filesize(TEST_FILENAME) == expected_size);
2016-04-20 16:57:49 +08:00
}
TEST_CASE("file_helper_reopen", "[file_helper::reopen()]") {
2016-04-20 16:57:49 +08:00
prepare_logdir();
2021-01-07 07:55:57 +08:00
spdlog::filename_t target_filename = SPDLOG_FILENAME_T(TEST_FILENAME);
file_helper helper;
2016-04-20 16:57:49 +08:00
helper.open(target_filename);
write_with_helper(helper, 12);
2020-04-29 19:50:25 +08:00
REQUIRE(helper.size() == 12);
2016-04-20 16:57:49 +08:00
helper.reopen(true);
REQUIRE(helper.size() == 0);
}
TEST_CASE("file_helper_reopen2", "[file_helper::reopen(false)]") {
2016-04-20 16:57:49 +08:00
prepare_logdir();
2021-01-07 07:55:57 +08:00
spdlog::filename_t target_filename = SPDLOG_FILENAME_T(TEST_FILENAME);
size_t expected_size = 14;
2016-10-13 04:08:44 +08:00
file_helper helper;
2016-04-20 16:57:49 +08:00
helper.open(target_filename);
write_with_helper(helper, expected_size);
REQUIRE(helper.size() == expected_size);
helper.reopen(false);
REQUIRE(helper.size() == expected_size);
}
static void test_split_ext(const spdlog::filename_t::value_type *fname,
const spdlog::filename_t::value_type *expect_base,
const spdlog::filename_t::value_type *expect_ext) {
2017-12-23 00:55:19 +08:00
spdlog::filename_t filename(fname);
spdlog::filename_t expected_base(expect_base);
spdlog::filename_t expected_ext(expect_ext);
2020-04-08 23:17:21 +08:00
spdlog::filename_t basename;
spdlog::filename_t ext;
std::tie(basename, ext) = file_helper::split_by_extension(filename);
2017-12-23 00:55:19 +08:00
REQUIRE(basename == expected_base);
REQUIRE(ext == expected_ext);
}
TEST_CASE("file_helper_split_by_extension", "[file_helper::split_by_extension()]") {
test_split_ext(SPDLOG_FILENAME_T("mylog.txt"), SPDLOG_FILENAME_T("mylog"),
SPDLOG_FILENAME_T(".txt"));
test_split_ext(SPDLOG_FILENAME_T(".mylog.txt"), SPDLOG_FILENAME_T(".mylog"),
SPDLOG_FILENAME_T(".txt"));
test_split_ext(SPDLOG_FILENAME_T(".mylog"), SPDLOG_FILENAME_T(".mylog"), SPDLOG_FILENAME_T(""));
test_split_ext(SPDLOG_FILENAME_T("/aaa/bb.d/mylog"), SPDLOG_FILENAME_T("/aaa/bb.d/mylog"),
SPDLOG_FILENAME_T(""));
test_split_ext(SPDLOG_FILENAME_T("/aaa/bb.d/mylog.txt"), SPDLOG_FILENAME_T("/aaa/bb.d/mylog"),
SPDLOG_FILENAME_T(".txt"));
test_split_ext(SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog.txt"),
SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog"), SPDLOG_FILENAME_T(".txt"));
test_split_ext(SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog."), SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog."),
SPDLOG_FILENAME_T(""));
test_split_ext(SPDLOG_FILENAME_T("aaa/bbb/ccc/.mylog.txt"),
SPDLOG_FILENAME_T("aaa/bbb/ccc/.mylog"), SPDLOG_FILENAME_T(".txt"));
test_split_ext(SPDLOG_FILENAME_T("/aaa/bbb/ccc/mylog.txt"),
SPDLOG_FILENAME_T("/aaa/bbb/ccc/mylog"), SPDLOG_FILENAME_T(".txt"));
test_split_ext(SPDLOG_FILENAME_T("/aaa/bbb/ccc/.mylog"),
SPDLOG_FILENAME_T("/aaa/bbb/ccc/.mylog"), SPDLOG_FILENAME_T(""));
test_split_ext(SPDLOG_FILENAME_T("../mylog.txt"), SPDLOG_FILENAME_T("../mylog"),
SPDLOG_FILENAME_T(".txt"));
test_split_ext(SPDLOG_FILENAME_T(".././mylog.txt"), SPDLOG_FILENAME_T(".././mylog"),
SPDLOG_FILENAME_T(".txt"));
test_split_ext(SPDLOG_FILENAME_T(".././mylog.txt/xxx"), SPDLOG_FILENAME_T(".././mylog.txt/xxx"),
SPDLOG_FILENAME_T(""));
test_split_ext(SPDLOG_FILENAME_T("/mylog.txt"), SPDLOG_FILENAME_T("/mylog"),
SPDLOG_FILENAME_T(".txt"));
test_split_ext(SPDLOG_FILENAME_T("//mylog.txt"), SPDLOG_FILENAME_T("//mylog"),
SPDLOG_FILENAME_T(".txt"));
test_split_ext(SPDLOG_FILENAME_T(""), SPDLOG_FILENAME_T(""), SPDLOG_FILENAME_T(""));
test_split_ext(SPDLOG_FILENAME_T("."), SPDLOG_FILENAME_T("."), SPDLOG_FILENAME_T(""));
test_split_ext(SPDLOG_FILENAME_T("..txt"), SPDLOG_FILENAME_T("."), SPDLOG_FILENAME_T(".txt"));
}
2021-12-10 05:43:49 +08:00
TEST_CASE("file_event_handlers", "[file_helper]") {
enum class flags { before_open, after_open, before_close, after_close };
2021-12-10 05:43:49 +08:00
prepare_logdir();
2021-12-11 06:38:41 +08:00
spdlog::filename_t test_filename = SPDLOG_FILENAME_T(TEST_FILENAME);
2021-12-10 06:12:20 +08:00
// define event handles that update vector of flags when called
std::vector<flags> events;
2021-12-10 05:43:49 +08:00
spdlog::file_event_handlers handlers;
handlers.before_open = [&](spdlog::filename_t filename) {
REQUIRE(filename == test_filename);
2021-12-10 06:12:20 +08:00
events.push_back(flags::before_open);
2021-12-10 05:43:49 +08:00
};
handlers.after_open = [&](spdlog::filename_t filename, std::FILE *fstream) {
REQUIRE(filename == test_filename);
2021-12-10 05:43:49 +08:00
REQUIRE(fstream);
2021-12-10 06:28:25 +08:00
fputs("after_open\n", fstream);
2021-12-10 06:12:20 +08:00
events.push_back(flags::after_open);
2021-12-10 05:43:49 +08:00
};
handlers.before_close = [&](spdlog::filename_t filename, std::FILE *fstream) {
REQUIRE(filename == test_filename);
2021-12-10 05:43:49 +08:00
REQUIRE(fstream);
2021-12-10 06:28:25 +08:00
fputs("before_close\n", fstream);
2021-12-10 06:12:20 +08:00
events.push_back(flags::before_close);
2021-12-10 05:43:49 +08:00
};
handlers.after_close = [&](spdlog::filename_t filename) {
REQUIRE(filename == test_filename);
2021-12-10 06:12:20 +08:00
events.push_back(flags::after_close);
2021-12-10 05:43:49 +08:00
};
2021-12-10 06:12:20 +08:00
{
spdlog::details::file_helper helper{handlers};
REQUIRE(events.empty());
helper.open(test_filename);
2021-12-10 06:12:20 +08:00
REQUIRE(events == std::vector<flags>{flags::before_open, flags::after_open});
events.clear();
helper.close();
2021-12-10 06:12:20 +08:00
REQUIRE(events == std::vector<flags>{flags::before_close, flags::after_close});
2021-12-11 04:31:08 +08:00
REQUIRE(file_contents(TEST_FILENAME) == "after_open\nbefore_close\n");
2021-12-10 06:12:20 +08:00
helper.reopen(true);
events.clear();
2021-12-10 06:12:20 +08:00
}
// make sure that the file_helper destrcutor calls the close callbacks if needed
2021-12-10 06:12:20 +08:00
REQUIRE(events == std::vector<flags>{flags::before_close, flags::after_close});
2021-12-11 04:31:08 +08:00
REQUIRE(file_contents(TEST_FILENAME) == "after_open\nbefore_close\n");
2021-12-10 05:43:49 +08:00
}
TEST_CASE("file_helper_open", "[file_helper]") {
prepare_logdir();
spdlog::filename_t target_filename = SPDLOG_FILENAME_T(TEST_FILENAME);
file_helper helper;
helper.open(target_filename);
helper.close();
target_filename += SPDLOG_FILENAME_T("/invalid");
REQUIRE_THROWS_AS(helper.open(target_filename), spdlog::spdlog_ex);
}