cmCTestTestHandler.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 "cmCTestGenericHandler.h"
  7. #include "cmDuration.h"
  8. #include "cmListFileCache.h"
  9. #include "cmsys/RegularExpression.hxx"
  10. #include <chrono>
  11. #include <cstdint>
  12. #include <iosfwd>
  13. #include <map>
  14. #include <set>
  15. #include <stddef.h>
  16. #include <string>
  17. #include <utility>
  18. #include <vector>
  19. class cmCTest;
  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. typedef cmCTestGenericHandler Superclass;
  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. // NOTE: This struct is Saved/Restored
  84. // in cmCTestTestHandler, if you add to this class
  85. // then you must add the new members to that code or
  86. // ctest -j N will break for that feature
  87. struct cmCTestTestProperties
  88. {
  89. std::string Name;
  90. std::string Directory;
  91. std::vector<std::string> Args;
  92. std::vector<std::string> RequiredFiles;
  93. std::vector<std::string> Depends;
  94. std::vector<std::string> AttachedFiles;
  95. std::vector<std::string> AttachOnFail;
  96. std::vector<std::pair<cmsys::RegularExpression, std::string>>
  97. ErrorRegularExpressions;
  98. std::vector<std::pair<cmsys::RegularExpression, std::string>>
  99. RequiredRegularExpressions;
  100. std::vector<std::pair<cmsys::RegularExpression, std::string>>
  101. SkipRegularExpressions;
  102. std::vector<std::pair<cmsys::RegularExpression, std::string>>
  103. TimeoutRegularExpressions;
  104. std::map<std::string, std::string> Measurements;
  105. bool IsInBasedOnREOptions;
  106. bool WillFail;
  107. bool Disabled;
  108. float Cost;
  109. int PreviousRuns;
  110. bool RunSerial;
  111. cmDuration Timeout;
  112. bool ExplicitTimeout;
  113. cmDuration AlternateTimeout;
  114. int Index;
  115. // Requested number of process slots
  116. int Processors;
  117. bool WantAffinity;
  118. std::vector<size_t> Affinity;
  119. // return code of test which will mark test as "not run"
  120. int SkipReturnCode;
  121. std::vector<std::string> Environment;
  122. std::vector<std::string> Labels;
  123. std::set<std::string> LockedResources;
  124. std::set<std::string> FixturesSetup;
  125. std::set<std::string> FixturesCleanup;
  126. std::set<std::string> FixturesRequired;
  127. std::set<std::string> RequireSuccessDepends;
  128. // Private test generator properties used to track backtraces
  129. cmListFileBacktrace Backtrace;
  130. };
  131. struct cmCTestTestResult
  132. {
  133. std::string Name;
  134. std::string Path;
  135. std::string Reason;
  136. std::string FullCommandLine;
  137. cmDuration ExecutionTime;
  138. std::int64_t ReturnValue;
  139. int Status;
  140. std::string ExceptionStatus;
  141. bool CompressOutput;
  142. std::string CompletionStatus;
  143. std::string Output;
  144. std::string DartString;
  145. int TestCount;
  146. cmCTestTestProperties* Properties;
  147. };
  148. struct cmCTestTestResultLess
  149. {
  150. bool operator()(const cmCTestTestResult& lhs,
  151. const cmCTestTestResult& rhs) const
  152. {
  153. return lhs.TestCount < rhs.TestCount;
  154. }
  155. };
  156. // add configurations to a search path for an executable
  157. static void AddConfigurations(cmCTest* ctest,
  158. std::vector<std::string>& attempted,
  159. std::vector<std::string>& attemptedConfigs,
  160. std::string filepath, std::string& filename);
  161. // full signature static method to find an executable
  162. static std::string FindExecutable(cmCTest* ctest, const char* testCommand,
  163. std::string& resultingConfig,
  164. std::vector<std::string>& extraPaths,
  165. std::vector<std::string>& failed);
  166. typedef std::vector<cmCTestTestProperties> ListOfTests;
  167. protected:
  168. // compute a final test list
  169. virtual int PreProcessHandler();
  170. virtual int PostProcessHandler();
  171. virtual void GenerateTestCommand(std::vector<std::string>& args, int test);
  172. int ExecuteCommands(std::vector<std::string>& vec);
  173. void WriteTestResultHeader(cmXMLWriter& xml,
  174. cmCTestTestResult const& result);
  175. void WriteTestResultFooter(cmXMLWriter& xml,
  176. cmCTestTestResult const& result);
  177. // Write attached test files into the xml
  178. void AttachFiles(cmXMLWriter& xml, cmCTestTestResult& result);
  179. //! Clean test output to specified length
  180. bool CleanTestOutput(std::string& output, size_t length);
  181. cmDuration ElapsedTestingTime;
  182. typedef std::vector<cmCTestTestResult> TestResultsVector;
  183. TestResultsVector TestResults;
  184. std::vector<std::string> CustomTestsIgnore;
  185. std::string StartTest;
  186. std::string EndTest;
  187. std::chrono::system_clock::time_point StartTestTime;
  188. std::chrono::system_clock::time_point EndTestTime;
  189. bool MemCheck;
  190. int CustomMaximumPassedTestOutputSize;
  191. int CustomMaximumFailedTestOutputSize;
  192. int MaxIndex;
  193. public:
  194. enum
  195. { // Program statuses
  196. NOT_RUN = 0,
  197. TIMEOUT,
  198. SEGFAULT,
  199. ILLEGAL,
  200. INTERRUPT,
  201. NUMERICAL,
  202. OTHER_FAULT,
  203. FAILED,
  204. BAD_COMMAND,
  205. COMPLETED
  206. };
  207. private:
  208. /**
  209. * Generate the Dart compatible output
  210. */
  211. virtual void GenerateDartOutput(cmXMLWriter& xml);
  212. void PrintLabelOrSubprojectSummary(bool isSubProject);
  213. /**
  214. * Run the tests for a directory and any subdirectories
  215. */
  216. void ProcessDirectory(std::vector<std::string>& passed,
  217. std::vector<std::string>& failed);
  218. /**
  219. * Get the list of tests in directory and subdirectories.
  220. */
  221. void GetListOfTests();
  222. // compute the lists of tests that will actually run
  223. // based on union regex and -I stuff
  224. void ComputeTestList();
  225. // compute the lists of tests that will actually run
  226. // based on LastTestFailed.log
  227. void ComputeTestListForRerunFailed();
  228. // add required setup/cleanup tests not already in the
  229. // list of tests to be run and update dependencies between
  230. // tests to account for fixture setup/cleanup
  231. void UpdateForFixtures(ListOfTests& tests) const;
  232. void UpdateMaxTestNameWidth();
  233. bool GetValue(const char* tag, std::string& value, std::istream& fin);
  234. bool GetValue(const char* tag, int& value, std::istream& fin);
  235. bool GetValue(const char* tag, size_t& value, std::istream& fin);
  236. bool GetValue(const char* tag, bool& value, std::istream& fin);
  237. bool GetValue(const char* tag, double& value, std::istream& fin);
  238. /**
  239. * Find the executable for a test
  240. */
  241. std::string FindTheExecutable(const char* exe);
  242. std::string GetTestStatus(cmCTestTestResult const&);
  243. void ExpandTestsToRunInformation(size_t numPossibleTests);
  244. void ExpandTestsToRunInformationForRerunFailed();
  245. std::vector<std::string> CustomPreTest;
  246. std::vector<std::string> CustomPostTest;
  247. std::vector<int> TestsToRun;
  248. bool UseIncludeLabelRegExpFlag;
  249. bool UseExcludeLabelRegExpFlag;
  250. bool UseIncludeRegExpFlag;
  251. bool UseExcludeRegExpFlag;
  252. bool UseExcludeRegExpFirst;
  253. std::string IncludeLabelRegExp;
  254. std::string ExcludeLabelRegExp;
  255. std::string IncludeRegExp;
  256. std::string ExcludeRegExp;
  257. std::string ExcludeFixtureRegExp;
  258. std::string ExcludeFixtureSetupRegExp;
  259. std::string ExcludeFixtureCleanupRegExp;
  260. cmsys::RegularExpression IncludeLabelRegularExpression;
  261. cmsys::RegularExpression ExcludeLabelRegularExpression;
  262. cmsys::RegularExpression IncludeTestsRegularExpression;
  263. cmsys::RegularExpression ExcludeTestsRegularExpression;
  264. void GenerateRegressionImages(cmXMLWriter& xml, const std::string& dart);
  265. cmsys::RegularExpression DartStuff1;
  266. void CheckLabelFilter(cmCTestTestProperties& it);
  267. void CheckLabelFilterExclude(cmCTestTestProperties& it);
  268. void CheckLabelFilterInclude(cmCTestTestProperties& it);
  269. std::string TestsToRunString;
  270. bool UseUnion;
  271. ListOfTests TestList;
  272. size_t TotalNumberOfTests;
  273. cmsys::RegularExpression DartStuff;
  274. std::ostream* LogFile;
  275. bool RerunFailed;
  276. };
  277. #endif