regulators: Make TmpFile class common
A TmpFile class was implemented in two different test cases to
automatically create and delete temporary files.
Moved the TmpFile class into a standalone file so that the code could be
common among test cases. Improved the error handling.
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: Ifa8ddd3b442bcf8fbf0dc4d0a39dd9fc5f233e3f
diff --git a/phosphor-regulators/test/config_file_parser_tests.cpp b/phosphor-regulators/test/config_file_parser_tests.cpp
index 490cac7..83d5699 100644
--- a/phosphor-regulators/test/config_file_parser_tests.cpp
+++ b/phosphor-regulators/test/config_file_parser_tests.cpp
@@ -20,10 +20,9 @@
#include "pmbus_utils.hpp"
#include "pmbus_write_vout_command_action.hpp"
#include "rule.hpp"
+#include "tmp_file.hpp"
-#include <stdlib.h>
-#include <sys/stat.h>
-#include <unistd.h>
+#include <sys/stat.h> // for chmod()
#include <nlohmann/json.hpp>
@@ -46,41 +45,6 @@
using namespace phosphor::power::regulators::config_file_parser::internal;
using json = nlohmann::json;
-/**
- * @class TmpFile
- *
- * Temporary file.
- *
- * File is deleted automatically by the destructor when the object goes out of
- * scope.
- */
-class TmpFile
-{
- public:
- TmpFile()
- {
- int fd = mkstemp(fileName);
- if (fd == -1)
- {
- throw std::runtime_error{"Unable to create temporary file"};
- }
- close(fd);
- }
-
- std::string getName()
- {
- return fileName;
- }
-
- ~TmpFile()
- {
- unlink(fileName);
- }
-
- private:
- char fileName[17] = "/tmp/temp-XXXXXX";
-};
-
void writeConfigFile(const std::filesystem::path& pathName,
const std::string& contents)
{