From 0e9a5237bdd1009572cf3e280fb07ddec7c4a02a Mon Sep 17 00:00:00 2001 From: Jamie Counsell Date: Sun, 25 Oct 2026 11:11:34 -0400 Subject: [PATCH] Fix inverted rmdir() result in the POSIX and stdio adapters rmdir() in the POSIX and stdio adapters returned false when the directory was removed and true when it wasn't, the opposite of every other adapter (and of mkdir()). Adds test/test_posix_fs, which runs the adapters against a real temp directory. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019rUgoKJdB4U8fNS9HybY8S --- include/microStore/Adapters/PosixFileSystem.h | 5 +- include/microStore/Adapters/StdioFileSystem.h | 5 +- test/test_posix_fs/test_posix_fs.cpp | 75 +++++++++++++++++++ 3 files changed, 77 insertions(+), 8 deletions(-) create mode 100644 test/test_posix_fs/test_posix_fs.cpp diff --git a/include/microStore/Adapters/PosixFileSystem.h b/include/microStore/Adapters/PosixFileSystem.h index 9c58927..ee9335f 100644 --- a/include/microStore/Adapters/PosixFileSystem.h +++ b/include/microStore/Adapters/PosixFileSystem.h @@ -315,10 +315,7 @@ class PosixFileSystem : public microStore::FileSystem { } inline virtual bool rmdir(const char* path) override { - if (::rmdir(path) == 0) { - return false; - } - return true; + return (::rmdir(path) == 0); } diff --git a/include/microStore/Adapters/StdioFileSystem.h b/include/microStore/Adapters/StdioFileSystem.h index cecffb0..9e54e7d 100644 --- a/include/microStore/Adapters/StdioFileSystem.h +++ b/include/microStore/Adapters/StdioFileSystem.h @@ -303,10 +303,7 @@ class StdioFileSystem : public microStore::FileSystem { } inline virtual bool rmdir(const char* path) override { - if (::rmdir(path) == 0) { - return false; - } - return true; + return (::rmdir(path) == 0); } diff --git a/test/test_posix_fs/test_posix_fs.cpp b/test/test_posix_fs/test_posix_fs.cpp new file mode 100644 index 0000000..68d2bff --- /dev/null +++ b/test/test_posix_fs/test_posix_fs.cpp @@ -0,0 +1,75 @@ +/* + * Tests for the POSIX and stdio filesystem adapters against a real + * temporary directory. + */ + +#include +#include +#include + +#include +#include +#include +#include + +static std::string tmp; + +static bool is_dir(const std::string& path) { + struct stat st; + return ::stat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode); +} + +/* ---- rmdir ---- */ + +static void check_rmdir(microStore::FileSystem fs) { + const std::string dir = tmp + "/sub"; + TEST_ASSERT_TRUE(fs.mkdir(dir.c_str())); + TEST_ASSERT_TRUE(fs.rmdir(dir.c_str())); + TEST_ASSERT_FALSE(is_dir(dir)); + TEST_ASSERT_FALSE(fs.rmdir(dir.c_str())); +} + +void test_posix_rmdir_reports_result() { + check_rmdir(microStore::Adapters::PosixFileSystem()); +} + +void test_stdio_rmdir_reports_result() { + check_rmdir(microStore::Adapters::StdioFileSystem()); +} + +/* ---- Main ---- */ + +void setUp() { + char tmpl[] = "/tmp/microstore_fs_XXXXXX"; + TEST_ASSERT_NOT_NULL(::mkdtemp(tmpl)); + tmp = tmpl; +} + +void tearDown() { + std::string cmd = "rm -rf '" + tmp + "'"; + (void)std::system(cmd.c_str()); +} + +int runUnityTests(void) { + UNITY_BEGIN(); + RUN_TEST(test_posix_rmdir_reports_result); + RUN_TEST(test_stdio_rmdir_reports_result); + return UNITY_END(); +} + +// For native dev-platform or for some embedded frameworks +int main(void) { + return runUnityTests(); +} + +#ifdef ARDUINO +// For Arduino framework +void setup() { + // Wait ~2 seconds before the Unity test runner + // establishes connection with a board Serial interface + delay(2000); + + runUnityTests(); +} +void loop() {} +#endif