cmParseBlanketJSCoverage.cxx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc.
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmStandardIncludes.h"
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include "cmSystemTools.h"
  14. #include "cmParseBlanketJSCoverage.h"
  15. #include <cmsys/Directory.hxx>
  16. #include <cmsys/Glob.hxx>
  17. #include <cmsys/FStream.hxx>
  18. class cmParseBlanketJSCoverage::JSONParser
  19. {
  20. public:
  21. typedef cmCTestCoverageHandlerContainer::
  22. SingleFileCoverageVector FileLinesType;
  23. JSONParser(cmCTestCoverageHandlerContainer& cont)
  24. : Coverage(cont)
  25. {
  26. }
  27. virtual ~JSONParser()
  28. {
  29. }
  30. std::string getValue(std::string line, int type)
  31. {
  32. size_t begIndex;
  33. size_t endIndex;
  34. endIndex = line.rfind(',');
  35. begIndex = line.find_first_of(':');
  36. if(type == 0)
  37. {
  38. // A unique substring to remove the extra characters
  39. // around the files name in the JSON (extra " and ,)
  40. std::string foundFileName =
  41. line.substr(begIndex+3,endIndex-(begIndex+4));
  42. return foundFileName;
  43. }
  44. else
  45. {
  46. return line.substr(begIndex,line.npos);
  47. }
  48. }
  49. bool ParseFile(std::string file)
  50. {
  51. FileLinesType localCoverageVector;
  52. std::string filename;
  53. bool foundFile = false;
  54. bool inSource = false;
  55. std::string covResult;
  56. std::string line;
  57. cmsys::ifstream in(file.c_str());
  58. if(!in)
  59. {
  60. return false;
  61. }
  62. while( cmSystemTools::GetLineFromStream(in, line))
  63. {
  64. if(line.find("filename") != line.npos)
  65. {
  66. if(foundFile)
  67. {
  68. /*
  69. * Upon finding a second file name, generate a
  70. * vector within the total coverage to capture the
  71. * information in the local vector
  72. */
  73. FileLinesType& CoverageVector =
  74. this->Coverage.TotalCoverage[filename.c_str()];
  75. CoverageVector = localCoverageVector;
  76. localCoverageVector.clear();
  77. foundFile=false;
  78. }
  79. foundFile= true;
  80. inSource = false;
  81. filename = getValue(line,0).c_str();
  82. }
  83. else if((line.find("coverage") != line.npos) && foundFile && inSource )
  84. {
  85. /*
  86. * two types of "coverage" in the JSON structure
  87. *
  88. * The coverage result over the file or set of files
  89. * and the coverage for each individual line
  90. *
  91. * FoundFile and foundSource ensure that
  92. * only the value of the line coverage is captured
  93. */
  94. std::string result = getValue(line,1).c_str();
  95. result = result.substr(2,result.npos);
  96. if(result == "\"\"")
  97. {
  98. // Empty quotation marks indicate that the
  99. // line is not executable
  100. localCoverageVector.push_back(-1);
  101. }
  102. else
  103. {
  104. // Else, it contains the number of time executed
  105. localCoverageVector.push_back(atoi(result.c_str()));
  106. }
  107. }
  108. else if(line.find("source") != line.npos)
  109. {
  110. inSource=true;
  111. }
  112. }
  113. // On exit, capture end of last file covered.
  114. FileLinesType& CoverageVector =
  115. this->Coverage.TotalCoverage[filename.c_str()];
  116. CoverageVector = localCoverageVector;
  117. foundFile=false;
  118. localCoverageVector.clear();
  119. return true;
  120. }
  121. private:
  122. cmCTestCoverageHandlerContainer& Coverage;
  123. };
  124. cmParseBlanketJSCoverage::cmParseBlanketJSCoverage(
  125. cmCTestCoverageHandlerContainer& cont, cmCTest* ctest)
  126. :Coverage(cont), CTest(ctest)
  127. {
  128. }
  129. bool cmParseBlanketJSCoverage::LoadCoverageData(std::vector<std::string> files)
  130. {
  131. size_t i=0;
  132. std::string path;
  133. cmCTestLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
  134. "Found " << files.size() <<" Files" << std::endl);
  135. for(i=0;i<files.size();i++)
  136. {
  137. cmCTestLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
  138. "Reading JSON File " << files[i] << std::endl);
  139. if(!this->ReadJSONFile(files[i]))
  140. {
  141. return false;
  142. }
  143. }
  144. return true;
  145. }
  146. bool cmParseBlanketJSCoverage::ReadJSONFile(std::string file)
  147. {
  148. cmParseBlanketJSCoverage::JSONParser parser
  149. (this->Coverage);
  150. cmCTestLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
  151. "Parsing " << file << std::endl);
  152. parser.ParseFile(file);
  153. return true;
  154. }