Skip to content

Commit

Permalink
[Fix] Replace PWD with getcwd
Browse files Browse the repository at this point in the history
PWD is not always the same as current working directory.

This is a problem when calling tests from ctest: the working directory
is changed, but the value of env::PWD is then incorrect.
Kernel files are then not found.

Using this system call the directory is correctly returned.
  • Loading branch information
SFrijters committed Apr 8, 2020
1 parent 60bb3c6 commit db7e8e4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/occa/io/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace occa {
const std::string& cachePath();
const std::string& libraryPath();

std::string currentWorkingDirectory();

void endWithSlash(std::string &dir);
std::string endWithSlash(const std::string &dir);

Expand Down
12 changes: 11 additions & 1 deletion src/io/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ namespace occa {
return path;
}

std::string currentWorkingDirectory() {
char cwdBuff[FILENAME_MAX];
#if (OCCA_OS == OCCA_WINDOWS_OS)
_getcwd(cwdBuff, sizeof(cwdBuff));
#else
getcwd(cwdBuff, sizeof(cwdBuff));
#endif
return endWithSlash(std::string(cwdBuff));
}

void endWithSlash(std::string &dir) {
const int chars = (int) dir.size();
if ((0 < chars) &&
Expand Down Expand Up @@ -167,7 +177,7 @@ namespace occa {
expFilename = fo.expand(expFilename);

if (makeAbsolute && !isAbsolutePath(expFilename)) {
expFilename = env::PWD + expFilename;
expFilename = currentWorkingDirectory() + expFilename;
}
return expFilename;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/src/io/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void testPathMethods() {
"b");

ASSERT_EQ(occa::io::dirname("b.okl"),
occa::env::PWD);
occa::io::currentWorkingDirectory());
ASSERT_EQ(occa::io::dirname("/a/b.okl"),
"/a/");
ASSERT_EQ(occa::io::dirname("/a"),
Expand Down

0 comments on commit db7e8e4

Please sign in to comment.