cmParsePHPCoverage.cxx 5.5 KB

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