2016-04-29 16:40:20 +03:00
|
|
|
#include "cmParseJacocoCoverage.h"
|
|
|
|
|
2014-05-21 23:19:35 +04:00
|
|
|
#include "cmSystemTools.h"
|
|
|
|
#include "cmXMLParser.h"
|
|
|
|
#include <cmsys/Directory.hxx>
|
|
|
|
#include <cmsys/FStream.hxx>
|
2016-04-29 17:53:13 +03:00
|
|
|
#include <cmsys/Glob.hxx>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-05-21 23:19:35 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
class cmParseJacocoCoverage::XMLParser : public cmXMLParser
|
2014-05-21 23:19:35 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
public:
|
|
|
|
XMLParser(cmCTest* ctest, cmCTestCoverageHandlerContainer& cont)
|
|
|
|
: CTest(ctest)
|
|
|
|
, Coverage(cont)
|
|
|
|
{
|
|
|
|
this->FilePath = "";
|
|
|
|
this->PackagePath = "";
|
|
|
|
this->PackageName = "";
|
|
|
|
}
|
2014-05-21 23:19:35 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
virtual ~XMLParser() {}
|
2014-05-21 23:19:35 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
protected:
|
|
|
|
virtual void EndElement(const std::string&) {}
|
2014-05-21 23:19:35 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
virtual void StartElement(const std::string& name, const char** atts)
|
|
|
|
{
|
|
|
|
if (name == "package") {
|
|
|
|
this->PackageName = atts[1];
|
|
|
|
this->PackagePath = "";
|
|
|
|
} else if (name == "sourcefile") {
|
|
|
|
std::string fileName = atts[1];
|
|
|
|
|
|
|
|
if (this->PackagePath == "") {
|
|
|
|
if (!this->FindPackagePath(fileName)) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
|
|
|
|
"Reading file: " << fileName << std::endl,
|
|
|
|
this->Coverage.Quiet);
|
2015-08-28 20:35:47 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
this->FilePath = this->PackagePath + "/" + fileName;
|
|
|
|
cmsys::ifstream fin(this->FilePath.c_str());
|
|
|
|
if (!fin) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
} 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]);
|
2014-05-21 23:19:35 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
if (ci > -1 && nr > 0) {
|
|
|
|
FileLinesType& curFileLines =
|
|
|
|
this->Coverage.TotalCoverage[this->FilePath];
|
|
|
|
if (!curFileLines.empty()) {
|
|
|
|
curFileLines[nr - 1] = ci;
|
2014-05-21 23:19:35 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
break;
|
2014-05-21 23:19:35 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
++tagCount;
|
2014-05-21 23:19:35 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
}
|
2014-05-21 23:19:35 +04:00
|
|
|
|
2016-05-26 22:58:51 +03:00
|
|
|
virtual bool FindPackagePath(std::string const& fileName)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
// Search for the source file in the source directory.
|
|
|
|
if (this->PackagePathFound(fileName, this->Coverage.SourceDir)) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-28 20:35:47 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
// If not found there, check the binary directory.
|
|
|
|
if (this->PackagePathFound(fileName, this->Coverage.BinaryDir)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-28 20:35:47 +03:00
|
|
|
|
2016-05-26 22:58:51 +03:00
|
|
|
virtual bool PackagePathFound(std::string const& fileName,
|
|
|
|
std::string const& baseDir)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
// 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) {
|
2015-08-28 20:35:47 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2015-08-28 20:35:47 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-28 20:35:47 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
private:
|
|
|
|
std::string FilePath;
|
|
|
|
std::string PackagePath;
|
|
|
|
std::string PackageName;
|
|
|
|
typedef cmCTestCoverageHandlerContainer::SingleFileCoverageVector
|
|
|
|
FileLinesType;
|
|
|
|
cmCTest* CTest;
|
|
|
|
cmCTestCoverageHandlerContainer& Coverage;
|
2014-05-21 23:19:35 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
cmParseJacocoCoverage::cmParseJacocoCoverage(
|
2016-05-16 17:34:04 +03:00
|
|
|
cmCTestCoverageHandlerContainer& cont, cmCTest* ctest)
|
|
|
|
: Coverage(cont)
|
|
|
|
, CTest(ctest)
|
|
|
|
{
|
|
|
|
}
|
2014-05-21 23:19:35 +04:00
|
|
|
|
|
|
|
bool cmParseJacocoCoverage::LoadCoverageData(
|
2016-05-26 22:58:51 +03:00
|
|
|
std::vector<std::string> const& files)
|
2014-05-21 23:19:35 +04:00
|
|
|
{
|
|
|
|
// load all the jacoco.xml files in the source directory
|
|
|
|
cmsys::Directory dir;
|
|
|
|
size_t i;
|
|
|
|
std::string path;
|
|
|
|
size_t numf = files.size();
|
2016-05-16 17:34:04 +03:00
|
|
|
for (i = 0; i < numf; i++) {
|
2014-05-21 23:19:35 +04:00
|
|
|
path = files[i];
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
|
|
|
|
"Reading XML File " << path << std::endl,
|
|
|
|
this->Coverage.Quiet);
|
|
|
|
if (cmSystemTools::GetFilenameLastExtension(path) == ".xml") {
|
|
|
|
if (!this->ReadJacocoXML(path.c_str())) {
|
2014-05-21 23:19:35 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-05-21 23:19:35 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmParseJacocoCoverage::ReadJacocoXML(const char* file)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
cmParseJacocoCoverage::XMLParser parser(this->CTest, this->Coverage);
|
2014-05-21 23:19:35 +04:00
|
|
|
parser.ParseFile(file);
|
|
|
|
return true;
|
|
|
|
}
|