FileUtilsTest DirExists function added

BUG=webrtc:7218

Review-Url: https://codereview.webrtc.org/2749163003
Cr-Commit-Position: refs/heads/master@{#17247}
This commit is contained in:
alessiob
2017-03-15 06:04:59 -07:00
committed by Commit bot
parent 996a83c4c8
commit e49fede158
3 changed files with 29 additions and 0 deletions

View File

@ -106,6 +106,12 @@ bool FileExists(const std::string& file_name) {
return stat(file_name.c_str(), &file_info) == 0;
}
bool DirExists(const std::string& directory_name) {
struct stat directory_info = {0};
return stat(directory_name.c_str(), &directory_info) == 0 && S_ISDIR(
directory_info.st_mode);
}
#ifdef WEBRTC_ANDROID
std::string ProjectRootPath() {

View File

@ -74,6 +74,9 @@ bool CreateDir(const std::string& directory_name);
// Checks if a file exists.
bool FileExists(const std::string& file_name);
// Checks if a directory exists.
bool DirExists(const std::string& directory_name);
// File size of the supplied file in bytes. Will return 0 if the file is
// empty or if the file does not exist/is readable.
size_t GetFileSize(const std::string& filename);

View File

@ -150,4 +150,24 @@ TEST_F(FileUtilsTest, GetFileSizeNonExistingFile) {
ASSERT_EQ(0u, webrtc::test::GetFileSize("non-existing-file.tmp"));
}
TEST_F(FileUtilsTest, DirExists) {
// Check that an existing directory is recognized as such.
ASSERT_TRUE(webrtc::test::DirExists(webrtc::test::OutputPath()))
<< "Existing directory not found";
// Check that a non-existing directory is recognized as such.
std::string directory = "direxists-unittest-non_existing-dir";
ASSERT_FALSE(webrtc::test::DirExists(directory))
<< "Non-existing directory found";
// Check that an existing file is not recognized as an existing directory.
std::string temp_filename = webrtc::test::TempFilename(
webrtc::test::OutputPath(), "TempFilenameTest");
ASSERT_TRUE(webrtc::test::FileExists(temp_filename))
<< "Couldn't find file: " << temp_filename;
ASSERT_FALSE(webrtc::test::DirExists(temp_filename))
<< "Existing file recognized as existing directory";
remove(temp_filename.c_str());
}
} // namespace webrtc