diff --git a/LCS/Common/src/FileLocator.cc b/LCS/Common/src/FileLocator.cc index 22342b26a195b0139ba5b78552dbe66a0da8f2b3..2b26e268e0ed009d21c4650542e2d0e748959fc4 100644 --- a/LCS/Common/src/FileLocator.cc +++ b/LCS/Common/src/FileLocator.cc @@ -235,12 +235,13 @@ string FileLocator::locate (const string& aFile) while (iter != chainEnd) { // When itsSubdir is filled test subdir and basedir, in that order string basedir = *iter + (*iter != "/" ? "/" : ""); - string file = basedir + (itsSubdir.empty() ? aFile : itsSubdir + "/" + aFile); - if (stat(file.c_str(), &fileStat) == 0) - return (file); + string file; + if (!itsSubdir.empty()) { + file = basedir + itsSubdir + "/" + aFile; + if (stat(file.c_str(), &fileStat) == 0) return (file); + } file = basedir + aFile; - if (stat(file.c_str(), &fileStat) == 0) - return (file); + if (stat(file.c_str(), &fileStat) == 0) return (file); ++iter; } // not found, return empty string. diff --git a/LCS/Common/test/tFileLocator.cc b/LCS/Common/test/tFileLocator.cc index 69e8e6224c45d46515b6cb4fe97dac5fc55d773f..31174039f19dbe5096444cbe319a55e239f41b35 100644 --- a/LCS/Common/test/tFileLocator.cc +++ b/LCS/Common/test/tFileLocator.cc @@ -27,20 +27,38 @@ #include <Common/LofarLocators.h> #include <Common/StringUtil.h> #include <Common/SystemUtil.h> +#include <Common/lofar_fstream.h> #include <cstdlib> // for setenv() -#define CHECK(cond) \ - do { \ - if (!(cond)) { \ - errors++; \ +#define CHECK(cond) \ + do { \ + if (!(cond)) { \ + errors++; \ LOG_ERROR("Check '" #cond "' failed."); \ - } \ + } \ } while(0) using namespace LOFAR; int errors; +// Helper function that creates some files and directories that will be used by +// some of the tests below. +// Note that the test framework will do an automatic cleanup. +void setup(const string& progname) +{ + string tmpdir(progname + "_tmp"); + string subdir(tmpdir + "/foo"); + string filename("bar.baz"); + // Ignore mkdir() errors; they will be caught later anyway. + mkdir(tmpdir.c_str(), S_IRWXU); + mkdir(subdir.c_str(), S_IRWXU); + ASSERT(ofstream((tmpdir + "/" + filename).c_str())); + ASSERT(ofstream((subdir + "/" + filename).c_str())); + ASSERT(chdir(tmpdir.c_str()) == 0); +} + + // Helper function that expands environment variables in a path. Do not try to // be smart, we only handle the case where the environment variable itself is // used, i.e. without leading or trailing stuff. @@ -66,13 +84,23 @@ string expandPath(const string& path) int main (int, char *argv[]) { using LOFAR::basename; + string progname(argv[0]); // Read in the log-environment configuration - INIT_LOGGER(argv[0]); + INIT_LOGGER(progname); // Show operator were are on the air - LOG_INFO (formatString("Program %s has started", argv[0])); - + LOG_INFO_STR ("Program " << progname << " has started"); + + // Setup test environment + try { + setup(progname); + } catch (Exception& err) { + LOG_FATAL_STR ("Test setup failed: " << err.what()); + return 1; + } + LOG_INFO_STR ("Test environment setup done"); + LOG_INFO ("Creating fileLocator with path: /usr"); FileLocator Locator1("/usr"); LOG_INFO_STR ("registered path = " << Locator1.getPath()); @@ -133,19 +161,15 @@ int main (int, char *argv[]) { LOG_INFO_STR ("fullname = " << Locator1.locate("../namewithslash")); CHECK(Locator1.locate("../namewithslash") == ""); - LOG_INFO ("Searching myself"); - LOG_INFO_STR ("fullname = " << Locator1.locate(basename(argv[0]))); - CHECK(Locator1.locate(basename(argv[0])) != ""); - #if RESOLVE_INPUT_NOT_PRIVATE LOG_INFO_STR("'$iserniet': " << Locator1.resolveInput("$iserniet")); LOG_INFO_STR("'$LOFARROOT': " << Locator1.resolveInput("$LOFARROOT")); LOG_INFO_STR("'$LOFARROOT/bin': " << - Locator1.resolveInput("$LOFARROOT/bin")); + Locator1.resolveInput("$LOFARROOT/bin")); LOG_INFO_STR("'/sbin:$LOFARROOT/bin': " << - Locator1.resolveInput("/sbin:$LOFARROOT/bin")); + Locator1.resolveInput("/sbin:$LOFARROOT/bin")); LOG_INFO_STR("'/sbin:$LOFARROOT/bin:/usr/sbin': " << - Locator1.resolveInput("/sbin:$LOFARROOT/bin:/usr/sbin")); + Locator1.resolveInput("/sbin:$LOFARROOT/bin:/usr/sbin")); #endif LOG_INFO ("FOR THE NEXT TESTS THE ENVVAR $LOFARROOT IS SET TO /usr/local"); @@ -155,8 +179,12 @@ int main (int, char *argv[]) { FileLocator Locator2; LOG_INFO_STR ("registered path = " << Locator2.getPath()); CHECK(Locator2.getPath() == expandPath(BASE_SEARCH_DIR) + ":" + - dirname(getExecutablePath()) + ":" + - dirname(dirname(getExecutablePath()))); + dirname(getExecutablePath()) + ":" + + dirname(dirname(getExecutablePath()))); + + LOG_INFO ("Searching myself"); + LOG_INFO_STR ("fullname = " << Locator2.locate(progname)); + CHECK(Locator2.locate(progname) != ""); path1 = Locator2.hasPath("$LOFARROOT"); path2 = Locator2.hasPath("/opt/lofar/"); @@ -174,8 +202,8 @@ int main (int, char *argv[]) { LOG_INFO_STR ("registered path = " << Locator2.getPath()); LOG_INFO_STR ("registered subdir = " << Locator2.getSubdir()); CHECK(Locator2.getPath() == expandPath(BASE_SEARCH_DIR) + ":" + - dirname(getExecutablePath()) + ":" + - dirname(dirname(getExecutablePath()))); + dirname(getExecutablePath()) + ":" + + dirname(dirname(getExecutablePath()))); CHECK(Locator2.getSubdir() == "foo"); path1 = Locator2.hasPath("/opt/lofar/foo"); @@ -185,28 +213,48 @@ int main (int, char *argv[]) { LOG_INFO ("Searching file 'ServiceBroker.conf'"); LOG_INFO_STR ("fullname = " << Locator2.locate("ServiceBroker.conf")); CHECK(Locator2.locate("ServiceBroker.conf") == ""); + + // Note: the files and directories used in the two tests below + // were created by the setup() function. + string filename("bar.baz"); + string subdir; + string refpath; + LOG_INFO_STR ("Setting subdir to '" << subdir << "'"); + Locator2.setSubdir(subdir); + LOG_INFO_STR ("Trying to locate file '" << filename << "'"); + refpath = "./" + (subdir.empty() ? "" : subdir + "/") + filename; + LOG_INFO_STR("fullname = " << Locator2.locate(filename)); + CHECK(Locator2.locate(filename) == refpath); + + subdir += "foo"; + LOG_INFO_STR ("Setting subdir to '" << subdir << "'"); + Locator2.setSubdir(subdir); + LOG_INFO_STR ("Trying to locate file '" << filename << "'"); + refpath = "./" + (subdir.empty() ? "" : subdir + "/") + filename; + LOG_INFO_STR("fullname = " << Locator2.locate(filename)); + CHECK(Locator2.locate(filename) == refpath); - LOG_INFO ("Creating fileLocator with two variables"); - FileLocator Locator3("$LOFARROOT:$LOFARROOT"); - LOG_INFO_STR ("Locator3.getPath() = " <<Locator3.getPath()); - CHECK(Locator3.getPath() == "/usr/local:/usr/local"); + LOG_INFO ("Creating fileLocator with two variables"); + FileLocator Locator3("$LOFARROOT:$LOFARROOT"); + LOG_INFO_STR ("Locator3.getPath() = " <<Locator3.getPath()); + CHECK(Locator3.getPath() == "/usr/local:/usr/local"); LOG_INFO ("Testing ConfigLocator"); ConfigLocator aCL; LOG_INFO_STR ("registered path = " << aCL.getPath()); LOG_INFO_STR ("registered subdir = " << aCL.getSubdir()); CHECK(aCL.getPath() == expandPath(BASE_SEARCH_DIR) + ":" + - dirname(getExecutablePath()) + ":" + - dirname(dirname(getExecutablePath()))); + dirname(getExecutablePath()) + ":" + + dirname(dirname(getExecutablePath()))); CHECK(aCL.getSubdir() == "etc"); if(errors) { LOG_FATAL_STR("**** " << errors << " error" << - (errors > 1 ? "s" : "") << " detected."); - } else { + (errors > 1 ? "s" : "") << " detected."); + } else { LOG_INFO("Normal termination of program"); - } + } return (errors ? 1 : 0); }