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); 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 +}