cmCTestTestHandler.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <chrono>
  6. #include <cstddef>
  7. #include <cstdint>
  8. #include <iosfwd>
  9. #include <map>
  10. #include <set>
  11. #include <string>
  12. #include <utility>
  13. #include <vector>
  14. #include <cm/optional>
  15. #include <cm/string_view>
  16. #include "cmsys/RegularExpression.hxx"
  17. #include "cmCTest.h"
  18. #include "cmCTestGenericHandler.h"
  19. #include "cmCTestTypes.h" // IWYU pragma: keep
  20. #include "cmDuration.h"
  21. #include "cmListFileCache.h"
  22. class cmXMLWriter;
  23. struct cmCTestTestOptions
  24. {
  25. bool RerunFailed = false;
  26. bool ScheduleRandom = false;
  27. bool StopOnFailure = false;
  28. bool UseUnion = false;
  29. cm::optional<unsigned int> ScheduleRandomSeed;
  30. int OutputSizePassed = 1 * 1024;
  31. int OutputSizeFailed = 300 * 1024;
  32. cmCTestTypes::TruncationMode OutputTruncation =
  33. cmCTestTypes::TruncationMode::Tail;
  34. std::string TestsToRunInformation;
  35. std::string IncludeRegularExpression;
  36. std::string ExcludeRegularExpression;
  37. std::vector<std::string> LabelRegularExpression;
  38. std::vector<std::string> ExcludeLabelRegularExpression;
  39. std::string ExcludeFixtureRegularExpression;
  40. std::string ExcludeFixtureSetupRegularExpression;
  41. std::string ExcludeFixtureCleanupRegularExpression;
  42. std::string TestListFile;
  43. std::string ExcludeTestListFile;
  44. std::string ResourceSpecFile;
  45. std::string JUnitXMLFileName;
  46. };
  47. /** \class cmCTestTestHandler
  48. * \brief A class that handles ctest -S invocations
  49. *
  50. */
  51. class cmCTestTestHandler : public cmCTestGenericHandler
  52. {
  53. friend class cmCTest;
  54. friend class cmCTestRunTest;
  55. friend class cmCTestMultiProcessHandler;
  56. public:
  57. using Superclass = cmCTestGenericHandler;
  58. /**
  59. * The main entry point for this class
  60. */
  61. int ProcessHandler() override;
  62. /**
  63. * This method is called when reading CTest custom file
  64. */
  65. void PopulateCustomVectors(cmMakefile* mf) override;
  66. //! Control the use of the regular expressions, call these methods to turn
  67. /// them on
  68. void UseIncludeRegExp();
  69. void UseExcludeRegExp();
  70. void SetMaxIndex(int n) { this->MaxIndex = n; }
  71. int GetMaxIndex() { return this->MaxIndex; }
  72. //! pass the -I argument down
  73. void SetTestsToRunInformation(std::string const& in);
  74. cmCTestTestHandler(cmCTest* ctest);
  75. /*
  76. * Add the test to the list of tests to be executed
  77. */
  78. bool AddTest(std::vector<std::string> const& args);
  79. /*
  80. * Set tests properties
  81. */
  82. bool SetTestsProperties(std::vector<std::string> const& args);
  83. /**
  84. * Set directory properties
  85. */
  86. bool SetDirectoryProperties(std::vector<std::string> const& args);
  87. struct cmCTestTestResourceRequirement
  88. {
  89. std::string ResourceType;
  90. int SlotsNeeded;
  91. int UnitsNeeded;
  92. bool operator==(cmCTestTestResourceRequirement const& other) const;
  93. bool operator!=(cmCTestTestResourceRequirement const& other) const;
  94. };
  95. struct Signal
  96. {
  97. int Number = 0;
  98. std::string Name;
  99. };
  100. struct cmCTestTestProperties
  101. {
  102. void AppendError(cm::string_view err);
  103. cm::optional<std::string> Error;
  104. std::string Name;
  105. std::string Directory;
  106. std::vector<std::string> Args;
  107. std::vector<std::string> RequiredFiles;
  108. std::vector<std::string> Depends;
  109. std::vector<std::string> AttachedFiles;
  110. std::vector<std::string> AttachOnFail;
  111. std::vector<std::pair<cmsys::RegularExpression, std::string>>
  112. ErrorRegularExpressions;
  113. std::vector<std::pair<cmsys::RegularExpression, std::string>>
  114. RequiredRegularExpressions;
  115. std::vector<std::pair<cmsys::RegularExpression, std::string>>
  116. SkipRegularExpressions;
  117. std::vector<std::pair<cmsys::RegularExpression, std::string>>
  118. TimeoutRegularExpressions;
  119. std::map<std::string, std::string> Measurements;
  120. std::map<std::string, std::string> CustomProperties;
  121. bool IsInBasedOnREOptions = true;
  122. bool WillFail = false;
  123. bool Disabled = false;
  124. float Cost = 0;
  125. int PreviousRuns = 0;
  126. bool RunSerial = false;
  127. cm::optional<cmDuration> Timeout;
  128. cm::optional<Signal> TimeoutSignal;
  129. cm::optional<cmDuration> TimeoutGracePeriod;
  130. cmDuration AlternateTimeout;
  131. int Index = 0;
  132. // Requested number of process slots
  133. int Processors = 1;
  134. bool WantAffinity = false;
  135. std::vector<size_t> Affinity;
  136. // return code of test which will mark test as "not run"
  137. int SkipReturnCode = -1;
  138. std::vector<std::string> Environment;
  139. std::vector<std::string> EnvironmentModification;
  140. std::vector<std::string> Labels;
  141. std::set<std::string> ProjectResources; // RESOURCE_LOCK
  142. std::set<std::string> FixturesSetup;
  143. std::set<std::string> FixturesCleanup;
  144. std::set<std::string> FixturesRequired;
  145. std::set<std::string> RequireSuccessDepends;
  146. std::vector<std::vector<cmCTestTestResourceRequirement>> ResourceGroups;
  147. std::string GeneratedResourceSpecFile;
  148. // Private test generator properties used to track backtraces
  149. cmListFileBacktrace Backtrace;
  150. };
  151. struct cmCTestTestResult
  152. {
  153. std::string Name;
  154. std::string Path;
  155. std::string Reason;
  156. std::string FullCommandLine;
  157. std::string Environment;
  158. cmDuration ExecutionTime = cmDuration::zero();
  159. std::int64_t ReturnValue = 0;
  160. int Status = NOT_RUN;
  161. std::string ExceptionStatus;
  162. bool CompressOutput;
  163. std::string CompletionStatus;
  164. std::string CustomCompletionStatus;
  165. std::string Output;
  166. std::string TestMeasurementsOutput;
  167. std::string InstrumentationFile;
  168. int TestCount = 0;
  169. cmCTestTestProperties* Properties = nullptr;
  170. };
  171. struct cmCTestTestResultLess
  172. {
  173. bool operator()(cmCTestTestResult const& lhs,
  174. cmCTestTestResult const& rhs) const
  175. {
  176. return lhs.TestCount < rhs.TestCount;
  177. }
  178. };
  179. // add configurations to a search path for an executable
  180. static void AddConfigurations(cmCTest* ctest,
  181. std::vector<std::string>& attempted,
  182. std::vector<std::string>& attemptedConfigs,
  183. std::string filepath, std::string& filename);
  184. // full signature static method to find an executable
  185. static std::string FindExecutable(cmCTest* ctest,
  186. std::string const& testCommand,
  187. std::string& resultingConfig,
  188. std::vector<std::string>& extraPaths,
  189. std::vector<std::string>& failed);
  190. static bool ParseResourceGroupsProperty(
  191. std::string const& val,
  192. std::vector<std::vector<cmCTestTestResourceRequirement>>& resourceGroups);
  193. using ListOfTests = std::vector<cmCTestTestProperties>;
  194. // Support for writing test results in JUnit XML format.
  195. void SetJUnitXMLFileName(std::string const& id);
  196. protected:
  197. using SetOfTests =
  198. std::set<cmCTestTestHandler::cmCTestTestResult, cmCTestTestResultLess>;
  199. // compute a final test list
  200. virtual int PreProcessHandler();
  201. virtual int PostProcessHandler();
  202. virtual void GenerateTestCommand(std::vector<std::string>& args, int test);
  203. int ExecuteCommands(std::vector<std::string>& vec);
  204. bool ProcessOptions();
  205. void LogTestSummary(std::vector<std::string> const& passed,
  206. std::vector<std::string> const& failed,
  207. cmDuration durationInSecs);
  208. void LogDisabledTests(std::vector<cmCTestTestResult> const& disabledTests);
  209. void LogFailedTests(std::vector<std::string> const& failed,
  210. SetOfTests const& resultsSet);
  211. bool GenerateXML();
  212. void WriteTestResultHeader(cmXMLWriter& xml,
  213. cmCTestTestResult const& result);
  214. void WriteTestResultFooter(cmXMLWriter& xml,
  215. cmCTestTestResult const& result);
  216. // Write attached test files into the xml
  217. void AttachFiles(cmXMLWriter& xml, cmCTestTestResult& result);
  218. void AttachFile(cmXMLWriter& xml, std::string const& file,
  219. std::string const& name);
  220. //! Clean test output to specified length and truncation mode
  221. void CleanTestOutput(std::string& output, size_t length,
  222. cmCTestTypes::TruncationMode truncate);
  223. cmCTestTestOptions TestOptions;
  224. cmDuration ElapsedTestingTime;
  225. using TestResultsVector = std::vector<cmCTestTestResult>;
  226. TestResultsVector TestResults;
  227. std::vector<std::string> CustomTestsIgnore;
  228. std::string StartTest;
  229. std::string EndTest;
  230. std::chrono::system_clock::time_point StartTestTime;
  231. std::chrono::system_clock::time_point EndTestTime;
  232. bool MemCheck = false;
  233. int MaxIndex;
  234. public:
  235. enum
  236. { // Program statuses
  237. NOT_RUN = 0,
  238. TIMEOUT,
  239. SEGFAULT,
  240. ILLEGAL,
  241. INTERRUPT,
  242. NUMERICAL,
  243. OTHER_FAULT,
  244. FAILED,
  245. BAD_COMMAND,
  246. COMPLETED
  247. };
  248. private:
  249. /**
  250. * Write test results in CTest's Test.xml format
  251. */
  252. virtual void GenerateCTestXML(cmXMLWriter& xml);
  253. /**
  254. * Write test results in JUnit XML format
  255. */
  256. bool WriteJUnitXML();
  257. void PrintLabelOrSubprojectSummary(bool isSubProject);
  258. /**
  259. * Run the tests for a directory and any subdirectories
  260. */
  261. bool ProcessDirectory(std::vector<std::string>& passed,
  262. std::vector<std::string>& failed);
  263. /**
  264. * Get the list of tests in directory and subdirectories.
  265. */
  266. bool GetListOfTests();
  267. // compute the lists of tests that will actually run
  268. // based on union regex and -I stuff
  269. bool ComputeTestList();
  270. // compute the lists of tests that will actually run
  271. // based on LastTestFailed.log
  272. bool ComputeTestListForRerunFailed();
  273. // add required setup/cleanup tests not already in the
  274. // list of tests to be run and update dependencies between
  275. // tests to account for fixture setup/cleanup
  276. void UpdateForFixtures(ListOfTests& tests) const;
  277. void UpdateMaxTestNameWidth();
  278. bool GetValue(char const* tag, std::string& value, std::istream& fin);
  279. bool GetValue(char const* tag, int& value, std::istream& fin);
  280. bool GetValue(char const* tag, size_t& value, std::istream& fin);
  281. bool GetValue(char const* tag, bool& value, std::istream& fin);
  282. bool GetValue(char const* tag, double& value, std::istream& fin);
  283. /**
  284. * Find the executable for a test
  285. */
  286. std::string FindTheExecutable(std::string const& exe);
  287. std::string GetTestStatus(cmCTestTestResult const&);
  288. void ExpandTestsToRunInformation(size_t numPossibleTests);
  289. void ExpandTestsToRunInformationForRerunFailed();
  290. cm::optional<std::set<std::string>> ReadTestListFile(
  291. std::string const& testListFileName) const;
  292. std::vector<std::string> CustomPreTest;
  293. std::vector<std::string> CustomPostTest;
  294. std::vector<int> TestsToRun;
  295. bool UseIncludeRegExpFlag = false;
  296. bool UseExcludeRegExpFlag = false;
  297. bool UseExcludeRegExpFirst = false;
  298. std::vector<cmsys::RegularExpression> IncludeLabelRegularExpressions;
  299. std::vector<cmsys::RegularExpression> ExcludeLabelRegularExpressions;
  300. cmsys::RegularExpression IncludeTestsRegularExpression;
  301. cmsys::RegularExpression ExcludeTestsRegularExpression;
  302. cm::optional<std::set<std::string>> TestsToRunByName;
  303. cm::optional<std::set<std::string>> TestsToExcludeByName;
  304. cm::optional<std::string> ParallelLevel;
  305. cm::optional<std::string> Repeat;
  306. void RecordCustomTestMeasurements(cmXMLWriter& xml, std::string content);
  307. void CheckLabelFilter(cmCTestTestProperties& it);
  308. void CheckLabelFilterExclude(cmCTestTestProperties& it);
  309. void CheckLabelFilterInclude(cmCTestTestProperties& it);
  310. std::string TestsToRunString;
  311. ListOfTests TestList;
  312. size_t TotalNumberOfTests;
  313. cmsys::RegularExpression AllTestMeasurementsRegex;
  314. cmsys::RegularExpression SingleTestMeasurementRegex;
  315. cmsys::RegularExpression CustomCompletionStatusRegex;
  316. cmsys::RegularExpression CustomLabelRegex;
  317. std::ostream* LogFile = nullptr;
  318. cmCTest::Repeat RepeatMode = cmCTest::Repeat::Never;
  319. int RepeatCount = 1;
  320. friend class cmCTestTestCommand;
  321. };