cmCTestRunTest.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 cmCTestRunTest_h
  4. #define cmCTestRunTest_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <map>
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. #include <vector>
  11. #include <stddef.h>
  12. #include "cmCTest.h"
  13. #include "cmCTestMultiProcessHandler.h"
  14. #include "cmCTestTestHandler.h"
  15. #include "cmDuration.h"
  16. #include "cmProcess.h"
  17. /** \class cmRunTest
  18. * \brief represents a single test to be run
  19. *
  20. * cmRunTest contains the information related to running a single test
  21. */
  22. class cmCTestRunTest
  23. {
  24. public:
  25. explicit cmCTestRunTest(cmCTestMultiProcessHandler& multiHandler);
  26. void SetNumberOfRuns(int n)
  27. {
  28. this->NumberOfRunsLeft = n;
  29. this->NumberOfRunsTotal = n;
  30. }
  31. void SetRepeatMode(cmCTest::Repeat r) { this->RepeatMode = r; }
  32. void SetTestProperties(cmCTestTestHandler::cmCTestTestProperties* prop)
  33. {
  34. this->TestProperties = prop;
  35. }
  36. cmCTestTestHandler::cmCTestTestProperties* GetTestProperties()
  37. {
  38. return this->TestProperties;
  39. }
  40. void SetIndex(int i) { this->Index = i; }
  41. int GetIndex() { return this->Index; }
  42. void AddFailedDependency(const std::string& failedTest)
  43. {
  44. this->FailedDependencies.insert(failedTest);
  45. }
  46. std::string GetProcessOutput() { return this->ProcessOutput; }
  47. cmCTestTestHandler::cmCTestTestResult GetTestResults()
  48. {
  49. return this->TestResult;
  50. }
  51. // Read and store output. Returns true if it must be called again.
  52. void CheckOutput(std::string const& line);
  53. static bool StartTest(std::unique_ptr<cmCTestRunTest> runner,
  54. size_t completed, size_t total);
  55. static bool StartAgain(std::unique_ptr<cmCTestRunTest> runner,
  56. size_t completed);
  57. static void StartFailure(std::unique_ptr<cmCTestRunTest> runner,
  58. std::string const& output,
  59. std::string const& detail);
  60. // launch the test process, return whether it started correctly
  61. bool StartTest(size_t completed, size_t total);
  62. // capture and report the test results
  63. bool EndTest(size_t completed, size_t total, bool started);
  64. // Called by ctest -N to log the command string
  65. void ComputeArguments();
  66. void ComputeWeightedCost();
  67. void StartFailure(std::string const& output, std::string const& detail);
  68. cmCTest* GetCTest() const { return this->CTest; }
  69. std::string& GetActualCommand() { return this->ActualCommand; }
  70. const std::vector<std::string>& GetArguments() { return this->Arguments; }
  71. void FinalizeTest(bool started = true);
  72. bool TimedOutForStopTime() const { return this->TimeoutIsForStopTime; }
  73. void SetUseAllocatedResources(bool use)
  74. {
  75. this->UseAllocatedResources = use;
  76. }
  77. void SetAllocatedResources(
  78. const std::vector<
  79. std::map<std::string,
  80. std::vector<cmCTestMultiProcessHandler::ResourceAllocation>>>&
  81. resources)
  82. {
  83. this->AllocatedResources = resources;
  84. }
  85. private:
  86. bool NeedsToRepeat();
  87. void DartProcessing();
  88. void ExeNotFound(std::string exe);
  89. bool ForkProcess(cmDuration testTimeOut, bool explicitTimeout,
  90. std::vector<std::string>* environment,
  91. std::vector<size_t>* affinity);
  92. void WriteLogOutputTop(size_t completed, size_t total);
  93. // Run post processing of the process output for MemCheck
  94. void MemCheckPostProcess();
  95. void SetupResourcesEnvironment(std::vector<std::string>* log = nullptr);
  96. // Returns "completed/total Test #Index: "
  97. std::string GetTestPrefix(size_t completed, size_t total) const;
  98. cmCTestTestHandler::cmCTestTestProperties* TestProperties;
  99. bool TimeoutIsForStopTime = false;
  100. // Pointer back to the "parent"; the handler that invoked this test run
  101. cmCTestTestHandler* TestHandler;
  102. cmCTest* CTest;
  103. std::unique_ptr<cmProcess> TestProcess;
  104. std::string ProcessOutput;
  105. // The test results
  106. cmCTestTestHandler::cmCTestTestResult TestResult;
  107. cmCTestMultiProcessHandler& MultiTestHandler;
  108. int Index;
  109. std::set<std::string> FailedDependencies;
  110. std::string StartTime;
  111. std::string ActualCommand;
  112. std::vector<std::string> Arguments;
  113. bool UseAllocatedResources = false;
  114. std::vector<std::map<
  115. std::string, std::vector<cmCTestMultiProcessHandler::ResourceAllocation>>>
  116. AllocatedResources;
  117. cmCTest::Repeat RepeatMode = cmCTest::Repeat::Never;
  118. int NumberOfRunsLeft = 1; // default to 1 run of the test
  119. int NumberOfRunsTotal = 1; // default to 1 run of the test
  120. bool RunAgain = false; // default to not having to run again
  121. size_t TotalNumberOfTests;
  122. };
  123. inline int getNumWidth(size_t n)
  124. {
  125. int w = 1;
  126. while (n >= 10) {
  127. n /= 10;
  128. ++w;
  129. }
  130. return w;
  131. }
  132. #endif