| 1 | /* |
| 2 | * Copyright (C) 2016-2018 Edward d'Auvergne |
| 3 | * |
| 4 | * This file is part of the program FlightGear. |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include <algorithm> |
| 21 | #include <cstring> |
| 22 | #include <iostream> |
| 23 | |
| 24 | #include <simgear/debug/logstream.hxx> |
| 25 | |
| 26 | #include "dataStore.hxx" |
| 27 | #include "fgTestRunner.hxx" |
| 28 | #include "formatting.hxx" |
| 29 | #include "logging.hxx" |
| 30 | |
| 31 | using namespace std; |
| 32 | |
| 33 | |
| 34 | // The help message. |
| 35 | void helpPrintout(std::ostream &stream) { |
| 36 | stream << "Usage: fgfs_test_suite [options]\n"; |
| 37 | stream << '\n'; |
| 38 | stream << "Options:\n"; |
| 39 | stream << " -h, --help show this help message and exit.\n"; |
| 40 | stream << '\n'; |
| 41 | stream << " Test selection options:\n"; |
| 42 | stream << " -s, --system-tests execute the system/functional tests.\n"; |
| 43 | stream << " -u, --unit-tests execute the unit tests.\n"; |
| 44 | stream << " -g, --gui-tests execute the GUI tests.\n"; |
| 45 | stream << " -m, --simgear-tests execute the simgear tests.\n"; |
| 46 | stream << " -f, --fgdata-tests execute the FGData tests.\n"; |
| 47 | stream << '\n'; |
| 48 | stream << " The -s, -u, -g, and -m options accept an optional argument to perform a\n"; |
| 49 | stream << " subset of all tests. This argument should either be the name of a test\n"; |
| 50 | stream << " suite, the full name of an individual test, or a comma separated list.\n"; |
| 51 | stream << '\n'; |
| 52 | stream << " Full test names consist of the test suite name, the separator '::' and\n"; |
| 53 | stream << " then the individual test name. The test names can revealed with the -t\n"; |
| 54 | stream << " option.\n"; |
| 55 | stream << '\n'; |
| 56 | stream << " Examples:\n"; |
| 57 | stream << " - perform and print the names of all available unit tests:\n"; |
| 58 | stream << " --> fgfs_test_suite -t -u\n"; |
| 59 | stream << " - run all tests from the NavRadioTests test suite:\n"; |
| 60 | stream << " --> fgfs_test_suite -u NavRadioTests\n"; |
| 61 | stream << " - run a specific test without discarding its output:\n"; |
| 62 | stream << " --> fgfs_test_suite --no-summary -d -u NavRadioTests::testGS\n"; |
| 63 | stream << '\n'; |
| 64 | stream << " Logging options:\n"; |
| 65 | stream << " --log-level={bulk,debug,info,warn,alert,popup,dev_warn,dev_alert}\n"; |
| 66 | stream << " specify the minimum logging level to output\n"; |
| 67 | stream << " --log-class=[none, terrain, astro, flight, input, gl, view, cockpit,\n"; |
| 68 | stream << " general, math, event, aircraft, autopilot, io, clipper,\n"; |
| 69 | stream << " network, atc, nasal, instrumentation, systems, ai, environment,\n"; |
| 70 | stream << " sound, navaid, gui, terrasync, particles, headless, osg,\n"; |
| 71 | stream << " undefined, all]\n"; |
| 72 | stream << " select the logging class(es) to output.\n"; |
| 73 | stream << " --log-split output the different non-interleaved log streams\n"; |
| 74 | stream << " sequentially.\n"; |
| 75 | stream << '\n'; |
| 76 | stream << " Verbosity options:\n"; |
| 77 | stream << " -t, --timings verbose output including names and timings for all\n"; |
| 78 | stream << " tests.\n"; |
| 79 | stream << " -c, --ctest simplified output suitable for running via CTest.\n"; |
| 80 | stream << " -d, --debug disable IO capture for debugging (super verbose output).\n"; |
| 81 | stream << " --no-summary disable the final summary printout.\n"; |
| 82 | stream << '\n'; |
| 83 | stream << " FG options:\n"; |
| 84 | stream << " --fg-root the path to FGData.\n"; |
| 85 | stream << '\n'; |
| 86 | stream << "Environmental variables:\n"; |
| 87 | stream << " FG_TEST_LOG_LEVEL equivalent to the --log-level option.\n"; |
| 88 | stream << " FG_TEST_LOG_CLASS equivalent to the --log-class option.\n"; |
| 89 | stream << " FG_TEST_LOG_SPLIT equivalent to the --log-split option.\n"; |
| 90 | stream << " FG_TEST_TIMINGS equivalent to the -t or --timings option.\n"; |
| 91 | stream << " FG_TEST_DEBUG equivalent to the -d or --debug option.\n"; |
| 92 | stream << " FG_ROOT the path to FGData. The order of precedence is\n"; |
| 93 | stream << " --fg-root, the FG_DATA_DIR CMake option, FG_ROOT,\n"; |
| 94 | stream << " '../fgdata/', and '../data/'.\n"; |
| 95 | stream.flush(); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | // Convert the log class comma separated string list into a simgear debug class value. |
| 100 | sgDebugClass processLogClass(std::string classList, bool &failure) { |
| 101 | // Declarations. |
| 102 | std::string logClassItem, val; |
| 103 | unsigned int logClass=0; |
| 104 | |
| 105 | // Convert the command line value into a string array. |
| 106 | std::replace(classList.begin(), classList.end(), ',', ' '); |
| 107 | std::vector<std::string> logClasses; |
| 108 | stringstream temp(classList); |
| 109 | while (temp >> val) |
| 110 | logClasses.push_back(val); |
| 111 | |
| 112 | // Build up the value. |
| 113 | for (auto const& logClassItem: logClasses) |
| 114 | if (logClassItem == "none") |
| 115 | logClass |= SG_NONE; |
| 116 | else if (logClassItem == "terrain") |
| 117 | logClass |= SG_TERRAIN; |
| 118 | else if (logClassItem == "astro") |
| 119 | logClass |= SG_ASTRO; |
| 120 | else if (logClassItem == "flight") |
| 121 | logClass |= SG_FLIGHT; |
| 122 | else if (logClassItem == "input") |
| 123 | logClass |= SG_INPUT; |
| 124 | else if (logClassItem == "gl") |
| 125 | logClass |= SG_GL; |
| 126 | else if (logClassItem == "view") |
| 127 | logClass |= SG_VIEW; |
| 128 | else if (logClassItem == "cockpit") |
| 129 | logClass |= SG_COCKPIT; |
| 130 | else if (logClassItem == "general") |
| 131 | logClass |= SG_GENERAL; |
| 132 | else if (logClassItem == "math") |
| 133 | logClass |= SG_MATH; |
| 134 | else if (logClassItem == "event") |
| 135 | logClass |= SG_EVENT; |
| 136 | else if (logClassItem == "aircraft") |
| 137 | logClass |= SG_AIRCRAFT; |
| 138 | else if (logClassItem == "autopilot") |
| 139 | logClass |= SG_AUTOPILOT; |
| 140 | else if (logClassItem == "io") |
| 141 | logClass |= SG_IO; |
| 142 | else if (logClassItem == "clipper") |
| 143 | logClass |= SG_CLIPPER; |
| 144 | else if (logClassItem == "network") |
| 145 | logClass |= SG_NETWORK; |
| 146 | else if (logClassItem == "atc") |
| 147 | logClass |= SG_ATC; |
| 148 | else if (logClassItem == "nasal") |
| 149 | logClass |= SG_NASAL; |
| 150 | else if (logClassItem == "instrumentation") |
| 151 | logClass |= SG_INSTR; |
| 152 | else if (logClassItem == "systems") |
| 153 | logClass |= SG_SYSTEMS; |
| 154 | else if (logClassItem == "ai") |
| 155 | logClass |= SG_AI; |
| 156 | else if (logClassItem == "environment") |
| 157 | logClass |= SG_ENVIRONMENT; |
| 158 | else if (logClassItem == "sound") |
| 159 | logClass |= SG_SOUND; |
| 160 | else if (logClassItem == "navaid") |
| 161 | logClass |= SG_NAVAID; |
| 162 | else if (logClassItem == "gui") |
| 163 | logClass |= SG_GUI; |
| 164 | else if (logClassItem == "terrasync") |
| 165 | logClass |= SG_TERRASYNC; |
| 166 | else if (logClassItem == "particles") |
| 167 | logClass |= SG_PARTICLES; |
| 168 | else if (logClassItem == "headless") |
| 169 | logClass |= SG_HEADLESS; |
| 170 | else if (logClassItem == "osg") |
| 171 | logClass |= SG_OSG; |
| 172 | else if (logClassItem == "undefined") |
| 173 | logClass |= SG_UNDEFD; |
| 174 | else if (logClassItem == "all") |
| 175 | logClass |= SG_ALL; |
| 176 | else { |
| 177 | std::cout << "The log class \"" << logClassItem << "\" must be one of:" << std::endl; |
| 178 | std::cout << " {none, terrain, astro, flight, input, gl, view, cockpit, general, math," << std::endl; |
| 179 | std::cout << " event, aircraft, autopilot, io, clipper, network, atc, nasal," << std::endl; |
| 180 | std::cout << " instrumentation, systems, ai, environment, sound, navaid, gui, terrasync," << std::endl; |
| 181 | std::cout << " particles, headless, osg, undefined, all}" << std::endl << std::endl; |
| 182 | std::cout.flush(); |
| 183 | failure = true; |
| 184 | } |
| 185 | |
| 186 | // Return a simgear debug class. |
| 187 | return sgDebugClass(logClass); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | // Convert the log priority string into a simgear debug priority value. |
| 192 | sgDebugPriority processLogPriority(std::string logLevel, bool &failure) { |
| 193 | // Declarations. |
| 194 | sgDebugPriority logPriority=SG_INFO; |
| 195 | |
| 196 | // Conversion. |
| 197 | if (logLevel == "bulk") |
| 198 | logPriority = SG_BULK; |
| 199 | else if (logLevel == "debug") |
| 200 | logPriority = SG_DEBUG; |
| 201 | else if (logLevel == "info") |
| 202 | logPriority = SG_INFO; |
| 203 | else if (logLevel == "warn") |
| 204 | logPriority = SG_WARN; |
| 205 | else if (logLevel == "alert") |
| 206 | logPriority = SG_ALERT; |
| 207 | else if (logLevel == "popup") |
| 208 | logPriority = SG_POPUP; |
| 209 | else if (logLevel == "dev_warn") |
| 210 | logPriority = SG_DEV_WARN; |
| 211 | else if (logLevel == "dev_alert") |
| 212 | logPriority = SG_DEV_ALERT; |
| 213 | else { |
| 214 | std::cout << "The log level setting of \"" << logLevel << "\" must be one of {bulk,debug,info,warn,alert,popup,dev_warn,dev_alert}.\n\n"; |
| 215 | std::cout.flush(); |
| 216 | failure = true; |
| 217 | } |
| 218 | |
| 219 | // Return the simgear debug priority. |
| 220 | return logPriority; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | // Print out a summary of the relax test suite. |
| 225 | void summary(CppUnit::OStream &stream, int system_result, int unit_result, int gui_result, int simgear_result, int fgdata_result) |
| 226 | { |
| 227 | int synopsis = 0; |
| 228 | |
| 229 | // Title. |
| 230 | string text = "Summary of the FlightGear test suite"; |
| 231 | printTitle(stream, text); |
| 232 | |
| 233 | // Subtitle. |
| 234 | text = "Synopsis"; |
| 235 | printSection(stream, text); |
| 236 | |
| 237 | // System/functional test summary. |
| 238 | if (system_result != -1) { |
| 239 | text = "System/functional tests"; |
| 240 | printSummaryLine(stream, text, system_result); |
| 241 | synopsis += system_result; |
| 242 | } |
| 243 | |
| 244 | // Unit test summary. |
| 245 | if (unit_result != -1) { |
| 246 | text = "Unit tests"; |
| 247 | printSummaryLine(stream, text, unit_result); |
| 248 | synopsis += unit_result; |
| 249 | } |
| 250 | |
| 251 | // GUI test summary. |
| 252 | if (gui_result != -1) { |
| 253 | text = "GUI tests"; |
| 254 | printSummaryLine(stream, text, gui_result); |
| 255 | synopsis += gui_result; |
| 256 | } |
| 257 | |
| 258 | // Simgear unit test summary. |
| 259 | if (simgear_result != -1) { |
| 260 | text = "Simgear unit tests"; |
| 261 | printSummaryLine(stream, text, simgear_result); |
| 262 | synopsis += simgear_result; |
| 263 | } |
| 264 | |
| 265 | // FGData test summary. |
| 266 | if (fgdata_result != -1) { |
| 267 | text = "FGData tests"; |
| 268 | printSummaryLine(stream, text, fgdata_result); |
| 269 | synopsis += fgdata_result; |
| 270 | } |
| 271 | |
| 272 | // Synopsis. |
| 273 | text ="Synopsis"; |
| 274 | printSummaryLine(stream, text, synopsis); |
| 275 | |
| 276 | // End. |
| 277 | stream << endl << endl; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | int main(int argc, char **argv) |
| 282 | { |
| 283 | // Declarations. |
| 284 | int status_gui=-1, status_simgear=-1, status_system=-1, status_unit=-1, status_fgdata=-1; |
| 285 | bool run_system=false, run_unit=false, run_gui=false, run_simgear=false, run_fgdata=false; |
| 286 | bool logSplit=false; |
| 287 | bool timings=false, ctest_output=false, debug=false, printSummary=true, help=false; |
| 288 | char *subset_system=NULL, *subset_unit=NULL, *subset_gui=NULL, *subset_simgear=NULL, *subset_fgdata=NULL; |
| 289 | bool failure=false; |
| 290 | char firstchar; |
| 291 | std::string arg, delim, fgRoot, logClassVal, logLevel; |
| 292 | size_t delimPos; |
| 293 | |
| 294 | // The default logging class and priority to show. |
| 295 | sgDebugClass logClass=SG_ALL; |
| 296 | sgDebugPriority logPriority=SG_INFO; |
| 297 | |
| 298 | // Process environmental variables before the command line options. |
| 299 | if (getenv("FG_TEST_LOG_LEVEL")) |
| 300 | logPriority = processLogPriority(getenv("FG_TEST_LOG_LEVEL"), failure); |
| 301 | if (getenv("FG_TEST_LOG_CLASS")) |
| 302 | logClass = processLogClass(getenv("FG_TEST_LOG_CLASS"), failure); |
| 303 | if (getenv("FG_TEST_LOG_SPLIT")) |
| 304 | logSplit = true; |
| 305 | if (getenv("FG_TEST_TIMINGS")) |
| 306 | timings = true; |
| 307 | if (getenv("FG_TEST_DEBUG")) |
| 308 | debug = true; |
| 309 | if (failure) |
| 310 | return 1; |
| 311 | |
| 312 | // Argument parsing. |
| 313 | for (int i = 1; i < argc; i++) { |
| 314 | firstchar = '\0'; |
| 315 | arg = argv[i]; |
| 316 | |
| 317 | if (i < argc-1) |
| 318 | firstchar = argv[i+1][0]; |
| 319 | |
| 320 | // System test. |
| 321 | if (arg == "-s" || arg == "--system-tests") { |
| 322 | run_system = true; |
| 323 | if (firstchar != '-') |
| 324 | subset_system = argv[i+1]; |
| 325 | |
| 326 | // Unit test. |
| 327 | } else if (arg == "-u" || arg == "--unit-tests") { |
| 328 | run_unit = true; |
| 329 | if (firstchar != '-') |
| 330 | subset_unit = argv[i+1]; |
| 331 | |
| 332 | // GUI test. |
| 333 | } else if (arg == "-g" || arg == "--gui-tests") { |
| 334 | run_gui = true; |
| 335 | if (firstchar != '-') |
| 336 | subset_gui = argv[i+1]; |
| 337 | |
| 338 | // Simgear test. |
| 339 | } else if (arg == "-m" || arg == "--simgear-tests") { |
| 340 | run_simgear = true; |
| 341 | if (firstchar != '-') |
| 342 | subset_simgear = argv[i+1]; |
| 343 | |
| 344 | // FGData test. |
| 345 | } else if (strcmp(argv[i], "-f") == 0 || strcmp(argv[i], "--fgdata-tests") == 0) { |
| 346 | run_fgdata = true; |
| 347 | if (firstchar != '-') |
| 348 | subset_fgdata = argv[i+1]; |
| 349 | |
| 350 | // Log class. |
| 351 | } else if (arg.find( "--log-class" ) == 0) { |
| 352 | // Process the command line. |
| 353 | logClass = SG_NONE; |
| 354 | delimPos = arg.find('='); |
| 355 | logClassVal = arg.substr(delimPos + 1); |
| 356 | logClass = processLogClass(logClassVal, failure); |
| 357 | if (failure) |
| 358 | return 1; |
| 359 | |
| 360 | // Log level. |
| 361 | } else if (arg.find( "--log-level" ) == 0) { |
| 362 | // Process the command line level. |
| 363 | delimPos = arg.find('='); |
| 364 | logLevel = arg.substr(delimPos + 1); |
| 365 | logPriority = processLogPriority(logLevel, failure); |
| 366 | if (failure) |
| 367 | return 1; |
| 368 | |
| 369 | // Log splitting. |
| 370 | } else if (arg == "--log-split") { |
| 371 | logSplit = true; |
| 372 | |
| 373 | // Timing output. |
| 374 | } else if (arg == "-t" || arg == "--timings") { |
| 375 | timings = true; |
| 376 | |
| 377 | // CTest suitable output. |
| 378 | } else if (arg == "-c" || arg == "--ctest") { |
| 379 | ctest_output = true; |
| 380 | |
| 381 | // Debug output. |
| 382 | } else if (arg == "-d" || arg == "--debug") { |
| 383 | debug = true; |
| 384 | |
| 385 | // No summary output. |
| 386 | } else if (arg == "--no-summary") { |
| 387 | printSummary = false; |
| 388 | |
| 389 | // Help. |
| 390 | } else if (arg == "-h" || arg == "--help") { |
| 391 | help = true; |
| 392 | |
| 393 | // FGData path. |
| 394 | } else if (arg == "--fg-root") { |
| 395 | if (firstchar != '-') |
| 396 | fgRoot = argv[i+1]; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | // Help. |
| 401 | if (help) { |
| 402 | helpPrintout(std::cout); |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | // Turn on all tests if no subset was specified. |
| 407 | if (!run_system && !run_unit && !run_gui && !run_simgear && !run_fgdata) { |
| 408 | run_system = true; |
| 409 | run_unit = true; |
| 410 | run_gui = true; |
| 411 | run_simgear = true; |
| 412 | run_fgdata = true; |
| 413 | } |
| 414 | |
| 415 | // Set up the data store singleton and FGData path. |
| 416 | DataStore& data = DataStore::get(); |
| 417 | if (data.findFGRoot(fgRoot, debug) != 0) { |
| 418 | return 1; |
| 419 | } |
| 420 | if (data.validateFGRoot() != 0) { |
| 421 | return 1; |
| 422 | } |
| 423 | |
| 424 | // Set up logging. |
| 425 | sglog().setDeveloperMode(true); |
| 426 | if (debug) |
| 427 | sglog().setLogLevels(sgDebugClass(logClass), logPriority); |
| 428 | else |
| 429 | setupLogging(sgDebugClass(logClass), logPriority, logSplit); |
| 430 | |
| 431 | // Execute each of the test suite categories. |
| 432 | if (run_system) |
| 433 | status_system = testRunner("System tests", "System / functional tests", subset_system, timings, ctest_output, debug); |
| 434 | if (run_unit) |
| 435 | status_unit = testRunner("Unit tests", "Unit tests", subset_unit, timings, ctest_output, debug); |
| 436 | if (run_gui && 0) // Disabled as there are no GUI tests yet. |
| 437 | status_gui = testRunner("GUI tests", "GUI tests", subset_gui, timings, ctest_output, debug); |
| 438 | if (run_simgear) |
| 439 | status_simgear = testRunner("Simgear unit tests", "Simgear unit tests", subset_simgear, timings, ctest_output, debug); |
| 440 | if (run_fgdata) |
| 441 | status_fgdata = testRunner("FGData tests", "FGData tests", subset_fgdata, timings, ctest_output, debug); |
| 442 | |
| 443 | // Summary printout. |
| 444 | if (printSummary && !ctest_output) |
| 445 | summary(cerr, status_system, status_unit, status_gui, status_simgear, status_fgdata); |
| 446 | |
| 447 | // Deactivate the logging. |
| 448 | if (!debug) |
| 449 | stopLogging(); |
| 450 | |
| 451 | // Failure. |
| 452 | if (status_system > 0) |
| 453 | return 1; |
| 454 | if (status_unit > 0) |
| 455 | return 1; |
| 456 | if (status_gui > 0) |
| 457 | return 1; |
| 458 | if (status_simgear > 0) |
| 459 | return 1; |
| 460 | if (status_fgdata > 0) |
| 461 | return 1; |
| 462 | |
| 463 | // Success. |
| 464 | return 0; |
| 465 | } |