ctest.cxx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "CTest/cmCTestLaunch.h"
  4. #include "CTest/cmCTestScriptHandler.h"
  5. #include "cmCTest.h"
  6. #include "cmDocumentation.h"
  7. #include "cmSystemTools.h"
  8. #include "cmsys/Encoding.hxx"
  9. #if defined(_WIN32) && defined(CMAKE_BUILD_WITH_CMAKE)
  10. #include "cmsys/ConsoleBuf.hxx"
  11. #endif
  12. #include <iostream>
  13. #include <string.h>
  14. #include <string>
  15. #include <vector>
  16. static const char* cmDocumentationName[][2] = {
  17. { nullptr, " ctest - Testing driver provided by CMake." },
  18. { nullptr, nullptr }
  19. };
  20. static const char* cmDocumentationUsage[][2] = { { nullptr,
  21. " ctest [options]" },
  22. { nullptr, nullptr } };
  23. static const char* cmDocumentationOptions[][2] = {
  24. { "-C <cfg>, --build-config <cfg>", "Choose configuration to test." },
  25. { "-V,--verbose", "Enable verbose output from tests." },
  26. { "-VV,--extra-verbose", "Enable more verbose output from tests." },
  27. { "--debug", "Displaying more verbose internals of CTest." },
  28. { "--output-on-failure", "Output anything outputted by the test program "
  29. "if the test should fail." },
  30. { "--test-output-size-passed <size>", "Limit the output for passed tests "
  31. "to <size> bytes" },
  32. { "--test-output-size-failed <size>", "Limit the output for failed tests "
  33. "to <size> bytes" },
  34. { "-F", "Enable failover." },
  35. { "-j <jobs>, --parallel <jobs>", "Run the tests in parallel using the "
  36. "given number of jobs." },
  37. { "-Q,--quiet", "Make ctest quiet." },
  38. { "-O <file>, --output-log <file>", "Output to log file" },
  39. { "-N,--show-only", "Disable actual execution of tests." },
  40. { "-L <regex>, --label-regex <regex>", "Run tests with labels matching "
  41. "regular expression." },
  42. { "-R <regex>, --tests-regex <regex>", "Run tests matching regular "
  43. "expression." },
  44. { "-E <regex>, --exclude-regex <regex>", "Exclude tests matching regular "
  45. "expression." },
  46. { "-LE <regex>, --label-exclude <regex>", "Exclude tests with labels "
  47. "matching regular expression." },
  48. { "-FA <regex>, --fixture-exclude-any <regex>", "Do not automatically "
  49. "add any tests for "
  50. "fixtures matching "
  51. "regular expression." },
  52. { "-FS <regex>, --fixture-exclude-setup <regex>", "Do not automatically "
  53. "add setup tests for "
  54. "fixtures matching "
  55. "regular expression." },
  56. { "-FC <regex>, --fixture-exclude-cleanup <regex>", "Do not automatically "
  57. "add cleanup tests for "
  58. "fixtures matching "
  59. "regular expression." },
  60. { "-D <dashboard>, --dashboard <dashboard>", "Execute dashboard test" },
  61. { "-D <var>:<type>=<value>", "Define a variable for script mode" },
  62. { "-M <model>, --test-model <model>", "Sets the model for a dashboard" },
  63. { "-T <action>, --test-action <action>", "Sets the dashboard action to "
  64. "perform" },
  65. { "--track <track>", "Specify the track to submit dashboard to" },
  66. { "-S <script>, --script <script>", "Execute a dashboard for a "
  67. "configuration" },
  68. { "-SP <script>, --script-new-process <script>", "Execute a dashboard for a "
  69. "configuration" },
  70. { "-A <file>, --add-notes <file>", "Add a notes file with submission" },
  71. { "-I [Start,End,Stride,test#,test#|Test file], --tests-information",
  72. "Run a specific number of tests by number." },
  73. { "-U, --union", "Take the Union of -I and -R" },
  74. { "--rerun-failed", "Run only the tests that failed previously" },
  75. { "--repeat-until-fail <n>", "Require each test to run <n> "
  76. "times without failing in order to pass" },
  77. { "--max-width <width>", "Set the max width for a test name to output" },
  78. { "--interactive-debug-mode [0|1]", "Set the interactive mode to 0 or 1." },
  79. { "--no-label-summary", "Disable timing summary information for labels." },
  80. { "--no-subproject-summary", "Disable timing summary information for "
  81. "subprojects." },
  82. { "--build-and-test", "Configure, build and run a test." },
  83. { "--build-target", "Specify a specific target to build." },
  84. { "--build-nocmake", "Run the build without running cmake first." },
  85. { "--build-run-dir", "Specify directory to run programs from." },
  86. { "--build-two-config", "Run CMake twice" },
  87. { "--build-exe-dir", "Specify the directory for the executable." },
  88. { "--build-generator", "Specify the generator to use." },
  89. { "--build-generator-platform", "Specify the generator-specific platform." },
  90. { "--build-generator-toolset", "Specify the generator-specific toolset." },
  91. { "--build-project", "Specify the name of the project to build." },
  92. { "--build-makeprogram", "Specify the make program to use." },
  93. { "--build-noclean", "Skip the make clean step." },
  94. { "--build-config-sample",
  95. "A sample executable to use to determine the configuration" },
  96. { "--build-options", "Add extra options to the build step." },
  97. { "--test-command", "The test to run with the --build-and-test option." },
  98. { "--test-timeout", "The time limit in seconds, internal use only." },
  99. { "--test-load", "CPU load threshold for starting new parallel tests." },
  100. { "--tomorrow-tag", "Nightly or experimental starts with next day tag." },
  101. { "--overwrite", "Overwrite CTest configuration option." },
  102. { "--extra-submit <file>[;<file>]", "Submit extra files to the dashboard." },
  103. { "--force-new-ctest-process",
  104. "Run child CTest instances as new processes" },
  105. { "--schedule-random", "Use a random order for scheduling tests" },
  106. { "--submit-index",
  107. "Submit individual dashboard tests with specific index" },
  108. { "--timeout <seconds>", "Set a global timeout on all tests." },
  109. { "--stop-time <time>",
  110. "Set a time at which all tests should stop running." },
  111. { "--http1.0", "Submit using HTTP 1.0." },
  112. { "--no-compress-output", "Do not compress test output when submitting." },
  113. { "--print-labels", "Print all available test labels." },
  114. { nullptr, nullptr }
  115. };
  116. // this is a test driver program for cmCTest.
  117. int main(int argc, char const* const* argv)
  118. {
  119. #if defined(_WIN32) && defined(CMAKE_BUILD_WITH_CMAKE)
  120. // Replace streambuf so we can output Unicode to console
  121. cmsys::ConsoleBuf::Manager consoleOut(std::cout);
  122. consoleOut.SetUTF8Pipes();
  123. cmsys::ConsoleBuf::Manager consoleErr(std::cerr, true);
  124. consoleErr.SetUTF8Pipes();
  125. #endif
  126. cmsys::Encoding::CommandLineArguments encoding_args =
  127. cmsys::Encoding::CommandLineArguments::Main(argc, argv);
  128. argc = encoding_args.argc();
  129. argv = encoding_args.argv();
  130. cmSystemTools::DoNotInheritStdPipes();
  131. cmSystemTools::EnableMSVCDebugHook();
  132. cmSystemTools::InitializeLibUV();
  133. cmSystemTools::FindCMakeResources(argv[0]);
  134. // Dispatch 'ctest --launch' mode directly.
  135. if (argc >= 2 && strcmp(argv[1], "--launch") == 0) {
  136. return cmCTestLaunch::Main(argc, argv);
  137. }
  138. cmCTest inst;
  139. if (cmSystemTools::GetCurrentWorkingDirectory().empty()) {
  140. cmCTestLog(&inst, ERROR_MESSAGE,
  141. "Current working directory cannot be established."
  142. << std::endl);
  143. return 1;
  144. }
  145. // If there is a testing input file, check for documentation options
  146. // only if there are actually arguments. We want running without
  147. // arguments to run tests.
  148. if (argc > 1 ||
  149. !(cmSystemTools::FileExists("CTestTestfile.cmake") ||
  150. cmSystemTools::FileExists("DartTestfile.txt"))) {
  151. if (argc == 1) {
  152. cmCTestLog(&inst, ERROR_MESSAGE, "*********************************"
  153. << std::endl
  154. << "No test configuration file found!" << std::endl
  155. << "*********************************" << std::endl);
  156. }
  157. cmDocumentation doc;
  158. doc.addCTestStandardDocSections();
  159. if (doc.CheckOptions(argc, argv)) {
  160. // Construct and print requested documentation.
  161. cmCTestScriptHandler* ch =
  162. static_cast<cmCTestScriptHandler*>(inst.GetHandler("script"));
  163. ch->CreateCMake();
  164. doc.SetShowGenerators(false);
  165. doc.SetName("ctest");
  166. doc.SetSection("Name", cmDocumentationName);
  167. doc.SetSection("Usage", cmDocumentationUsage);
  168. doc.PrependSection("Options", cmDocumentationOptions);
  169. return doc.PrintRequestedDocumentation(std::cout) ? 0 : 1;
  170. }
  171. }
  172. // copy the args to a vector
  173. std::vector<std::string> args;
  174. args.reserve(argc);
  175. for (int i = 0; i < argc; ++i) {
  176. args.push_back(argv[i]);
  177. }
  178. // run ctest
  179. std::string output;
  180. int res = inst.Run(args, &output);
  181. cmCTestLog(&inst, OUTPUT, output);
  182. return res;
  183. }