Check existing temp files in system's temp folder
Signed-off-by: austinfcui <austinfcui@gmail.com>
Change-Id: Ic76c058cb18d5e723794a86c6695b73fbc409fa3
diff --git a/test/test-util-data-file.cpp b/test/test-util-data-file.cpp
index de37677..46e43ae 100644
--- a/test/test-util-data-file.cpp
+++ b/test/test-util-data-file.cpp
@@ -11,9 +11,19 @@
TEST(UtilDataFile, TestFindFiles)
{
+ // The parent folder is determined by the file system.
+ // On Linux it is /tmp.
+ fs::path dataDir = fs::temp_directory_path();
+ // Regex pattern of the temp files used by TemporaryFile class.
+ auto regexPattern = R"(openpower\-hw\-diags\-.*)";
+ // The vector to store temp files.
+ vector<fs::path> dataPaths;
+
+ // Create two new temp file objects.
TemporaryFile tempFile1{};
TemporaryFile tempFile2{};
+ // Get the string of paths of temp files.
string fullPathTempFile1 = tempFile1.getPath();
string fullPathTempFile2 = tempFile2.getPath();
EXPECT_NE(fullPathTempFile1, fullPathTempFile2);
@@ -21,31 +31,24 @@
trace::inf("fullPathTempFile1: %s", fullPathTempFile1.c_str());
trace::inf("fullPathTempFile2: %s", fullPathTempFile2.c_str());
+ // Path objects of two temp files
// path1 and path2 will be used to test later.
fs::path path1 = fullPathTempFile1;
fs::path path2 = fullPathTempFile2;
- // parent pathes for path1 and path2 are the same.
- fs::path dataDir = path1.parent_path();
- auto regexPattern = R"(openpower\-hw\-diags\-.*)";
- vector<fs::path> dataPaths;
-
- trace::inf("parent path: %s", string(dataDir).c_str());
- // call the function under test.
+ // After creating new temp files, call the function under test.
util::findFiles(dataDir, regexPattern, dataPaths);
- for (auto path : dataPaths)
- {
- trace::inf("path in dataPath vector: %s", path.c_str());
- }
-
- EXPECT_EQ(2, dataPaths.size());
-
vector<fs::path>::iterator it;
+ // Verify that the newly created temp files were in vector dataPaths
it = find(dataPaths.begin(), dataPaths.end(), path1);
EXPECT_TRUE(it != dataPaths.end());
it = find(dataPaths.begin(), dataPaths.end(), path2);
EXPECT_TRUE(it != dataPaths.end());
+
+ // Remove the newly created temp files.
+ tempFile1.remove();
+ tempFile2.remove();
}
TEST(UtilDataFile, TestValidateJson)