2
votes

I'm trying to hook up my project in sonarcloud. I build and launch my tests using Azure Devops Yaml pipeline. Here is a summary of what's happening.

First I have a Prepare analysis task :

  - task: SonarCloudPrepare@1
    displayName: 'Prepare analysis on Sonarcloud'
    inputs:
      SonarCloud: 'Sonarcloud'
      organization: ***redacted***
      scannerMode: 'MSBuild'
      projectKey: ***redacted***
      projectName: ***redacted***
      extraProperties: |
        sonar.exclusions=**/obj/**,**/*.dll
        sonar.branch.name=$(Build.SourceBranchName)
        sonar.cs.opencover.reportsPaths=$(Build.SourcesDirectory)/**/coverage.opencover.xml
        sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx

Then I build the project, everything is fine here. Then I launch the tests, and create a html report for my azure pipeline using report generator:

  - task: DotNetCoreCLI@2
    displayName: dotnet test
    inputs:
      command: test
      arguments: '--configuration $(BuildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --logger trx'
      projects: 'Tests/**/*.csproj'
      nobuild: true

  - script: |
      dotnet tool install -g dotnet-reportgenerator-globaltool
      reportgenerator "-reports:$(Build.SourcesDirectory)/**/coverage.opencover.xml" "-targetdir:$(Build.SourcesDirectory)/CodeCoverage" "-reporttypes:HtmlInline_AzurePipelines;Cobertura"
    displayName: Create Code coverage report

  - task: PublishCodeCoverageResults@1
    displayName: 'Publish code coverage'
    inputs:
      codeCoverageTool: Cobertura
      summaryFileLocation: '$(Build.SourcesDirectory)/CodeCoverage/Cobertura.xml'
      reportDirectory: '$(Build.SourcesDirectory)/CodeCoverage'

Here again everything is fine, the tests run, the coverage is computed, the report is generated.

Then I install typescript (for sonar analysis) and I run the code analysis:

  - script: |
      cd $(Build.SourcesDirectory)/Src/***ProjectDir***
      npm install typescript
    displayName: 'Install typescript for sonar analysis'
  - task: SonarCloudAnalyze@1
    displayName: 'Run code analysis'
    continueOnError: false
  - task: SonarCloudPublish@1
    displayName: 'Publish code analysis'
    continueOnError: false
    inputs:
      pollingTimeoutSec: '300'

The sonar analysis finds the trx file but not the opencover coverage reports even though the path is provided in the sonar.cs.opencover.reportsPaths parameter in the prepare analysis task.

Here is the logs:

D:\a\_tasks\SonarCloudPrepare_14d9cde6-c1da-4d55-aa01-2965cd301255\1.10.0\classic-sonar-scanner-msbuild\SonarScanner.MSBuild.exe end
SonarScanner for MSBuild 4.8
Using the .NET Framework version of the Scanner for MSBuild
Post-processing started.
17:31:43.118  Property 'sonar.cs.vstest.reportsPaths' provided, skipping the search for TRX files in default folders...
17:31:43.197  Did not find any binary coverage files in the expected location.
17:31:43.197  Falling back on locating coverage files in the agent temp directory.
17:31:43.197  Searching for coverage files in D:\a\_temp
17:31:43.243  No coverage files found in the agent temp directory.

Analysis is correctly uploaded on sonarcloud but the coverage shows 0%...

1
Are you using self-host or host agent? Did the ../coverage.opencover.xml contain the coverage data that you expect? Could you be able to see this file generated in your build log?PatrickLu-MSFT
I'm using the self-host, the file coverage.opencover.xml is correctly generated, I use it already in the reportgenerator task. I can see it in the log also. It's just sonar that doesn't pick it up...Nicolas C

1 Answers

1
votes

Ok my bad the coverage is in fact correctly reported.

The culprit was sonar.branch.name=$(Build.SourceBranchName) I didn’t have this option the first time I ran the build, and the coverage was not working, and the report was at the full branch name: folder/branch. Then I added it with the other coverage parameters and this parameter strip the folder of the branch, so the new report with the coverage was in the branch (without the folder)

Then I was refreshing the report which was of course not updated.

Thanks for all.