Create TemporaryFileHandle class

TemporaryFileHandle class is used to create temp files in the
filesystem, and hold a handle to them until the class goes out of scope,
at which time they can be removed.  It replaces makeFile(), which was
not RAII safe if an exception gets thrown, and could potentially leave
files in the filesystem if the tests fail.

Tested: Unit tests pass

Change-Id: I03eb0d342a6cd7b78115a8c42be9175f30c4ccd0
Signed-off-by: Ed Tanous <ed@tanous.net>
diff --git a/test/http/file_test_utilities.hpp b/test/http/file_test_utilities.hpp
index 073a074..bd11a90 100644
--- a/test/http/file_test_utilities.hpp
+++ b/test/http/file_test_utilities.hpp
@@ -5,15 +5,33 @@
 
 #include <gtest/gtest.h>
 
-inline std::string makeFile(std::string_view sampleData)
+struct TemporaryFileHandle
 {
-    std::filesystem::path path = std::filesystem::temp_directory_path();
-    path /= "bmcweb_http_response_test_XXXXXXXXXXX";
-    std::string stringPath = path.string();
-    int fd = mkstemp(stringPath.data());
-    EXPECT_GT(fd, 0);
-    EXPECT_EQ(write(fd, sampleData.data(), sampleData.size()),
-              sampleData.size());
-    close(fd);
-    return stringPath;
-}
+    std::filesystem::path path;
+    std::string stringPath;
+
+    // Creates a temporary file with the contents provided, removes it on
+    // destruction.
+    explicit TemporaryFileHandle(std::string_view sampleData) :
+        path(std::filesystem::temp_directory_path() /
+             "bmcweb_http_response_test_XXXXXXXXXXX")
+    {
+        stringPath = path.string();
+
+        int fd = mkstemp(stringPath.data());
+        EXPECT_GT(fd, 0);
+        EXPECT_EQ(write(fd, sampleData.data(), sampleData.size()),
+                  sampleData.size());
+        close(fd);
+    }
+
+    TemporaryFileHandle(const TemporaryFileHandle&) = delete;
+    TemporaryFileHandle(TemporaryFileHandle&&) = delete;
+    TemporaryFileHandle& operator=(const TemporaryFileHandle&) = delete;
+    TemporaryFileHandle& operator=(TemporaryFileHandle&&) = delete;
+
+    ~TemporaryFileHandle()
+    {
+        std::filesystem::remove(path);
+    }
+};