cmCTestTestHandler.h 9.2 KB

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