cmParsePHPCoverage.cxx 5.5 KB

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