cmParseBlanketJSCoverage.cxx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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];
  75. CoverageVector = localCoverageVector;
  76. localCoverageVector.clear();
  77. }
  78. foundFile= true;
  79. inSource = false;
  80. filename = getValue(line,0).c_str();
  81. }
  82. else if((line.find("coverage") != line.npos) && foundFile && inSource )
  83. {
  84. /*
  85. * two types of "coverage" in the JSON structure
  86. *
  87. * The coverage result over the file or set of files
  88. * and the coverage for each individual line
  89. *
  90. * FoundFile and foundSource ensure that
  91. * only the value of the line coverage is captured
  92. */
  93. std::string result = getValue(line,1);
  94. result = result.substr(2,result.npos);
  95. if(result == "\"\"")
  96. {
  97. // Empty quotation marks indicate that the
  98. // line is not executable
  99. localCoverageVector.push_back(-1);
  100. }
  101. else
  102. {
  103. // Else, it contains the number of time executed
  104. localCoverageVector.push_back(atoi(result.c_str()));
  105. }
  106. }
  107. else if(line.find("source") != line.npos)
  108. {
  109. inSource=true;
  110. }
  111. }
  112. // On exit, capture end of last file covered.
  113. FileLinesType& CoverageVector =
  114. this->Coverage.TotalCoverage[filename];
  115. CoverageVector = localCoverageVector;
  116. localCoverageVector.clear();
  117. return true;
  118. }
  119. private:
  120. cmCTestCoverageHandlerContainer& Coverage;
  121. };
  122. cmParseBlanketJSCoverage::cmParseBlanketJSCoverage(
  123. cmCTestCoverageHandlerContainer& cont, cmCTest* ctest)
  124. :Coverage(cont), CTest(ctest)
  125. {
  126. }
  127. bool cmParseBlanketJSCoverage::LoadCoverageData(std::vector<std::string> files)
  128. {
  129. size_t i=0;
  130. std::string path;
  131. cmCTestOptionalLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
  132. "Found " << files.size() <<" Files" << std::endl, this->Coverage.Quiet);
  133. for(i=0;i<files.size();i++)
  134. {
  135. cmCTestOptionalLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
  136. "Reading JSON File " << files[i] << std::endl, this->Coverage.Quiet);
  137. if(!this->ReadJSONFile(files[i]))
  138. {
  139. return false;
  140. }
  141. }
  142. return true;
  143. }
  144. bool cmParseBlanketJSCoverage::ReadJSONFile(std::string file)
  145. {
  146. cmParseBlanketJSCoverage::JSONParser parser
  147. (this->Coverage);
  148. cmCTestOptionalLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
  149. "Parsing " << file << std::endl, this->Coverage.Quiet);
  150. parser.ParseFile(file);
  151. return true;
  152. }