spdlog/tests/test_create_dir.cpp
2019-10-25 14:14:50 +03:00

71 lines
2.7 KiB
C++

/*
* 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;
using spdlog::details::os::file_exists;
void test_create_dir(const char *path, const char *normalized_path)
{
printf("Test Create dir %s\n", path);
auto rv = create_dir(path);
REQUIRE(rv == true);
REQUIRE(file_exists(normalized_path));
}
#include "spdlog/sinks/stdout_color_sinks.h"
TEST_CASE("create_dir", "[create_dir]")
{
prepare_logdir();
#ifdef WIN32
test_create_dir("test_logs/dir1/dir1", "test_logs\\dir1\\dir1");
test_create_dir("test_logs/dir1/dir1", "test_logs\\dir1\\dir1"); //test existing
test_create_dir("test_logs/dir1///dir2//", "test_logs\\dir1\\dir2");
test_create_dir("./test_logs/dir1/dir3", "test_logs\\dir1\\dir3");
test_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs\\dir1\\dir4");
// test backslash
test_create_dir("test_logs\\dir1\\dir222", "test_logs\\dir1\\dir222");
test_create_dir("test_logs\\dir1\\dir223\\", "test_logs\\dir1\\dir223\\");
test_create_dir(".\\test_logs\\dir1\\dir2\\dir99\\..\\dir23", "test_logs\\dir1\\dir2\\dir23");
#else
test_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1");
test_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1"); // test existing
test_create_dir("test_logs/dir1///dir2", "test_logs/dir1/dir2");
test_create_dir("./test_logs/dir1/dir3", "test_logs/dir1/dir3");
test_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4");
#endif
}
TEST_CASE("dir_name", "[create_dir]")
{
using spdlog::details::os::dir_name;
REQUIRE(dir_name("").empty());
REQUIRE(dir_name("dir").empty());
#ifdef WIN32
REQUIRE(dir_name(R"(dir\)") == "dir");
REQUIRE(dir_name(R"(dir\\\)") == R"(dir\\)");
REQUIRE(dir_name(R"(dir\file)") == "dir");
REQUIRE(dir_name(R"(dir\file.txt)") == "dir");
REQUIRE(dir_name(R"(dir\file.txt\)") == R"(dir\file.txt)");
REQUIRE(dir_name(R"(\dir\file.txt)") == R"(\dir)");
REQUIRE(dir_name(R"(\\dir\file.txt)") == R"(\\dir)");
REQUIRE(dir_name(R"(..\file.txt)") == "..");
REQUIRE(dir_name(R"(.\file.txt)") == ".");
REQUIRE(dir_name(R"(c:\\a\b\c\d\file.txt)") == R"(c:\\a\b\c\d)");
#else
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") == ".");
#endif
}