cmCTestTestHandler.h 9.4 KB

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