cmCTestTestHandler.h 9.3 KB

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