ctest.cxx 8.7 KB

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