Use src dir as fallback unless arg *and* env var specified

This commit is contained in:
Martchus 2018-02-03 17:08:43 +01:00
parent 6a1280ba8a
commit 3a768508fd
2 changed files with 32 additions and 26 deletions

View File

@ -60,11 +60,11 @@ TestApplication::TestApplication(int argc, char **argv)
// determine fallback path for testfiles which is used when --test-files-path/-p not present // determine fallback path for testfiles which is used when --test-files-path/-p not present
// -> read TEST_FILE_PATH environment variable // -> read TEST_FILE_PATH environment variable
readFallbackTestfilePathFromEnv(); m_fallbackTestFilesPath = readTestfilePathFromEnv();
// -> find source directory if TEST_FILE_PATH not present // -> find source directory if TEST_FILE_PATH not present
const bool fallbackIsSourceDir = m_fallbackTestFilesPath.empty(); bool fallbackIsSourceDir = m_fallbackTestFilesPath.empty();
if (fallbackIsSourceDir) { if (fallbackIsSourceDir) {
readFallbackTestfilePathFromSrcRef(); m_fallbackTestFilesPath = readTestfilePathFromSrcRef();
} }
// handle specified arguments (if present) // handle specified arguments (if present)
@ -103,6 +103,14 @@ TestApplication::TestApplication(int argc, char **argv)
} else { } else {
cerr << (m_testFilesPath = "./") << endl; cerr << (m_testFilesPath = "./") << endl;
} }
} else {
// use fallback path if --test-files-path/-p not present
m_testFilesPath.swap(m_fallbackTestFilesPath);
}
// if it wasn't already the case, use the source directory as fallback dir
if (m_fallbackTestFilesPath.empty() && !fallbackIsSourceDir) {
m_fallbackTestFilesPath = readTestfilePathFromSrcRef();
fallbackIsSourceDir = true;
} }
if (!m_fallbackTestFilesPath.empty() && m_testFilesPath != m_fallbackTestFilesPath) { if (!m_fallbackTestFilesPath.empty() && m_testFilesPath != m_fallbackTestFilesPath) {
cerr << m_fallbackTestFilesPath << endl; cerr << m_fallbackTestFilesPath << endl;
@ -116,10 +124,8 @@ TestApplication::TestApplication(int argc, char **argv)
m_workingDir = "./"; m_workingDir = "./";
} }
} else if (const char *workingDirEnv = getenv("WORKING_DIR")) { } else if (const char *workingDirEnv = getenv("WORKING_DIR")) {
if (const auto len = strlen(workingDirEnv)) { if (*workingDirEnv) {
m_workingDir.reserve(len + 1); m_workingDir = argsToString(workingDirEnv, '/');
m_workingDir += workingDirEnv;
m_workingDir += '/';
} }
} else { } else {
if (m_testFilesPathArg.isPresent()) { if (m_testFilesPathArg.isPresent()) {
@ -156,23 +162,25 @@ TestApplication::~TestApplication()
* *
* The following directories are searched for the specified testfile: * The following directories are searched for the specified testfile:
* 1. The directory specified as CLI argument. * 1. The directory specified as CLI argument.
* 2. The fallback directory, which can set by setting the environment * 2. The fallback directory, which can be set by setting the environment
* variable `TEST_FILE_PATH`. * variable `TEST_FILE_PATH`.
* 3. The source directory, if it could be determined via "srcref"-file
* unless both, the CLI argument and environment variable are present.
*/ */
string TestApplication::testFilePath(const string &name) const string TestApplication::testFilePath(const string &name) const
{ {
string path; string path;
fstream file; // used to check whether the file is present fstream file; // used to check whether the file is present
// check the path specified by command line argument // check the path specified by command line argument or via environment variable
if (m_testFilesPathArg.isPresent()) { if (!m_testFilesPath.empty()) {
file.open(path = m_testFilesPath + name, ios_base::in); file.open(path = m_testFilesPath + name, ios_base::in);
if (file.good()) { if (file.good()) {
return path; return path;
} }
} }
// check the path specified by environment variable // check the fallback path (value from environment variable or source directory)
if (!m_fallbackTestFilesPath.empty()) { if (!m_fallbackTestFilesPath.empty()) {
file.clear(); file.clear();
file.open(path = m_fallbackTestFilesPath + name, ios_base::in); file.open(path = m_fallbackTestFilesPath + name, ios_base::in);
@ -431,26 +439,24 @@ int execHelperApp(const char *appPath, const char *const *args, std::string &out
} }
#endif // PLATFORM_UNIX #endif // PLATFORM_UNIX
void TestApplication::readFallbackTestfilePathFromEnv() string TestApplication::readTestfilePathFromEnv()
{ {
if (const char *testFilesPathEnv = getenv("TEST_FILE_PATH")) { const char *const testFilesPathEnv = getenv("TEST_FILE_PATH");
if (const auto len = strlen(testFilesPathEnv)) { if (!testFilesPathEnv || !*testFilesPathEnv) {
m_fallbackTestFilesPath.reserve(len + 1); return string();
m_fallbackTestFilesPath += testFilesPathEnv;
m_fallbackTestFilesPath += '/';
}
} }
return argsToString(testFilesPathEnv, '/');
} }
void TestApplication::readFallbackTestfilePathFromSrcRef() string TestApplication::readTestfilePathFromSrcRef()
{ {
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"
const string srcDirContent(readFile("srcdirref", 2 * 1024)); string srcDirContent(readFile("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; return string();
} }
// check whether the referenced source directory contains a "testfiles" directory // check whether the referenced source directory contains a "testfiles" directory
@ -466,15 +472,15 @@ void TestApplication::readFallbackTestfilePathFromSrcRef()
cerr << Phrases::Warning cerr << Phrases::Warning
<< "The source directory referenced by the file \"srcdirref\" does not contain a \"testfiles\" directory or does not exist." << "The source directory referenced by the file \"srcdirref\" does not contain a \"testfiles\" directory or does not exist."
<< Phrases::End << "Referenced source directory: " << srcDirContent << endl; << Phrases::End << "Referenced source directory: " << srcDirContent << endl;
return; return string();
} }
#endif // PLATFORM_UNIX #endif // PLATFORM_UNIX
m_fallbackTestFilesPath = move(srcDirContent); return srcDirContent += "/testfiles/";
m_fallbackTestFilesPath += "/testfiles/";
} catch (...) { } catch (...) {
// the "srcdirref" file likely just does not exist, so ignore the error case for now // the "srcdirref" file likely just does not exist, so ignore the error case for now
catchIoFailure(); catchIoFailure();
} }
return string();
} }
} // namespace TestUtilities } // namespace TestUtilities

View File

@ -37,8 +37,8 @@ public:
static const char *appPath(); static const char *appPath();
private: private:
void readFallbackTestfilePathFromEnv(); static std::string readTestfilePathFromEnv();
void readFallbackTestfilePathFromSrcRef(); static std::string readTestfilePathFromSrcRef();
ApplicationUtilities::ArgumentParser m_parser; ApplicationUtilities::ArgumentParser m_parser;
ApplicationUtilities::HelpArgument m_helpArg; ApplicationUtilities::HelpArgument m_helpArg;