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