cmCTestTestHandler.h 11 KB

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