cmCTestTestHandler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmCTestTestHandler_h
  4. #define cmCTestTestHandler_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <chrono>
  7. #include <cstdint>
  8. #include <iosfwd>
  9. #include <map>
  10. #include <set>
  11. #include <string>
  12. #include <utility>
  13. #include <vector>
  14. #include <stddef.h>
  15. #include "cmsys/RegularExpression.hxx"
  16. #include "cmCTestGenericHandler.h"
  17. #include "cmCTestHardwareSpec.h"
  18. #include "cmDuration.h"
  19. #include "cmListFileCache.h"
  20. class cmCTest;
  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 char*);
  57. void SetExcludeRegExp(const char*);
  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(const char*);
  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> Labels;
  132. std::set<std::string> LockedResources;
  133. std::set<std::string> FixturesSetup;
  134. std::set<std::string> FixturesCleanup;
  135. std::set<std::string> FixturesRequired;
  136. std::set<std::string> RequireSuccessDepends;
  137. std::vector<std::vector<cmCTestTestResourceRequirement>> ResourceGroups;
  138. // Private test generator properties used to track backtraces
  139. cmListFileBacktrace Backtrace;
  140. };
  141. struct cmCTestTestResult
  142. {
  143. std::string Name;
  144. std::string Path;
  145. std::string Reason;
  146. std::string FullCommandLine;
  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, const char* testCommand,
  173. std::string& resultingConfig,
  174. std::vector<std::string>& extraPaths,
  175. std::vector<std::string>& failed);
  176. static bool ParseResourceGroupsProperty(
  177. const std::string& val,
  178. std::vector<std::vector<cmCTestTestResourceRequirement>>& resourceGroups);
  179. using ListOfTests = std::vector<cmCTestTestProperties>;
  180. protected:
  181. using SetOfTests =
  182. std::set<cmCTestTestHandler::cmCTestTestResult, cmCTestTestResultLess>;
  183. // compute a final test list
  184. virtual int PreProcessHandler();
  185. virtual int PostProcessHandler();
  186. virtual void GenerateTestCommand(std::vector<std::string>& args, int test);
  187. int ExecuteCommands(std::vector<std::string>& vec);
  188. bool ProcessOptions();
  189. void LogTestSummary(const std::vector<std::string>& passed,
  190. const std::vector<std::string>& failed,
  191. const cmDuration& durationInSecs);
  192. void LogDisabledTests(const std::vector<cmCTestTestResult>& disabledTests);
  193. void LogFailedTests(const std::vector<std::string>& failed,
  194. const SetOfTests& resultsSet);
  195. bool GenerateXML();
  196. void WriteTestResultHeader(cmXMLWriter& xml,
  197. cmCTestTestResult const& result);
  198. void WriteTestResultFooter(cmXMLWriter& xml,
  199. cmCTestTestResult const& result);
  200. // Write attached test files into the xml
  201. void AttachFiles(cmXMLWriter& xml, cmCTestTestResult& result);
  202. //! Clean test output to specified length
  203. bool CleanTestOutput(std::string& output, size_t length);
  204. cmDuration ElapsedTestingTime;
  205. using TestResultsVector = std::vector<cmCTestTestResult>;
  206. TestResultsVector TestResults;
  207. std::vector<std::string> CustomTestsIgnore;
  208. std::string StartTest;
  209. std::string EndTest;
  210. std::chrono::system_clock::time_point StartTestTime;
  211. std::chrono::system_clock::time_point EndTestTime;
  212. bool MemCheck;
  213. int CustomMaximumPassedTestOutputSize;
  214. int CustomMaximumFailedTestOutputSize;
  215. int MaxIndex;
  216. public:
  217. enum
  218. { // Program statuses
  219. NOT_RUN = 0,
  220. TIMEOUT,
  221. SEGFAULT,
  222. ILLEGAL,
  223. INTERRUPT,
  224. NUMERICAL,
  225. OTHER_FAULT,
  226. FAILED,
  227. BAD_COMMAND,
  228. COMPLETED
  229. };
  230. private:
  231. /**
  232. * Generate the Dart compatible output
  233. */
  234. virtual void GenerateDartOutput(cmXMLWriter& xml);
  235. void PrintLabelOrSubprojectSummary(bool isSubProject);
  236. /**
  237. * Run the tests for a directory and any subdirectories
  238. */
  239. void ProcessDirectory(std::vector<std::string>& passed,
  240. std::vector<std::string>& failed);
  241. /**
  242. * Get the list of tests in directory and subdirectories.
  243. */
  244. void GetListOfTests();
  245. // compute the lists of tests that will actually run
  246. // based on union regex and -I stuff
  247. void ComputeTestList();
  248. // compute the lists of tests that will actually run
  249. // based on LastTestFailed.log
  250. void ComputeTestListForRerunFailed();
  251. // add required setup/cleanup tests not already in the
  252. // list of tests to be run and update dependencies between
  253. // tests to account for fixture setup/cleanup
  254. void UpdateForFixtures(ListOfTests& tests) const;
  255. void UpdateMaxTestNameWidth();
  256. bool GetValue(const char* tag, std::string& value, std::istream& fin);
  257. bool GetValue(const char* tag, int& value, std::istream& fin);
  258. bool GetValue(const char* tag, size_t& value, std::istream& fin);
  259. bool GetValue(const char* tag, bool& value, std::istream& fin);
  260. bool GetValue(const char* tag, double& value, std::istream& fin);
  261. /**
  262. * Find the executable for a test
  263. */
  264. std::string FindTheExecutable(const char* exe);
  265. std::string GetTestStatus(cmCTestTestResult const&);
  266. void ExpandTestsToRunInformation(size_t numPossibleTests);
  267. void ExpandTestsToRunInformationForRerunFailed();
  268. std::vector<std::string> CustomPreTest;
  269. std::vector<std::string> CustomPostTest;
  270. std::vector<int> TestsToRun;
  271. bool UseIncludeLabelRegExpFlag;
  272. bool UseExcludeLabelRegExpFlag;
  273. bool UseIncludeRegExpFlag;
  274. bool UseExcludeRegExpFlag;
  275. bool UseExcludeRegExpFirst;
  276. std::string IncludeLabelRegExp;
  277. std::string ExcludeLabelRegExp;
  278. std::string IncludeRegExp;
  279. std::string ExcludeRegExp;
  280. std::string ExcludeFixtureRegExp;
  281. std::string ExcludeFixtureSetupRegExp;
  282. std::string ExcludeFixtureCleanupRegExp;
  283. cmsys::RegularExpression IncludeLabelRegularExpression;
  284. cmsys::RegularExpression ExcludeLabelRegularExpression;
  285. cmsys::RegularExpression IncludeTestsRegularExpression;
  286. cmsys::RegularExpression ExcludeTestsRegularExpression;
  287. bool UseHardwareSpec;
  288. cmCTestHardwareSpec HardwareSpec;
  289. void GenerateRegressionImages(cmXMLWriter& xml, const std::string& dart);
  290. cmsys::RegularExpression DartStuff1;
  291. void CheckLabelFilter(cmCTestTestProperties& it);
  292. void CheckLabelFilterExclude(cmCTestTestProperties& it);
  293. void CheckLabelFilterInclude(cmCTestTestProperties& it);
  294. std::string TestsToRunString;
  295. bool UseUnion;
  296. ListOfTests TestList;
  297. size_t TotalNumberOfTests;
  298. cmsys::RegularExpression DartStuff;
  299. std::ostream* LogFile;
  300. bool RerunFailed;
  301. };
  302. #endif