How to exclude specific files in Sonar? - c#

I am using SonarQube on a C# .Net application via TeamCity. This application has jQuery in a folder with a bunch of other scripts. I would like to exclude a number of JQuery files from analysis but have Sonar analyze all the other files in the folder. One such JQuery file is at the following path:
...MyProject\Content\Scripts\jquery-ui-1.8.custom.min.js
I have added the following line to the additional parameters section of the SonarQube Runner build step:
-Dsonar.exclusions=**/Content/Scripts/jquery*
in an effort to exclude all files that start with jquery in that folder however the file is not being excluded from the analysis. Any ideas here?

I locally created an example project, containing a java file and a file called Content\Scripts\jquery-ui-1.8.custom.min.js.
~/sonar-scanner/bin/sonar-scanner led to these logs:
...
INFO: Quality profile for js: Sonar way
...
INFO: Sensor JavaScript Squid Sensor [javascript]
INFO: 1 source files to be analyzed
...
and the javascript file was visible in SonarQube.
~/sonar-scanner/bin/sonar-scanner -Dsonar.exclusions=**/Content/Scripts/jquery* led to these logs:
...
INFO: Excluded sources:
INFO: **/Content/Scripts/jquery*
INFO: 2 files indexed
INFO: 1 file ignored because of inclusion/exclusion patterns
...
and the javascript file was successfully excluded (not visible in SonarQube).
Conclusion
Your mentioned configuration should work. My guess is, that the analysis report cannot be processed for another reason and you are seeing old data in SonarQube.
Please verify your sonar runner logs and the status of your background tasks in the SonarQube administration pages.

Related

How to correctly configure SonarQube Scanner for MSBuild with OpenCover

I have a .NET solution with bunch of projects. I wan to analyze the project with SonarQube, and show unit tests details. I'm using NUnit for tests and OpenCover to generate coverage reports. I have a simple batch file to run the required SonarQube tasks in sequence.
Now I want some of the assemblies to be excluded from the coverage analysis & calculation, and do the aggregate analysis only on a subset of assemblies. I'm using opencover filters for the purpose, but they do not seem to work.
Let's say I have assemblies like
MyProject.ViewModels
MyProject.DomainModels
MyProject.Core
MyProject.CoreTests
My.Shared.Data
MyProject.Data.Tests
MyProject.Data.Console
MyShared.Console
UIModules.Services
UIModules.Services.UnitTests
UIModules.Web
UIModules.Web.Tests
And to process them I have a batch file like the following. I have added some filter to exclude the Tests, Models & Console assemblies.
Seems like the filters are not working properly. In the code coverage shown on SonarQube, though the Test projects are excluded, still the Models & Console projects are taken into account for overall coverage calculation.
SonarQube.Scanner.MSBuild.exe begin /k:"key" /n:"My Project" /v:"1.0" /d:sonar.cs.nunit.reportsPaths="NUnitResults.xml" /d:sonar.cs.opencover.reportsPaths="opencover.xml"
REM build the project/solution
msbuild MyProject.sln /t:rebuild
REM run NUnit tests
"C:\Program Files (x86)\NUnit 2.6.4\bin\nunit-console.exe" /result=NUnitResults.xml Core\bin\Debug\MyProject.CoreTests.dll Data\bin\Debug\MyProject.Data.Tests.dll Services\bin\Debug\UIModules.ServicesTests.dll Web\bin\Debug\UIModules.Web.Tests.dll
REM run OpenCover coverage
"C:\Users\Me\AppData\Local\Apps\OpenCover\OpenCover.Console.exe" -register:user "-target:C:\Program Files (x86)\NUnit 2.6.4\bin\nunit-console.exe" "-targetargs:Core\bin\Debug\MyProject.CoreTests.dll Data\bin\Debug\MyProject.Data.Tests.dll Services\bin\Debug\UIModules.ServicesTests.dll Web\bin\Debug\UIModules.Web.Tests.dll /noshadow" "-output:opencover.xml" "-filter:+[MyProject*]* +[My.Shared*]* +[UIModules*]* -[*Tests]* -[*.Console]* -[*Models]*"
REM process end analysis
SonarQube.Scanner.MSBuild.exe end
Can someone guide me to the correct direction.
Not sure where you are looking for the rules.
The coverage filter rules in the coverage tool should influence the output of the coverage.
When the coverage gets imported additional rules apply.
Sonar Qube has additional rules. And the runner can have filter rules built in as well.
Some Runners can ignore Sonar settings.
Since you are talking about coverage I assume you want to skip testing data.
For that the Testing projects names need to end in Test or Tests.
This is mentioned here:
https://docs.sonarqube.org/display/SCAN/Miscellaneous+Advanced+Usages
I also tend to exclude AssemblyInfo.cs which I set in my Sonar Host.
In Configuration -> Analysis Scope -> Global Source File Exclusions with my Pattern of **/AssemblyInfo.cs
I hope this gets you started.
I think you should leave the Other projects as not covered and list that.
If you really want to exclude per project you could try to pass additional parameters to the sonar scanner like sonar.exclusions
It is possible that sonar.exclusions is ignored by the runner.
See this answer for inspiration on different exclusion methods:
The root directory used for the resolution of the sonar.exclusions relative path is the location of each .csproj file.

How to analyze existing .cs files with SonarQube Scanner for MSBuild?

I want to scan existing .cs files with sonarqube.
I understood that I need to use SonarQube Scanner for MSBuild, and this one requires to create a project to analyze. So I created a project on Visual studio from existing files and declared it as Class Library. But while executing SonarQube Scanner for MSBuild, I've got this error:
WARNING: The project has an invalid GUID "00000000-0000-0000-0000-
000000000000". The project will not be analyzed by SonarQube. Project
file: C:\Users\sas1\Documents\Visual Studio
2015\Projects\MSBuildTest\MSBuildTest.csproj
No analysable projects were found. SonarQube analysis will not be
performed. Check the build summary report for details.
Generation of the sonar-properties file failed. Unable to complete
SonarQube analysis.
09:54:16.866 Creating a summary markdown file...
09:54:16.872 Post-processing failed. Exit code: 1
I can not create a console application with those files, because thus I will have many compilation errors, which I am not supposed to correct, since the code is not mine, but just delegated to me.
So my question is: if someone gives me several .cs files to analyze using SonarQube, how can I analyse them using SonarQube Scanner for MSBuild?
Thank you

SonarQube failing to analyze auto-generated target framework moniker files

I'm using SonarQube 5.2 and MSBuild.SonarQube.Runner to analyze a solution containing a number of C# projects. When the runner encounters auto-generated files like...
bin\Release\.NETFramework,Version=v4.5.AssemblyAttributes.cs
bin\Release\.NETPortable,Version=v4.5,Profile=Profile7.AssemblyAttributes.cs
...it fails with the following error...
ERROR: Error during Sonar runner execution
org.sonar.runner.impl.RunnerException: Unable to execute Sonar
at org.sonar.runner.impl.BatchLauncher$1.delegateExecution(BatchLauncher.java:91)
at org.sonar.runner.impl.BatchLauncher$1.run(BatchLauncher.java:75)
at java.security.AccessController.doPrivileged(Native Method)
at org.sonar.runner.impl.BatchLauncher.doExecute(BatchLauncher.java:69)
at org.sonar.runner.impl.BatchLauncher.execute(BatchLauncher.java:50)
at org.sonar.runner.api.EmbeddedRunner.doExecute(EmbeddedRunner.java:102)
at org.sonar.runner.api.Runner.execute(Runner.java:100)
at org.sonar.runner.Main.executeTask(Main.java:70)
at org.sonar.runner.Main.execute(Main.java:59)
at org.sonar.runner.Main.main(Main.java:53)
Caused by: java.lang.IllegalStateException: The folder 'C:\Source\Project1\bin\Release\.NETFramework' does not exist for 'TheProject:TheBranch:TheProject:TheBranch:GUID' (base directory = C:\Source\Project1)
At the moment I'm working around the problem by deleting those files before invoking the runner with the 'end' verb, but would like to know if there is a better way or if this is a known bug.
Can anyone tell me if SonarQube should be able to handle these files, or if there is a configuration I should tweak to avoid the problem?
This isn't a known issue - thanks for reporting it. I've created SONARMSBRU-199 to track it.
Normally XXXAssemblyInfo.cs files are generated under the %TEMP% directory so they are automatically excluded from the analysis as they are not under the project folder. However, it's a valid file name so it should be possible to analyse it if it happens to be created under the project directory.

How to integrate the dotcover HTML reports with sonarqube 4.5?

I have been struggling with this issue. I have been able to generate the HTML reports with the help of DotCover. Also, I have given the path of the reports in the Sonar Portal too under Project Settings--> C# --> CodeCoverage --> DotCoverReportPath and also in the Sonar.Properties file for the particular solution (Mine is a DotNet application).
When I run the Sonar-Runner it seems to parse the HTML files and also says that Execution success, but however my dashboard on the Sonar System shows "No Data" for the coverage.
Please help me out if any of you have faced a similar issue and have been able to figure out the error.
SonarQube requires that the absolute paths of the source files found in the coverage HTML reports to be strictly equal to the absolute paths of the files being analyzed.
This implies that you cannot run your tests and collect code coverage on a machine A, transfer the report to machine B, and then run the SonarQube analysis from machine B. From within the same machine, you also cannot checkout the code once to run the tests & collect code coverage, and then check it out another time to run the SonarQube analysis.
This is a common case of why code coverage doesn't get imported into SonarQube, but without the logs, it is hard to tell whether this is your case or not.

Unit Testing and Code Coverage are showing as 0% in Sonar

I am trying to set up unit testing and code coverage for a C# project. I am using sonar runner as the analyzer, Gallio 3.4.14 OpenCover4.5.1
This is the sonar-project.properties file:
sonar.projectKey=Foo
sonar.projectName=Foo-SonarQube Runner
sonar.projectVersion=1.0
# Path to the source directories (required)
sonar.sources=.
sonar.language=cs
sonar.sourceEncoding=UTF-8
sonar.dotnet.visualstudio.solution.file=Foo.sln
sonar.silverlight.4.mscorlib.location=C:\Program Files (x86)\Reference Assemblies
\Microsoft\Framework\Silverlight\v5.0
sonar.dotnet.excludeGeneratedCode=true
sonar.dotnet.4.0.sdk.directory=C:/WIndows/Microsoft.NET/Framework/v4.0.30319
sonar.dotnet.version=4.0
sonar.gallio.runner=IsolatedProcess
sonar.gallio.coverage.tool=OpenCover
sonar.donet.visualstudio.testProjectPattern=**.Tests; **.UnitTests
sonar.opencover.installDirectory=C:/Program Files (x86)/OpenCover
sonar.dotnet.test.assemblies=FooUnitTests\bin\Release\FooUnitTests.dll
It runs successfully but in the logs I see that Gallio didnt execute:
No assembly to check with Gendarme
Skipping the non generated assembly of project : Foo
No assembly to check with NDeps
Gallio won't execute as there are no test projects.
Any help will be appreciated
Double-check your test project patterns:
sonar.donet.visualstudio.testProjectPattern=**.Tests; **.UnitTests
Try this instead:
sonar.donet.visualstudio.testProjectPattern=*Tests;*UnitTests
Also, toward the top of the sonar-runner output (within the first 20 or so lines), you should see some lines like this:
05:40:07.021 INFO - Initializing Hibernate
05:40:13.601 INFO - Load project settings
05:40:13.653 INFO - The following 'sln' file has been found and will be used: C:\TeamCity\buildAgent\work\920b95942ca1a758\Sonar\MyProject-Sonar.sln
05:40:17.276 INFO - The project 'MyProject.Tests' has been qualified as a test project.
05:40:19.235 INFO - The project 'MyOther.Project.Tests' has been qualified as a test project.
If you're not seeing this, try running sonar-runner with -X to enable debugging output. This should give you more details as to what patterns are being used to match test projects.
Additionally, any paths need to use forward slashes (/), so the following need to be changed:
sonar.silverlight.4.mscorlib.location=C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v5.0
sonar.dotnet.test.assemblies=FooUnitTests\bin\Release\FooUnitTests.dll
To this:
sonar.silverlight.4.mscorlib.location=C:/Program Files (x86)/Reference Assemblies/Microsoft/Framework/Silverlight/v5.0
sonar.dotnet.test.assemblies=FooUnitTests/bin/Release/FooUnitTests.dll

Categories

Resources