ctest.cxx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include <cstring>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include "cmsys/Encoding.hxx"
  8. #include "cmCTest.h"
  9. #include "cmDocumentation.h"
  10. #include "cmDocumentationEntry.h"
  11. #include "cmInstrumentation.h"
  12. #include "cmInstrumentationQuery.h"
  13. #include "cmStdIoConsole.h"
  14. #include "cmSystemTools.h"
  15. #include "CTest/cmCTestLaunch.h"
  16. namespace {
  17. cmDocumentationEntry const cmDocumentationName = {
  18. {},
  19. " ctest - Testing driver provided by CMake."
  20. };
  21. cmDocumentationEntry const cmDocumentationUsage = { {}, " ctest [options]" };
  22. cmDocumentationEntry const cmDocumentationOptions[] = {
  23. { "--preset <preset>, --preset=<preset>",
  24. "Read arguments from a test preset." },
  25. { "--list-presets", "List available test presets." },
  26. { "-C <cfg>, --build-config <cfg>", "Choose configuration to test." },
  27. { "--progress", "Enable short progress output from tests." },
  28. { "-V,--verbose", "Enable verbose output from tests." },
  29. { "-VV,--extra-verbose", "Enable more verbose output from tests." },
  30. { "--debug", "Displaying more verbose internals of CTest." },
  31. { "--output-on-failure",
  32. "Output anything outputted by the test program "
  33. "if the test should fail." },
  34. { "--stop-on-failure", "Stop running the tests after one has failed." },
  35. { "--test-output-size-passed <size>",
  36. "Limit the output for passed tests "
  37. "to <size> bytes" },
  38. { "--test-output-size-failed <size>",
  39. "Limit the output for failed tests "
  40. "to <size> bytes" },
  41. { "--test-output-truncation <mode>",
  42. "Truncate 'tail' (default), 'middle' or 'head' of test output once "
  43. "maximum output size is reached" },
  44. { "-F", "Enable failover." },
  45. { "-j [<level>], --parallel [<level>]",
  46. "Run tests in parallel, "
  47. "optionally limited to a given level of parallelism." },
  48. { "-Q,--quiet", "Make ctest quiet." },
  49. { "-O <file>, --output-log <file>", "Output to log file" },
  50. { "--output-junit <file>", "Output test results to JUnit XML file." },
  51. { "-N,--show-only[=format]",
  52. "Disable actual execution of tests. The optional 'format' defines the "
  53. "format of the test information and can be 'human' for the current text "
  54. "format or 'json-v1' for json format. Defaults to 'human'." },
  55. { "-L <regex>, --label-regex <regex>",
  56. "Run tests with labels matching regular expression. "
  57. "With multiple -L, run tests where each "
  58. "regular expression matches at least one label." },
  59. { "-R <regex>, --tests-regex <regex>",
  60. "Run tests matching regular "
  61. "expression." },
  62. { "-E <regex>, --exclude-regex <regex>",
  63. "Exclude tests matching regular "
  64. "expression." },
  65. { "-LE <regex>, --label-exclude <regex>",
  66. "Exclude tests with labels matching regular expression. "
  67. "With multiple -LE, exclude tests where each "
  68. "regular expression matches at least one label." },
  69. { "-FA <regex>, --fixture-exclude-any <regex>",
  70. "Do not automatically "
  71. "add any tests for "
  72. "fixtures matching "
  73. "regular expression." },
  74. { "-FS <regex>, --fixture-exclude-setup <regex>",
  75. "Do not automatically "
  76. "add setup tests for "
  77. "fixtures matching "
  78. "regular expression." },
  79. { "-FC <regex>, --fixture-exclude-cleanup <regex>",
  80. "Do not automatically "
  81. "add cleanup tests for "
  82. "fixtures matching "
  83. "regular expression." },
  84. { "-D <dashboard>, --dashboard <dashboard>", "Execute dashboard test" },
  85. { "-D <var>:<type>=<value>", "Define a variable for script mode" },
  86. { "-M <model>, --test-model <model>", "Sets the model for a dashboard" },
  87. { "-T <action>, --test-action <action>",
  88. "Sets the dashboard action to "
  89. "perform" },
  90. { "--group <group>",
  91. "Specify what build group on the dashboard you'd like to "
  92. "submit results to." },
  93. { "-S <script>, --script <script>",
  94. "Execute a dashboard for a "
  95. "configuration" },
  96. { "-SP <script>, --script-new-process <script>",
  97. "Execute a dashboard for a "
  98. "configuration" },
  99. { "-A <file>, --add-notes <file>", "Add a notes file with submission" },
  100. { "-I [Start,End,Stride,test#,test#|Test file], --tests-information",
  101. "Run a specific number of tests by number." },
  102. { "-U, --union", "Take the Union of -I and -R" },
  103. { "--rerun-failed", "Run only the tests that failed previously" },
  104. { "--tests-from-file <file>", "Run the tests listed in the given file" },
  105. { "--exclude-from-file <file>",
  106. "Run tests except those listed in the given file" },
  107. { "--repeat until-fail:<n>, --repeat-until-fail <n>",
  108. "Require each test to run <n> times without failing in order to pass" },
  109. { "--repeat until-pass:<n>",
  110. "Allow each test to run up to <n> times in order to pass" },
  111. { "--repeat after-timeout:<n>",
  112. "Allow each test to run up to <n> times if it times out" },
  113. { "--max-width <width>", "Set the max width for a test name to output" },
  114. { "--interactive-debug-mode [0|1]", "Set the interactive mode to 0 or 1." },
  115. { "--resource-spec-file <file>", "Set the resource spec file to use." },
  116. { "--no-label-summary", "Disable timing summary information for labels." },
  117. { "--no-subproject-summary",
  118. "Disable timing summary information for "
  119. "subprojects." },
  120. { "--test-dir <dir>", "Specify the directory in which to look for tests." },
  121. { "--build-and-test", "Configure, build and run a test." },
  122. { "--build-target", "Specify a specific target to build." },
  123. { "--build-nocmake", "Run the build without running cmake first." },
  124. { "--build-run-dir", "Specify directory to run programs from." },
  125. { "--build-two-config", "Run CMake twice" },
  126. { "--build-exe-dir", "Specify the directory for the executable." },
  127. { "--build-generator", "Specify the generator to use." },
  128. { "--build-generator-platform", "Specify the generator-specific platform." },
  129. { "--build-generator-toolset", "Specify the generator-specific toolset." },
  130. { "--build-project", "Specify the name of the project to build." },
  131. { "--build-makeprogram", "Specify the make program to use." },
  132. { "--build-noclean", "Skip the make clean step." },
  133. { "--build-config-sample",
  134. "A sample executable to use to determine the configuration" },
  135. { "--build-options", "Add extra options to the build step." },
  136. { "--test-command", "The test to run with the --build-and-test option." },
  137. { "--test-timeout", "The time limit in seconds, internal use only." },
  138. { "--test-load", "CPU load threshold for starting new parallel tests." },
  139. { "--tomorrow-tag", "Nightly or experimental starts with next day tag." },
  140. { "--overwrite", "Overwrite CTest configuration option." },
  141. { "--extra-submit <file>[;<file>]", "Submit extra files to the dashboard." },
  142. { "--http-header <header>", "Append HTTP header when submitting" },
  143. { "--schedule-random", "Use a random order for scheduling tests" },
  144. { "--schedule-random-seed", "Override seed for random order of tests" },
  145. { "--submit-index",
  146. "Submit individual dashboard tests with specific index" },
  147. { "--timeout <seconds>", "Set the default test timeout." },
  148. { "--stop-time <time>",
  149. "Set a time at which all tests should stop running." },
  150. { "--http1.0", "Submit using HTTP 1.0." },
  151. { "--no-compress-output", "Do not compress test output when submitting." },
  152. { "--print-labels", "Print all available test labels." },
  153. { "--no-tests=<[error|ignore]>",
  154. "Regard no tests found either as 'error' or 'ignore' it." }
  155. };
  156. } // anonymous namespace
  157. // this is a test driver program for cmCTest.
  158. int main(int argc, char const* const* argv)
  159. {
  160. cm::StdIo::Console console;
  161. cmsys::Encoding::CommandLineArguments encoding_args =
  162. cmsys::Encoding::CommandLineArguments::Main(argc, argv);
  163. argc = encoding_args.argc();
  164. argv = encoding_args.argv();
  165. cmSystemTools::InitializeLibUV();
  166. cmSystemTools::FindCMakeResources(argv[0]);
  167. // Dispatch 'ctest --launch' mode directly.
  168. if (argc >= 2 && strcmp(argv[1], "--launch") == 0) {
  169. return cmCTestLaunch::Main(argc, argv, cmCTestLaunch::Op::Normal);
  170. }
  171. // Dispatch 'ctest --instrument' mode directly.
  172. if (argc >= 2 && strcmp(argv[1], "--instrument") == 0) {
  173. return cmCTestLaunch::Main(argc, argv, cmCTestLaunch::Op::Instrument);
  174. }
  175. // Dispatch post-build instrumentation daemon for ninja
  176. if (argc == 3 && strcmp(argv[1], "--start-instrumentation") == 0) {
  177. return cmInstrumentation(argv[2]).SpawnBuildDaemon();
  178. }
  179. // Dispatch 'ctest --collect-instrumentation' once given PID finishes
  180. if (argc == 4 &&
  181. strcmp(argv[1], "--wait-and-collect-instrumentation") == 0) {
  182. return cmInstrumentation(argv[2]).CollectTimingAfterBuild(
  183. std::stoi(argv[3]));
  184. }
  185. // Dispatch 'ctest --collect-instrumentation' mode directly.
  186. if (argc == 3 && strcmp(argv[1], "--collect-instrumentation") == 0) {
  187. return cmInstrumentation(argv[2]).CollectTimingData(
  188. cmInstrumentationQuery::Hook::Manual);
  189. }
  190. if (cmSystemTools::GetLogicalWorkingDirectory().empty()) {
  191. std::cerr << "Current working directory cannot be established.\n";
  192. return 1;
  193. }
  194. // If there is a testing input file, check for documentation options
  195. // only if there are actually arguments. We want running without
  196. // arguments to run tests.
  197. if (argc > 1 ||
  198. !(cmSystemTools::FileExists("CTestTestfile.cmake") ||
  199. cmSystemTools::FileExists("DartTestfile.txt"))) {
  200. if (argc == 1) {
  201. std::cerr << "*********************************\n"
  202. "No test configuration file found!\n"
  203. "*********************************\n";
  204. }
  205. cmDocumentation doc;
  206. doc.addCTestStandardDocSections();
  207. if (doc.CheckOptions(argc, argv)) {
  208. doc.SetShowGenerators(false);
  209. doc.SetName("ctest");
  210. doc.SetSection("Name", cmDocumentationName);
  211. doc.SetSection("Usage", cmDocumentationUsage);
  212. doc.PrependSection("Options", cmDocumentationOptions);
  213. return !doc.PrintRequestedDocumentation(std::cout);
  214. }
  215. }
  216. // copy the args to a vector
  217. auto args = std::vector<std::string>(argv, argv + argc);
  218. // run ctest
  219. return cmCTest{}.Run(args);
  220. }