test utils: Use /proc/self/exe to find srcdirref, at least where supported

So it also works if e.g. chdir() is called before instantiating
the TestApplication.
This commit is contained in:
Martchus 2020-04-11 22:12:49 +02:00
parent 4b67736adf
commit 06e29dd751
1 changed files with 13 additions and 1 deletions

View File

@ -17,6 +17,9 @@
#include <limits> #include <limits>
#ifdef PLATFORM_UNIX #ifdef PLATFORM_UNIX
#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
#include <filesystem>
#endif
#include <poll.h> #include <poll.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/wait.h> #include <sys/wait.h>
@ -544,10 +547,19 @@ string TestApplication::readTestfilePathFromEnv()
string TestApplication::readTestfilePathFromSrcRef() string TestApplication::readTestfilePathFromSrcRef()
{ {
std::string binaryPath;
#if defined(CPP_UTILITIES_USE_STANDARD_FILESYSTEM) && defined(PLATFORM_UNIX)
try {
binaryPath = std::filesystem::read_symlink("/proc/self/exe").parent_path();
binaryPath += '/';
} catch (const std::filesystem::filesystem_error &e) {
cerr << Phrases::Warning << "Unable to detect binary path for finding \"srcdirref\": " << e.what() << Phrases::EndFlush;
}
#endif
try { try {
// read "srcdirref" file which should contain the path of the source directory; this file should have been // read "srcdirref" file which should contain the path of the source directory; this file should have been
// create by the CMake module "TestTarget.cmake" // create by the CMake module "TestTarget.cmake"
auto srcDirContent(readFile("srcdirref", 2 * 1024)); auto srcDirContent(readFile(binaryPath + "srcdirref", 2 * 1024));
if (srcDirContent.empty()) { if (srcDirContent.empty()) {
cerr << Phrases::Warning << "The file \"srcdirref\" is empty." << Phrases::EndFlush; cerr << Phrases::Warning << "The file \"srcdirref\" is empty." << Phrases::EndFlush;
return string(); return string();