cmCTestTestHandler.h 9.4 KB

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