From 9583e2d7ad956ade40dcdbe36280391b2bc39427 Mon Sep 17 00:00:00 2001 From: Edward d'Auvergne Date: Wed, 28 Mar 2018 17:30:59 +0200 Subject: [PATCH] TestSuite: Addition of a test data store singleton. This is for currently for storing the path to FGData for use in any tests requiring it. --- test_suite/CMakeLists.txt | 2 + test_suite/dataStore.cxx | 103 +++++++++++++++++++++++++++++++++ test_suite/dataStore.hxx | 53 +++++++++++++++++ test_suite/helpers/globals.cxx | 45 +++----------- test_suite/testSuite.cxx | 26 +++++++-- tests/CMakeLists.txt | 1 + 6 files changed, 187 insertions(+), 43 deletions(-) create mode 100644 test_suite/dataStore.cxx create mode 100644 test_suite/dataStore.hxx diff --git a/test_suite/CMakeLists.txt b/test_suite/CMakeLists.txt index e8a51d07d..bc9fb875f 100644 --- a/test_suite/CMakeLists.txt +++ b/test_suite/CMakeLists.txt @@ -21,6 +21,7 @@ add_subdirectory(helpers) set(TESTSUITE_SOURCES ${TESTSUITE_SOURCES} bootstrap.cxx + dataStore.cxx fgCompilerOutputter.cxx fgTestListener.cxx fgTestRunner.cxx @@ -30,6 +31,7 @@ set(TESTSUITE_SOURCES ) set(TESTSUITE_HEADERS ${TESTSUITE_HEADERS} + dataStore.hxx fgCompilerOutputter.hxx fgTestListener.hxx fgTestRunner.hxx diff --git a/test_suite/dataStore.cxx b/test_suite/dataStore.cxx new file mode 100644 index 000000000..382aa4a2b --- /dev/null +++ b/test_suite/dataStore.cxx @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2018 James Turner + * Copyright (C) 2018 Edward d'Auvergne + * + * This file is part of the program FlightGear. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include "config.h" + +#include "dataStore.hxx" + + +// Sanity check. +bool looksLikeFGData(const SGPath& path) +{ + return (path / "defaults.xml").exists(); +} + + +// Set up the path to FGData. +int DataStore::findFGRoot(const std::string& fgRootCmdLineOpt, bool debug) +{ + SGPath fgRoot; + + // Command line supplied path. + if (!fgRootCmdLineOpt.empty()) { + fgRoot = SGPath::fromUtf8(fgRootCmdLineOpt); + if (looksLikeFGData(fgRoot)) { + if (debug) + std::cerr << "FGdata from the command line: " << fgRoot << std::endl; + _fgRootPath = fgRoot; + return 0; + + // Fail if an invalid path is supplied. + } else { + std::cerr << "The supplied path \"" << fgRootCmdLineOpt << "\" is not a valid FGData path." << std::endl; + return 1; + } + } + + // The FG_DATA_DIR CMake option. + fgRoot = SGPath::fromUtf8(PKGLIBDIR); + if (looksLikeFGData(fgRoot)) { + if (debug) + std::cerr << "FGdata found via $PKGLIBDIR: " << fgRoot << std::endl; + _fgRootPath = fgRoot; + return 0; + } + + // The FG_ROOT environmental variable. + if (std::getenv("FG_ROOT")) { + fgRoot = SGPath::fromEnv("FG_ROOT"); + if (looksLikeFGData(fgRoot)) { + if (debug) + std::cerr << "FGdata found via $FG_ROOT: " << fgRoot << std::endl; + _fgRootPath = fgRoot; + return 0; + } + } + + // Try in ../fgdata. + fgRoot = SGPath::fromUtf8(FGSRCDIR) / ".." / "fgdata"; + if (looksLikeFGData(fgRoot)) { + if (debug) + std::cerr << "FGdata found at \"$FGSRCDIR/../fgdata\": " << fgRoot << std::endl; + _fgRootPath = fgRoot; + return 0; + } + + // Try in ../data. + fgRoot = SGPath::fromUtf8(FGSRCDIR) / ".." / "data"; + if (looksLikeFGData(fgRoot)) { + if (debug) + std::cerr << "FGdata found at \"$FGSRCDIR/../data\": " << std::endl; + _fgRootPath = fgRoot; + return 0; + } + + // No FGData. + std::cerr << "FGData not found." << std::endl; + return 1; +} + +// Get the path to FGData. +SGPath DataStore::getFGRoot() +{ + return _fgRootPath; +} diff --git a/test_suite/dataStore.hxx b/test_suite/dataStore.hxx new file mode 100644 index 000000000..a36dbe0e2 --- /dev/null +++ b/test_suite/dataStore.hxx @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2018 Edward d'Auvergne + * + * This file is part of the program FlightGear. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef _FG_TEST_SUITE_DATA_STORE_HXX +#define _FG_TEST_SUITE_DATA_STORE_HXX + +#include + + +// The data store singleton. +class DataStore +{ +public: + // Return the singleton, instantiating it if required. + static DataStore& get() + { + static DataStore instance; + return instance; + } + + // Function deletion to allow the class to be a singleton. + DataStore(DataStore const&) = delete; + void operator=(DataStore const&) = delete; + + // FGData path functions. + int findFGRoot(const std::string& fgRootCmdLineOpt, bool debug = false); + SGPath getFGRoot(); + +private: + DataStore() = default; + + // The path to FGData. + SGPath _fgRootPath; +}; + + +#endif // _FG_TEST_SUITE_DATA_STORE_HXX diff --git a/test_suite/helpers/globals.cxx b/test_suite/helpers/globals.cxx index 094e01e0d..54dc27e11 100644 --- a/test_suite/helpers/globals.cxx +++ b/test_suite/helpers/globals.cxx @@ -1,6 +1,8 @@ #include "config.h" +#include "test_suite/dataStore.hxx" + #include "globals.hxx" #if defined(HAVE_QT) && !defined(FG_TESTLIB) @@ -24,12 +26,6 @@ static SGPath tests_fgdata; namespace fgtest { - bool looksLikeFGData(const SGPath& path) - { - return (path / "defaults.xml").exists(); - } - - SGPath fgdataPath() { return tests_fgdata; @@ -39,36 +35,9 @@ namespace fgtest { globals = new FGGlobals; - bool foundRoot = false; - if (std::getenv("FG_ROOT")) { - SGPath fg_root = SGPath::fromEnv("FG_ROOT"); - if (looksLikeFGData(fg_root)) { - globals->set_fg_root(fg_root); - foundRoot = true; - } - } - - if (!foundRoot) { - SGPath pkgLibDir = SGPath::fromUtf8(PKGLIBDIR); - if (looksLikeFGData(pkgLibDir)) { - globals->set_fg_root(pkgLibDir); - foundRoot = true; - } - } - - if (!foundRoot) { - SGPath dataDir = SGPath::fromUtf8(FGSRCDIR) / ".." / "fgdata"; - if (looksLikeFGData(dataDir)) { - globals->set_fg_root(dataDir); - foundRoot = true; - } - } - - if (!foundRoot) { - std::cerr << "FGData not found" << std::endl; - } - - tests_fgdata = globals->get_fg_root(); + DataStore &data = DataStore::get(); + globals->set_fg_root(data.getFGRoot()); + tests_fgdata = data.getFGRoot(); // current dir SGPath homePath = SGPath::fromUtf8(FGBUILDDIR) / "test_home"; @@ -85,8 +54,8 @@ namespace fgtest std::cerr << "Navcache rebuild for testing" << std::flush; while (cache->rebuild() != flightgear::NavDataCache::REBUILD_DONE) { - SGTimeStamp::sleepForMSec(1000); - std::cerr << "." << std::flush; + SGTimeStamp::sleepForMSec(1000); + std::cerr << "." << std::flush; } } diff --git a/test_suite/testSuite.cxx b/test_suite/testSuite.cxx index 03fd1aea5..61a489e2b 100644 --- a/test_suite/testSuite.cxx +++ b/test_suite/testSuite.cxx @@ -20,6 +20,7 @@ #include #include +#include "dataStore.hxx" #include "fgTestRunner.hxx" #include "formatting.hxx" #include "logging.hxx" @@ -80,11 +81,12 @@ void summary(CppUnit::OStream &stream, int system_result, int unit_result, int g int main(int argc, char **argv) { // Declarations. - int status_gui=-1, status_simgear=-1, status_system=-1, status_unit=-1; - bool run_system=false, run_unit=false, run_gui=false, run_simgear=false; - bool verbose=false, ctest_output=false, debug=false, help=false; - char *subset_system=NULL, *subset_unit=NULL, *subset_gui=NULL, *subset_simgear=NULL; - char firstchar; + int status_gui=-1, status_simgear=-1, status_system=-1, status_unit=-1; + bool run_system=false, run_unit=false, run_gui=false, run_simgear=false; + bool verbose=false, ctest_output=false, debug=false, help=false; + char *subset_system=NULL, *subset_unit=NULL, *subset_gui=NULL, *subset_simgear=NULL; + char firstchar; + std::string fgRoot; // Argument parsing. for (int i = 1; i < argc; i++) { @@ -131,6 +133,11 @@ int main(int argc, char **argv) // Help. } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { help = true; + + // FGData path. + } else if (strcmp(argv[i], "--fg-root") == 0) { + if (firstchar != '-') + fgRoot = argv[i+1]; } } @@ -159,6 +166,9 @@ int main(int argc, char **argv) std::cout << " tests." << std::endl; std::cout << " -c, --ctest simplified output suitable for running via CTest." << std::endl; std::cout << " -d, --debug disable IO capture for debugging (super verbose output)." << std::endl; + std::cout << std::endl; + std::cout << " FG options:" << std::endl; + std::cout << " --fg-root the path to FGData" << std::endl; std::cout.flush(); return 0; } @@ -171,6 +181,12 @@ int main(int argc, char **argv) run_simgear = true; } + // Set up the data store singleton and FGData path. + DataStore& data = DataStore::get(); + if (data.findFGRoot(fgRoot, debug) != 0) { + return 1; + } + // Set up logging. sglog().setDeveloperMode(true); if (debug) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9ce27cea8..8a8b4ced5 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -89,6 +89,7 @@ get_property(fgtestlib_sources DIRECTORY PROPERTY fgtestlib_sources) add_library(fgtestlib SHARED ${fgtestlib_sources} "${CMAKE_SOURCE_DIR}/test_suite/helpers/globals.cxx" + "${CMAKE_SOURCE_DIR}/test_suite/dataStore.cxx" testStubs.cxx fake_sgSky.cxx fake_sgPrecipitation.cxx