cmParsePHPCoverage.cxx 5.5 KB

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