From c19e325b83fcd42e40bd99b9325d7c60bf482c90 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:17:02 +0300 Subject: [PATCH] Added some tests for create_dir --- include/spdlog/details/file_helper-inl.h | 6 ++---- include/spdlog/details/os-inl.h | 7 ++++++- tests/test_create_dir.cpp | 8 ++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index eabc9aaa..be670347 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -34,10 +34,8 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) for (int tries = 0; tries < open_tries_; ++tries) { - if (!folder_name.empty()) - { - os::create_dir(folder_name); // will not created if already exists - } + // will not created if already exists or empty. + os::create_dir(folder_name); if (!os::fopen_s(&fd_, fname, mode)) { diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index b5669ac7..95a67917 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -478,7 +478,7 @@ SPDLOG_INLINE bool mkdir_(const filename_t &path) } // create the given directory - and all directories leading to it -// return true on success +// return true on success or if the directory already exists SPDLOG_INLINE bool create_dir(filename_t path) { if (path_exists(path)) @@ -486,6 +486,11 @@ SPDLOG_INLINE bool create_dir(filename_t path) return true; } + if(path.empty()) + { + return false; + } + #ifdef _WIN32 // support forward slash in windows std::replace(path.begin(), path.end(), '/', folder_sep); diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp index a41c63a5..41f91ef3 100644 --- a/tests/test_create_dir.cpp +++ b/tests/test_create_dir.cpp @@ -32,6 +32,14 @@ TEST_CASE("create_dir", "[create_dir]") #endif } +TEST_CASE("create_invalid_dir", "[create_dir]") +{ + REQUIRE(create_dir("") == false); +#ifdef __linux__ + REQUIRE(create_dir("/proc/spdlog-utest") == false); +#endif +} + TEST_CASE("dir_name", "[create_dir]") { using spdlog::details::os::dir_name;