cmCTestRunTest.h 4.5 KB

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