0

I have managed to perform static code analysis for a .NET core project using SonarQube Scanner for MSBuild, on a debian:stretch docker container.

I am now trying to produce a coverage report.

Unless I am wrong, the relevant guidelines hint that you cannot just use an existing report, but rather follow a dynamic process of

  • beginning the analysis by pointing to the report (to be done) path
  • running the actual coverage via one of the following tools:

a) Visual Studio Code Coverage

b) dotCover

c) OpenCover

  • importing the report
  • ending the analysis initiated by msbuild

My question is whether it is possible to run the above process on Linux (haven't managed to do so yet or find any resources)

pkaramol
  • 16,451
  • 43
  • 149
  • 324

2 Answers2

1

I've managed to get coverage out of coverlet (https://github.com/tonerdo/coverlet), but so far have been unable to get sonar-scanner for msbuild to finish. some config problem that hopefully I'll sort out soon.

The script I use is:

#!/bin/bash -e

api_key=$API_KEY

# Get nuget packages and then build DEBUG
dotnet restore --packages packages --configfile nuget.config /nowarn:NU1701,NU1603,NU1605 MyApp.sln

# set the version on all the projects
echo Set SDK Versions to [$apiversion]
find sdk -name "*.csproj" -print | xargs -I £ sed -ie "s/<Version>.*<\/Version>/<Version>${apiversion}<\/Version>/g" £

echo Set Implementation Versions to [$version]
find implementation -name "*.csproj" -print | xargs -I £ sed -ie "s/<Version>.*<\/Version>/<Version>${version}<\/Version>/g" £

echo Starting SONAR...
export token=$SONAR_TOKEN

dotnet /bin/SonarScanner.MSBuild.dll begin \
    /d:sonar.login=${token}  \
    /d:sonar.host.url=https://my-sonar-server.com \
    /v:$version \
    /k:$key \
    /d:sonar.analysis.mode=preview \
    /d:sonar.cs.opencover.reportsPaths="$(find . -name coverage.xml | tr '\n' ',')" \
    /d:sonar.coverage.exclusions=tests/** \
    /d:sonar.cs.vstest.reportsPaths="$(pwd)/.output/*.trx" \
    /d:sonar.verbose=true

echo Build solution...
dotnet build --no-restore /nowarn:NU1701,NU1603,NU1605 Core.sln /clp:PerformanceSummary

# Run the tests
for testproj in $(ls tests/*.Tests -d); do 
    echo $testproj; 
    set +e
    time \
        dotnet test \
            --no-build \
            --no-restore \
            --filter "TestCategory!=Integration" \
            --logger:trx \
            -r .output/ \
            $testproj \
            /nowarn:NU1701,NU1603,NU1605 \
            /p:CollectCoverage=true \
            /p:CoverletOutputFormat=opencover
    set -e
done

dotnet /bin/SonarScanner.MSBuild.dll end /d:sonar.login=$token

You need to reference coverlet in each of your test assemblies.

pms1969
  • 3,354
  • 1
  • 25
  • 34
1

Coverage reporting as also SonarQube integration is possible thanx to minicover.

Minicover now uses mini-OpenCover to generate (and upload to a SQ server) SQ-compatible coverage reports; The procedure that should be followed more or less, scripted:

(assuming tools is the folder that performs the nugget installation for minicover)

echo "Starting sonarqube integration"
mono /home/pathTo/SonarQube.Scanner.MSBuild.exe begin /k:"MyProject" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="myLoginId" /d:sonar.cs.opencover.reportsPaths="coverage.xml"


dotnet restore
dotnet build
cd tools

export "MiniCover=dotnet minicover"

# Instrument assemblies inside 'MyTestFolder' folder to detect hits for source files inside 'MySrcFolder' folder
$MiniCover instrument --workdir ../ --assemblies MyTestFolder/**/bin/**/*.dll --sources MySrcFolder/**/*.cs 

# Reset hits count in case minicover was run for this project  
$MiniCover reset

cd ..    

dotnet test --no-build ./MyTestFolder/MyTest.csproj

cd tools    
# Uninstrument assemblies, it's important if you're going to publish or deploy build outputs
$MiniCover uninstrument --workdir ../

# Create html reports inside folder coverage-html
$MiniCover opencoverreport --workdir ../ --threshold 90

# Print opencover report
$MiniCover opencoverreport --workdir ../ --threshold 90

cd ..
echo "Ending sonarqube integration..."
mono /home/pathTo/SonarQube.Scanner.MSBuild.exe end /d:sonar.login="myLoginId"

More extensive discussion/details can be found on this and this threads.

pkaramol
  • 16,451
  • 43
  • 149
  • 324