diff --git a/include/CLI/Validators.hpp b/include/CLI/Validators.hpp index 8cf567c1..75c9b397 100644 --- a/include/CLI/Validators.hpp +++ b/include/CLI/Validators.hpp @@ -51,7 +51,7 @@ bool ExistingDirectory(std::string filename) { return true; } else { std::cerr << "Directory is actually a file: " << filename << std::endl; - return true; + return false; } } diff --git a/tests/HelpersTest.cpp b/tests/HelpersTest.cpp index 886fa3cd..2553daa3 100644 --- a/tests/HelpersTest.cpp +++ b/tests/HelpersTest.cpp @@ -83,6 +83,32 @@ TEST(Validators, FileNotExists) { EXPECT_TRUE(CLI::NonexistentPath(myfile)); } +TEST(Validators, FileIsDir) { + std::string mydir{"../tests"}; + EXPECT_FALSE(CLI::ExistingFile(mydir)); +} + +TEST(Validators, DirectoryExists) { + std::string mydir{"../tests"}; + EXPECT_TRUE(CLI::ExistingDirectory(mydir)); +} + +TEST(Validators, DirectoryNotExists) { + std::string mydir{"nondirectory"}; + EXPECT_FALSE(CLI::ExistingDirectory(mydir)); +} + +TEST(Validators, DirectoryIsFile) { + std::string myfile{"TestFileNotUsed.txt"}; + EXPECT_TRUE(CLI::NonexistentPath(myfile)); + bool ok = static_cast(std::ofstream(myfile.c_str()).put('a')); // create file + EXPECT_TRUE(ok); + EXPECT_FALSE(CLI::ExistingDirectory(myfile)); + + std::remove(myfile.c_str()); + EXPECT_TRUE(CLI::NonexistentPath(myfile)); +} + // Yes, this is testing an app_helper :) TEST(AppHelper, TempfileCreated) { std::string name = "TestFileNotUsed.txt";