cmParseJacocoCoverage.cxx 5.1 KB

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