cmCTestTestHandler.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 "cmCTestResourceSpec.h"
  20. #include "cmCTestTypes.h" // IWYU pragma: keep
  21. #include "cmDuration.h"
  22. #include "cmListFileCache.h"
  23. #include "cmValue.h"
  24. class cmMakefile;
  25. class cmXMLWriter;
  26. /** \class cmCTestTestHandler
  27. * \brief A class that handles ctest -S invocations
  28. *
  29. */
  30. class cmCTestTestHandler : public cmCTestGenericHandler
  31. {
  32. friend class cmCTest;
  33. friend class cmCTestRunTest;
  34. friend class cmCTestMultiProcessHandler;
  35. public:
  36. using Superclass = cmCTestGenericHandler;
  37. /**
  38. * The main entry point for this class
  39. */
  40. int ProcessHandler() override;
  41. /**
  42. * When both -R and -I are used should the resulting test list be the
  43. * intersection or the union of the lists. By default it is the
  44. * intersection.
  45. */
  46. void SetUseUnion(bool val) { this->UseUnion = val; }
  47. /**
  48. * Set whether or not CTest should only execute the tests that failed
  49. * on the previous run. By default this is false.
  50. */
  51. void SetRerunFailed(bool val) { this->RerunFailed = val; }
  52. /**
  53. * This method is called when reading CTest custom file
  54. */
  55. void PopulateCustomVectors(cmMakefile* mf) override;
  56. //! Control the use of the regular expresisons, call these methods to turn
  57. /// them on
  58. void UseIncludeRegExp();
  59. void UseExcludeRegExp();
  60. void SetIncludeRegExp(const std::string&);
  61. void SetExcludeRegExp(const std::string&);
  62. void SetMaxIndex(int n) { this->MaxIndex = n; }
  63. int GetMaxIndex() { return this->MaxIndex; }
  64. void SetTestOutputSizePassed(int n)
  65. {
  66. this->CustomMaximumPassedTestOutputSize = n;
  67. }
  68. void SetTestOutputSizeFailed(int n)
  69. {
  70. this->CustomMaximumFailedTestOutputSize = n;
  71. }
  72. //! Set test output truncation mode. Return false if unknown mode.
  73. bool SetTestOutputTruncation(const std::string& mode);
  74. //! pass the -I argument down
  75. void SetTestsToRunInformation(cmValue);
  76. cmCTestTestHandler();
  77. /*
  78. * Add the test to the list of tests to be executed
  79. */
  80. bool AddTest(const std::vector<std::string>& args);
  81. /*
  82. * Set tests properties
  83. */
  84. bool SetTestsProperties(const std::vector<std::string>& args);
  85. /**
  86. * Set directory properties
  87. */
  88. bool SetDirectoryProperties(const std::vector<std::string>& args);
  89. void Initialize() override;
  90. struct cmCTestTestResourceRequirement
  91. {
  92. std::string ResourceType;
  93. int SlotsNeeded;
  94. int UnitsNeeded;
  95. bool operator==(const cmCTestTestResourceRequirement& other) const;
  96. bool operator!=(const cmCTestTestResourceRequirement& other) const;
  97. };
  98. struct Signal
  99. {
  100. int Number = 0;
  101. std::string Name;
  102. };
  103. struct cmCTestTestProperties
  104. {
  105. void AppendError(cm::string_view err);
  106. cm::optional<std::string> Error;
  107. std::string Name;
  108. std::string Directory;
  109. std::vector<std::string> Args;
  110. std::vector<std::string> RequiredFiles;
  111. std::vector<std::string> Depends;
  112. std::vector<std::string> AttachedFiles;
  113. std::vector<std::string> AttachOnFail;
  114. std::vector<std::pair<cmsys::RegularExpression, std::string>>
  115. ErrorRegularExpressions;
  116. std::vector<std::pair<cmsys::RegularExpression, std::string>>
  117. RequiredRegularExpressions;
  118. std::vector<std::pair<cmsys::RegularExpression, std::string>>
  119. SkipRegularExpressions;
  120. std::vector<std::pair<cmsys::RegularExpression, std::string>>
  121. TimeoutRegularExpressions;
  122. std::map<std::string, std::string> Measurements;
  123. bool IsInBasedOnREOptions = true;
  124. bool WillFail = false;
  125. bool Disabled = false;
  126. float Cost = 0;
  127. int PreviousRuns = 0;
  128. bool RunSerial = false;
  129. cm::optional<cmDuration> Timeout;
  130. cm::optional<Signal> TimeoutSignal;
  131. cm::optional<cmDuration> TimeoutGracePeriod;
  132. cmDuration AlternateTimeout;
  133. int Index = 0;
  134. // Requested number of process slots
  135. int Processors = 1;
  136. bool WantAffinity = false;
  137. std::vector<size_t> Affinity;
  138. // return code of test which will mark test as "not run"
  139. int SkipReturnCode = -1;
  140. std::vector<std::string> Environment;
  141. std::vector<std::string> EnvironmentModification;
  142. std::vector<std::string> Labels;
  143. std::set<std::string> LockedResources;
  144. std::set<std::string> FixturesSetup;
  145. std::set<std::string> FixturesCleanup;
  146. std::set<std::string> FixturesRequired;
  147. std::set<std::string> RequireSuccessDepends;
  148. std::vector<std::vector<cmCTestTestResourceRequirement>> ResourceGroups;
  149. // Private test generator properties used to track backtraces
  150. cmListFileBacktrace Backtrace;
  151. };
  152. struct cmCTestTestResult
  153. {
  154. std::string Name;
  155. std::string Path;
  156. std::string Reason;
  157. std::string FullCommandLine;
  158. std::string Environment;
  159. cmDuration ExecutionTime = cmDuration::zero();
  160. std::int64_t ReturnValue = 0;
  161. int Status = NOT_RUN;
  162. std::string ExceptionStatus;
  163. bool CompressOutput;
  164. std::string CompletionStatus;
  165. std::string CustomCompletionStatus;
  166. std::string Output;
  167. std::string TestMeasurementsOutput;
  168. int TestCount = 0;
  169. cmCTestTestProperties* Properties = nullptr;
  170. };
  171. struct cmCTestTestResultLess
  172. {
  173. bool operator()(const cmCTestTestResult& lhs,
  174. const cmCTestTestResult& 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. const std::string& testCommand,
  187. std::string& resultingConfig,
  188. std::vector<std::string>& extraPaths,
  189. std::vector<std::string>& failed);
  190. static bool ParseResourceGroupsProperty(
  191. const std::string& 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(const std::string& 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(const std::vector<std::string>& passed,
  206. const std::vector<std::string>& failed,
  207. const cmDuration& durationInSecs);
  208. void LogDisabledTests(const std::vector<cmCTestTestResult>& disabledTests);
  209. void LogFailedTests(const std::vector<std::string>& failed,
  210. const SetOfTests& 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. 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 CustomMaximumPassedTestOutputSize;
  233. int CustomMaximumFailedTestOutputSize;
  234. cmCTestTypes::TruncationMode TestOutputTruncation;
  235. int MaxIndex;
  236. public:
  237. enum
  238. { // Program statuses
  239. NOT_RUN = 0,
  240. TIMEOUT,
  241. SEGFAULT,
  242. ILLEGAL,
  243. INTERRUPT,
  244. NUMERICAL,
  245. OTHER_FAULT,
  246. FAILED,
  247. BAD_COMMAND,
  248. COMPLETED
  249. };
  250. private:
  251. /**
  252. * Write test results in CTest's Test.xml format
  253. */
  254. virtual void GenerateCTestXML(cmXMLWriter& xml);
  255. /**
  256. * Write test results in JUnit XML format
  257. */
  258. bool WriteJUnitXML();
  259. void PrintLabelOrSubprojectSummary(bool isSubProject);
  260. /**
  261. * Run the tests for a directory and any subdirectories
  262. */
  263. bool ProcessDirectory(std::vector<std::string>& passed,
  264. std::vector<std::string>& failed);
  265. /**
  266. * Get the list of tests in directory and subdirectories.
  267. */
  268. bool GetListOfTests();
  269. // compute the lists of tests that will actually run
  270. // based on union regex and -I stuff
  271. bool ComputeTestList();
  272. // compute the lists of tests that will actually run
  273. // based on LastTestFailed.log
  274. void ComputeTestListForRerunFailed();
  275. // add required setup/cleanup tests not already in the
  276. // list of tests to be run and update dependencies between
  277. // tests to account for fixture setup/cleanup
  278. void UpdateForFixtures(ListOfTests& tests) const;
  279. void UpdateMaxTestNameWidth();
  280. bool GetValue(const char* tag, std::string& value, std::istream& fin);
  281. bool GetValue(const char* tag, int& value, std::istream& fin);
  282. bool GetValue(const char* tag, size_t& value, std::istream& fin);
  283. bool GetValue(const char* tag, bool& value, std::istream& fin);
  284. bool GetValue(const char* tag, double& value, std::istream& fin);
  285. /**
  286. * Find the executable for a test
  287. */
  288. std::string FindTheExecutable(const std::string& exe);
  289. std::string GetTestStatus(cmCTestTestResult const&);
  290. void ExpandTestsToRunInformation(size_t numPossibleTests);
  291. void ExpandTestsToRunInformationForRerunFailed();
  292. std::vector<std::string> CustomPreTest;
  293. std::vector<std::string> CustomPostTest;
  294. std::vector<int> TestsToRun;
  295. bool UseIncludeRegExpFlag;
  296. bool UseExcludeRegExpFlag;
  297. bool UseExcludeRegExpFirst;
  298. std::string IncludeRegExp;
  299. std::string ExcludeRegExp;
  300. std::string ExcludeFixtureRegExp;
  301. std::string ExcludeFixtureSetupRegExp;
  302. std::string ExcludeFixtureCleanupRegExp;
  303. std::vector<cmsys::RegularExpression> IncludeLabelRegularExpressions;
  304. std::vector<cmsys::RegularExpression> ExcludeLabelRegularExpressions;
  305. cmsys::RegularExpression IncludeTestsRegularExpression;
  306. cmsys::RegularExpression ExcludeTestsRegularExpression;
  307. bool UseResourceSpec;
  308. cmCTestResourceSpec ResourceSpec;
  309. std::string ResourceSpecFile;
  310. void RecordCustomTestMeasurements(cmXMLWriter& xml, std::string content);
  311. void CheckLabelFilter(cmCTestTestProperties& it);
  312. void CheckLabelFilterExclude(cmCTestTestProperties& it);
  313. void CheckLabelFilterInclude(cmCTestTestProperties& it);
  314. std::string TestsToRunString;
  315. bool UseUnion;
  316. ListOfTests TestList;
  317. size_t TotalNumberOfTests;
  318. cmsys::RegularExpression AllTestMeasurementsRegex;
  319. cmsys::RegularExpression SingleTestMeasurementRegex;
  320. cmsys::RegularExpression CustomCompletionStatusRegex;
  321. cmsys::RegularExpression CustomLabelRegex;
  322. std::ostream* LogFile;
  323. cmCTest::Repeat RepeatMode = cmCTest::Repeat::Never;
  324. int RepeatCount = 1;
  325. bool RerunFailed;
  326. std::string JUnitXMLFileName;
  327. };