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/validate-regulators-config_tests.cpp b/phosphor-regulators/test/validate-regulators-config_tests.cpp
index 2b915b9..987a492 100644
--- a/phosphor-regulators/test/validate-regulators-config_tests.cpp
+++ b/phosphor-regulators/test/validate-regulators-config_tests.cpp
@@ -13,14 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/stat.h>
-#include <sys/wait.h>
+#include "tmp_file.hpp"
+
+#include <stdio.h> // for popen(), pclose(), fgets()
+#include <sys/stat.h> // for chmod()
+#include <sys/wait.h> // for WEXITSTATUS
#include <nlohmann/json.hpp>
+#include <cstdio>
#include <fstream>
#include <gtest/gtest.h>
@@ -35,6 +36,7 @@
expectJsonInvalid(configFileJson, expectedErrorMessage, \
expectedOutputMessage)
+using namespace phosphor::power::regulators;
using json = nlohmann::json;
const json validConfigFile = R"(
@@ -103,33 +105,6 @@
}
)"_json;
-class TmpFile
-{
- public:
- TmpFile()
- {
- int fd = mkstemp(fileName);
- if (fd == -1)
- {
- perror("Can't create temporary file");
- }
- close(fd);
- }
-
- std::string getName()
- {
- return fileName;
- }
-
- ~TmpFile()
- {
- unlink(fileName);
- }
-
- private:
- char fileName[17] = "/tmp/temp-XXXXXX";
-};
-
std::string getValidationToolCommand(const std::string& configFileName)
{
std::string command =