teamcity_messages.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* Copyright 2011 JetBrains s.r.o.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. *
  15. * $Revision: 88625 $
  16. */
  17. #include <stdlib.h>
  18. #include <sstream>
  19. #include "teamcity_messages.h"
  20. using namespace std;
  21. namespace JetBrains {
  22. std::string getFlowIdFromEnvironment() {
  23. const char *flowId = getenv("TEAMCITY_PROCESS_FLOW_ID");
  24. return flowId == NULL ? "" : flowId;
  25. }
  26. bool underTeamcity() {
  27. return getenv("TEAMCITY_PROJECT_NAME") != NULL;
  28. }
  29. TeamcityMessages::TeamcityMessages()
  30. : m_out(&cout)
  31. {}
  32. void TeamcityMessages::setOutput(ostream &out) {
  33. m_out = &out;
  34. }
  35. string TeamcityMessages::escape(string s) {
  36. string result;
  37. for (size_t i = 0; i < s.length(); i++) {
  38. char c = s[i];
  39. switch (c) {
  40. case '\n': result.append("|n"); break;
  41. case '\r': result.append("|r"); break;
  42. case '\'': result.append("|'"); break;
  43. case '|': result.append("||"); break;
  44. case ']': result.append("|]"); break;
  45. default: result.append(&c, 1);
  46. }
  47. }
  48. return result;
  49. }
  50. void TeamcityMessages::openMsg(const string &name) {
  51. // endl for http://jetbrains.net/tracker/issue/TW-4412
  52. *m_out << endl << "##teamcity[" << name;
  53. }
  54. void TeamcityMessages::closeMsg() {
  55. *m_out << "]";
  56. // endl for http://jetbrains.net/tracker/issue/TW-4412
  57. *m_out << endl;
  58. m_out->flush();
  59. }
  60. void TeamcityMessages::writeProperty(string name, string value) {
  61. *m_out << " " << name << "='" << escape(value) << "'";
  62. }
  63. void TeamcityMessages::suiteStarted(string name, string flowid) {
  64. openMsg("testSuiteStarted");
  65. writeProperty("name", name);
  66. if(flowid.length() > 0) {
  67. writeProperty("flowId", flowid);
  68. }
  69. closeMsg();
  70. }
  71. void TeamcityMessages::suiteFinished(string name, string flowid) {
  72. openMsg("testSuiteFinished");
  73. writeProperty("name", name);
  74. if(flowid.length() > 0) {
  75. writeProperty("flowId", flowid);
  76. }
  77. closeMsg();
  78. }
  79. void TeamcityMessages::testStarted(string name, string flowid) {
  80. openMsg("testStarted");
  81. writeProperty("name", name);
  82. if(flowid.length() > 0) {
  83. writeProperty("flowId", flowid);
  84. }
  85. closeMsg();
  86. }
  87. void TeamcityMessages::testFinished(string name, int durationMs, string flowid) {
  88. openMsg("testFinished");
  89. writeProperty("name", name);
  90. if(flowid.length() > 0) {
  91. writeProperty("flowId", flowid);
  92. }
  93. if(durationMs >= 0) {
  94. stringstream out;
  95. out << durationMs;
  96. writeProperty("duration", out.str());
  97. }
  98. closeMsg();
  99. }
  100. void TeamcityMessages::testFailed(string name, string message, string details, string flowid) {
  101. openMsg("testFailed");
  102. writeProperty("name", name);
  103. writeProperty("message", message);
  104. writeProperty("details", details);
  105. if(flowid.length() > 0) {
  106. writeProperty("flowId", flowid);
  107. }
  108. closeMsg();
  109. }
  110. void TeamcityMessages::testIgnored(std::string name, std::string message, string flowid) {
  111. openMsg("testIgnored");
  112. writeProperty("name", name);
  113. writeProperty("message", message);
  114. if(flowid.length() > 0) {
  115. writeProperty("flowId", flowid);
  116. }
  117. closeMsg();
  118. }
  119. void TeamcityMessages::messageError(const std::string& text)
  120. {
  121. openMsg("message");
  122. writeProperty("text", text);
  123. writeProperty("status", "ERROR");
  124. closeMsg();
  125. }
  126. void TeamcityMessages::messageWarning(const std::string& text)
  127. {
  128. openMsg("message");
  129. writeProperty("text", text);
  130. writeProperty("status", "WARNING");
  131. closeMsg();
  132. }
  133. void TeamcityMessages::messageNormal(const std::string& text)
  134. {
  135. openMsg("message");
  136. writeProperty("text", text);
  137. writeProperty("status", "NORMAL");
  138. closeMsg();
  139. }
  140. }