regulators: Remove TmpFile class
The TmpFile class was created in the test directory for managing
temporary files used by testcases.
A new TemporaryFile class has replaced it. TemporaryFile has more
features and can be used by production (non-test) code as well.
This commit removes the TmpFile class and modifies testcases to use
TemporaryFile instead.
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: Id06d333416e43d5685c377ee0780b475d20cba8f
diff --git a/phosphor-regulators/test/config_file_parser_tests.cpp b/phosphor-regulators/test/config_file_parser_tests.cpp
index 80e65ba..edfa958 100644
--- a/phosphor-regulators/test/config_file_parser_tests.cpp
+++ b/phosphor-regulators/test/config_file_parser_tests.cpp
@@ -40,7 +40,7 @@
#include "run_rule_action.hpp"
#include "sensor_monitoring.hpp"
#include "set_device_action.hpp"
-#include "tmp_file.hpp"
+#include "temporary_file.hpp"
#include <sys/stat.h> // for chmod()
@@ -107,8 +107,8 @@
}
)"_json;
- TmpFile configFile;
- std::filesystem::path pathName{configFile.getName()};
+ TemporaryFile configFile;
+ std::filesystem::path pathName{configFile.getPath()};
writeConfigFile(pathName, configFileContents);
std::vector<std::unique_ptr<Rule>> rules{};
@@ -146,8 +146,8 @@
}
)"_json;
- TmpFile configFile;
- std::filesystem::path pathName{configFile.getName()};
+ TemporaryFile configFile;
+ std::filesystem::path pathName{configFile.getPath()};
writeConfigFile(pathName, configFileContents);
chmod(pathName.c_str(), 0222);
@@ -165,8 +165,8 @@
{
const std::string configFileContents = "] foo [";
- TmpFile configFile;
- std::filesystem::path pathName{configFile.getName()};
+ TemporaryFile configFile;
+ std::filesystem::path pathName{configFile.getPath()};
writeConfigFile(pathName, configFileContents);
parse(pathName);
@@ -182,8 +182,8 @@
{
const json configFileContents = R"( { "foo": "bar" } )"_json;
- TmpFile configFile;
- std::filesystem::path pathName{configFile.getName()};
+ TemporaryFile configFile;
+ std::filesystem::path pathName{configFile.getPath()};
writeConfigFile(pathName, configFileContents);
parse(pathName);
diff --git a/phosphor-regulators/test/tmp_file.hpp b/phosphor-regulators/test/tmp_file.hpp
deleted file mode 100644
index dd0a66c..0000000
--- a/phosphor-regulators/test/tmp_file.hpp
+++ /dev/null
@@ -1,99 +0,0 @@
-/**
- * Copyright © 2020 IBM Corporation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include <errno.h> // for errno
-#include <stdio.h> // for perror()
-#include <stdlib.h> // for mkstemp()
-#include <string.h> // for strerror()
-#include <unistd.h> // for close(), unlink()
-
-#include <stdexcept>
-#include <string>
-
-namespace phosphor::power::regulators
-{
-
-/**
- * @class TmpFile
- *
- * Temporary file.
- *
- * File is created by the constructor and deleted by the destructor. The file
- * name can be obtained from getName().
- */
-class TmpFile
-{
- public:
- // Specify which compiler-generated methods we want
- TmpFile(const TmpFile&) = delete;
- TmpFile(TmpFile&&) = delete;
- TmpFile& operator=(const TmpFile&) = delete;
- TmpFile& operator=(TmpFile&&) = delete;
-
- /**
- * Constructor.
- *
- * Creates the temporary file.
- */
- TmpFile()
- {
- // Generate unique file name, create file, and open it. The XXXXXX
- // characters are replaced by mkstemp() to make the file name unique.
- char fileNameTemplate[17] = "/tmp/temp-XXXXXX";
- int fd = mkstemp(fileNameTemplate);
- if (fd == -1)
- {
- // If mkstemp() fails, throw an exception. No temporary file has
- // been created and calling getName() would not work.
- throw std::runtime_error{"Unable to create temporary file: " +
- std::string{strerror(errno)}};
- }
-
- // Close file
- if (close(fd) == -1)
- {
- // If close() fails, write a message to standard error but do not
- // throw an exception. If an exception is thrown, the destructor
- // will not be run and the temporary file will not be deleted.
- perror("Unable to close temporary file");
- }
-
- // Save file name
- fileName = fileNameTemplate;
- }
-
- const std::string& getName()
- {
- return fileName;
- }
-
- ~TmpFile()
- {
- // Delete temporary file
- if (unlink(fileName.c_str()) == -1)
- {
- // If unlink() fails, write a message to standard error but do not
- // throw an exception. Destructors must not throw exceptions.
- perror("Unable to delete temporary file");
- }
- }
-
- private:
- std::string fileName{};
-};
-
-} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/test/validate-regulators-config_tests.cpp b/phosphor-regulators/test/validate-regulators-config_tests.cpp
index 139b3ca..e62a88c 100644
--- a/phosphor-regulators/test/validate-regulators-config_tests.cpp
+++ b/phosphor-regulators/test/validate-regulators-config_tests.cpp
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-#include "tmp_file.hpp"
+#include "temporary_file.hpp"
#include <stdio.h> // for popen(), pclose(), fgets()
#include <sys/stat.h> // for chmod()
@@ -120,8 +120,8 @@
{
// run the validation tool with the temporary file and return the output
// of the validation tool.
- TmpFile tmpFile;
- command += " 2> " + tmpFile.getName();
+ TemporaryFile tmpFile;
+ command += " 2> " + tmpFile.getPath().string();
// get the jsonschema print from validation tool.
char buffer[256];
std::string result;
@@ -151,7 +151,7 @@
int exitStatus = WEXITSTATUS(returnValue);
// Read the standardError from tmpFile.
- std::ifstream input(tmpFile.getName().c_str());
+ std::ifstream input(tmpFile.getPath());
std::string line;
if (std::getline(input, line))
@@ -199,9 +199,8 @@
void expectJsonValid(const json configFileJson)
{
- std::string fileName;
- TmpFile tmpFile;
- fileName = tmpFile.getName();
+ TemporaryFile tmpFile;
+ std::string fileName = tmpFile.getPath().string();
writeDataToFile(configFileJson, fileName);
EXPECT_FILE_VALID(fileName);
@@ -211,9 +210,8 @@
const std::string& expectedErrorMessage,
const std::string& expectedOutputMessage)
{
- std::string fileName;
- TmpFile tmpFile;
- fileName = tmpFile.getName();
+ TemporaryFile tmpFile;
+ std::string fileName = tmpFile.getPath().string();
writeDataToFile(configFileJson, fileName);
EXPECT_FILE_INVALID(fileName, expectedErrorMessage, expectedOutputMessage);
@@ -2867,9 +2865,8 @@
"usage: validate-regulators-config.py [-h] [-s SCHEMA_FILE]";
int valid = 0;
- std::string fileName;
- TmpFile tmpFile;
- fileName = tmpFile.getName();
+ TemporaryFile tmpFile;
+ std::string fileName = tmpFile.getPath().string();
writeDataToFile(validConfigFile, fileName);
// Valid: -s specified
{
@@ -2954,9 +2951,8 @@
}
// Invalid: File specified after -s is not right data format
{
- std::string wrongFormatFileName;
- TmpFile wrongFormatFile;
- wrongFormatFileName = wrongFormatFile.getName();
+ TemporaryFile wrongFormatFile;
+ std::string wrongFormatFileName = wrongFormatFile.getPath().string();
std::ofstream out(wrongFormatFileName);
out << "foo";
out.close();
@@ -2967,9 +2963,8 @@
}
// Invalid: File specified after -c is not readable
{
- std::string notReadableFileName;
- TmpFile notReadableFile;
- notReadableFileName = notReadableFile.getName();
+ TemporaryFile notReadableFile;
+ std::string notReadableFileName = notReadableFile.getPath().string();
writeDataToFile(validConfigFile, notReadableFileName);
command = validateTool + schema + schemaFile + configuration +
notReadableFileName;
@@ -2979,9 +2974,8 @@
}
// Invalid: File specified after -s is not readable
{
- std::string notReadableFileName;
- TmpFile notReadableFile;
- notReadableFileName = notReadableFile.getName();
+ TemporaryFile notReadableFile;
+ std::string notReadableFileName = notReadableFile.getPath().string();
writeDataToFile(validConfigFile, notReadableFileName);
command = validateTool + schema + notReadableFileName + configuration +
fileName;