summaryrefslogtreecommitdiffstats
path: root/Source/CTest/cmParsePHPCoverage.cxx
blob: d8bb31bad5a576b5c7f2811884693eba736a49ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "cmParsePHPCoverage.h"

#include "cmCTest.h"
#include "cmCTestCoverageHandler.h"
#include "cmSystemTools.h"

#include <cmsys/Directory.hxx>
#include <cmsys/FStream.hxx>
#include <stdlib.h>
#include <string.h>

/*
  To setup coverage for php.

  - edit php.ini to add auto prepend and append php files from phpunit
  auto_prepend_file =
  auto_append_file =
  - run the tests
  - run this program on all the files in c:/tmp

*/

cmParsePHPCoverage::cmParsePHPCoverage(cmCTestCoverageHandlerContainer& cont,
                                       cmCTest* ctest)
  : Coverage(cont)
  , CTest(ctest)
{
}

bool cmParsePHPCoverage::ReadUntil(std::istream& in, char until)
{
  char c = 0;
  while (in.get(c) && c != until) {
  }
  return c == until;
}
bool cmParsePHPCoverage::ReadCoverageArray(std::istream& in,
                                           std::string const& fileName)
{
  cmCTestCoverageHandlerContainer::SingleFileCoverageVector& coverageVector =
    this->Coverage.TotalCoverage[fileName];

  char c;
  char buf[4];
  in.read(buf, 3);
  buf[3] = 0;
  if (strcmp(buf, ";a:") != 0) {
    cmCTestLog(this->CTest, ERROR_MESSAGE,
               "failed to read start of coverage array, found : " << buf
                                                                  << "\n");
    return false;
  }
  int size = 0;
  if (!this->ReadInt(in, size)) {
    cmCTestLog(this->CTest, ERROR_MESSAGE, "failed to read size ");
    return false;
  }
  if (!in.get(c) && c == '{') {
    cmCTestLog(this->CTest, ERROR_MESSAGE, "failed to read open {\n");
    return false;
  }
  for (int i = 0; i < size; i++) {
    this->ReadUntil(in, ':');
    int line = 0;
    this->ReadInt(in, line);
    // ok xdebug may have a bug here
    // it seems to be 1 based but often times
    // seems to have a 0'th line.
    line--;
    if (line < 0) {
      line = 0;
    }
    this->ReadUntil(in, ':');
    int value = 0;
    this->ReadInt(in, value);
    // make sure the vector is the right size and is
    // initialized with -1 for each line
    while (coverageVector.size() <= static_cast<size_t>(line)) {
      coverageVector.push_back(-1);
    }
    // if value is less than 0, set it to zero
    // TODO figure out the difference between
    // -1 and -2 in xdebug coverage??  For now
    // assume less than 0 is just not covered
    // CDash expects -1 for non executable code (like comments)
    // and 0 for uncovered code, and a positive value
    // for number of times a line was executed
    if (value < 0) {
      value = 0;
    }
    // if unset then set it to value
    if (coverageVector[line] == -1) {
      coverageVector[line] = value;
    }
    // otherwise increment by value
    else {
      coverageVector[line] += value;
    }
  }
  return true;
}

bool cmParsePHPCoverage::ReadInt(std::istream& in, int& v)
{
  std::string s;
  char c = 0;
  while (in.get(c) && c != ':' && c != ';') {
    s += c;
  }
  v = atoi(s.c_str());
  return true;
}

bool cmParsePHPCoverage::ReadArraySize(std::istream& in, int& size)
{
  char c = 0;
  in.get(c);
  if (c != 'a') {
    return false;
  }
  if (in.get(c) && c == ':') {
    if (this->ReadInt(in, size)) {
      return true;
    }
  }
  return false;
}

bool cmParsePHPCoverage::ReadFileInformation(std::istream& in)
{
  char buf[4];
  in.read(buf, 2);
  buf[2] = 0;
  if (strcmp(buf, "s:") != 0) {
    cmCTestLog(this->CTest, ERROR_MESSAGE,
               "failed to read start of file info found: [" << buf << "]\n");
    return false;
  }
  char c;
  int size = 0;
  if (this->ReadInt(in, size)) {
    size++; // add one for null termination
    char* s = new char[size + 1];
    // read open quote
    if (in.get(c) && c != '"') {
      delete[] s;
      return false;
    }
    // read the string data
    in.read(s, size - 1);
    s[size - 1] = 0;
    std::string fileName = s;
    delete[] s;
    // read close quote
    if (in.get(c) && c != '"') {
      cmCTestLog(this->CTest, ERROR_MESSAGE, "failed to read close quote\n"
                   << "read [" << c << "]\n");
      return false;
    }
    if (!this->ReadCoverageArray(in, fileName)) {
      cmCTestLog(this->CTest, ERROR_MESSAGE,
                 "failed to read coverage array for file: " << fileName
                                                            << "\n");
      return false;
    }
    return true;
  }
  return false;
}

bool cmParsePHPCoverage::ReadPHPData(const char* file)
{
  cmsys::ifstream in(file);
  if (!in) {
    return false;
  }
  int size = 0;
  this->ReadArraySize(in, size);
  char c = 0;
  in.get(c);
  if (c != '{') {
    cmCTestLog(this->CTest, ERROR_MESSAGE, "failed to read open array\n");
    return false;
  }
  for (int i = 0; i < size; i++) {
    if (!this->ReadFileInformation(in)) {
      cmCTestLog(this->CTest, ERROR_MESSAGE, "Failed to read file #" << i
                                                                     << "\n");
      return false;
    }
    in.get(c);
    if (c != '}') {
      cmCTestLog(this->CTest, ERROR_MESSAGE, "failed to read close array\n");
      return false;
    }
  }
  return true;
}

bool cmParsePHPCoverage::ReadPHPCoverageDirectory(const char* d)
{
  cmsys::Directory dir;
  if (!dir.Load(d)) {
    return false;
  }
  size_t numf;
  unsigned int i;
  numf = dir.GetNumberOfFiles();
  for (i = 0; i < numf; i++) {
    std::string file = dir.GetFile(i);
    if (file != "." && file != ".." && !cmSystemTools::FileIsDirectory(file)) {
      std::string path = d;
      path += "/";
      path += file;
      if (!this->ReadPHPData(path.c_str())) {
        return false;
      }
    }
  }
  return true;
}