cmParsePHPCoverage.cxx 5.5 KB

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