cmParseJacocoCoverage.cxx 5.2 KB

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