cmParseJacocoCoverage.cxx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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->FilePath = "";
  17. this->PackagePath = "";
  18. this->PackageName = "";
  19. }
  20. virtual ~XMLParser()
  21. {
  22. }
  23. protected:
  24. virtual void EndElement(const std::string&)
  25. {
  26. }
  27. virtual void StartElement(const std::string& name,
  28. const char** atts)
  29. {
  30. if(name == "package")
  31. {
  32. this->PackageName = atts[1];
  33. this->PackagePath = "";
  34. }
  35. else if(name == "sourcefile")
  36. {
  37. std::string fileName = atts[1];
  38. if (this->PackagePath == "")
  39. {
  40. if(!this->FindPackagePath(fileName))
  41. {
  42. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot find file: "
  43. << this->PackageName << "/" << fileName << std::endl);
  44. this->Coverage.Error++;
  45. return;
  46. }
  47. }
  48. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  49. "Reading file: " << fileName << std::endl,
  50. this->Coverage.Quiet);
  51. this->FilePath = this->PackagePath + "/" + fileName;
  52. cmsys::ifstream fin(this->FilePath.c_str());
  53. if (!fin)
  54. {
  55. cmCTestLog(this->CTest, ERROR_MESSAGE,
  56. "Jacoco Coverage: Error opening " << this->FilePath
  57. << std::endl);
  58. }
  59. std::string line;
  60. FileLinesType& curFileLines =
  61. this->Coverage.TotalCoverage[this->FilePath];
  62. if(fin)
  63. {
  64. curFileLines.push_back(-1);
  65. }
  66. while(cmSystemTools::GetLineFromStream(fin, line))
  67. {
  68. curFileLines.push_back(-1);
  69. }
  70. }
  71. else if(name == "line")
  72. {
  73. int tagCount = 0;
  74. int nr = -1;
  75. int ci = -1;
  76. while(true)
  77. {
  78. if(strcmp(atts[tagCount],"ci") == 0)
  79. {
  80. ci = atoi(atts[tagCount+1]);
  81. }
  82. else if (strcmp(atts[tagCount],"nr") == 0)
  83. {
  84. nr = atoi(atts[tagCount+1]);
  85. }
  86. if (ci > -1 && nr > 0)
  87. {
  88. FileLinesType& curFileLines=
  89. this->Coverage.TotalCoverage[this->FilePath];
  90. if(!curFileLines.empty())
  91. {
  92. curFileLines[nr-1] = ci;
  93. }
  94. break;
  95. }
  96. ++tagCount;
  97. }
  98. }
  99. }
  100. virtual bool FindPackagePath(const std::string fileName)
  101. {
  102. // Search for the source file in the source directory.
  103. if (this->PackagePathFound(fileName, this->Coverage.SourceDir))
  104. {
  105. return true;
  106. }
  107. // If not found there, check the binary directory.
  108. if (this->PackagePathFound(fileName, this->Coverage.BinaryDir))
  109. {
  110. return true;
  111. }
  112. return false;
  113. }
  114. virtual bool PackagePathFound(const std::string fileName,
  115. const std::string baseDir)
  116. {
  117. // Search for the file in the baseDir and its subdirectories.
  118. std::string packageGlob = baseDir;
  119. packageGlob += "/";
  120. packageGlob += fileName;
  121. cmsys::Glob gl;
  122. gl.RecurseOn();
  123. gl.RecurseThroughSymlinksOn();
  124. gl.FindFiles(packageGlob);
  125. std::vector<std::string> const& files = gl.GetFiles();
  126. if (files.size() == 0)
  127. {
  128. return false;
  129. }
  130. // Check if any of the locations found match our package.
  131. for(std::vector<std::string>::const_iterator fi = files.begin();
  132. fi != files.end(); ++fi)
  133. {
  134. std::string dir = cmsys::SystemTools::GetParentDirectory(*fi);
  135. if (cmsys::SystemTools::StringEndsWith(dir, this->PackageName.c_str()))
  136. {
  137. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  138. "Found package directory for " << fileName <<
  139. ": " << dir << std::endl,
  140. this->Coverage.Quiet);
  141. this->PackagePath = dir;
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. private:
  148. std::string FilePath;
  149. std::string PackagePath;
  150. std::string PackageName;
  151. typedef cmCTestCoverageHandlerContainer::SingleFileCoverageVector
  152. FileLinesType;
  153. cmCTest* CTest;
  154. cmCTestCoverageHandlerContainer& Coverage;
  155. };
  156. cmParseJacocoCoverage::cmParseJacocoCoverage(
  157. cmCTestCoverageHandlerContainer& cont,
  158. cmCTest* ctest)
  159. :Coverage(cont), CTest(ctest)
  160. {
  161. }
  162. bool cmParseJacocoCoverage::LoadCoverageData(
  163. const std::vector<std::string> files)
  164. {
  165. // load all the jacoco.xml files in the source directory
  166. cmsys::Directory dir;
  167. size_t i;
  168. std::string path;
  169. size_t numf = files.size();
  170. for (i = 0; i < numf; i++)
  171. {
  172. path = files[i];
  173. cmCTestOptionalLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
  174. "Reading XML File " << path << std::endl, this->Coverage.Quiet);
  175. if(cmSystemTools::GetFilenameLastExtension(path) == ".xml")
  176. {
  177. if(!this->ReadJacocoXML(path.c_str()))
  178. {
  179. return false;
  180. }
  181. }
  182. }
  183. return true;
  184. }
  185. bool cmParseJacocoCoverage::ReadJacocoXML(const char* file)
  186. {
  187. cmParseJacocoCoverage::XMLParser
  188. parser(this->CTest, this->Coverage);
  189. parser.ParseFile(file);
  190. return true;
  191. }