ctest.cxx 9.1 KB

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