spdlog/tests/test_create_dir.cpp

145 lines
6.3 KiB
C++
Raw Normal View History

2019-10-20 22:40:56 +08:00
/*
2023-09-25 21:08:29 +08:00
* This content is released under the MIT License as specified in
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
2019-10-20 22:40:56 +08:00
*/
#include "includes.h"
using spdlog::details::os::create_dir;
2019-10-25 20:56:23 +08:00
using spdlog::details::os::path_exists;
2019-10-20 22:40:56 +08:00
2023-09-25 21:08:29 +08:00
bool try_create_dir(const spdlog::filename_t &path, const spdlog::filename_t &normalized_path) {
2019-10-20 22:40:56 +08:00
auto rv = create_dir(path);
REQUIRE(rv == true);
2019-10-25 20:56:23 +08:00
return path_exists(normalized_path);
2019-10-20 22:40:56 +08:00
}
2023-09-25 21:08:29 +08:00
TEST_CASE("create_dir", "[create_dir]") {
2019-10-20 22:40:56 +08:00
prepare_logdir();
2019-10-25 19:29:57 +08:00
2023-09-25 21:08:29 +08:00
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs/dir1/dir1"),
SPDLOG_FILENAME_T("test_logs/dir1/dir1")));
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs/dir1/dir1"),
2023-09-25 21:40:36 +08:00
SPDLOG_FILENAME_T("test_logs/dir1/dir1"))); // test existing
2023-09-25 21:08:29 +08:00
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs/dir1///dir2//"),
SPDLOG_FILENAME_T("test_logs/dir1/dir2")));
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("./test_logs/dir1/dir3"),
SPDLOG_FILENAME_T("test_logs/dir1/dir3")));
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs/../test_logs/dir1/dir4"),
SPDLOG_FILENAME_T("test_logs/dir1/dir4")));
2019-10-25 19:29:57 +08:00
2019-10-25 19:14:50 +08:00
#ifdef WIN32
2019-10-25 20:44:53 +08:00
// test backslash folder separator
2023-09-25 21:08:29 +08:00
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs\\dir1\\dir222"),
SPDLOG_FILENAME_T("test_logs\\dir1\\dir222")));
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs\\dir1\\dir223\\"),
SPDLOG_FILENAME_T("test_logs\\dir1\\dir223\\")));
REQUIRE(try_create_dir(SPDLOG_FILENAME_T(".\\test_logs\\dir1\\dir2\\dir99\\..\\dir23"),
SPDLOG_FILENAME_T("test_logs\\dir1\\dir2\\dir23")));
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs\\..\\test_logs\\dir1\\dir5"),
SPDLOG_FILENAME_T("test_logs\\dir1\\dir5")));
2019-10-25 19:14:50 +08:00
#endif
2019-10-21 18:42:25 +08:00
}
2023-09-25 21:08:29 +08:00
TEST_CASE("create_invalid_dir", "[create_dir]") {
REQUIRE(create_dir(SPDLOG_FILENAME_T("")) == false);
2019-10-25 21:20:24 +08:00
REQUIRE(create_dir(spdlog::filename_t{}) == false);
2019-10-25 21:17:02 +08:00
#ifdef __linux__
REQUIRE(create_dir("/proc/spdlog-utest") == false);
#endif
}
2023-09-25 21:08:29 +08:00
TEST_CASE("dir_name", "[create_dir]") {
2019-10-21 18:42:25 +08:00
using spdlog::details::os::dir_name;
REQUIRE(dir_name(SPDLOG_FILENAME_T("")).empty());
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir")).empty());
2019-10-25 20:44:53 +08:00
2019-10-21 18:42:25 +08:00
#ifdef WIN32
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\)")) == SPDLOG_FILENAME_T("dir"));
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\\\)")) == SPDLOG_FILENAME_T(R"(dir\\)"));
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\file)")) == SPDLOG_FILENAME_T("dir"));
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir/file)")) == SPDLOG_FILENAME_T("dir"));
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\file.txt)")) == SPDLOG_FILENAME_T("dir"));
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir/file)")) == SPDLOG_FILENAME_T("dir"));
2023-09-25 21:08:29 +08:00
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\file.txt\)")) ==
SPDLOG_FILENAME_T(R"(dir\file.txt)"));
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(\dir\file.txt)")) == SPDLOG_FILENAME_T(R"(\dir)"));
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(\\dir\file.txt)")) == SPDLOG_FILENAME_T(R"(\\dir)"));
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(..\file.txt)")) == SPDLOG_FILENAME_T(".."));
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(.\file.txt)")) == SPDLOG_FILENAME_T("."));
2023-09-25 21:08:29 +08:00
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(c:\\a\b\c\d\file.txt)")) ==
SPDLOG_FILENAME_T(R"(c:\\a\b\c\d)"));
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(c://a/b/c/d/file.txt)")) ==
SPDLOG_FILENAME_T(R"(c://a/b/c/d)"));
2019-10-21 18:42:25 +08:00
#endif
2021-01-07 08:39:47 +08:00
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir/")) == SPDLOG_FILENAME_T("dir"));
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir///")) == SPDLOG_FILENAME_T("dir//"));
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir/file")) == SPDLOG_FILENAME_T("dir"));
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir/file.txt")) == SPDLOG_FILENAME_T("dir"));
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir/file.txt/")) == SPDLOG_FILENAME_T("dir/file.txt"));
REQUIRE(dir_name(SPDLOG_FILENAME_T("/dir/file.txt")) == SPDLOG_FILENAME_T("/dir"));
REQUIRE(dir_name(SPDLOG_FILENAME_T("//dir/file.txt")) == SPDLOG_FILENAME_T("//dir"));
REQUIRE(dir_name(SPDLOG_FILENAME_T("../file.txt")) == SPDLOG_FILENAME_T(".."));
REQUIRE(dir_name(SPDLOG_FILENAME_T("./file.txt")) == SPDLOG_FILENAME_T("."));
2019-10-20 22:40:56 +08:00
}
2024-04-30 00:46:59 +08:00
#ifdef _WIN32
2024-04-30 17:28:13 +08:00
//
// test windows cases when drive letter is given e.g. C:\\some-folder
//
#include <windows.h>
#include <fileapi.h>
2024-04-30 00:46:59 +08:00
std::string get_full_path(const std::string &relative_folder_path) {
char full_path[MAX_PATH];
DWORD result = ::GetFullPathNameA(relative_folder_path.c_str(), MAX_PATH, full_path, nullptr);
// Return an empty string if failed to get full path
2024-04-30 17:28:13 +08:00
return result > 0 && result < MAX_PATH ? std::string(full_path) : std::string();
2024-04-30 00:46:59 +08:00
}
std::wstring get_full_path(const std::wstring &relative_folder_path) {
wchar_t full_path[MAX_PATH];
2024-04-30 17:28:13 +08:00
DWORD result = ::GetFullPathNameW(relative_folder_path.c_str(), MAX_PATH, full_path, nullptr);
2024-04-30 00:46:59 +08:00
return result > 0 && result < MAX_PATH ? std::wstring(full_path) : std::wstring();
}
spdlog::filename_t::value_type find_non_existing_drive() {
for (char drive = 'A'; drive <= 'Z'; ++drive) {
std::string root_path = std::string(1, drive) + ":\\";
UINT drive_type = GetDriveTypeA(root_path.c_str());
if (drive_type == DRIVE_NO_ROOT_DIR) {
2024-04-30 17:28:13 +08:00
return static_cast<spdlog::filename_t::value_type>(drive);
2024-04-30 00:46:59 +08:00
}
}
return '\0'; // No available drive found
}
TEST_CASE("create_abs_path1", "[create_dir]") {
2024-04-30 17:28:13 +08:00
prepare_logdir();
2024-04-30 00:46:59 +08:00
auto abs_path = get_full_path(SPDLOG_FILENAME_T("test_logs\\logdir1"));
2024-04-30 17:28:13 +08:00
REQUIRE(!abs_path.empty());
2024-04-30 00:46:59 +08:00
REQUIRE(create_dir(abs_path) == true);
}
TEST_CASE("create_abs_path2", "[create_dir]") {
prepare_logdir();
auto abs_path = get_full_path(SPDLOG_FILENAME_T("test_logs/logdir2"));
REQUIRE(!abs_path.empty());
2024-04-30 17:28:13 +08:00
REQUIRE(create_dir(abs_path) == true);
2024-04-30 00:46:59 +08:00
}
2024-04-30 17:28:13 +08:00
TEST_CASE("non_existing_drive", "[create_dir]") {
prepare_logdir();
2024-04-30 00:46:59 +08:00
spdlog::filename_t path;
2024-04-30 17:28:13 +08:00
2024-04-30 00:46:59 +08:00
auto non_existing_drive = find_non_existing_drive();
2024-04-30 17:28:13 +08:00
path += non_existing_drive;
path += SPDLOG_FILENAME_T(":\\");
REQUIRE(create_dir(path) == false);
2024-04-30 00:46:59 +08:00
path += SPDLOG_FILENAME_T("subdir");
REQUIRE(create_dir(path) == false);
}
2024-04-30 17:28:13 +08:00
// #endif // SPDLOG_WCHAR_FILENAMES
2024-04-30 00:46:59 +08:00
#endif // _WIN32