| 
 | 1 | +import * as htmlparser2 from "htmlparser2";  | 
 | 2 | +import * as domutils from "domutils";  | 
 | 3 | +import * as domhandler from "domhandler";  | 
 | 4 | +import * as vscode from "vscode";  | 
 | 5 | +import * as fs from "fs/promises";  | 
 | 6 | +import * as path from "path";  | 
 | 7 | +import { exec } from "./utils";  | 
 | 8 | +import { workspaceState } from "./extension";  | 
 | 9 | + | 
 | 10 | +class MyFileCoverage extends vscode.FileCoverage {  | 
 | 11 | +  lineDetails: vscode.StatementCoverage[];  | 
 | 12 | + | 
 | 13 | +  constructor(  | 
 | 14 | +    detailedCoverage: vscode.StatementCoverage[],  | 
 | 15 | +    uri: vscode.Uri,  | 
 | 16 | +    statementCoverage: vscode.TestCoverageCount,  | 
 | 17 | +    branchCoverage?: vscode.TestCoverageCount,  | 
 | 18 | +    declarationCoverage?: vscode.TestCoverageCount,  | 
 | 19 | +  ) {  | 
 | 20 | +    super(uri, statementCoverage, branchCoverage, declarationCoverage);  | 
 | 21 | +    this.lineDetails = detailedCoverage;  | 
 | 22 | +  }  | 
 | 23 | +}  | 
 | 24 | + | 
 | 25 | +export async function loadDetailedCoverage(  | 
 | 26 | +  _testRun: vscode.TestRun,  | 
 | 27 | +  fileCoverage: vscode.FileCoverage,  | 
 | 28 | +  _token: vscode.CancellationToken,  | 
 | 29 | +): Promise<vscode.StatementCoverage[]> {  | 
 | 30 | +  return fileCoverage instanceof MyFileCoverage ? fileCoverage.lineDetails : [];  | 
 | 31 | +}  | 
 | 32 | + | 
 | 33 | +/**  | 
 | 34 | + * Read coverage data generated during test execution.  | 
 | 35 | + * @param buildDir Meson build directory  | 
 | 36 | + * @returns `FileCoverage[]` to be added to be used with `run.addCoverage()`  | 
 | 37 | + */  | 
 | 38 | +export async function getCoverage(buildDir: string): Promise<vscode.FileCoverage[]> {  | 
 | 39 | +  await exec("ninja", ["-C", buildDir, "coverage-xml"]);  | 
 | 40 | +  return parseXml(await fs.readFile(path.join(buildDir, "meson-logs", "coverage.xml"), "utf-8"));  | 
 | 41 | +}  | 
 | 42 | + | 
 | 43 | +/**  | 
 | 44 | + * Parses a Cobertura xml file as generated by gcovr.  | 
 | 45 | + * The schema is documented at  | 
 | 46 | + * https://github.com/cobertura/cobertura/blob/master/cobertura/src/site/htdocs/xml/coverage-04.dtd  | 
 | 47 | + * @param xml Contents of the Cobertura report  | 
 | 48 | + * @returns `FileCoverage[]` to be added to be used with `run.addCoverage()`  | 
 | 49 | + */  | 
 | 50 | +function parseXml(xml: string): vscode.FileCoverage[] {  | 
 | 51 | +  const ret: vscode.FileCoverage[] = [];  | 
 | 52 | + | 
 | 53 | +  const sourceDir = workspaceState.get<string>("mesonbuild.sourceDir")!;  | 
 | 54 | + | 
 | 55 | +  const dom = htmlparser2.parseDocument(xml, { xmlMode: true });  | 
 | 56 | +  const packages = domutils.findOne(  | 
 | 57 | +    (node) => {  | 
 | 58 | +      return node.name == "packages";  | 
 | 59 | +    },  | 
 | 60 | +    dom,  | 
 | 61 | +    true,  | 
 | 62 | +  )!;  | 
 | 63 | +  for (const pkg of packages.childNodes) {  | 
 | 64 | +    // The Cobertura format was designed for Java,  | 
 | 65 | +    // accordingly it considers classes the highest form of abstraction.  | 
 | 66 | +    // However, gcovr just creates one "class" per covered file, and fills it with line data.  | 
 | 67 | +    for (const cls of domutils.findOne((node) => node.name == "classes", pkg as domhandler.Element)!.childNodes) {  | 
 | 68 | +      const classElem = cls as domhandler.Element;  | 
 | 69 | +      const filePath = path.join(sourceDir, classElem.attribs["filename"]);  | 
 | 70 | +      const lineDetails: vscode.StatementCoverage[] = [];  | 
 | 71 | + | 
 | 72 | +      let coveredBranches = 0;  | 
 | 73 | +      let totalBranches = 0;  | 
 | 74 | +      let coveredLines = 0;  | 
 | 75 | +      let totalLines = 0;  | 
 | 76 | + | 
 | 77 | +      for (const line of domutils.findOne((node) => node.name == "lines", cls as domhandler.Element)!.childNodes) {  | 
 | 78 | +        totalLines++;  | 
 | 79 | +        const val = processLine(line as domhandler.Element);  | 
 | 80 | +        coveredLines += val.lineCovered ? 1 : 0;  | 
 | 81 | +        lineDetails.push(...val.lineDetails);  | 
 | 82 | +        coveredBranches += val.coveredBranches;  | 
 | 83 | +        totalBranches += val.totalBranches;  | 
 | 84 | +      }  | 
 | 85 | + | 
 | 86 | +      const lineData = new vscode.TestCoverageCount(coveredLines, totalLines);  | 
 | 87 | +      const branchData = new vscode.TestCoverageCount(coveredBranches, totalBranches);  | 
 | 88 | +      ret.push(new MyFileCoverage(lineDetails, vscode.Uri.file(filePath), lineData, branchData));  | 
 | 89 | +    }  | 
 | 90 | +  }  | 
 | 91 | +  return ret;  | 
 | 92 | +}  | 
 | 93 | + | 
 | 94 | +function processLine(line: domhandler.Element): {  | 
 | 95 | +  lineCovered: boolean;  | 
 | 96 | +  lineDetails: vscode.StatementCoverage[];  | 
 | 97 | +  coveredBranches: number;  | 
 | 98 | +  totalBranches: number;  | 
 | 99 | +} {  | 
 | 100 | +  const hits = parseInt(line.attribs["hits"]);  | 
 | 101 | +  const lineNo = parseInt(line.attribs["number"]);  | 
 | 102 | +  // Position is 0-indexed  | 
 | 103 | +  const position = new vscode.Position(lineNo - 1, 0);  | 
 | 104 | + | 
 | 105 | +  const lineCovered = hits > 0;  | 
 | 106 | +  const lineDetails: vscode.StatementCoverage[] = [];  | 
 | 107 | +  let coveredBranches = 0;  | 
 | 108 | +  let totalBranches = 0;  | 
 | 109 | + | 
 | 110 | +  if (line.attribs["branch"] == "true") {  | 
 | 111 | +    const val = processBranch(line, position);  | 
 | 112 | +    coveredBranches = val.coveredBranches;  | 
 | 113 | +    totalBranches = val.totalBranches;  | 
 | 114 | +    lineDetails.push(new vscode.StatementCoverage(hits, position, val.branchCoverage));  | 
 | 115 | +  } else {  | 
 | 116 | +    lineDetails.push(new vscode.StatementCoverage(hits, position));  | 
 | 117 | +  }  | 
 | 118 | + | 
 | 119 | +  return { lineCovered, lineDetails, coveredBranches, totalBranches };  | 
 | 120 | +}  | 
 | 121 | + | 
 | 122 | +function processBranch(  | 
 | 123 | +  line: domhandler.Element,  | 
 | 124 | +  position: vscode.Position,  | 
 | 125 | +): {  | 
 | 126 | +  branchCoverage: vscode.BranchCoverage[];  | 
 | 127 | +  coveredBranches: number;  | 
 | 128 | +  totalBranches: number;  | 
 | 129 | +} {  | 
 | 130 | +  const branchCoverage: vscode.BranchCoverage[] = [];  | 
 | 131 | +  // format is "50% (1/2)"  | 
 | 132 | +  const conditionStr = line.attribs["condition-coverage"];  | 
 | 133 | +  const match = RegExp(/\d+% \((\d+)\/(\d+)\)/).exec(conditionStr)!;  | 
 | 134 | + | 
 | 135 | +  const coveredBranches = parseInt(match[1]);  | 
 | 136 | +  const totalBranches = parseInt(match[2]);  | 
 | 137 | + | 
 | 138 | +  // The gcov report does not specify which branches were missed,  | 
 | 139 | +  // so the best we can do is report whether a branch site was  | 
 | 140 | +  // fully, partially, or not at all covered  | 
 | 141 | +  if (coveredBranches > 0) {  | 
 | 142 | +    branchCoverage.push(new vscode.BranchCoverage(true, position));  | 
 | 143 | +  }  | 
 | 144 | +  if (coveredBranches < totalBranches) {  | 
 | 145 | +    branchCoverage.push(new vscode.BranchCoverage(false, position));  | 
 | 146 | +  }  | 
 | 147 | +  return { branchCoverage, coveredBranches, totalBranches };  | 
 | 148 | +}  | 
0 commit comments