2016-04-29 16:40:20 +03:00
|
|
|
#include "cmParseJacocoCoverage.h"
|
|
|
|
|
2014-05-21 23:19:35 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "cmSystemTools.h"
|
|
|
|
#include "cmXMLParser.h"
|
|
|
|
#include <cmsys/Directory.hxx>
|
|
|
|
#include <cmsys/Glob.hxx>
|
|
|
|
#include <cmsys/FStream.hxx>
|
|
|
|
|
|
|
|
|
|
|
|
class cmParseJacocoCoverage::XMLParser: public cmXMLParser
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
XMLParser(cmCTest* ctest, cmCTestCoverageHandlerContainer& cont)
|
|
|
|
: CTest(ctest), Coverage(cont)
|
|
|
|
{
|
2015-08-28 20:35:47 +03:00
|
|
|
this->FilePath = "";
|
|
|
|
this->PackagePath = "";
|
2014-05-21 23:19:35 +04:00
|
|
|
this->PackageName = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~XMLParser()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual void EndElement(const std::string&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void StartElement(const std::string& name,
|
|
|
|
const char** atts)
|
|
|
|
{
|
|
|
|
if(name == "package")
|
|
|
|
{
|
|
|
|
this->PackageName = atts[1];
|
2015-08-28 20:35:47 +03:00
|
|
|
this->PackagePath = "";
|
2014-05-21 23:19:35 +04:00
|
|
|
}
|
|
|
|
else if(name == "sourcefile")
|
|
|
|
{
|
2015-08-28 20:35:47 +03:00
|
|
|
std::string fileName = atts[1];
|
|
|
|
|
|
|
|
if (this->PackagePath == "")
|
2014-05-21 23:19:35 +04:00
|
|
|
{
|
2015-08-28 20:35:47 +03:00
|
|
|
if(!this->FindPackagePath(fileName))
|
2014-05-21 23:19:35 +04:00
|
|
|
{
|
2015-08-28 20:35:47 +03:00
|
|
|
cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot find file: "
|
|
|
|
<< this->PackageName << "/" << fileName << std::endl);
|
|
|
|
this->Coverage.Error++;
|
|
|
|
return;
|
2014-05-21 23:19:35 +04:00
|
|
|
}
|
|
|
|
}
|
2015-08-28 20:35:47 +03:00
|
|
|
|
|
|
|
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
|
|
|
|
"Reading file: " << fileName << std::endl,
|
|
|
|
this->Coverage.Quiet);
|
|
|
|
|
|
|
|
this->FilePath = this->PackagePath + "/" + fileName;
|
|
|
|
cmsys::ifstream fin(this->FilePath.c_str());
|
|
|
|
if (!fin)
|
2014-05-21 23:19:35 +04:00
|
|
|
{
|
2015-08-28 20:35:47 +03:00
|
|
|
cmCTestLog(this->CTest, ERROR_MESSAGE,
|
|
|
|
"Jacoco Coverage: Error opening " << this->FilePath
|
|
|
|
<< std::endl);
|
|
|
|
}
|
|
|
|
std::string line;
|
|
|
|
FileLinesType& curFileLines =
|
|
|
|
this->Coverage.TotalCoverage[this->FilePath];
|
|
|
|
if(fin)
|
|
|
|
{
|
|
|
|
curFileLines.push_back(-1);
|
|
|
|
}
|
|
|
|
while(cmSystemTools::GetLineFromStream(fin, line))
|
|
|
|
{
|
|
|
|
curFileLines.push_back(-1);
|
2014-05-21 23:19:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(name == "line")
|
|
|
|
{
|
|
|
|
int tagCount = 0;
|
|
|
|
int nr = -1;
|
|
|
|
int ci = -1;
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
if(strcmp(atts[tagCount],"ci") == 0)
|
|
|
|
{
|
|
|
|
ci = atoi(atts[tagCount+1]);
|
|
|
|
}
|
|
|
|
else if (strcmp(atts[tagCount],"nr") == 0)
|
|
|
|
{
|
|
|
|
nr = atoi(atts[tagCount+1]);
|
|
|
|
}
|
|
|
|
if (ci > -1 && nr > 0)
|
|
|
|
{
|
|
|
|
FileLinesType& curFileLines=
|
2015-08-28 20:35:47 +03:00
|
|
|
this->Coverage.TotalCoverage[this->FilePath];
|
2015-01-15 02:06:11 +03:00
|
|
|
if(!curFileLines.empty())
|
2014-05-21 23:19:35 +04:00
|
|
|
{
|
|
|
|
curFileLines[nr-1] = ci;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++tagCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-28 20:35:47 +03:00
|
|
|
virtual bool FindPackagePath(const std::string fileName)
|
|
|
|
{
|
|
|
|
// Search for the source file in the source directory.
|
|
|
|
if (this->PackagePathFound(fileName, this->Coverage.SourceDir))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not found there, check the binary directory.
|
|
|
|
if (this->PackagePathFound(fileName, this->Coverage.BinaryDir))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool PackagePathFound(const std::string fileName,
|
|
|
|
const std::string baseDir)
|
|
|
|
{
|
|
|
|
// Search for the file in the baseDir and its subdirectories.
|
|
|
|
std::string packageGlob = baseDir;
|
|
|
|
packageGlob += "/";
|
|
|
|
packageGlob += fileName;
|
|
|
|
cmsys::Glob gl;
|
|
|
|
gl.RecurseOn();
|
|
|
|
gl.RecurseThroughSymlinksOn();
|
|
|
|
gl.FindFiles(packageGlob);
|
|
|
|
std::vector<std::string> const& files = gl.GetFiles();
|
|
|
|
if (files.size() == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if any of the locations found match our package.
|
|
|
|
for(std::vector<std::string>::const_iterator fi = files.begin();
|
|
|
|
fi != files.end(); ++fi)
|
|
|
|
{
|
|
|
|
std::string dir = cmsys::SystemTools::GetParentDirectory(*fi);
|
|
|
|
if (cmsys::SystemTools::StringEndsWith(dir, this->PackageName.c_str()))
|
|
|
|
{
|
|
|
|
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
|
|
|
|
"Found package directory for " << fileName <<
|
|
|
|
": " << dir << std::endl,
|
|
|
|
this->Coverage.Quiet);
|
|
|
|
this->PackagePath = dir;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-21 23:19:35 +04:00
|
|
|
private:
|
2015-08-28 20:35:47 +03:00
|
|
|
std::string FilePath;
|
|
|
|
std::string PackagePath;
|
2014-05-21 23:19:35 +04:00
|
|
|
std::string PackageName;
|
|
|
|
typedef cmCTestCoverageHandlerContainer::SingleFileCoverageVector
|
|
|
|
FileLinesType;
|
|
|
|
cmCTest* CTest;
|
|
|
|
cmCTestCoverageHandlerContainer& Coverage;
|
|
|
|
};
|
|
|
|
|
|
|
|
cmParseJacocoCoverage::cmParseJacocoCoverage(
|
|
|
|
cmCTestCoverageHandlerContainer& cont,
|
|
|
|
cmCTest* ctest)
|
|
|
|
:Coverage(cont), CTest(ctest)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmParseJacocoCoverage::LoadCoverageData(
|
|
|
|
const std::vector<std::string> files)
|
|
|
|
{
|
|
|
|
// load all the jacoco.xml files in the source directory
|
|
|
|
cmsys::Directory dir;
|
|
|
|
size_t i;
|
|
|
|
std::string path;
|
|
|
|
size_t numf = files.size();
|
|
|
|
for (i = 0; i < numf; i++)
|
|
|
|
{
|
|
|
|
path = files[i];
|
|
|
|
|
2015-02-17 23:57:26 +03:00
|
|
|
cmCTestOptionalLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
|
|
|
|
"Reading XML File " << path << std::endl, this->Coverage.Quiet);
|
2014-05-21 23:19:35 +04:00
|
|
|
if(cmSystemTools::GetFilenameLastExtension(path) == ".xml")
|
|
|
|
{
|
|
|
|
if(!this->ReadJacocoXML(path.c_str()))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmParseJacocoCoverage::ReadJacocoXML(const char* file)
|
|
|
|
{
|
|
|
|
cmParseJacocoCoverage::XMLParser
|
|
|
|
parser(this->CTest, this->Coverage);
|
|
|
|
parser.ParseFile(file);
|
|
|
|
return true;
|
|
|
|
}
|