diff --git a/src/fsfw/filesystem/HasFileSystemIF.h b/src/fsfw/filesystem/HasFileSystemIF.h index a9f2b8ea..9771dda0 100644 --- a/src/fsfw/filesystem/HasFileSystemIF.h +++ b/src/fsfw/filesystem/HasFileSystemIF.h @@ -132,6 +132,9 @@ class HasFileSystemIF { * @return */ virtual ReturnValue_t createDirectory(FilesystemParams params, bool createParentDirs) = 0; + virtual ReturnValue_t createDirectory(FilesystemParams params) { + return createDirectory(params, false); + } /** * @brief Generic function to remove a directory @@ -143,10 +146,11 @@ class HasFileSystemIF { return removeDirectory(params, false); } - virtual ReturnValue_t renameFile(const char* oldPath, char* newPath) { + virtual ReturnValue_t rename(const char* oldPath, const char* newPath) { return rename(oldPath, newPath, nullptr); } - virtual ReturnValue_t rename(const char* oldPath, char* newPath, FileSystemArgsIF* args) = 0; + virtual ReturnValue_t rename(const char* oldPath, const char* newPath, + FileSystemArgsIF* args) = 0; }; #endif /* FSFW_MEMORY_HASFILESYSTEMIF_H_ */ diff --git a/src/fsfw_hal/host/HostFilesystem.cpp b/src/fsfw_hal/host/HostFilesystem.cpp index c4a00baf..8a861d47 100644 --- a/src/fsfw_hal/host/HostFilesystem.cpp +++ b/src/fsfw_hal/host/HostFilesystem.cpp @@ -132,7 +132,8 @@ ReturnValue_t HostFilesystem::removeDirectory(FilesystemParams params, bool dele return HasFileSystemIF::GENERIC_DIR_ERROR; } -ReturnValue_t HostFilesystem::rename(const char *oldPath_, char *newPath_, FileSystemArgsIF *args) { +ReturnValue_t HostFilesystem::rename(const char *oldPath_, const char *newPath_, + FileSystemArgsIF *args) { if (oldPath_ == nullptr or newPath_ == nullptr) { return HasReturnvaluesIF::RETURN_FAILED; } diff --git a/src/fsfw_hal/host/HostFilesystem.h b/src/fsfw_hal/host/HostFilesystem.h index 4dda3ff7..c35e0fda 100644 --- a/src/fsfw_hal/host/HostFilesystem.h +++ b/src/fsfw_hal/host/HostFilesystem.h @@ -15,11 +15,15 @@ class HostFilesystem : public HasFileSystemIF { ReturnValue_t removeFile(const char *path, FileSystemArgsIF *args) override; ReturnValue_t createDirectory(FilesystemParams params, bool createParentDirs) override; ReturnValue_t removeDirectory(FilesystemParams params, bool deleteRecurively) override; - ReturnValue_t rename(const char *oldPath, char *newPath, FileSystemArgsIF *args) override; + ReturnValue_t rename(const char *oldPath, const char *newPath, FileSystemArgsIF *args) override; std::error_code errorCode; + using HasFileSystemIF::createDirectory; using HasFileSystemIF::createFile; + using HasFileSystemIF::removeDirectory; using HasFileSystemIF::removeFile; + using HasFileSystemIF::rename; + private: }; #endif // FSFW_HAL_HOSTFILESYSTEM_H diff --git a/unittests/hal/testHostFilesystem.cpp b/unittests/hal/testHostFilesystem.cpp index a7166b01..d3fd880a 100644 --- a/unittests/hal/testHostFilesystem.cpp +++ b/unittests/hal/testHostFilesystem.cpp @@ -1,17 +1,22 @@ #include +#include #include "fsfw_hal/host/HostFilesystem.h" -#include TEST_CASE("Host Filesystem", "[hal][host]") { namespace fs = std::filesystem; auto hostFs = HostFilesystem(); auto tmpDir = fs::temp_directory_path(); + fs::path file0 = tmpDir / "hello.txt"; + fs::path file1 = tmpDir / "hello.txt"; + REQUIRE_NOTHROW(fs::remove(file0)); + REQUIRE_NOTHROW(fs::remove(file1)); SECTION("Create file") { fs::path file = tmpDir / "hello.txt"; FilesystemParams params(file.c_str()); REQUIRE(hostFs.createFile(params) == result::OK); + CHECK(fs::is_regular_file(file)); REQUIRE(fs::exists(file)); REQUIRE_NOTHROW(fs::remove(file)); } @@ -20,9 +25,38 @@ TEST_CASE("Host Filesystem", "[hal][host]") { fs::path file = tmpDir / "hello.txt"; FilesystemParams params(file.c_str()); REQUIRE(hostFs.createFile(params) == result::OK); + CHECK(fs::is_regular_file(file)); REQUIRE(fs::exists(file)); REQUIRE(hostFs.removeFile(file.c_str()) == result::OK); REQUIRE(not fs::exists(file)); } + SECTION("Create Directory") { + fs::path dirPath = tmpDir / "test_dir"; + FilesystemParams params(dirPath.c_str()); + REQUIRE(hostFs.createDirectory(params) == result::OK); + CHECK(fs::is_directory(dirPath)); + REQUIRE(fs::exists(dirPath)); + REQUIRE_NOTHROW(fs::remove(dirPath)); + } + + SECTION("Remove Directory") { + fs::path dirPath = tmpDir / "test_dir"; + FilesystemParams params(dirPath.c_str()); + REQUIRE(hostFs.createDirectory(params) == result::OK); + REQUIRE(fs::exists(dirPath)); + REQUIRE(hostFs.removeDirectory(params) == result::OK); + REQUIRE(not fs::exists(dirPath)); + } + + SECTION("Rename File") { + fs::path file = tmpDir / "hello.txt"; + fs::path newFile = tmpDir / "hello2.txt"; + FilesystemParams params(file.c_str()); + REQUIRE(hostFs.createFile(params) == result::OK); + CHECK(fs::is_regular_file(file)); + REQUIRE(fs::exists(file)); + REQUIRE(hostFs.rename(file.c_str(), newFile.c_str()) == result::OK); + REQUIRE_NOTHROW(fs::remove(newFile)); + } } \ No newline at end of file diff --git a/unittests/mocks/FilesystemMock.cpp b/unittests/mocks/FilesystemMock.cpp index b52c744f..5b195ffc 100644 --- a/unittests/mocks/FilesystemMock.cpp +++ b/unittests/mocks/FilesystemMock.cpp @@ -64,7 +64,8 @@ ReturnValue_t FilesystemMock::removeDirectory(FilesystemParams params, bool dele return HasReturnvaluesIF::RETURN_OK; } -ReturnValue_t FilesystemMock::rename(const char *oldPath, char *newPath, FileSystemArgsIF *args) { +ReturnValue_t FilesystemMock::rename(const char *oldPath, const char *newPath, + FileSystemArgsIF *args) { renameQueue.push(RenameInfo(oldPath, newPath)); return HasReturnvaluesIF::RETURN_OK; } diff --git a/unittests/mocks/FilesystemMock.h b/unittests/mocks/FilesystemMock.h index a1af45f0..f7c53eda 100644 --- a/unittests/mocks/FilesystemMock.h +++ b/unittests/mocks/FilesystemMock.h @@ -60,7 +60,7 @@ class FilesystemMock : public HasFileSystemIF { ReturnValue_t removeFile(const char *path, FileSystemArgsIF *args) override; ReturnValue_t createDirectory(FilesystemParams params, bool createParentDirs) override; ReturnValue_t removeDirectory(FilesystemParams params, bool deleteRecurively) override; - ReturnValue_t rename(const char *oldPath, char *newPath, FileSystemArgsIF *args) override; + ReturnValue_t rename(const char *oldPath, const char *newPath, FileSystemArgsIF *args) override; void reset();