cmParsePHPCoverage.cxx 5.5 KB

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