1
0

cmParseJacocoCoverage.cxx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. }
  40. else if(name == "sourcefile")
  41. {
  42. this->FileName = atts[1];
  43. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Reading file: "
  44. << this->FileName << std::endl);
  45. for(size_t i=0;i < FilePaths.size();i++)
  46. {
  47. std::string finalpath = FilePaths[i] + "/" + this->FileName;
  48. if(cmSystemTools::FileExists(finalpath.c_str()))
  49. {
  50. this->CurFileName = finalpath;
  51. break;
  52. }
  53. }
  54. cmsys::ifstream fin(this->CurFileName.c_str());
  55. if(this->CurFileName == "" || !fin )
  56. {
  57. this->CurFileName = this->Coverage.BinaryDir + "/" +
  58. this->FileName;
  59. fin.open(this->CurFileName.c_str());
  60. if (!fin)
  61. {
  62. cmCTestLog(this->CTest, ERROR_MESSAGE,
  63. "Jacoco Coverage: Error opening " << this->CurFileName
  64. << std::endl);
  65. this->Coverage.Error++;
  66. }
  67. }
  68. std::string line;
  69. FileLinesType& curFileLines =
  70. this->Coverage.TotalCoverage[this->CurFileName];
  71. curFileLines.push_back(-1);
  72. while(cmSystemTools::GetLineFromStream(fin, line))
  73. {
  74. curFileLines.push_back(-1);
  75. }
  76. }
  77. else if(name == "report")
  78. {
  79. this->ModuleName=atts[1];
  80. }
  81. else if(name == "line")
  82. {
  83. int tagCount = 0;
  84. int nr = -1;
  85. int ci = -1;
  86. while(true)
  87. {
  88. if(strcmp(atts[tagCount],"ci") == 0)
  89. {
  90. ci = atoi(atts[tagCount+1]);
  91. }
  92. else if (strcmp(atts[tagCount],"nr") == 0)
  93. {
  94. nr = atoi(atts[tagCount+1]);
  95. }
  96. if (ci > -1 && nr > 0)
  97. {
  98. FileLinesType& curFileLines=
  99. this->Coverage.TotalCoverage[this->CurFileName];
  100. if(curFileLines.size() > 0)
  101. {
  102. curFileLines[nr-1] = ci;
  103. }
  104. break;
  105. }
  106. ++tagCount;
  107. }
  108. }
  109. }
  110. private:
  111. std::string PackageName;
  112. std::string FileName;
  113. std::string ModuleName;
  114. std::string CurFileName;
  115. std::vector<std::string> FilePaths;
  116. typedef cmCTestCoverageHandlerContainer::SingleFileCoverageVector
  117. FileLinesType;
  118. cmCTest* CTest;
  119. cmCTestCoverageHandlerContainer& Coverage;
  120. };
  121. cmParseJacocoCoverage::cmParseJacocoCoverage(
  122. cmCTestCoverageHandlerContainer& cont,
  123. cmCTest* ctest)
  124. :Coverage(cont), CTest(ctest)
  125. {
  126. }
  127. bool cmParseJacocoCoverage::LoadCoverageData(
  128. const std::vector<std::string> files)
  129. {
  130. // load all the jacoco.xml files in the source directory
  131. cmsys::Directory dir;
  132. size_t i;
  133. std::string path;
  134. size_t numf = files.size();
  135. for (i = 0; i < numf; i++)
  136. {
  137. path = files[i];
  138. cmCTestLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
  139. "Reading XML File " << path << std::endl);
  140. if(cmSystemTools::GetFilenameLastExtension(path) == ".xml")
  141. {
  142. if(!this->ReadJacocoXML(path.c_str()))
  143. {
  144. return false;
  145. }
  146. }
  147. }
  148. return true;
  149. }
  150. bool cmParseJacocoCoverage::ReadJacocoXML(const char* file)
  151. {
  152. cmParseJacocoCoverage::XMLParser
  153. parser(this->CTest, this->Coverage);
  154. parser.ParseFile(file);
  155. return true;
  156. }