spdlog/tests/test_create_dir.cpp

81 lines
3.1 KiB
C++
Raw Normal View History

2019-10-20 22:40:56 +08:00
/*
* This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
*/
#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
2019-10-25 19:29:57 +08:00
bool try_create_dir(const char *path, const char *normalized_path)
2019-10-25 20:44:53 +08:00
{
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
}
TEST_CASE("create_dir", "[create_dir]")
{
prepare_logdir();
2019-10-25 19:29:57 +08:00
2019-10-25 20:44:53 +08:00
REQUIRE(try_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1"));
REQUIRE(try_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1")); // test existing
REQUIRE(try_create_dir("test_logs/dir1///dir2//", "test_logs/dir1/dir2"));
REQUIRE(try_create_dir("./test_logs/dir1/dir3", "test_logs/dir1/dir3"));
REQUIRE(try_create_dir("test_logs/../test_logs/dir1/dir4", "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
REQUIRE(try_create_dir("test_logs\\dir1\\dir222", "test_logs\\dir1\\dir222"));
REQUIRE(try_create_dir("test_logs\\dir1\\dir223\\", "test_logs\\dir1\\dir223\\"));
REQUIRE(try_create_dir(".\\test_logs\\dir1\\dir2\\dir99\\..\\dir23", "test_logs\\dir1\\dir2\\dir23"));
REQUIRE(try_create_dir("test_logs\\..\\test_logs\\dir1\\dir5", "test_logs\\dir1\\dir5"));
2019-10-25 19:14:50 +08:00
#endif
2019-10-21 18:42:25 +08:00
}
2019-10-25 21:17:02 +08:00
TEST_CASE("create_invalid_dir", "[create_dir]")
{
REQUIRE(create_dir("") == 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
}
2019-10-21 18:42:25 +08:00
TEST_CASE("dir_name", "[create_dir]")
{
using spdlog::details::os::dir_name;
REQUIRE(dir_name("").empty());
REQUIRE(dir_name("dir").empty());
2019-10-25 20:44:53 +08:00
2019-10-21 18:42:25 +08:00
#ifdef WIN32
REQUIRE(dir_name(R"(dir\)") == "dir");
2019-10-25 19:14:50 +08:00
REQUIRE(dir_name(R"(dir\\\)") == R"(dir\\)");
2019-10-21 18:42:25 +08:00
REQUIRE(dir_name(R"(dir\file)") == "dir");
2019-10-25 20:44:53 +08:00
REQUIRE(dir_name(R"(dir/file)") == "dir");
2019-10-21 18:42:25 +08:00
REQUIRE(dir_name(R"(dir\file.txt)") == "dir");
2019-10-25 20:44:53 +08:00
REQUIRE(dir_name(R"(dir/file)") == "dir");
2019-10-25 19:14:50 +08:00
REQUIRE(dir_name(R"(dir\file.txt\)") == R"(dir\file.txt)");
2019-10-25 20:44:53 +08:00
REQUIRE(dir_name(R"(dir/file.txt/)") == R"(dir\file.txt)");
2019-10-25 19:14:50 +08:00
REQUIRE(dir_name(R"(\dir\file.txt)") == R"(\dir)");
2019-10-25 20:44:53 +08:00
REQUIRE(dir_name(R"(/dir/file.txt)") == R"(\dir)");
REQUIRE(dir_name(R"(\\dir\file.txt)") == R"(\\dir)");
REQUIRE(dir_name(R"(//dir/file.txt)") == R"(\\dir)");
2019-10-21 18:42:25 +08:00
REQUIRE(dir_name(R"(..\file.txt)") == "..");
2019-10-25 20:44:53 +08:00
REQUIRE(dir_name(R"(../file.txt)") == "..");
2019-10-21 18:42:25 +08:00
REQUIRE(dir_name(R"(.\file.txt)") == ".");
2019-10-25 20:44:53 +08:00
REQUIRE(dir_name(R"(./file.txt)") == ".");
2019-10-25 19:14:50 +08:00
REQUIRE(dir_name(R"(c:\\a\b\c\d\file.txt)") == R"(c:\\a\b\c\d)");
2019-10-25 20:44:53 +08:00
REQUIRE(dir_name(R"(c://a/b/c/d/file.txt)") == R"(c:\\a\b\c\d)");
2019-10-25 19:14:50 +08:00
#else
2019-10-25 20:44:53 +08:00
REQUIRE(dir_name("dir/") == "dir");
REQUIRE(dir_name("dir///") == "dir//");
REQUIRE(dir_name("dir/file") == "dir");
REQUIRE(dir_name("dir/file.txt") == "dir");
REQUIRE(dir_name("dir/file.txt/") == "dir/file.txt");
REQUIRE(dir_name("/dir/file.txt") == "/dir");
REQUIRE(dir_name("//dir/file.txt") == "//dir");
REQUIRE(dir_name("../file.txt") == "..");
REQUIRE(dir_name("./file.txt") == ".");
2019-10-21 18:42:25 +08:00
#endif
2019-10-20 22:40:56 +08:00
}