question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Switch to use relative paths in OpenCover report

See original GitHub issue

I am running coverlet in a self-hosted GitLab instance to collect code coverage for Sonarqube. Problem is, we have Windows runners and the test job and the sonar job can run in different absolute paths. This causes Sonarqube to be unable to calculate code coverage because the absolute paths from the first job point nowhere.

This is the command I am using:

coverlet $TEST_PROJECT\bin\Debug\$TEST_PROJECT.dll \
--output "./TestResults/" \
--target nunit3-console \
--targetargs "$TEST_PROJECT\bin\Debug\$TEST_PROJECT.dll --result ./TestResults/TestResult.xml" \
--format opencover

This is what the report looks like (with anonymized paths):

<File uid="1" fullPath="C:\Gitlab-Runner-02\builds\Fu_ads7S\0\APP_PROJECT\File1.cs"/>
<File uid="2" fullPath="C:\Gitlab-Runner-02\builds\Fu_ads7S\0\APP_PROJECT\File2.cs"/>
<File uid="3" fullPath="C:\Gitlab-Runner-02\builds\Fu_ads7S\0\APP_PROJECT\File2.cs"/>

How can I make coverlet generate the opencover with paths relative to execution/project instead of absolute ones? I looked at multiple issues similar to this one but didn’t see any resolution on what to do. I also read the docs, but the only options I saw --use-source-link or somehow using deterministic builds. With source link enabled it still generated absolute paths and I haven’t tried deterministic build because it didn’t seem straightforward.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
andrei-epure-sonarsourcecommented, Jul 27, 2022

Thanks @Evangelink @RedlineTriad , I’ve mentioned this to our PM (about adding support for the cobertura format for the Sonar C# offering).

0reactions
zyskocommented, Jan 13, 2023

Same problem here. My workaround (for linux): Runner1:

  1. run tests and generate report to file opencover.xml
  2. sed -i “s|$PWD|CURRENTDIRECTORYREPLACEMENT|g” opencover.xml

Runner2:

  1. sed -i “s|CURRENTDIRECTORYREPLACEMENT|$PWD|g” opencover.xml
  2. run sonar scanner

Explanation: $PWD - variable with current path sed -i “s|$PWD|CURRENTDIRECTORYREPLACEMENT|g” opencover.xml - replace all occurrences of current path with string “CURRENTDIRECTORYREPLACEMENT” sed -i “s|CURRENTDIRECTORYREPLACEMENT|$PWD|g” opencover.xml - replace all occurances of string “CURRENTDIRECTORYREPLACEMENT” with current path After that when i run sonar scanner i got opencover.xml with correct paths

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to write batch file for OpenCover for MSTest with ...
You can use "forfiles" command to recursively search directories for the paths to those specific files. The below example will search as ...
Read more >
SourceLink breaks coverage report generation - SonarCloud
Also: Switch to use relative paths in OpenCover report · Issue #1315 · coverlet-coverage/coverlet · GitHub. Those were the top two hits in ......
Read more >
How to Use OpenCover Tool for Measuring the Code ...
This tool works with reports created by OpenCover, dotCover, Visual Studio, NCover, Cobertura, JaCoCo, Clover, Gcov, and Lcov. ReportGenerator ...
Read more >
Resolved - Need help for displaying coverage history
1. Run edit mode tests, publish file to folder. 2. Convert the Unity OpenCover files to Cobertura with ReportGenerator tool. historydir input is ......
Read more >
Using Code Coverage in batchmode
You can change the file paths in the coverage results xml to relative paths so that coverage data generated on different machines can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found