ctest.cxx 9.4 KB

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