Skip to content

Commit

Permalink
✨ (filemanager): Add clear file
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Dec 9, 2022
1 parent 7de1587 commit d249709
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions libs/FileManagerKit/include/FileManagerKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ struct File : public interface::File, public mbed::NonCopyable<File> {

auto create_directory(const std::filesystem::path &path) -> bool;
auto remove(const std::filesystem::path &path) -> bool;
void clear(const std::filesystem::path &path);
} // namespace leka::FileManagerKit
7 changes: 7 additions & 0 deletions libs/FileManagerKit/source/FileManagerKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ auto FileManagerKit::remove(const std::filesystem::path &path) -> bool
{
return std::filesystem::remove(path);
}

void FileManagerKit::clear(const std::filesystem::path &path)
{
if (std::filesystem::exists(path)) {
std::filesystem::resize_file(path, 0);
}
}
27 changes: 27 additions & 0 deletions libs/FileManagerKit/tests/FileManagerKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,30 @@ TEST_F(FileSystemTest, removeNotExistingDirectory)
auto removed = FileManagerKit::remove(path_B);
ASSERT_FALSE(removed);
}

TEST_F(FileSystemTest, clear)
{
std::ofstream(path_A).put('a');

auto initial_file_size = std::filesystem::file_size(path_A);
auto initial_free_space = std::filesystem::space(path_A).free;

EXPECT_NE(initial_file_size, 0);

FileManagerKit::clear(path_A);

auto final_file_size = std::filesystem::file_size(path_A);
auto final_free_space = std::filesystem::space(path_A).free;

EXPECT_LT(final_file_size, initial_file_size);
EXPECT_GT(final_free_space, initial_free_space);
}

TEST_F(FileSystemTest, clearNotExistingFile)
{
auto removed = FileManagerKit::remove(path_A);

FileManagerKit::clear(path_A);

// nothing expected
}

0 comments on commit d249709

Please sign in to comment.