2010-05-25 17:23:25 +04:00
|
|
|
#include "cmParsePHPCoverage.h"
|
2016-04-29 16:40:20 +03:00
|
|
|
|
|
|
|
#include "cmSystemTools.h"
|
2010-05-25 17:23:25 +04:00
|
|
|
#include <cmsys/Directory.hxx>
|
2014-01-04 09:47:13 +04:00
|
|
|
#include <cmsys/FStream.hxx>
|
2010-05-25 17:23:25 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
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,
|
2016-05-16 17:34:04 +03:00
|
|
|
cmCTest* ctest)
|
|
|
|
: Coverage(cont)
|
|
|
|
, CTest(ctest)
|
2010-05-25 17:23:25 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-01-04 09:47:13 +04:00
|
|
|
bool cmParsePHPCoverage::ReadUntil(std::istream& in, char until)
|
2010-05-25 17:23:25 +04:00
|
|
|
{
|
|
|
|
char c = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
while (in.get(c) && c != until) {
|
|
|
|
}
|
|
|
|
if (c != until) {
|
2010-05-25 17:23:25 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-05-25 17:23:25 +04:00
|
|
|
return true;
|
|
|
|
}
|
2014-01-04 09:47:13 +04:00
|
|
|
bool cmParsePHPCoverage::ReadCoverageArray(std::istream& in,
|
2014-02-10 09:21:34 +04:00
|
|
|
std::string const& fileName)
|
2010-05-25 17:23:25 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
cmCTestCoverageHandlerContainer::SingleFileCoverageVector& coverageVector =
|
|
|
|
this->Coverage.TotalCoverage[fileName];
|
2010-05-25 17:23:25 +04:00
|
|
|
|
|
|
|
char c;
|
|
|
|
char buf[4];
|
|
|
|
in.read(buf, 3);
|
|
|
|
buf[3] = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (strcmp(buf, ";a:") != 0) {
|
2010-05-25 17:23:25 +04:00
|
|
|
cmCTestLog(this->CTest, ERROR_MESSAGE,
|
2016-05-16 17:34:04 +03:00
|
|
|
"failed to read start of coverage array, found : " << buf
|
|
|
|
<< "\n");
|
2010-05-25 17:23:25 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-05-25 17:23:25 +04:00
|
|
|
int size = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->ReadInt(in, size)) {
|
|
|
|
cmCTestLog(this->CTest, ERROR_MESSAGE, "failed to read size ");
|
2010-05-25 17:23:25 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!in.get(c) && c == '{') {
|
|
|
|
cmCTestLog(this->CTest, ERROR_MESSAGE, "failed to read open {\n");
|
2010-05-25 17:23:25 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
for (int i = 0; i < size; i++) {
|
2010-05-25 17:23:25 +04:00
|
|
|
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--;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (line < 0) {
|
2010-05-25 17:23:25 +04:00
|
|
|
line = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-05-25 17:23:25 +04:00
|
|
|
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
|
2016-05-16 17:34:04 +03:00
|
|
|
while (coverageVector.size() <= static_cast<size_t>(line)) {
|
2010-05-25 17:23:25 +04:00
|
|
|
coverageVector.push_back(-1);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-05-25 17:23:25 +04:00
|
|
|
// 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
|
2016-05-16 17:34:04 +03:00
|
|
|
if (value < 0) {
|
2010-05-25 17:23:25 +04:00
|
|
|
value = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-05-25 17:23:25 +04:00
|
|
|
// if unset then set it to value
|
2016-05-16 17:34:04 +03:00
|
|
|
if (coverageVector[line] == -1) {
|
2010-05-25 17:23:25 +04:00
|
|
|
coverageVector[line] = value;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-05-25 17:23:25 +04:00
|
|
|
// otherwise increment by value
|
2016-05-16 17:34:04 +03:00
|
|
|
else {
|
2010-05-25 17:23:25 +04:00
|
|
|
coverageVector[line] += value;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-05-25 17:23:25 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-04 09:47:13 +04:00
|
|
|
bool cmParsePHPCoverage::ReadInt(std::istream& in, int& v)
|
2010-05-25 17:23:25 +04:00
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
char c = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
while (in.get(c) && c != ':' && c != ';') {
|
2010-05-25 17:23:25 +04:00
|
|
|
s += c;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-05-25 17:23:25 +04:00
|
|
|
v = atoi(s.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-04 09:47:13 +04:00
|
|
|
bool cmParsePHPCoverage::ReadArraySize(std::istream& in, int& size)
|
2010-05-25 17:23:25 +04:00
|
|
|
{
|
|
|
|
char c = 0;
|
|
|
|
in.get(c);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (c != 'a') {
|
2010-05-25 17:23:25 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (in.get(c) && c == ':') {
|
|
|
|
if (this->ReadInt(in, size)) {
|
2010-05-25 17:23:25 +04:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-05-25 17:23:25 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-04 09:47:13 +04:00
|
|
|
bool cmParsePHPCoverage::ReadFileInformation(std::istream& in)
|
2010-05-25 17:23:25 +04:00
|
|
|
{
|
|
|
|
char buf[4];
|
|
|
|
in.read(buf, 2);
|
|
|
|
buf[2] = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (strcmp(buf, "s:") != 0) {
|
2010-05-25 17:23:25 +04:00
|
|
|
cmCTestLog(this->CTest, ERROR_MESSAGE,
|
|
|
|
"failed to read start of file info found: [" << buf << "]\n");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-05-25 17:23:25 +04:00
|
|
|
char c;
|
|
|
|
int size = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->ReadInt(in, size)) {
|
2010-05-25 17:23:25 +04:00
|
|
|
size++; // add one for null termination
|
2016-05-16 17:34:04 +03:00
|
|
|
char* s = new char[size + 1];
|
2010-05-25 17:23:25 +04:00
|
|
|
// read open quote
|
2016-05-16 17:34:04 +03:00
|
|
|
if (in.get(c) && c != '"') {
|
2011-09-01 23:56:53 +04:00
|
|
|
delete[] s;
|
2010-05-25 17:23:25 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-05-25 17:23:25 +04:00
|
|
|
// read the string data
|
2016-05-16 17:34:04 +03:00
|
|
|
in.read(s, size - 1);
|
|
|
|
s[size - 1] = 0;
|
2014-02-10 09:21:34 +04:00
|
|
|
std::string fileName = s;
|
2016-05-16 17:34:04 +03:00
|
|
|
delete[] s;
|
2010-05-25 17:23:25 +04:00
|
|
|
// read close quote
|
2016-05-16 17:34:04 +03:00
|
|
|
if (in.get(c) && c != '"') {
|
|
|
|
cmCTestLog(this->CTest, ERROR_MESSAGE, "failed to read close quote\n"
|
|
|
|
<< "read [" << c << "]\n");
|
2010-05-25 17:23:25 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!this->ReadCoverageArray(in, fileName)) {
|
2010-05-25 17:23:25 +04:00
|
|
|
cmCTestLog(this->CTest, ERROR_MESSAGE,
|
2016-05-16 17:34:04 +03:00
|
|
|
"failed to read coverage array for file: " << fileName
|
|
|
|
<< "\n");
|
2010-05-25 17:23:25 +04:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return true;
|
|
|
|
}
|
2010-05-25 17:23:25 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmParsePHPCoverage::ReadPHPData(const char* file)
|
|
|
|
{
|
2014-01-04 09:47:13 +04:00
|
|
|
cmsys::ifstream in(file);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!in) {
|
2010-05-25 17:23:25 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-05-25 17:23:25 +04:00
|
|
|
int size = 0;
|
|
|
|
this->ReadArraySize(in, size);
|
|
|
|
char c = 0;
|
|
|
|
in.get(c);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (c != '{') {
|
|
|
|
cmCTestLog(this->CTest, ERROR_MESSAGE, "failed to read open array\n");
|
2010-05-25 17:23:25 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
if (!this->ReadFileInformation(in)) {
|
|
|
|
cmCTestLog(this->CTest, ERROR_MESSAGE, "Failed to read file #" << i
|
|
|
|
<< "\n");
|
2010-05-25 17:23:25 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-05-25 17:23:25 +04:00
|
|
|
in.get(c);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (c != '}') {
|
|
|
|
cmCTestLog(this->CTest, ERROR_MESSAGE, "failed to read close array\n");
|
2010-05-25 17:23:25 +04:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-05-25 17:23:25 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmParsePHPCoverage::ReadPHPCoverageDirectory(const char* d)
|
|
|
|
{
|
|
|
|
cmsys::Directory dir;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!dir.Load(d)) {
|
2010-05-25 17:23:25 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-05-25 17:23:25 +04:00
|
|
|
size_t numf;
|
|
|
|
unsigned int i;
|
|
|
|
numf = dir.GetNumberOfFiles();
|
2016-05-16 17:34:04 +03:00
|
|
|
for (i = 0; i < numf; i++) {
|
2010-05-25 17:23:25 +04:00
|
|
|
std::string file = dir.GetFile(i);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (file != "." && file != ".." && !cmSystemTools::FileIsDirectory(file)) {
|
2010-05-25 17:23:25 +04:00
|
|
|
std::string path = d;
|
|
|
|
path += "/";
|
|
|
|
path += file;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->ReadPHPData(path.c_str())) {
|
2010-05-25 17:23:25 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-05-25 17:23:25 +04:00
|
|
|
return true;
|
|
|
|
}
|