cmCTest.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <chrono>
  6. #include <ctime>
  7. #include <map>
  8. #include <memory>
  9. #include <sstream>
  10. #include <string>
  11. #include <utility>
  12. #include <vector>
  13. #include <cm/optional>
  14. #include <cm/string_view>
  15. #include "cmDuration.h"
  16. #include "cmProcessOutput.h"
  17. class cmCTestBuildHandler;
  18. class cmCTestCoverageHandler;
  19. class cmCTestScriptHandler;
  20. class cmCTestTestHandler;
  21. class cmCTestUpdateHandler;
  22. class cmCTestConfigureHandler;
  23. class cmCTestMemCheckHandler;
  24. class cmCTestSubmitHandler;
  25. class cmCTestUploadHandler;
  26. class cmCTestStartCommand;
  27. class cmGeneratedFileStream;
  28. class cmMakefile;
  29. class cmXMLWriter;
  30. /** \class cmCTest
  31. * \brief Represents a ctest invocation.
  32. *
  33. * This class represents a ctest invocation. It is the top level class when
  34. * running ctest.
  35. *
  36. */
  37. class cmCTest
  38. {
  39. public:
  40. using Encoding = cmProcessOutput::Encoding;
  41. /** Enumerate parts of the testing and submission process. */
  42. enum Part
  43. {
  44. PartStart,
  45. PartUpdate,
  46. PartConfigure,
  47. PartBuild,
  48. PartTest,
  49. PartCoverage,
  50. PartMemCheck,
  51. PartSubmit,
  52. PartNotes,
  53. PartExtraFiles,
  54. PartUpload,
  55. PartDone,
  56. PartCount // Update names in constructor when adding a part
  57. };
  58. /** Get a testing part id from its string name. Returns PartCount
  59. if the string does not name a valid part. */
  60. Part GetPartFromName(const std::string& name);
  61. /** Process Command line arguments */
  62. int Run(std::vector<std::string> const& args);
  63. /**
  64. * Initialize and finalize testing
  65. */
  66. bool InitializeFromCommand(cmCTestStartCommand* command);
  67. /**
  68. * Process the dashboard client steps.
  69. */
  70. int ProcessSteps();
  71. /**
  72. * A utility function that returns the nightly time
  73. */
  74. struct tm* GetNightlyTime(std::string const& str, bool tomorrowtag);
  75. /**
  76. * Is the tomorrow tag set?
  77. */
  78. bool GetTomorrowTag() const;
  79. /**
  80. * Try to run tests of the project
  81. */
  82. int TestDirectory(bool memcheck);
  83. /** what is the configuration type, e.g. Debug, Release etc. */
  84. std::string const& GetConfigType();
  85. cmDuration GetTimeOut() const;
  86. void SetTimeOut(cmDuration t);
  87. cmDuration GetGlobalTimeout() const;
  88. /** how many test to run at the same time */
  89. cm::optional<size_t> GetParallelLevel() const;
  90. void SetParallelLevel(cm::optional<size_t> level);
  91. unsigned long GetTestLoad() const;
  92. void SetTestLoad(unsigned long);
  93. /**
  94. * Check if CTest file exists
  95. */
  96. bool CTestFileExists(const std::string& filename);
  97. bool AddIfExists(Part part, const std::string& file);
  98. /**
  99. * Set the cmake test
  100. */
  101. bool SetTest(const std::string&, bool report = true);
  102. /**
  103. * Set the cmake test mode (experimental, nightly, continuous).
  104. */
  105. void SetTestModel(int mode);
  106. int GetTestModel() const;
  107. std::string GetTestModelString();
  108. static int GetTestModelFromString(const std::string& str);
  109. static std::string CleanString(const std::string& str,
  110. std::string::size_type spos = 0);
  111. std::string GetCTestConfiguration(const std::string& name);
  112. void SetCTestConfiguration(const char* name, const std::string& value,
  113. bool suppress = false);
  114. void EmptyCTestConfiguration();
  115. std::string GetSubmitURL();
  116. /**
  117. * constructor and destructor
  118. */
  119. cmCTest();
  120. ~cmCTest();
  121. cmCTest(const cmCTest&) = delete;
  122. cmCTest& operator=(const cmCTest&) = delete;
  123. /** Set the notes files to be created. */
  124. void SetNotesFiles(const std::string& notes);
  125. void PopulateCustomVector(cmMakefile* mf, const std::string& definition,
  126. std::vector<std::string>& vec);
  127. void PopulateCustomInteger(cmMakefile* mf, const std::string& def, int& val);
  128. /** Get the current time as string */
  129. std::string CurrentTime();
  130. /** tar/gzip and then base 64 encode a file */
  131. std::string Base64GzipEncodeFile(std::string const& file);
  132. /** base64 encode a file */
  133. std::string Base64EncodeFile(std::string const& file);
  134. /**
  135. * Return the time remaining that the script is allowed to run in
  136. * seconds if the user has set the variable CTEST_TIME_LIMIT. If that has
  137. * not been set it returns a very large duration.
  138. */
  139. cmDuration GetRemainingTimeAllowed();
  140. static cmDuration MaxDuration();
  141. /**
  142. * Open file in the output directory and set the stream
  143. */
  144. bool OpenOutputFile(const std::string& path, const std::string& name,
  145. cmGeneratedFileStream& stream, bool compress = false);
  146. /** Should we only show what we would do? */
  147. bool GetShowOnly();
  148. bool GetOutputAsJson();
  149. int GetOutputAsJsonVersion();
  150. bool ShouldUseHTTP10() const;
  151. bool ShouldPrintLabels() const;
  152. bool ShouldCompressTestOutput();
  153. bool CompressString(std::string& str);
  154. bool GetStopOnFailure() const;
  155. void SetStopOnFailure(bool stop);
  156. std::chrono::system_clock::time_point GetStopTime() const;
  157. void SetStopTime(std::string const& time);
  158. /** Used for parallel ctest job scheduling */
  159. std::string GetScheduleType() const;
  160. void SetScheduleType(std::string const& type);
  161. /** The max output width */
  162. int GetMaxTestNameWidth() const;
  163. void SetMaxTestNameWidth(int w);
  164. /**
  165. * Run a single executable command and put the stdout and stderr
  166. * in output.
  167. *
  168. * If verbose is false, no user-viewable output from the program
  169. * being run will be generated.
  170. *
  171. * If timeout is specified, the command will be terminated after
  172. * timeout expires. Timeout is specified in seconds.
  173. *
  174. * Argument retVal should be a pointer to the location where the
  175. * exit code will be stored. If the retVal is not specified and
  176. * the program exits with a code other than 0, then the this
  177. * function will return false.
  178. */
  179. bool RunCommand(std::vector<std::string> const& args, std::string* stdOut,
  180. std::string* stdErr, int* retVal = nullptr,
  181. const char* dir = nullptr,
  182. cmDuration timeout = cmDuration::zero(),
  183. Encoding encoding = cmProcessOutput::Auto);
  184. /**
  185. * Clean/make safe for xml the given value such that it may be used as
  186. * one of the key fields by CDash when computing the buildid.
  187. */
  188. static std::string SafeBuildIdField(const std::string& value);
  189. /** Start CTest XML output file */
  190. void StartXML(cmXMLWriter& xml, bool append);
  191. /** End CTest XML output file */
  192. void EndXML(cmXMLWriter& xml);
  193. /**
  194. * Run command specialized for make and configure. Returns process status
  195. * and retVal is return value or exception.
  196. */
  197. bool RunMakeCommand(const std::string& command, std::string& output,
  198. int* retVal, const char* dir, cmDuration timeout,
  199. std::ostream& ofs,
  200. Encoding encoding = cmProcessOutput::Auto);
  201. /** Return the current tag */
  202. std::string GetCurrentTag();
  203. /** Get the path to the build tree */
  204. std::string GetBinaryDir();
  205. /**
  206. * Get the short path to the file.
  207. *
  208. * This means if the file is in binary or
  209. * source directory, it will become /.../relative/path/to/file
  210. */
  211. std::string GetShortPathToFile(const std::string& fname);
  212. enum
  213. {
  214. UNKNOWN = -1,
  215. EXPERIMENTAL = 0,
  216. NIGHTLY = 1,
  217. CONTINUOUS = 2,
  218. };
  219. /** provide some more detailed info on the return code for ctest */
  220. enum
  221. {
  222. UPDATE_ERRORS = 0x01,
  223. CONFIGURE_ERRORS = 0x02,
  224. BUILD_ERRORS = 0x04,
  225. TEST_ERRORS = 0x08,
  226. MEMORY_ERRORS = 0x10,
  227. COVERAGE_ERRORS = 0x20,
  228. SUBMIT_ERRORS = 0x40
  229. };
  230. /** Are we producing XML */
  231. bool GetProduceXML();
  232. void SetProduceXML(bool v);
  233. /**
  234. * Get the handler object
  235. */
  236. cmCTestBuildHandler* GetBuildHandler();
  237. cmCTestCoverageHandler* GetCoverageHandler();
  238. cmCTestScriptHandler* GetScriptHandler();
  239. cmCTestTestHandler* GetTestHandler();
  240. cmCTestUpdateHandler* GetUpdateHandler();
  241. cmCTestConfigureHandler* GetConfigureHandler();
  242. cmCTestMemCheckHandler* GetMemCheckHandler();
  243. cmCTestSubmitHandler* GetSubmitHandler();
  244. cmCTestUploadHandler* GetUploadHandler();
  245. /**
  246. * Set the CTest variable from CMake variable
  247. */
  248. bool SetCTestConfigurationFromCMakeVariable(cmMakefile* mf,
  249. const char* dconfig,
  250. const std::string& cmake_var,
  251. bool suppress = false);
  252. /**
  253. * Set CMake variables from CTest Options
  254. */
  255. void SetCMakeVariables(cmMakefile& mf);
  256. /** Decode a URL to the original string. */
  257. static std::string DecodeURL(const std::string&);
  258. /**
  259. * Should ctect configuration be updated. When using new style ctest
  260. * script, this should be true.
  261. */
  262. void SetSuppressUpdatingCTestConfiguration(bool val);
  263. /**
  264. * Add overwrite to ctest configuration.
  265. *
  266. * The format is key=value
  267. */
  268. void AddCTestConfigurationOverwrite(const std::string& encstr);
  269. /** Create XML file that contains all the notes specified */
  270. int GenerateNotesFile(std::vector<std::string> const& files);
  271. /** Create XML file to indicate that build is complete */
  272. int GenerateDoneFile();
  273. /** Submit extra files to the server */
  274. bool SubmitExtraFiles(const std::string& files);
  275. bool SubmitExtraFiles(std::vector<std::string> const& files);
  276. /** Set the output log file name */
  277. void SetOutputLogFileName(const std::string& name);
  278. /** Set the output JUnit file name */
  279. void SetOutputJUnitFileName(const std::string& name);
  280. /** Set the visual studio or Xcode config type */
  281. void SetConfigType(const std::string& ct);
  282. /** Various log types */
  283. enum LogType
  284. {
  285. DEBUG,
  286. OUTPUT,
  287. HANDLER_OUTPUT,
  288. HANDLER_PROGRESS_OUTPUT,
  289. HANDLER_TEST_PROGRESS_OUTPUT,
  290. HANDLER_VERBOSE_OUTPUT,
  291. WARNING,
  292. ERROR_MESSAGE,
  293. };
  294. /** Add log to the output */
  295. void Log(LogType logType, std::string msg, bool suppress = false);
  296. /** Color values */
  297. enum class Color
  298. {
  299. CLEAR_COLOR = 0,
  300. RED = 31,
  301. GREEN = 32,
  302. YELLOW = 33,
  303. BLUE = 34
  304. };
  305. /** Get color code characters for a specific color */
  306. std::string GetColorCode(Color color) const;
  307. /** The Build ID is assigned by CDash */
  308. void SetBuildID(const std::string& id);
  309. std::string GetBuildID() const;
  310. /** Add file to be submitted */
  311. void AddSubmitFile(Part part, const std::string& name);
  312. std::vector<std::string> const& GetSubmitFiles(Part part) const;
  313. void ClearSubmitFiles(Part part);
  314. /**
  315. * Read the custom configuration files and apply them to the current ctest
  316. */
  317. int ReadCustomConfigurationFileTree(const std::string& dir, cmMakefile* mf);
  318. std::vector<std::string>& GetInitialCommandLineArguments();
  319. /** Set the group to submit to */
  320. void SetSpecificGroup(const char* group);
  321. const char* GetSpecificGroup();
  322. void SetFailover(bool failover);
  323. bool GetFailover() const;
  324. bool GetTestProgressOutput() const;
  325. bool GetVerbose() const;
  326. bool GetExtraVerbose() const;
  327. void AddSiteProperties(cmXMLWriter& xml);
  328. bool GetLabelSummary() const;
  329. bool GetSubprojectSummary() const;
  330. std::string GetCostDataFile();
  331. bool GetOutputTestOutputOnTestFailure() const;
  332. const std::map<std::string, std::string>& GetDefinitions() const;
  333. /** Return the number of times a test should be run */
  334. int GetRepeatCount() const;
  335. enum class Repeat
  336. {
  337. Never,
  338. UntilFail,
  339. UntilPass,
  340. AfterTimeout,
  341. };
  342. Repeat GetRepeatMode() const;
  343. enum class NoTests
  344. {
  345. Legacy,
  346. Error,
  347. Ignore
  348. };
  349. NoTests GetNoTestsMode() const;
  350. void GenerateSubprojectsOutput(cmXMLWriter& xml);
  351. std::vector<std::string> GetLabelsForSubprojects();
  352. private:
  353. void SetPersistentOptionIfNotEmpty(const std::string& value,
  354. const std::string& optionName);
  355. void AddPersistentMultiOptionIfNotEmpty(const std::string& value,
  356. const std::string& optionName);
  357. int GenerateNotesFile(const std::string& files);
  358. void BlockTestErrorDiagnostics();
  359. /**
  360. * Initialize a dashboard run in the given build tree. The "command"
  361. * argument is non-NULL when running from a command-driven (ctest_start)
  362. * dashboard script, and NULL when running from the CTest command
  363. * line. Note that a declarative dashboard script does not actually
  364. * call this method because it sets CTEST_COMMAND to drive a build
  365. * through the ctest command line.
  366. */
  367. int Initialize(const std::string& binary_dir, cmCTestStartCommand* command);
  368. /** parse the option after -D and convert it into the appropriate steps */
  369. bool AddTestsForDashboardType(std::string const& targ);
  370. /** read as "emit an error message for an unknown -D value" */
  371. void ErrorMessageUnknownDashDValue(std::string const& val);
  372. /** add a variable definition from a command line -D value */
  373. bool AddVariableDefinition(const std::string& arg);
  374. /** set command line arguments read from a test preset */
  375. bool SetArgsFromPreset(const std::string& presetName, bool listPresets);
  376. #if !defined(_WIN32)
  377. /** returns true iff the console supports progress output */
  378. static bool ConsoleIsNotDumb();
  379. #endif
  380. /** returns true iff the console supports progress output */
  381. static bool ProgressOutputSupportedByConsole();
  382. /** returns true iff the console supports colored output */
  383. static bool ColoredOutputSupportedByConsole();
  384. /** Reread the configuration file */
  385. bool UpdateCTestConfiguration();
  386. /** Create note from files. */
  387. int GenerateCTestNotesOutput(cmXMLWriter& xml,
  388. std::vector<std::string> const& files);
  389. /** Check if the argument is the one specified */
  390. static bool CheckArgument(const std::string& arg, cm::string_view varg1,
  391. const char* varg2 = nullptr);
  392. int RunCMakeAndTest();
  393. int RunScripts(std::vector<std::pair<std::string, bool>> const& scripts);
  394. int ExecuteTests();
  395. struct Private;
  396. std::unique_ptr<Private> Impl;
  397. };
  398. #define cmCTestLog(ctSelf, logType, msg) \
  399. do { \
  400. std::ostringstream cmCTestLog_msg; \
  401. cmCTestLog_msg << msg; \
  402. (ctSelf)->Log(cmCTest::logType, cmCTestLog_msg.str()); \
  403. } while (false)
  404. #define cmCTestOptionalLog(ctSelf, logType, msg, suppress) \
  405. do { \
  406. std::ostringstream cmCTestLog_msg; \
  407. cmCTestLog_msg << msg; \
  408. (ctSelf)->Log(cmCTest::logType, cmCTestLog_msg.str(), suppress); \
  409. } while (false)