From c7849502b451dd3f2e64ed3d0c648980c1ebfdbc Mon Sep 17 00:00:00 2001 From: Neal Kruis Date: Fri, 2 Oct 2020 09:24:37 -0600 Subject: [PATCH 1/2] Write unit test. --- tst/EnergyPlus/unit/FileSystem.unit.cc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tst/EnergyPlus/unit/FileSystem.unit.cc b/tst/EnergyPlus/unit/FileSystem.unit.cc index 1140b491526..25f64672c1a 100644 --- a/tst/EnergyPlus/unit/FileSystem.unit.cc +++ b/tst/EnergyPlus/unit/FileSystem.unit.cc @@ -53,6 +53,7 @@ // EnergyPlus Headers #include #include +#include #include @@ -95,3 +96,15 @@ TEST(FileSystem, movefile_test) EnergyPlus::FileSystem::removeFile(filename); EnergyPlus::FileSystem::removeFile(filename_temp); } + +TEST(FileSystem, getAbsolutePath) +{ + std::string pathName = "FileSystemTest.idf"; + std::string absPathName = EnergyPlus::FileSystem::getAbsolutePath(pathName); + EXPECT_TRUE(absPathName.find(pathName) != std::string::npos); // Check that the path name appears in the absolute path + + std::string currentDirWithSep = std::string(".") + EnergyPlus::DataStringGlobals::pathChar; + pathName = currentDirWithSep + std::string("FileSystemTest.idf"); // e.g., "./FileSystemTest.idf" + absPathName = EnergyPlus::FileSystem::getAbsolutePath(pathName); + EXPECT_FALSE(absPathName.find(currentDirWithSep) != std::string::npos); // Make sure "./" doesn't appear in absolute path +} From 7fc1c6fec404ac024a67b3f7da4552a41b3edd05 Mon Sep 17 00:00:00 2001 From: Neal Kruis Date: Fri, 2 Oct 2020 09:26:32 -0600 Subject: [PATCH 2/2] Fix issue where getAbsolutePath would strip off the first two characters of the file name. --- src/EnergyPlus/FileSystem.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/EnergyPlus/FileSystem.cc b/src/EnergyPlus/FileSystem.cc index 069b8f9ea7f..cbda3333ec0 100644 --- a/src/EnergyPlus/FileSystem.cc +++ b/src/EnergyPlus/FileSystem.cc @@ -126,9 +126,14 @@ namespace FileSystem { } std::string pathTail; - if (parentPath == ".") + std::string currentDir = "."; + std::string currentDirWithSep = currentDir + DataStringGlobals::pathChar; + if ((parentPath == currentDir || parentPath == currentDirWithSep) && path.find(currentDirWithSep) == std::string::npos) + // If parent path is the current directory and the original path does not already contain + // the current directory in the string, then leave the path tail as-is. pathTail = path; else + // otherwise strip off any preceding content from the path tail pathTail = path.substr(parentPath.size(), path.size() - parentPath.size()); char *absolutePathTemp = realpath(parentPath.c_str(), NULL);