cmCTestTestHandler.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 <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 cmMakefile;
  23. class cmXMLWriter;
  24. struct cmCTestTestOptions
  25. {
  26. bool RerunFailed = false;
  27. bool ScheduleRandom = false;
  28. bool StopOnFailure = false;
  29. bool UseUnion = false;
  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(const std::vector<std::string>& args);
  79. /*
  80. * Set tests properties
  81. */
  82. bool SetTestsProperties(const std::vector<std::string>& args);
  83. /**
  84. * Set directory properties
  85. */
  86. bool SetDirectoryProperties(const std::vector<std::string>& args);
  87. struct cmCTestTestResourceRequirement
  88. {
  89. std::string ResourceType;
  90. int SlotsNeeded;
  91. int UnitsNeeded;
  92. bool operator==(const cmCTestTestResourceRequirement& other) const;
  93. bool operator!=(const cmCTestTestResourceRequirement& 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. int TestCount = 0;
  168. cmCTestTestProperties* Properties = nullptr;
  169. };
  170. struct cmCTestTestResultLess
  171. {
  172. bool operator()(const cmCTestTestResult& lhs,
  173. const cmCTestTestResult& rhs) const
  174. {
  175. return lhs.TestCount < rhs.TestCount;
  176. }
  177. };
  178. // add configurations to a search path for an executable
  179. static void AddConfigurations(cmCTest* ctest,
  180. std::vector<std::string>& attempted,
  181. std::vector<std::string>& attemptedConfigs,
  182. std::string filepath, std::string& filename);
  183. // full signature static method to find an executable
  184. static std::string FindExecutable(cmCTest* ctest,
  185. const std::string& testCommand,
  186. std::string& resultingConfig,
  187. std::vector<std::string>& extraPaths,
  188. std::vector<std::string>& failed);
  189. static bool ParseResourceGroupsProperty(
  190. const std::string& val,
  191. std::vector<std::vector<cmCTestTestResourceRequirement>>& resourceGroups);
  192. using ListOfTests = std::vector<cmCTestTestProperties>;
  193. // Support for writing test results in JUnit XML format.
  194. void SetJUnitXMLFileName(const std::string& id);
  195. protected:
  196. using SetOfTests =
  197. std::set<cmCTestTestHandler::cmCTestTestResult, cmCTestTestResultLess>;
  198. // compute a final test list
  199. virtual int PreProcessHandler();
  200. virtual int PostProcessHandler();
  201. virtual void GenerateTestCommand(std::vector<std::string>& args, int test);
  202. int ExecuteCommands(std::vector<std::string>& vec);
  203. bool ProcessOptions();
  204. void LogTestSummary(const std::vector<std::string>& passed,
  205. const std::vector<std::string>& failed,
  206. const cmDuration& durationInSecs);
  207. void LogDisabledTests(const std::vector<cmCTestTestResult>& disabledTests);
  208. void LogFailedTests(const std::vector<std::string>& failed,
  209. const SetOfTests& resultsSet);
  210. bool GenerateXML();
  211. void WriteTestResultHeader(cmXMLWriter& xml,
  212. cmCTestTestResult const& result);
  213. void WriteTestResultFooter(cmXMLWriter& xml,
  214. cmCTestTestResult const& result);
  215. // Write attached test files into the xml
  216. void AttachFiles(cmXMLWriter& xml, cmCTestTestResult& result);
  217. void AttachFile(cmXMLWriter& xml, std::string const& file,
  218. std::string const& name);
  219. //! Clean test output to specified length and truncation mode
  220. void CleanTestOutput(std::string& output, size_t length,
  221. cmCTestTypes::TruncationMode truncate);
  222. cmCTestTestOptions TestOptions;
  223. cmDuration ElapsedTestingTime;
  224. using TestResultsVector = std::vector<cmCTestTestResult>;
  225. TestResultsVector TestResults;
  226. std::vector<std::string> CustomTestsIgnore;
  227. std::string StartTest;
  228. std::string EndTest;
  229. std::chrono::system_clock::time_point StartTestTime;
  230. std::chrono::system_clock::time_point EndTestTime;
  231. bool MemCheck;
  232. int MaxIndex;
  233. public:
  234. enum
  235. { // Program statuses
  236. NOT_RUN = 0,
  237. TIMEOUT,
  238. SEGFAULT,
  239. ILLEGAL,
  240. INTERRUPT,
  241. NUMERICAL,
  242. OTHER_FAULT,
  243. FAILED,
  244. BAD_COMMAND,
  245. COMPLETED
  246. };
  247. private:
  248. /**
  249. * Write test results in CTest's Test.xml format
  250. */
  251. virtual void GenerateCTestXML(cmXMLWriter& xml);
  252. /**
  253. * Write test results in JUnit XML format
  254. */
  255. bool WriteJUnitXML();
  256. void PrintLabelOrSubprojectSummary(bool isSubProject);
  257. /**
  258. * Run the tests for a directory and any subdirectories
  259. */
  260. bool ProcessDirectory(std::vector<std::string>& passed,
  261. std::vector<std::string>& failed);
  262. /**
  263. * Get the list of tests in directory and subdirectories.
  264. */
  265. bool GetListOfTests();
  266. // compute the lists of tests that will actually run
  267. // based on union regex and -I stuff
  268. bool ComputeTestList();
  269. // compute the lists of tests that will actually run
  270. // based on LastTestFailed.log
  271. bool ComputeTestListForRerunFailed();
  272. // add required setup/cleanup tests not already in the
  273. // list of tests to be run and update dependencies between
  274. // tests to account for fixture setup/cleanup
  275. void UpdateForFixtures(ListOfTests& tests) const;
  276. void UpdateMaxTestNameWidth();
  277. bool GetValue(const char* tag, std::string& value, std::istream& fin);
  278. bool GetValue(const char* tag, int& value, std::istream& fin);
  279. bool GetValue(const char* tag, size_t& value, std::istream& fin);
  280. bool GetValue(const char* tag, bool& value, std::istream& fin);
  281. bool GetValue(const char* tag, double& value, std::istream& fin);
  282. /**
  283. * Find the executable for a test
  284. */
  285. std::string FindTheExecutable(const std::string& exe);
  286. std::string GetTestStatus(cmCTestTestResult const&);
  287. void ExpandTestsToRunInformation(size_t numPossibleTests);
  288. void ExpandTestsToRunInformationForRerunFailed();
  289. cm::optional<std::set<std::string>> ReadTestListFile(
  290. std::string const& testListFileName) const;
  291. std::vector<std::string> CustomPreTest;
  292. std::vector<std::string> CustomPostTest;
  293. std::vector<int> TestsToRun;
  294. bool UseIncludeRegExpFlag;
  295. bool UseExcludeRegExpFlag;
  296. bool UseExcludeRegExpFirst;
  297. std::vector<cmsys::RegularExpression> IncludeLabelRegularExpressions;
  298. std::vector<cmsys::RegularExpression> ExcludeLabelRegularExpressions;
  299. cmsys::RegularExpression IncludeTestsRegularExpression;
  300. cmsys::RegularExpression ExcludeTestsRegularExpression;
  301. cm::optional<std::set<std::string>> TestsToRunByName;
  302. cm::optional<std::set<std::string>> TestsToExcludeByName;
  303. cm::optional<std::string> ParallelLevel;
  304. cm::optional<std::string> Repeat;
  305. void RecordCustomTestMeasurements(cmXMLWriter& xml, std::string content);
  306. void CheckLabelFilter(cmCTestTestProperties& it);
  307. void CheckLabelFilterExclude(cmCTestTestProperties& it);
  308. void CheckLabelFilterInclude(cmCTestTestProperties& it);
  309. std::string TestsToRunString;
  310. ListOfTests TestList;
  311. size_t TotalNumberOfTests;
  312. cmsys::RegularExpression AllTestMeasurementsRegex;
  313. cmsys::RegularExpression SingleTestMeasurementRegex;
  314. cmsys::RegularExpression CustomCompletionStatusRegex;
  315. cmsys::RegularExpression CustomLabelRegex;
  316. std::ostream* LogFile;
  317. cmCTest::Repeat RepeatMode = cmCTest::Repeat::Never;
  318. int RepeatCount = 1;
  319. friend class cmCTestTestCommand;
  320. };