Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
vvromanov committed Dec 4, 2024
1 parent 1cab2bf commit 0323ce7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/file_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ bool is_file_exists(const char *name) {
}
return false;
}
return true;
return S_ISREG(structstat.st_mode);
}

bool is_dir_exist(const char *name) {
bool is_dir_exists(const char *name) {
struct stat structstat;
if (stat(name, &structstat) == -1) {
if (errno != ENOENT) {
Expand Down Expand Up @@ -89,7 +89,7 @@ bool mkdir_for_file(const char *filename, __mode_t mode) {
if (*p == '/') {
*p = 0;
if (0 != mkdir(tmp, mode)) {
if (errno != EEXIST || !is_dir_exist(tmp)) {
if (errno != EEXIST || !is_dir_exists(tmp)) {
log_write(LOG_LEVEL_ERR_ERRNO, "mkdir(%s) call failed", tmp);
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/file_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "stdbool.h"

bool is_file_exists(const char *name);
bool is_dir_exist(const char* name);
bool is_dir_exists(const char* name);
bool remove_test_file(const char* path);
bool get_file_size(const char *name, size_t &size);
bool touch(const std::string& pathname);
Expand Down
20 changes: 19 additions & 1 deletion tests/FileUtils_Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "gtest/gtest.h"

#define TEST_FILE "/tmp/file_utils.tst"
#define TEST_FILE_INVALID "/_##/"
#define TEST_FILE_INVALID "/root/*"

TEST(FileUtils, FileExists) {
FILE *fp = fopen(TEST_FILE, "ab+");
Expand All @@ -23,6 +23,24 @@ TEST(FileUtils, FileNotExistsInvalid) {
EXPECT_FALSE(is_file_exists("../../../../../../../../../some_file.txt"));
}

TEST(FileUtils, DirExists) {
EXPECT_TRUE(is_dir_exists("."));
EXPECT_TRUE(is_dir_exists("/"));
}

TEST(FileUtils, DirNotExists) {
remove(TEST_FILE);
EXPECT_FALSE(is_dir_exists(TEST_FILE));
}


TEST(FileUtils, DirNotExistsInvalid) {
remove(TEST_FILE);
EXPECT_FALSE(is_dir_exists(""));
EXPECT_FALSE(is_dir_exists(TEST_FILE_INVALID));
EXPECT_FALSE(is_dir_exists("../../../../../../../../../some_file.txt"));
}

TEST(FileUtils, GetExt) {
remove(TEST_FILE);
EXPECT_STREQ(nullptr, get_ext(NULL));
Expand Down

0 comments on commit 0323ce7

Please sign in to comment.