cmParseJacocoCoverage.cxx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "cmStandardIncludes.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "cmSystemTools.h"
  5. #include "cmXMLParser.h"
  6. #include "cmParseJacocoCoverage.h"
  7. #include <cmsys/Directory.hxx>
  8. #include <cmsys/Glob.hxx>
  9. #include <cmsys/FStream.hxx>
  10. class cmParseJacocoCoverage::XMLParser: public cmXMLParser
  11. {
  12. public:
  13. XMLParser(cmCTest* ctest, cmCTestCoverageHandlerContainer& cont)
  14. : CTest(ctest), Coverage(cont)
  15. {
  16. this->PackageName = "";
  17. this->ModuleName = "";
  18. this->FileName = "";
  19. this->CurFileName = "";
  20. this->FilePaths.push_back(this->Coverage.SourceDir);
  21. }
  22. virtual ~XMLParser()
  23. {
  24. }
  25. protected:
  26. virtual void EndElement(const std::string&)
  27. {
  28. }
  29. virtual void StartElement(const std::string& name,
  30. const char** atts)
  31. {
  32. if(name == "package")
  33. {
  34. this->PackageName = atts[1];
  35. std::string FilePath = this->Coverage.SourceDir +
  36. "/" + this->ModuleName + "/src/main/java/" +
  37. this->PackageName;
  38. this->FilePaths.push_back(FilePath);
  39. FilePath = this->Coverage.SourceDir +
  40. "/src/main/java/" + this->PackageName;
  41. this->FilePaths.push_back(FilePath);
  42. }
  43. else if(name == "sourcefile")
  44. {
  45. this->FileName = atts[1];
  46. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Reading file: "
  47. << this->FileName << std::endl);
  48. for(size_t i=0;i < FilePaths.size();i++)
  49. {
  50. std::string finalpath = FilePaths[i] + "/" + this->FileName;
  51. if(cmSystemTools::FileExists(finalpath.c_str()))
  52. {
  53. this->CurFileName = finalpath;
  54. break;
  55. }
  56. }
  57. cmsys::ifstream fin(this->CurFileName.c_str());
  58. if(this->CurFileName == "" || !fin )
  59. {
  60. this->CurFileName = this->Coverage.BinaryDir + "/" +
  61. this->FileName;
  62. fin.open(this->CurFileName.c_str());
  63. if (!fin)
  64. {
  65. cmCTestLog(this->CTest, ERROR_MESSAGE,
  66. "Jacoco Coverage: Error opening " << this->CurFileName
  67. << std::endl);
  68. this->Coverage.Error++;
  69. }
  70. }
  71. std::string line;
  72. FileLinesType& curFileLines =
  73. this->Coverage.TotalCoverage[this->CurFileName];
  74. curFileLines.push_back(-1);
  75. while(cmSystemTools::GetLineFromStream(fin, line))
  76. {
  77. curFileLines.push_back(-1);
  78. }
  79. }
  80. else if(name == "report")
  81. {
  82. this->ModuleName=atts[1];
  83. }
  84. else if(name == "line")
  85. {
  86. int tagCount = 0;
  87. int nr = -1;
  88. int ci = -1;
  89. while(true)
  90. {
  91. if(strcmp(atts[tagCount],"ci") == 0)
  92. {
  93. ci = atoi(atts[tagCount+1]);
  94. }
  95. else if (strcmp(atts[tagCount],"nr") == 0)
  96. {
  97. nr = atoi(atts[tagCount+1]);
  98. }
  99. if (ci > -1 && nr > 0)
  100. {
  101. FileLinesType& curFileLines=
  102. this->Coverage.TotalCoverage[this->CurFileName];
  103. if(!curFileLines.empty())
  104. {
  105. curFileLines[nr-1] = ci;
  106. }
  107. break;
  108. }
  109. ++tagCount;
  110. }
  111. }
  112. }
  113. private:
  114. std::string PackageName;
  115. std::string FileName;
  116. std::string ModuleName;
  117. std::string CurFileName;
  118. std::vector<std::string> FilePaths;
  119. typedef cmCTestCoverageHandlerContainer::SingleFileCoverageVector
  120. FileLinesType;
  121. cmCTest* CTest;
  122. cmCTestCoverageHandlerContainer& Coverage;
  123. };
  124. cmParseJacocoCoverage::cmParseJacocoCoverage(
  125. cmCTestCoverageHandlerContainer& cont,
  126. cmCTest* ctest)
  127. :Coverage(cont), CTest(ctest)
  128. {
  129. }
  130. bool cmParseJacocoCoverage::LoadCoverageData(
  131. const std::vector<std::string> files)
  132. {
  133. // load all the jacoco.xml files in the source directory
  134. cmsys::Directory dir;
  135. size_t i;
  136. std::string path;
  137. size_t numf = files.size();
  138. for (i = 0; i < numf; i++)
  139. {
  140. path = files[i];
  141. cmCTestLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
  142. "Reading XML File " << path << std::endl);
  143. if(cmSystemTools::GetFilenameLastExtension(path) == ".xml")
  144. {
  145. if(!this->ReadJacocoXML(path.c_str()))
  146. {
  147. return false;
  148. }
  149. }
  150. }
  151. return true;
  152. }
  153. bool cmParseJacocoCoverage::ReadJacocoXML(const char* file)
  154. {
  155. cmParseJacocoCoverage::XMLParser
  156. parser(this->CTest, this->Coverage);
  157. parser.ParseFile(file);
  158. return true;
  159. }