cmCTestRunTest.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 <map>
  6. #include <memory>
  7. #include <set>
  8. #include <string>
  9. #include <vector>
  10. #include <stddef.h>
  11. #include "cmCTest.h"
  12. #include "cmCTestMultiProcessHandler.h"
  13. #include "cmCTestTestHandler.h"
  14. #include "cmDuration.h"
  15. #include "cmProcess.h"
  16. /** \class cmRunTest
  17. * \brief represents a single test to be run
  18. *
  19. * cmRunTest contains the information related to running a single test
  20. */
  21. class cmCTestRunTest
  22. {
  23. public:
  24. explicit cmCTestRunTest(cmCTestMultiProcessHandler& multiHandler);
  25. void SetNumberOfRuns(int n)
  26. {
  27. this->NumberOfRunsLeft = n;
  28. this->NumberOfRunsTotal = n;
  29. }
  30. void SetRepeatMode(cmCTest::Repeat r) { this->RepeatMode = r; }
  31. void SetTestProperties(cmCTestTestHandler::cmCTestTestProperties* prop)
  32. {
  33. this->TestProperties = prop;
  34. }
  35. cmCTestTestHandler::cmCTestTestProperties* GetTestProperties()
  36. {
  37. return this->TestProperties;
  38. }
  39. void SetIndex(int i) { this->Index = i; }
  40. int GetIndex() { return this->Index; }
  41. void AddFailedDependency(const std::string& failedTest)
  42. {
  43. this->FailedDependencies.insert(failedTest);
  44. }
  45. std::string GetProcessOutput() { return this->ProcessOutput; }
  46. cmCTestTestHandler::cmCTestTestResult GetTestResults()
  47. {
  48. return this->TestResult;
  49. }
  50. // Read and store output. Returns true if it must be called again.
  51. void CheckOutput(std::string const& line);
  52. static bool StartTest(std::unique_ptr<cmCTestRunTest> runner,
  53. size_t completed, size_t total);
  54. static bool StartAgain(std::unique_ptr<cmCTestRunTest> runner,
  55. size_t completed);
  56. static void StartFailure(std::unique_ptr<cmCTestRunTest> runner,
  57. std::string const& output,
  58. std::string const& detail);
  59. // launch the test process, return whether it started correctly
  60. bool StartTest(size_t completed, size_t total);
  61. // capture and report the test results
  62. bool EndTest(size_t completed, size_t total, bool started);
  63. // Called by ctest -N to log the command string
  64. void ComputeArguments();
  65. void ComputeWeightedCost();
  66. void StartFailure(std::string const& output, std::string const& detail);
  67. cmCTest* GetCTest() const { return this->CTest; }
  68. std::string& GetActualCommand() { return this->ActualCommand; }
  69. const std::vector<std::string>& GetArguments() { return this->Arguments; }
  70. void FinalizeTest(bool started = true);
  71. bool TimedOutForStopTime() const { return this->TimeoutIsForStopTime; }
  72. void SetUseAllocatedResources(bool use)
  73. {
  74. this->UseAllocatedResources = use;
  75. }
  76. void SetAllocatedResources(
  77. const std::vector<
  78. std::map<std::string,
  79. std::vector<cmCTestMultiProcessHandler::ResourceAllocation>>>&
  80. resources)
  81. {
  82. this->AllocatedResources = resources;
  83. }
  84. private:
  85. bool NeedsToRepeat();
  86. void DartProcessing();
  87. void ExeNotFound(std::string exe);
  88. bool ForkProcess(cmDuration testTimeOut, bool explicitTimeout,
  89. std::vector<std::string>* environment,
  90. std::vector<size_t>* affinity);
  91. void WriteLogOutputTop(size_t completed, size_t total);
  92. // Run post processing of the process output for MemCheck
  93. void MemCheckPostProcess();
  94. void SetupResourcesEnvironment(std::vector<std::string>* log = nullptr);
  95. // Returns "completed/total Test #Index: "
  96. std::string GetTestPrefix(size_t completed, size_t total) const;
  97. cmCTestTestHandler::cmCTestTestProperties* TestProperties;
  98. bool TimeoutIsForStopTime = false;
  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. }