cmCTestTestHandler.h 11 KB

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