cmCTestTestHandler.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. typedef std::set<cmCTestTestHandler::cmCTestTestResult,
  169. cmCTestTestResultLess>
  170. SetOfTests;
  171. // compute a final test list
  172. virtual int PreProcessHandler();
  173. virtual int PostProcessHandler();
  174. virtual void GenerateTestCommand(std::vector<std::string>& args, int test);
  175. int ExecuteCommands(std::vector<std::string>& vec);
  176. bool ProcessOptions();
  177. void LogTestSummary(const std::vector<std::string>& passed,
  178. const std::vector<std::string>& failed,
  179. const cmDuration& durationInSecs);
  180. void LogDisabledTests(const std::vector<cmCTestTestResult>& disabledTests);
  181. void LogFailedTests(const std::vector<std::string>& failed,
  182. const SetOfTests& resultsSet);
  183. bool GenerateXML();
  184. void WriteTestResultHeader(cmXMLWriter& xml,
  185. cmCTestTestResult const& result);
  186. void WriteTestResultFooter(cmXMLWriter& xml,
  187. cmCTestTestResult const& result);
  188. // Write attached test files into the xml
  189. void AttachFiles(cmXMLWriter& xml, cmCTestTestResult& result);
  190. //! Clean test output to specified length
  191. bool CleanTestOutput(std::string& output, size_t length);
  192. cmDuration ElapsedTestingTime;
  193. typedef std::vector<cmCTestTestResult> TestResultsVector;
  194. TestResultsVector TestResults;
  195. std::vector<std::string> CustomTestsIgnore;
  196. std::string StartTest;
  197. std::string EndTest;
  198. std::chrono::system_clock::time_point StartTestTime;
  199. std::chrono::system_clock::time_point EndTestTime;
  200. bool MemCheck;
  201. int CustomMaximumPassedTestOutputSize;
  202. int CustomMaximumFailedTestOutputSize;
  203. int MaxIndex;
  204. public:
  205. enum
  206. { // Program statuses
  207. NOT_RUN = 0,
  208. TIMEOUT,
  209. SEGFAULT,
  210. ILLEGAL,
  211. INTERRUPT,
  212. NUMERICAL,
  213. OTHER_FAULT,
  214. FAILED,
  215. BAD_COMMAND,
  216. COMPLETED
  217. };
  218. private:
  219. /**
  220. * Generate the Dart compatible output
  221. */
  222. virtual void GenerateDartOutput(cmXMLWriter& xml);
  223. void PrintLabelOrSubprojectSummary(bool isSubProject);
  224. /**
  225. * Run the tests for a directory and any subdirectories
  226. */
  227. void ProcessDirectory(std::vector<std::string>& passed,
  228. std::vector<std::string>& failed);
  229. /**
  230. * Get the list of tests in directory and subdirectories.
  231. */
  232. void GetListOfTests();
  233. // compute the lists of tests that will actually run
  234. // based on union regex and -I stuff
  235. void ComputeTestList();
  236. // compute the lists of tests that will actually run
  237. // based on LastTestFailed.log
  238. void ComputeTestListForRerunFailed();
  239. // add required setup/cleanup tests not already in the
  240. // list of tests to be run and update dependencies between
  241. // tests to account for fixture setup/cleanup
  242. void UpdateForFixtures(ListOfTests& tests) const;
  243. void UpdateMaxTestNameWidth();
  244. bool GetValue(const char* tag, std::string& value, std::istream& fin);
  245. bool GetValue(const char* tag, int& value, std::istream& fin);
  246. bool GetValue(const char* tag, size_t& value, std::istream& fin);
  247. bool GetValue(const char* tag, bool& value, std::istream& fin);
  248. bool GetValue(const char* tag, double& value, std::istream& fin);
  249. /**
  250. * Find the executable for a test
  251. */
  252. std::string FindTheExecutable(const char* exe);
  253. std::string GetTestStatus(cmCTestTestResult const&);
  254. void ExpandTestsToRunInformation(size_t numPossibleTests);
  255. void ExpandTestsToRunInformationForRerunFailed();
  256. std::vector<std::string> CustomPreTest;
  257. std::vector<std::string> CustomPostTest;
  258. std::vector<int> TestsToRun;
  259. bool UseIncludeLabelRegExpFlag;
  260. bool UseExcludeLabelRegExpFlag;
  261. bool UseIncludeRegExpFlag;
  262. bool UseExcludeRegExpFlag;
  263. bool UseExcludeRegExpFirst;
  264. std::string IncludeLabelRegExp;
  265. std::string ExcludeLabelRegExp;
  266. std::string IncludeRegExp;
  267. std::string ExcludeRegExp;
  268. std::string ExcludeFixtureRegExp;
  269. std::string ExcludeFixtureSetupRegExp;
  270. std::string ExcludeFixtureCleanupRegExp;
  271. cmsys::RegularExpression IncludeLabelRegularExpression;
  272. cmsys::RegularExpression ExcludeLabelRegularExpression;
  273. cmsys::RegularExpression IncludeTestsRegularExpression;
  274. cmsys::RegularExpression ExcludeTestsRegularExpression;
  275. void GenerateRegressionImages(cmXMLWriter& xml, const std::string& dart);
  276. cmsys::RegularExpression DartStuff1;
  277. void CheckLabelFilter(cmCTestTestProperties& it);
  278. void CheckLabelFilterExclude(cmCTestTestProperties& it);
  279. void CheckLabelFilterInclude(cmCTestTestProperties& it);
  280. std::string TestsToRunString;
  281. bool UseUnion;
  282. ListOfTests TestList;
  283. size_t TotalNumberOfTests;
  284. cmsys::RegularExpression DartStuff;
  285. std::ostream* LogFile;
  286. bool RerunFailed;
  287. };
  288. #endif