cmCTestRunTest.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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, int index);
  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. cmCTestTestHandler::cmCTestTestProperties* GetTestProperties()
  31. {
  32. return this->TestProperties;
  33. }
  34. int GetIndex() { return this->Index; }
  35. void AddFailedDependency(std::string const& failedTest)
  36. {
  37. this->FailedDependencies.insert(failedTest);
  38. }
  39. std::string GetProcessOutput() { return this->ProcessOutput; }
  40. cmCTestTestHandler::cmCTestTestResult GetTestResults()
  41. {
  42. return this->TestResult;
  43. }
  44. // Read and store output. Returns true if it must be called again.
  45. void CheckOutput(std::string const& line);
  46. static void StartTest(std::unique_ptr<cmCTestRunTest> runner,
  47. size_t completed, size_t total);
  48. static bool StartAgain(std::unique_ptr<cmCTestRunTest> runner,
  49. size_t completed);
  50. static void StartFailure(std::unique_ptr<cmCTestRunTest> runner,
  51. size_t total, std::string const& output,
  52. std::string const& detail);
  53. struct EndTestResult
  54. {
  55. bool Passed = false;
  56. bool StopTimePassed = false;
  57. };
  58. // launch the test process, return whether it started correctly
  59. bool StartTest(size_t completed, size_t total);
  60. // capture and report the test results
  61. EndTestResult EndTest(size_t completed, size_t total, bool started);
  62. // Called by ctest -N to log the command string
  63. void ComputeArguments();
  64. void ComputeWeightedCost();
  65. void StartFailure(size_t total, std::string const& output,
  66. std::string const& detail);
  67. cmCTest* GetCTest() const { return this->CTest; }
  68. std::string& GetActualCommand() { return this->ActualCommand; }
  69. std::vector<std::string> const& GetArguments() { return this->Arguments; }
  70. void FinalizeTest(bool started = true);
  71. void SetUseAllocatedResources(bool use)
  72. {
  73. this->UseAllocatedResources = use;
  74. }
  75. void SetAllocatedResources(
  76. std::vector<std::map<
  77. std::string,
  78. std::vector<cmCTestMultiProcessHandler::ResourceAllocation>>> const&
  79. resources)
  80. {
  81. this->AllocatedResources = resources;
  82. }
  83. private:
  84. bool NeedsToRepeat();
  85. void ParseOutputForMeasurements();
  86. void ExeNotFound(std::string exe);
  87. bool ForkProcess();
  88. void WriteLogOutputTop(size_t completed, size_t total);
  89. // Run post processing of the process output for MemCheck
  90. void MemCheckPostProcess();
  91. void SetupResourcesEnvironment(std::vector<std::string>* log = nullptr);
  92. // Returns "completed/total Test #Index: "
  93. std::string GetTestPrefix(size_t completed, size_t total) const;
  94. cmCTestMultiProcessHandler& MultiTestHandler;
  95. int Index;
  96. cmCTest* CTest;
  97. cmCTestTestHandler* TestHandler;
  98. cmCTestTestHandler::cmCTestTestProperties* TestProperties;
  99. std::unique_ptr<cmProcess> TestProcess;
  100. std::string ProcessOutput;
  101. cmCTestTestHandler::cmCTestTestResult TestResult;
  102. std::set<std::string> FailedDependencies;
  103. std::string StartTime;
  104. std::string ActualCommand;
  105. std::vector<std::string> Arguments;
  106. bool UseAllocatedResources = false;
  107. std::vector<std::map<
  108. std::string, std::vector<cmCTestMultiProcessHandler::ResourceAllocation>>>
  109. AllocatedResources;
  110. cmCTest::Repeat RepeatMode = cmCTest::Repeat::Never;
  111. int NumberOfRunsLeft = 1; // default to 1 run of the test
  112. int NumberOfRunsTotal = 1; // default to 1 run of the test
  113. bool RunAgain = false; // default to not having to run again
  114. size_t TotalNumberOfTests;
  115. };
  116. inline int getNumWidth(size_t n)
  117. {
  118. int w = 1;
  119. while (n >= 10) {
  120. n /= 10;
  121. ++w;
  122. }
  123. return w;
  124. }