cmParsePHPCoverage.cxx 5.4 KB

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