From 8598fd7ef1fc94c45e2ee6e4178cb53ca42fede9 Mon Sep 17 00:00:00 2001 From: Yann Locatelli Date: Mon, 12 Dec 2022 14:56:17 +0100 Subject: [PATCH] :sparkles: (file): Add clear Delete file content but file still exists --- include/interface/platform/File.h | 1 + libs/FileManagerKit/include/FileManagerKit.h | 3 +++ libs/FileManagerKit/source/File.cpp | 5 ++++ libs/FileManagerKit/tests/File_test.cpp | 27 ++++++++++++++++++++ tests/unit/mocks/mocks/leka/File.h | 1 + 5 files changed, 37 insertions(+) diff --git a/include/interface/platform/File.h b/include/interface/platform/File.h index 0d0cef9533..b991d7e40b 100644 --- a/include/interface/platform/File.h +++ b/include/interface/platform/File.h @@ -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; diff --git a/libs/FileManagerKit/include/FileManagerKit.h b/libs/FileManagerKit/include/FileManagerKit.h index 8a56a7434d..16027ca668 100644 --- a/libs/FileManagerKit/include/FileManagerKit.h +++ b/libs/FileManagerKit/include/FileManagerKit.h @@ -35,7 +35,10 @@ struct File : public interface::File, public mbed::NonCopyable { 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; diff --git a/libs/FileManagerKit/source/File.cpp b/libs/FileManagerKit/source/File.cpp index 5ec4e49057..ba30698850 100644 --- a/libs/FileManagerKit/source/File.cpp +++ b/libs/FileManagerKit/source/File.cpp @@ -100,6 +100,11 @@ void File::seek(size_t pos, int origin) std::fseek(_file.get(), static_cast(pos), origin); } +void File::clear() +{ + reopen(_path, "w+"); +} + void File::rewind() { seek(0); diff --git a/libs/FileManagerKit/tests/File_test.cpp b/libs/FileManagerKit/tests/File_test.cpp index 001bef7462..269323dd82 100644 --- a/libs/FileManagerKit/tests/File_test.cpp +++ b/libs/FileManagerKit/tests/File_test.cpp @@ -864,3 +864,30 @@ TEST_F(FileTest, clearError) ASSERT_FALSE(error_after_clear); } + +TEST_F(FileTest, clear) +{ + auto input_data = std::to_array({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 +} diff --git a/tests/unit/mocks/mocks/leka/File.h b/tests/unit/mocks/mocks/leka/File.h index 89b2138a46..eab081d9be 100644 --- a/tests/unit/mocks/mocks/leka/File.h +++ b/tests/unit/mocks/mocks/leka/File.h @@ -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));