Skip to content

Commit

Permalink
✨ (file): Add clear
Browse files Browse the repository at this point in the history
Delete file content but file still exists
  • Loading branch information
YannLocatelli committed Dec 12, 2022
1 parent e71df79 commit 8598fd7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/interface/platform/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct File {

virtual void seek(size_t pos, int origin) = 0;

virtual void clear() = 0;
virtual void rewind() = 0;

virtual auto size() -> std::size_t = 0;
Expand Down
3 changes: 3 additions & 0 deletions libs/FileManagerKit/include/FileManagerKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ struct File : public interface::File, public mbed::NonCopyable<File> {
auto write(const char *data, uint32_t size) -> std::size_t final;

void seek(size_t pos, int origin = SEEK_SET) final;

void clear() final;
void rewind() final;

auto size() -> std::size_t final;

auto tell() -> std::size_t final;
Expand Down
5 changes: 5 additions & 0 deletions libs/FileManagerKit/source/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ void File::seek(size_t pos, int origin)
std::fseek(_file.get(), static_cast<long>(pos), origin);
}

void File::clear()
{
reopen(_path, "w+");
}

void File::rewind()
{
seek(0);
Expand Down
27 changes: 27 additions & 0 deletions libs/FileManagerKit/tests/File_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,3 +864,30 @@ TEST_F(FileTest, clearError)

ASSERT_FALSE(error_after_clear);
}

TEST_F(FileTest, clear)
{
auto input_data = std::to_array<uint8_t>({0x61, 0x62, 0x63, 0x64, 0x65, 0x66}); // "abcdef"

writeTempFile(input_data);

file.open(tempFilename);
auto size = file.size();
EXPECT_NE(size, 0);

file.clear();

size = file.size();
EXPECT_EQ(size, 0);

file.close();
}

TEST_F(FileTest, clearNotExistingFile)
{
ASSERT_FALSE(file.is_open());

file.clear();

// nothing expected
}
1 change: 1 addition & 0 deletions tests/unit/mocks/mocks/leka/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class File : public interface::File

MOCK_METHOD(void, seek, (size_t, int), (override));

MOCK_METHOD(void, clear, (), (override));
MOCK_METHOD(void, rewind, (), (override));

MOCK_METHOD(size_t, size, (), (override));
Expand Down

0 comments on commit 8598fd7

Please sign in to comment.