cmCTestGenericHandler.cxx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCTestGenericHandler.h"
  4. #include <sstream>
  5. #include <utility>
  6. #include "cmCTest.h"
  7. #include "cmStringAlgorithms.h"
  8. #include "cmSystemTools.h"
  9. cmCTestGenericHandler::cmCTestGenericHandler()
  10. {
  11. this->HandlerVerbose = cmSystemTools::OUTPUT_NONE;
  12. this->CTest = nullptr;
  13. this->SubmitIndex = 0;
  14. this->AppendXML = false;
  15. this->Quiet = false;
  16. this->TestLoad = 0;
  17. }
  18. cmCTestGenericHandler::~cmCTestGenericHandler() = default;
  19. /* Modify the given `map`, setting key `op` to `value` if `value`
  20. * is non-null, otherwise removing key `op` (if it exists).
  21. */
  22. static void SetMapValue(cmCTestGenericHandler::t_StringToString& map,
  23. const std::string& op, const char* value)
  24. {
  25. if (!value) {
  26. map.erase(op);
  27. return;
  28. }
  29. map[op] = value;
  30. }
  31. void cmCTestGenericHandler::SetOption(const std::string& op, const char* value)
  32. {
  33. SetMapValue(this->Options, op, value);
  34. }
  35. void cmCTestGenericHandler::SetPersistentOption(const std::string& op,
  36. const char* value)
  37. {
  38. this->SetOption(op, value);
  39. SetMapValue(this->PersistentOptions, op, value);
  40. }
  41. void cmCTestGenericHandler::AddMultiOption(const std::string& op,
  42. const std::string& value)
  43. {
  44. if (!value.empty()) {
  45. this->MultiOptions[op].emplace_back(value);
  46. }
  47. }
  48. void cmCTestGenericHandler::AddPersistentMultiOption(const std::string& op,
  49. const std::string& value)
  50. {
  51. if (!value.empty()) {
  52. this->MultiOptions[op].emplace_back(value);
  53. this->PersistentMultiOptions[op].emplace_back(value);
  54. }
  55. }
  56. void cmCTestGenericHandler::Initialize()
  57. {
  58. this->AppendXML = false;
  59. this->TestLoad = 0;
  60. this->Options = this->PersistentOptions;
  61. this->MultiOptions = this->PersistentMultiOptions;
  62. }
  63. const char* cmCTestGenericHandler::GetOption(const std::string& op)
  64. {
  65. auto remit = this->Options.find(op);
  66. if (remit == this->Options.end()) {
  67. return nullptr;
  68. }
  69. return remit->second.c_str();
  70. }
  71. std::vector<std::string> cmCTestGenericHandler::GetMultiOption(
  72. const std::string& optionName) const
  73. {
  74. // Avoid inserting a key, which MultiOptions[op] would do.
  75. auto remit = this->MultiOptions.find(optionName);
  76. if (remit == this->MultiOptions.end()) {
  77. return {};
  78. }
  79. return remit->second;
  80. }
  81. bool cmCTestGenericHandler::StartResultingXML(cmCTest::Part part,
  82. const char* name,
  83. cmGeneratedFileStream& xofs)
  84. {
  85. if (!name) {
  86. cmCTestLog(this->CTest, ERROR_MESSAGE,
  87. "Cannot create resulting XML file without providing the name"
  88. << std::endl;);
  89. return false;
  90. }
  91. std::ostringstream ostr;
  92. ostr << name;
  93. if (this->SubmitIndex > 0) {
  94. ostr << "_" << this->SubmitIndex;
  95. }
  96. ostr << ".xml";
  97. if (this->CTest->GetCurrentTag().empty()) {
  98. cmCTestLog(this->CTest, ERROR_MESSAGE,
  99. "Current Tag empty, this may mean NightlyStartTime / "
  100. "CTEST_NIGHTLY_START_TIME was not set correctly. Or "
  101. "maybe you forgot to call ctest_start() before calling "
  102. "ctest_configure()."
  103. << std::endl);
  104. cmSystemTools::SetFatalErrorOccured();
  105. return false;
  106. }
  107. if (!this->CTest->OpenOutputFile(this->CTest->GetCurrentTag(), ostr.str(),
  108. xofs, true)) {
  109. cmCTestLog(this->CTest, ERROR_MESSAGE,
  110. "Cannot create resulting XML file: " << ostr.str()
  111. << std::endl);
  112. return false;
  113. }
  114. this->CTest->AddSubmitFile(part, ostr.str());
  115. return true;
  116. }
  117. bool cmCTestGenericHandler::StartLogFile(const char* name,
  118. cmGeneratedFileStream& xofs)
  119. {
  120. if (!name) {
  121. cmCTestLog(this->CTest, ERROR_MESSAGE,
  122. "Cannot create log file without providing the name"
  123. << std::endl;);
  124. return false;
  125. }
  126. std::ostringstream ostr;
  127. ostr << "Last" << name;
  128. if (this->SubmitIndex > 0) {
  129. ostr << "_" << this->SubmitIndex;
  130. }
  131. if (!this->CTest->GetCurrentTag().empty()) {
  132. ostr << "_" << this->CTest->GetCurrentTag();
  133. }
  134. ostr << ".log";
  135. this->LogFileNames[name] =
  136. cmStrCat(this->CTest->GetBinaryDir(), "/Testing/Temporary/", ostr.str());
  137. if (!this->CTest->OpenOutputFile("Temporary", ostr.str(), xofs)) {
  138. cmCTestLog(this->CTest, ERROR_MESSAGE,
  139. "Cannot create log file: " << ostr.str() << std::endl);
  140. return false;
  141. }
  142. return true;
  143. }