cmParsePHPCoverage.cxx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "cmParsePHPCoverage.h"
  2. #include "cmSystemTools.h"
  3. #include <cmsys/Directory.hxx>
  4. #include <cmsys/FStream.hxx>
  5. /*
  6. To setup coverage for php.
  7. - edit php.ini to add auto prepend and append php files from phpunit
  8. auto_prepend_file =
  9. auto_append_file =
  10. - run the tests
  11. - run this program on all the files in c:/tmp
  12. */
  13. cmParsePHPCoverage::cmParsePHPCoverage(cmCTestCoverageHandlerContainer& cont,
  14. cmCTest* ctest)
  15. : Coverage(cont)
  16. , CTest(ctest)
  17. {
  18. }
  19. bool cmParsePHPCoverage::ReadUntil(std::istream& in, char until)
  20. {
  21. char c = 0;
  22. while (in.get(c) && c != until) {
  23. }
  24. return c == until;
  25. }
  26. bool cmParsePHPCoverage::ReadCoverageArray(std::istream& in,
  27. std::string const& fileName)
  28. {
  29. cmCTestCoverageHandlerContainer::SingleFileCoverageVector& coverageVector =
  30. this->Coverage.TotalCoverage[fileName];
  31. char c;
  32. char buf[4];
  33. in.read(buf, 3);
  34. buf[3] = 0;
  35. if (strcmp(buf, ";a:") != 0) {
  36. cmCTestLog(this->CTest, ERROR_MESSAGE,
  37. "failed to read start of coverage array, found : " << buf
  38. << "\n");
  39. return false;
  40. }
  41. int size = 0;
  42. if (!this->ReadInt(in, size)) {
  43. cmCTestLog(this->CTest, ERROR_MESSAGE, "failed to read size ");
  44. return false;
  45. }
  46. if (!in.get(c) && c == '{') {
  47. cmCTestLog(this->CTest, ERROR_MESSAGE, "failed to read open {\n");
  48. return false;
  49. }
  50. for (int i = 0; i < size; i++) {
  51. this->ReadUntil(in, ':');
  52. int line = 0;
  53. this->ReadInt(in, line);
  54. // ok xdebug may have a bug here
  55. // it seems to be 1 based but often times
  56. // seems to have a 0'th line.
  57. line--;
  58. if (line < 0) {
  59. line = 0;
  60. }
  61. this->ReadUntil(in, ':');
  62. int value = 0;
  63. this->ReadInt(in, value);
  64. // make sure the vector is the right size and is
  65. // initialized with -1 for each line
  66. while (coverageVector.size() <= static_cast<size_t>(line)) {
  67. coverageVector.push_back(-1);
  68. }
  69. // if value is less than 0, set it to zero
  70. // TODO figure out the difference between
  71. // -1 and -2 in xdebug coverage?? For now
  72. // assume less than 0 is just not covered
  73. // CDash expects -1 for non executable code (like comments)
  74. // and 0 for uncovered code, and a positive value
  75. // for number of times a line was executed
  76. if (value < 0) {
  77. value = 0;
  78. }
  79. // if unset then set it to value
  80. if (coverageVector[line] == -1) {
  81. coverageVector[line] = value;
  82. }
  83. // otherwise increment by value
  84. else {
  85. coverageVector[line] += value;
  86. }
  87. }
  88. return true;
  89. }
  90. bool cmParsePHPCoverage::ReadInt(std::istream& in, int& v)
  91. {
  92. std::string s;
  93. char c = 0;
  94. while (in.get(c) && c != ':' && c != ';') {
  95. s += c;
  96. }
  97. v = atoi(s.c_str());
  98. return true;
  99. }
  100. bool cmParsePHPCoverage::ReadArraySize(std::istream& in, int& size)
  101. {
  102. char c = 0;
  103. in.get(c);
  104. if (c != 'a') {
  105. return false;
  106. }
  107. if (in.get(c) && c == ':') {
  108. if (this->ReadInt(in, size)) {
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. bool cmParsePHPCoverage::ReadFileInformation(std::istream& in)
  115. {
  116. char buf[4];
  117. in.read(buf, 2);
  118. buf[2] = 0;
  119. if (strcmp(buf, "s:") != 0) {
  120. cmCTestLog(this->CTest, ERROR_MESSAGE,
  121. "failed to read start of file info found: [" << buf << "]\n");
  122. return false;
  123. }
  124. char c;
  125. int size = 0;
  126. if (this->ReadInt(in, size)) {
  127. size++; // add one for null termination
  128. char* s = new char[size + 1];
  129. // read open quote
  130. if (in.get(c) && c != '"') {
  131. delete[] s;
  132. return false;
  133. }
  134. // read the string data
  135. in.read(s, size - 1);
  136. s[size - 1] = 0;
  137. std::string fileName = s;
  138. delete[] s;
  139. // read close quote
  140. if (in.get(c) && c != '"') {
  141. cmCTestLog(this->CTest, ERROR_MESSAGE, "failed to read close quote\n"
  142. << "read [" << c << "]\n");
  143. return false;
  144. }
  145. if (!this->ReadCoverageArray(in, fileName)) {
  146. cmCTestLog(this->CTest, ERROR_MESSAGE,
  147. "failed to read coverage array for file: " << fileName
  148. << "\n");
  149. return false;
  150. }
  151. return true;
  152. }
  153. return false;
  154. }
  155. bool cmParsePHPCoverage::ReadPHPData(const char* file)
  156. {
  157. cmsys::ifstream in(file);
  158. if (!in) {
  159. return false;
  160. }
  161. int size = 0;
  162. this->ReadArraySize(in, size);
  163. char c = 0;
  164. in.get(c);
  165. if (c != '{') {
  166. cmCTestLog(this->CTest, ERROR_MESSAGE, "failed to read open array\n");
  167. return false;
  168. }
  169. for (int i = 0; i < size; i++) {
  170. if (!this->ReadFileInformation(in)) {
  171. cmCTestLog(this->CTest, ERROR_MESSAGE, "Failed to read file #" << i
  172. << "\n");
  173. return false;
  174. }
  175. in.get(c);
  176. if (c != '}') {
  177. cmCTestLog(this->CTest, ERROR_MESSAGE, "failed to read close array\n");
  178. return false;
  179. }
  180. }
  181. return true;
  182. }
  183. bool cmParsePHPCoverage::ReadPHPCoverageDirectory(const char* d)
  184. {
  185. cmsys::Directory dir;
  186. if (!dir.Load(d)) {
  187. return false;
  188. }
  189. size_t numf;
  190. unsigned int i;
  191. numf = dir.GetNumberOfFiles();
  192. for (i = 0; i < numf; i++) {
  193. std::string file = dir.GetFile(i);
  194. if (file != "." && file != ".." && !cmSystemTools::FileIsDirectory(file)) {
  195. std::string path = d;
  196. path += "/";
  197. path += file;
  198. if (!this->ReadPHPData(path.c_str())) {
  199. return false;
  200. }
  201. }
  202. }
  203. return true;
  204. }