Skip to content

Commit

Permalink
Improve coverage, fix get_ext function
Browse files Browse the repository at this point in the history
  • Loading branch information
vvromanov committed Dec 4, 2024
1 parent 380b883 commit 1cab2bf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
27 changes: 24 additions & 3 deletions src/file_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,32 @@ bool mkdir_for_file(const char *filename, __mode_t mode) {
return true;
}

const char *get_ext(const char *filename) {
const char *p = strrchr(filename, '.');
static const char* get_filename(const char* path) {
if (path == nullptr) {
return nullptr;
}
const char* p = strrchr(path, '/');
if (p == nullptr) {
p = strrchr(path, '\\');
}
if (p) {
return p + 1;
} else {
return "";
return path;
}
}

const char *get_ext(const char *path) {
const char* filename = get_filename(path);
if (filename != nullptr) {
const char* p = strrchr(filename, '.');
if (p) {
return p + 1;
}
else {
return "";
}
} else {
return nullptr;
}
}
2 changes: 1 addition & 1 deletion src/file_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ bool remove_test_file(const char* path);
bool get_file_size(const char *name, size_t &size);
bool touch(const std::string& pathname);
bool mkdir_for_file(const char* filename, __mode_t mode);
const char* get_ext(const char* filename);
const char* get_ext(const char* path);
13 changes: 13 additions & 0 deletions tests/FileUtils_Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@ TEST(FileUtils, FileNotExistsInvalid) {
EXPECT_FALSE(is_file_exists(TEST_FILE_INVALID));
EXPECT_FALSE(is_file_exists("../../../../../../../../../some_file.txt"));
}

TEST(FileUtils, GetExt) {
remove(TEST_FILE);
EXPECT_STREQ(nullptr, get_ext(NULL));
EXPECT_STREQ("", get_ext(""));
EXPECT_STREQ("", get_ext("filename"));
EXPECT_STREQ("ext", get_ext("filename.ext"));
EXPECT_STREQ("ext", get_ext("dir/filename.ext"));
EXPECT_STREQ("ext", get_ext("dir\\filename.ext"));
EXPECT_STREQ("", get_ext("dir/filename"));
EXPECT_STREQ("", get_ext("dir.ext/filename"));
EXPECT_STREQ("", get_ext("dir.ext\\filename"));
}

0 comments on commit 1cab2bf

Please sign in to comment.