ryanluker / vscode-coverage-gutters

Display test coverage generated by lcov and xml - works with many languages
https://marketplace.visualstudio.com/items?itemName=ryanluker.vscode-coverage-gutters
MIT License
460 stars 88 forks source link

Attempting to parse cobertura.xml fails after converting from visual studio .coverage binary. #438

Open GrahamMThomas opened 7 months ago

GrahamMThomas commented 7 months ago

Describe the bug After converting to cobertura xml file from .coverage binary file, the parsing fails within the cobertura parser. I don't see any reason this shouldn't work given this looks like a valid cobertura xml file to me.

To Reproduce Steps to reproduce the behaviour:

  1. dotnet test --config
    • This config file specifying <DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Culture=neutral">
  2. Convert .coverage file to cobertura.xml using dotnet coverage tool.
    • dotnet-coverage merge -f cobertura -o $code_coverage_file_name $latest_file
    • image
  3. NOTE This file can be processed by report generator and work correctly.

Expected behaviour File to be parsed successfully and coverage gutters to appear.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Additional context Stack Trace:

[1709159241884][coverageservice]: READY
[1709159243317][coverageservice]: LOADING
[1709159244720][coverageservice]: Loading 1 file(s)
[1709159244720][coverageservice]: <REDACTED>/code_coverage.xml
[1709159244722][coverageservice]: Loaded 1 data file(s)
[1709159245196][coverageparser][cobertura-parse]: Error: filename: <REDACTED>/code_coverage.xml Cannot read properties of undefined (reading '0')
[1709159245196][coverageparser][cobertura-parse]: Stacktrace: TypeError: filename: /home/gthomas/repos/Service.HealthDeidentification/code_coverage.xml Cannot read properties of undefined (reading '0')
    at unpackage (/home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/node_modules/cobertura-parse/source/index.js:75:34)
    at /home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/node_modules/cobertura-parse/source/index.js:137:22
    at Parser.<anonymous> (/home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/node_modules/xml2js/lib/parser.js:303:18)
    at Parser.emit (node:events:514:28)
    at SAXParser.onclosetag (/home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/node_modules/xml2js/lib/parser.js:261:26)
    at emit (/home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/node_modules/sax/lib/sax.js:624:35)
    at emitNode (/home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/node_modules/sax/lib/sax.js:629:5)
    at closeTag (/home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/node_modules/sax/lib/sax.js:889:7)
    at SAXParser.write (/home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/node_modules/sax/lib/sax.js:1436:13)
    at Parser.exports.Parser.Parser.parseString (/home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/node_modules/xml2js/lib/parser.js:322:31)
    at Parser.parseString (/home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/node_modules/xml2js/lib/parser.js:5:59)
    at exports.parseString (/home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/node_modules/xml2js/lib/parser.js:354:19)
    at parse.parseContent (/home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/node_modules/cobertura-parse/source/index.js:130:5)
    at /home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/out/src/files/coverageparser.js:72:52
    at new Promise (<anonymous>)
    at CoverageParser.xmlExtractCobertura (/home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/out/src/files/coverageparser.js:63:16)
    at CoverageParser.<anonymous> (/home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/out/src/files/coverageparser.js:36:47)
    at Generator.next (<anonymous>)
    at /home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/node_modules/tslib/tslib.js:118:75
    at new Promise (<anonymous>)
    at __awaiter (/home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/node_modules/tslib/tslib.js:114:16)
    at CoverageParser.filesToSections (/home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/out/src/files/coverageparser.js:19:38)
    at CoverageService.<anonymous> (/home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/out/src/coverage-system/coverageservice.js:84:60)
    at Generator.next (<anonymous>)
    at fulfilled (/home/gthomas/.vscode-server/extensions/ryanluker.vscode-coverage-gutters-2.11.1/node_modules/tslib/tslib.js:115:62)
GrahamMThomas commented 7 months ago

I've found a workaround after viewing some of the sample files in https://github.com/fschwaiger/cobertura-parse.

Looks like the dotnet-compile doesn't include this: image

<sources> tag. Once I added that, even with dummy values, everything was working.

To codify the workaround:

dotnet-coverage merge -f cobertura -o $code_coverage_file_name $latest_file
sed -i '/<coverage/a <sources><source /></sources>' $code_coverage_file_name

This may be more of an issue for the cobertura-parse library. Let me know if I should punt this to over there instead.

ryanluker commented 6 months ago

I've found a workaround after viewing some of the sample files in https://github.com/fschwaiger/cobertura-parse.

Looks like the dotnet-compile doesn't include this: image

<sources> tag. Once I added that, even with dummy values, everything was working.

To codify the workaround:

dotnet-coverage merge -f cobertura -o $code_coverage_file_name $latest_file
sed -i '/<coverage/a <sources><source /></sources>' $code_coverage_file_name

This may be more of an issue for the cobertura-parse library. Let me know if I should punt this to over there instead.

Thanks for submitting an issue and figuring out a workaround! Yes, this definitely would be more of an upstream project issue.