Skip to content

Commit

Permalink
Merge pull request #8330 from bigladder/fix-absolute-path
Browse files Browse the repository at this point in the history
Fix file name in path returned by getAbsolutePath when a directory is not provided
  • Loading branch information
mitchute authored Nov 2, 2020
2 parents 879a064 + 7fc1c6f commit fa7ed01
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/EnergyPlus/FileSystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions tst/EnergyPlus/unit/FileSystem.unit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
// EnergyPlus Headers
#include <iostream>
#include <fstream>
#include <EnergyPlus/DataStringGlobals.hh>
#include <EnergyPlus/FileSystem.hh>


Expand Down Expand Up @@ -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
}

5 comments on commit fa7ed01

@nrel-bot-2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

develop (mitchute) - x86_64-Linux-Ubuntu-18.04-gcc-7.5: OK (2290 of 2290 tests passed, 0 test warnings)

Build Badge Test Badge

@nrel-bot-3
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

develop (mitchute) - x86_64-MacOS-10.15-clang-11.0.0: OK (2270 of 2270 tests passed, 0 test warnings)

Build Badge Test Badge

@nrel-bot-2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

develop (mitchute) - x86_64-Linux-Ubuntu-18.04-gcc-7.5-UnitTestsCoverage-Debug: OK (1551 of 1551 tests passed, 0 test warnings)

Build Badge Test Badge Coverage Badge

@nrel-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

develop (mitchute) - Win64-Windows-10-VisualStudio-16: OK (2243 of 2243 tests passed, 0 test warnings)

Build Badge Test Badge

@nrel-bot-2c
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

develop (mitchute) - x86_64-Linux-Ubuntu-18.04-gcc-7.5-IntegrationCoverage-Debug: OK (721 of 722 tests passed, 0 test warnings)

Failures:\n

integration Test Summary

  • Passed: 721
  • Timeout: 1

Build Badge Test Badge Coverage Badge

Please sign in to comment.