cmCTest.h 15 KB

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