cleanup: let the handler own the file system implementation

Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: Ic7c31237bd5440b2cfa171df93545a63708e404c
diff --git a/cleanup/cleanup.cpp b/cleanup/cleanup.cpp
index 278b079..baf5053 100644
--- a/cleanup/cleanup.cpp
+++ b/cleanup/cleanup.cpp
@@ -27,9 +27,11 @@
 
 std::unique_ptr<blobs::GenericBlobInterface>
     FileCleanupHandler::CreateCleanupHandler(
-        const std::string& blobId, const std::vector<std::string>& files)
+        const std::string& blobId, const std::vector<std::string>& files,
+        std::unique_ptr<FileSystemInterface> helper)
 {
-    return std::make_unique<FileCleanupHandler>(blobId, files);
+    return std::make_unique<FileCleanupHandler>(blobId, files,
+                                                std::move(helper));
 }
 
 bool FileCleanupHandler::canHandleBlob(const std::string& path)
diff --git a/cleanup/cleanup.hpp b/cleanup/cleanup.hpp
index 2266668..6ee8405 100644
--- a/cleanup/cleanup.hpp
+++ b/cleanup/cleanup.hpp
@@ -16,13 +16,14 @@
   public:
     static std::unique_ptr<blobs::GenericBlobInterface>
         CreateCleanupHandler(const std::string& blobId,
-                             const std::vector<std::string>& files);
+                             const std::vector<std::string>& files,
+                             std::unique_ptr<FileSystemInterface> helper);
 
     FileCleanupHandler(const std::string& blobId,
                        const std::vector<std::string>& files,
-                       const FileSystemInterface* helper) :
+                       std::unique_ptr<FileSystemInterface> helper) :
         supported(blobId),
-        files(files), helper(helper)
+        files(files), helper(std::move(helper))
     {}
 
     ~FileCleanupHandler() = default;
@@ -82,7 +83,7 @@
   private:
     std::string supported;
     std::vector<std::string> files;
-    const FileSystemInterface* helper;
+    std::unique_ptr<FileSystemInterface> helper;
 };
 
 } // namespace ipmi_flash
diff --git a/cleanup/main.cpp b/cleanup/main.cpp
index 0f82f4e..44cf0bf 100644
--- a/cleanup/main.cpp
+++ b/cleanup/main.cpp
@@ -31,15 +31,13 @@
 std::vector<std::string> files = {
     STATIC_HANDLER_STAGED_NAME, TARBALL_STAGED_NAME, HASH_FILENAME,
     VERIFY_STATUS_FILENAME, UPDATE_STATUS_FILENAME};
-
-FileSystem fileSystemHelper;
 } // namespace ipmi_flash
 
 extern "C" std::unique_ptr<blobs::GenericBlobInterface> createHandler()
 {
     auto handler = ipmi_flash::FileCleanupHandler::CreateCleanupHandler(
         ipmi_flash::cleanupBlobId, ipmi_flash::files,
-        &ipmi_flash::fileSystemHelper);
+        std::make_unique<ipmi_flash::FileSystem>());
 
     if (!handler)
     {
diff --git a/cleanup/test/cleanup_handler_unittest.cpp b/cleanup/test/cleanup_handler_unittest.cpp
index 5191234..33216a9 100644
--- a/cleanup/test/cleanup_handler_unittest.cpp
+++ b/cleanup/test/cleanup_handler_unittest.cpp
@@ -2,6 +2,7 @@
 #include "filesystem_mock.hpp"
 #include "util.hpp"
 
+#include <memory>
 #include <string>
 #include <vector>
 
@@ -18,24 +19,32 @@
 class CleanupHandlerTest : public ::testing::Test
 {
   protected:
+    CleanupHandlerTest() : mock(std::make_unique<FileSystemMock>())
+    {
+        mock_ptr = mock.get();
+        handler = std::make_unique<FileCleanupHandler>(cleanupBlobId, blobs,
+                                                       std::move(mock));
+    }
+
     std::vector<std::string> blobs = {"abcd", "efgh"};
-    FileSystemMock mock;
-    FileCleanupHandler handler{cleanupBlobId, blobs, &mock};
+    std::unique_ptr<FileSystemMock> mock;
+    FileSystemMock* mock_ptr;
+    std::unique_ptr<FileCleanupHandler> handler;
 };
 
 TEST_F(CleanupHandlerTest, GetBlobListReturnsExpectedList)
 {
-    EXPECT_TRUE(handler.canHandleBlob(cleanupBlobId));
-    EXPECT_THAT(handler.getBlobIds(),
+    EXPECT_TRUE(handler->canHandleBlob(cleanupBlobId));
+    EXPECT_THAT(handler->getBlobIds(),
                 UnorderedElementsAreArray({cleanupBlobId}));
 }
 
 TEST_F(CleanupHandlerTest, CommitShouldDeleteFiles)
 {
-    EXPECT_CALL(mock, remove("abcd")).WillOnce(Return());
-    EXPECT_CALL(mock, remove("efgh")).WillOnce(Return());
+    EXPECT_CALL(*mock_ptr, remove("abcd")).WillOnce(Return());
+    EXPECT_CALL(*mock_ptr, remove("efgh")).WillOnce(Return());
 
-    EXPECT_TRUE(handler.commit(1, {}));
+    EXPECT_TRUE(handler->commit(1, {}));
 }
 
 } // namespace