summaryrefslogtreecommitdiffstats
path: root/Source/CTest/cmParseJacocoCoverage.cxx
blob: 780debc1e42092147ac031e52bf29662dd3533a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "cmStandardIncludes.h"
#include <stdio.h>
#include <stdlib.h>
#include "cmSystemTools.h"
#include "cmXMLParser.h"
#include "cmParseJacocoCoverage.h"
#include <cmsys/Directory.hxx>
#include <cmsys/Glob.hxx>
#include <cmsys/FStream.hxx>


class cmParseJacocoCoverage::XMLParser: public cmXMLParser
{
  public:
    XMLParser(cmCTest* ctest, cmCTestCoverageHandlerContainer& cont)
      : CTest(ctest), Coverage(cont)
      {
      this->PackageName = "";
      this->ModuleName = "";
      this->FileName = "";
      this->CurFileName = "";
      this->FilePaths.push_back(this->Coverage.SourceDir);
      }

    virtual ~XMLParser()
      {
      }

  protected:

    virtual void EndElement(const std::string&)
      {
      }

    virtual void StartElement(const std::string& name,
      const char** atts)
      {
      if(name == "package")
        {
        this->PackageName = atts[1];
        std::string FilePath = this->Coverage.SourceDir +
          "/" + this->ModuleName + "/src/main/java/" +
          this->PackageName;
        this->FilePaths.push_back(FilePath);
        FilePath = this->Coverage.SourceDir +
         "/src/main/java/" + this->PackageName;
        this->FilePaths.push_back(FilePath);
        }
      else if(name == "sourcefile")
        {
        this->FileName = atts[1];
        cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Reading file: "
                     << this->FileName << std::endl);
          for(size_t i=0;i < FilePaths.size();i++)
            {
            std::string finalpath = FilePaths[i] + "/" + this->FileName;
            if(cmSystemTools::FileExists(finalpath.c_str()))
              {
              this->CurFileName = finalpath;
              break;
              }
            }
          cmsys::ifstream fin(this->CurFileName.c_str());
          if(this->CurFileName == "" || !fin )
          {
            this->CurFileName = this->Coverage.BinaryDir + "/" +
                                   this->FileName;
            fin.open(this->CurFileName.c_str());
            if (!fin)
            {
              cmCTestLog(this->CTest, ERROR_MESSAGE,
                         "Jacoco Coverage: Error opening " << this->CurFileName
                         << std::endl);
              this->Coverage.Error++;
            }
          }
          std::string line;
          FileLinesType& curFileLines =
            this->Coverage.TotalCoverage[this->CurFileName];
          curFileLines.push_back(-1);
          while(cmSystemTools::GetLineFromStream(fin, line))
          {
            curFileLines.push_back(-1);
          }
        }
      else if(name == "report")
        {
        this->ModuleName=atts[1];
        }
      else if(name == "line")
        {
        int tagCount = 0;
        int nr = -1;
        int ci = -1;
        while(true)
          {
          if(strcmp(atts[tagCount],"ci") == 0)
            {
            ci = atoi(atts[tagCount+1]);
            }
          else if (strcmp(atts[tagCount],"nr") == 0)
            {
            nr = atoi(atts[tagCount+1]);
            }
          if (ci > -1 && nr > 0)
            {
            FileLinesType& curFileLines=
              this->Coverage.TotalCoverage[this->CurFileName];
            if(!curFileLines.empty())
               {
               curFileLines[nr-1] = ci;
               }
            break;
            }
          ++tagCount;
          }
        }
      }

  private:
    std::string PackageName;
    std::string FileName;
    std::string ModuleName;
    std::string CurFileName;
    std::vector<std::string> FilePaths;
    typedef cmCTestCoverageHandlerContainer::SingleFileCoverageVector
     FileLinesType;
    cmCTest* CTest;
    cmCTestCoverageHandlerContainer& Coverage;
};

cmParseJacocoCoverage::cmParseJacocoCoverage(
  cmCTestCoverageHandlerContainer& cont,
  cmCTest* ctest)
  :Coverage(cont), CTest(ctest)
  {
  }

bool cmParseJacocoCoverage::LoadCoverageData(
  const std::vector<std::string> files)
{
  // load all the jacoco.xml files in the source directory
  cmsys::Directory dir;
  size_t i;
  std::string path;
  size_t numf = files.size();
  for (i = 0; i < numf; i++)
    {
    path = files[i];

    cmCTestLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
      "Reading XML File " << path  << std::endl);
    if(cmSystemTools::GetFilenameLastExtension(path) == ".xml")
      {
      if(!this->ReadJacocoXML(path.c_str()))
        {
        return false;
        }
      }
    }
  return true;
}

bool cmParseJacocoCoverage::ReadJacocoXML(const char* file)
{
  cmParseJacocoCoverage::XMLParser
    parser(this->CTest, this->Coverage);
  parser.ParseFile(file);
  return true;
}