Has sonar.tests an effect on C++ / SonarScanner projects?

I found the sonar.tests parameter in the Analysis Parameters | SonarQube Docs.
As so far we manually remove unit-test paths from coverage calculation (via sonar.coverage.exclusions), this seemed perfect to tell sq that these are unit-tests and also have it marked accordingly. However I noticed that it doesn’t have any effect, the test sources are ignored by the scanner completely. It’s just like added to exclusions.

I tried like that:

sonar.sources=a/src, b/src, c/src
sonar.tests=a/test, b/test, c/test

I also tried to add the parent folder to sonar.sources, exclude the test folders and then add to sonar.tests, like that:

sonar.sources=a, b, c
sonar.exclusions=a/test/**, b/test/**, c/test/**
sonar.tests=a/test, b/test, c/test

However no change, all test sources are simply ignored.

It’s a C++ project which is scanned with SonarScanner. Is this option only supported for Java and others?

Used versions are:
SonarQube Version 8.9.1 (build 44547)
SonarScanner 4.6.2.2472

Hi,

Yes, that’s what exclusions do. :smiley:

In fact, by (apparently) including your test files in your overall sonar.sources and them excluding them from coverage with sonar.coverage.exclusions what you’ve done is included your tests in your licensed lines of code, as well as in your duplications &etc. I.E. you’ve told analysis that they’re source files that just don’t need testing.

Rather than using exclusions, you should narrow your sonar.sources value to not include those files and use the sonar.tests parameter to tell analysis that they’re tests.

Off the top of my head, this should have worked:

It would be interesting to see your analysis logs.

 
Ann

Hi Ann,
Thanks for your response.
The second approach I tried because first one didn’t work and I found a similar example for Go language. However it didn’t make a change.

You mean the logs/output from SonarScanner? Can I send you as a direct message? Otherwise I need to remove some details from it.

Hi,

Please post them here. In the likely chance I can’t diagnose the problem others will need to see them too. Feel free to redact them as needed.

 
:smiley:
Ann

Ok, here is the log:

build	17-Nov-2021 14:01:57	INFO: Scanner configuration file: ***removed***/sonar-scanner.properties
build	17-Nov-2021 14:01:57	INFO: Project root configuration file: ***removed***/sonar-project.properties
build	17-Nov-2021 14:01:57	INFO: SonarScanner 4.6.2.2472
build	17-Nov-2021 14:01:57	INFO: Java 11.0.11 AdoptOpenJDK (64-bit)
build	17-Nov-2021 14:01:57	INFO: Linux 4.4.0-59-generic amd64
build	17-Nov-2021 14:01:57	INFO: User cache: /home/docker/.sonar/cache
build	17-Nov-2021 14:01:58	INFO: Scanner configuration file: ***removed***/sonar-scanner.properties
build	17-Nov-2021 14:01:58	INFO: Project root configuration file: ***removed***/sonar-project.properties
build	17-Nov-2021 14:01:58	INFO: Analyzing on SonarQube server 8.9.1
build	17-Nov-2021 14:01:58	INFO: Default locale: "en_US", source code encoding: "UTF-8"
build	17-Nov-2021 14:01:59	INFO: Load global settings
build	17-Nov-2021 14:01:59	INFO: Load global settings (done) | time=170ms
build	17-Nov-2021 14:01:59	INFO: Server id: ***removed***
build	17-Nov-2021 14:01:59	INFO: User cache: /home/docker/.sonar/cache
build	17-Nov-2021 14:01:59	INFO: Load/download plugins
build	17-Nov-2021 14:01:59	INFO: Load plugins index
build	17-Nov-2021 14:01:59	INFO: Load plugins index (done) | time=36ms
build	17-Nov-2021 14:02:03	INFO: Load/download plugins (done) | time=4416ms
build	17-Nov-2021 14:02:04	INFO: Loaded core extensions: developer-scanner
build	17-Nov-2021 14:02:04	INFO: JavaScript/TypeScript frontend is enabled
build	17-Nov-2021 14:02:04	INFO: Process project properties
build	17-Nov-2021 14:02:04	INFO: Process project properties (done) | time=6ms
build	17-Nov-2021 14:02:04	INFO: Execute project builders
build	17-Nov-2021 14:02:04	INFO: Execute project builders (done) | time=1ms
build	17-Nov-2021 14:02:04	INFO: Project key: ***removed***
build	17-Nov-2021 14:02:04	INFO: Base dir: /home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed***
build	17-Nov-2021 14:02:04	INFO: Working dir: /home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed***-JOB5/.scannerwork
build	17-Nov-2021 14:02:04	INFO: Load project settings for component key: '***removed***'
build	17-Nov-2021 14:02:04	INFO: Load project settings for component key: '***removed***' (done) | time=29ms
build	17-Nov-2021 14:02:04	INFO: Load project branches
build	17-Nov-2021 14:02:04	INFO: Load project branches (done) | time=19ms
build	17-Nov-2021 14:02:04	INFO: Load project pull requests
build	17-Nov-2021 14:02:04	INFO: Load project pull requests (done) | time=29ms
build	17-Nov-2021 14:02:04	INFO: Load branch configuration
build	17-Nov-2021 14:02:04	INFO: Found manual configuration of branch/PR analysis. Skipping automatic configuration.
build	17-Nov-2021 14:02:04	INFO: Load branch configuration (done) | time=2ms
build	17-Nov-2021 14:02:04	INFO: Load quality profiles
build	17-Nov-2021 14:02:05	INFO: Load quality profiles (done) | time=57ms
build	17-Nov-2021 14:02:05	INFO: Load active rules
build	17-Nov-2021 14:02:06	INFO: Load active rules (done) | time=1514ms
build	17-Nov-2021 14:02:06	INFO: Branch name: master
build	17-Nov-2021 14:02:06	INFO: Indexing files...
build	17-Nov-2021 14:02:06	INFO: Project configuration:
build	17-Nov-2021 14:02:06	INFO:   Excluded sources: ***removed one path with some other files***
build	17-Nov-2021 14:02:07	INFO: 3464 files indexed
build	17-Nov-2021 14:02:07	INFO: 0 files ignored because of inclusion/exclusion patterns
build	17-Nov-2021 14:02:07	INFO: 0 files ignored because of scm ignore settings
build	17-Nov-2021 14:02:07	INFO: Quality profile for cpp: ***removed***
build	17-Nov-2021 14:02:07	INFO: Quality profile for xml: Sonar way
build	17-Nov-2021 14:02:07	INFO: ------------- Run sensors on module ***removed***
build	17-Nov-2021 14:02:07	INFO: JavaScript/TypeScript frontend is enabled
build	17-Nov-2021 14:02:07	INFO: Load metrics repository
build	17-Nov-2021 14:02:07	INFO: Load metrics repository (done) | time=17ms
build	17-Nov-2021 14:02:08	INFO: Sensor CSS Rules [cssfamily]
build	17-Nov-2021 14:02:08	INFO: No CSS, PHP, HTML or VueJS files are found in the project. CSS analysis is skipped.
build	17-Nov-2021 14:02:08	INFO: Sensor CSS Rules [cssfamily] (done) | time=3ms
build	17-Nov-2021 14:02:08	INFO: Sensor C# Project Type Information [csharp]
build	17-Nov-2021 14:02:08	INFO: Sensor C# Project Type Information [csharp] (done) | time=2ms
build	17-Nov-2021 14:02:08	INFO: Sensor C# Properties [csharp]
build	17-Nov-2021 14:02:08	INFO: Sensor C# Properties [csharp] (done) | time=1ms
build	17-Nov-2021 14:02:08	INFO: Sensor JavaXmlSensor [java]
build	17-Nov-2021 14:02:08	INFO: 1994 source files to be analyzed
build	17-Nov-2021 14:02:08	INFO: Load project repositories
build	17-Nov-2021 14:02:09	INFO: Load project repositories (done) | time=350ms
build	17-Nov-2021 14:02:15	WARN: Invalid character encountered in file /home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed***.xml at line 706 for encoding UTF-8. Please fix file content or configure the encoding to be used using property 'sonar.sourceEncoding'.
build	17-Nov-2021 14:02:17	INFO: 1994/1994 source files have been analyzed
build	17-Nov-2021 14:02:17	INFO: Sensor JavaXmlSensor [java] (done) | time=8134ms
build	17-Nov-2021 14:02:17	INFO: Sensor HTML [web]
build	17-Nov-2021 14:02:17	INFO: Sensor HTML [web] (done) | time=5ms
build	17-Nov-2021 14:02:17	INFO: Sensor XML Sensor [xml]
build	17-Nov-2021 14:02:17	INFO: Sensor XML Sensor [xml] (done) | time=1ms
build	17-Nov-2021 14:02:17	INFO: Sensor VB.NET Project Type Information [vbnet]
build	17-Nov-2021 14:02:17	INFO: Sensor VB.NET Project Type Information [vbnet] (done) | time=1ms
build	17-Nov-2021 14:02:17	INFO: Sensor VB.NET Properties [vbnet]
build	17-Nov-2021 14:02:17	INFO: Sensor VB.NET Properties [vbnet] (done) | time=1ms
build	17-Nov-2021 14:02:17	INFO: Sensor gcov [cpp]
build	17-Nov-2021 14:02:17	INFO: Parsing /home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed**.gcov

*** lot of gcov files that are processed ***

build	17-Nov-2021 14:02:24	INFO: Sensor gcov [cpp] (done) | time=7281ms
build	17-Nov-2021 14:02:24	INFO: Sensor JaCoCo XML Report Importer [jacoco]
build	17-Nov-2021 14:02:24	INFO: 'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
build	17-Nov-2021 14:02:24	INFO: No report imported, no coverage information will be imported by JaCoCo XML Report Importer
build	17-Nov-2021 14:02:24	INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=11ms
build	17-Nov-2021 14:02:24	INFO: Sensor ThymeLeaf template sensor [securityjavafrontend]
build	17-Nov-2021 14:02:24	INFO: javasecurity:S5131 is not activated in quality profile: skipping execution of thymeleaf sensor.
build	17-Nov-2021 14:02:24	INFO: Sensor ThymeLeaf template sensor [securityjavafrontend] (done) | time=0ms
build	17-Nov-2021 14:02:24	INFO: Sensor CFamily [cpp]
build	17-Nov-2021 14:02:24	INFO: CFamily plugin version: 6.20.1.32841
build	17-Nov-2021 14:02:24	INFO: Using build-wrapper output: /home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed***/sonar_build/build-wrapper-dump.json
build	17-Nov-2021 14:02:24	INFO: Available processors: 16
build	17-Nov-2021 14:02:24	INFO: Using 16 threads for analysis according to value of "sonar.cfamily.threads" property.
build	17-Nov-2021 14:02:27	INFO: Cache is explicitly disabled: Optional[false]
build	17-Nov-2021 14:02:27	INFO: [pool-3-thread-1] /home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed***.cpp

*** lot of cpp files that are processed, but unit-test files are missing ***

build	17-Nov-2021 14:08:53	INFO: PCH: unique=0 use=0 (forceInclude=0,throughHeader=0,firstInclude=0) out of 353 (forceInclude=0,throughHeader=0)
build	17-Nov-2021 14:08:53	INFO: SE: 353 out of 353
build	17-Nov-2021 14:08:53	INFO: Subprocess(es) done in 386324ms
build	17-Nov-2021 14:08:53	INFO: 353 compilation units analyzed
build	17-Nov-2021 14:08:53	INFO: Sensor CFamily [cpp] (done) | time=389149ms
build	17-Nov-2021 14:08:53	INFO: Sensor JavaSecuritySensor [security]
build	17-Nov-2021 14:08:53	INFO: Reading type hierarchy from: /home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed***/.scannerwork/ucfg2/java
build	17-Nov-2021 14:08:53	INFO: Read 0 type definitions
build	17-Nov-2021 14:08:53	INFO: Reading UCFGs from: /home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed***/.scannerwork/ucfg2/java
build	17-Nov-2021 14:08:53	INFO: No UCFGs have been included for analysis.
build	17-Nov-2021 14:08:53	INFO: Sensor JavaSecuritySensor [security] (done) | time=2ms
build	17-Nov-2021 14:08:53	INFO: Sensor CSharpSecuritySensor [security]
build	17-Nov-2021 14:08:53	INFO: Reading type hierarchy from: /home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed***/ucfg_cs2
build	17-Nov-2021 14:08:53	INFO: Read 0 type definitions
build	17-Nov-2021 14:08:53	INFO: Reading UCFGs from: /home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed***/ucfg_cs2
build	17-Nov-2021 14:08:53	INFO: No UCFGs have been included for analysis.
build	17-Nov-2021 14:08:53	INFO: Sensor CSharpSecuritySensor [security] (done) | time=0ms
build	17-Nov-2021 14:08:53	INFO: Sensor PhpSecuritySensor [security]
build	17-Nov-2021 14:08:53	INFO: Reading type hierarchy from: /home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed***/.scannerwork/ucfg2/php
build	17-Nov-2021 14:08:53	INFO: Read 0 type definitions
build	17-Nov-2021 14:08:53	INFO: Reading UCFGs from: /home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed***/.scannerwork/ucfg2/php
build	17-Nov-2021 14:08:53	INFO: No UCFGs have been included for analysis.
build	17-Nov-2021 14:08:53	INFO: Sensor PhpSecuritySensor [security] (done) | time=0ms
build	17-Nov-2021 14:08:53	INFO: Sensor PythonSecuritySensor [security]
build	17-Nov-2021 14:08:53	INFO: Reading type hierarchy from: /home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed***/.scannerwork/ucfg2/python
build	17-Nov-2021 14:08:53	INFO: Read 0 type definitions
build	17-Nov-2021 14:08:53	INFO: Reading UCFGs from: /home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed***/.scannerwork/ucfg2/python
build	17-Nov-2021 14:08:53	INFO: No UCFGs have been included for analysis.
build	17-Nov-2021 14:08:53	INFO: Sensor PythonSecuritySensor [security] (done) | time=0ms
build	17-Nov-2021 14:08:53	INFO: Sensor JsSecuritySensor [security]
build	17-Nov-2021 14:08:53	INFO: Reading type hierarchy from: /home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed***/.scannerwork/ucfg2/js
build	17-Nov-2021 14:08:53	INFO: Read 0 type definitions
build	17-Nov-2021 14:08:53	INFO: Reading UCFGs from: /home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed***/.scannerwork/ucfg2/js
build	17-Nov-2021 14:08:53	INFO: No UCFGs have been included for analysis.
build	17-Nov-2021 14:08:53	INFO: Sensor JsSecuritySensor [security] (done) | time=0ms
build	17-Nov-2021 14:08:53	INFO: ------------- Run sensors on project
build	17-Nov-2021 14:08:53	INFO: Sensor Dependency-Check [dependencycheck]
build	17-Nov-2021 14:08:53	INFO: Process Dependency-Check report
build	17-Nov-2021 14:08:53	INFO: Using JSON-Reportparser
build	17-Nov-2021 14:08:53	INFO: Dependency-Check JSON report does not exists. Please check property sonar.dependencyCheck.jsonReportPath:/home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed***/${WORKSPACE}/dependency-check-report.json
build	17-Nov-2021 14:08:53	INFO: JSON-Analysis skipped/aborted due to missing report file
build	17-Nov-2021 14:08:53	INFO: Using XML-Reportparser
build	17-Nov-2021 14:08:53	INFO: Dependency-Check XML report does not exists. Please check property sonar.dependencyCheck.xmlReportPath:/home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed***/${WORKSPACE}/dependency-check-report.xml
build	17-Nov-2021 14:08:53	INFO: XML-Analysis skipped/aborted due to missing report file
build	17-Nov-2021 14:08:53	INFO: Dependency-Check HTML report does not exists. Please check property sonar.dependencyCheck.htmlReportPath:/home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed***/${WORKSPACE}/dependency-check-report.html
build	17-Nov-2021 14:08:53	INFO: HTML-Dependency-Check report does not exist.
build	17-Nov-2021 14:08:53	INFO: Process Dependency-Check report (done) | time=2ms
build	17-Nov-2021 14:08:53	INFO: Sensor Dependency-Check [dependencycheck] (done) | time=2ms
build	17-Nov-2021 14:08:53	INFO: Sensor Zero Coverage Sensor
build	17-Nov-2021 14:08:53	INFO: Sensor Zero Coverage Sensor (done) | time=9ms
build	17-Nov-2021 14:08:53	INFO: SCM Publisher SCM provider for this project is: git
build	17-Nov-2021 14:08:53	INFO: SCM Publisher 1994 source files to be analyzed
build	17-Nov-2021 14:08:53	INFO: This git repository references another local repository which is not well supported. SCM information might be missing for some files. You can avoid borrow objects from another local repository by not using --reference or --shared when cloning it.
build	17-Nov-2021 14:08:59	INFO: SCM Publisher 1994/1994 source files have been analyzed (done) | time=5530ms
build	17-Nov-2021 14:08:59	INFO: CPD Executor 36 files had no CPD blocks
build	17-Nov-2021 14:08:59	INFO: CPD Executor Calculating CPD for 968 files
build	17-Nov-2021 14:08:59	INFO: CPD Executor CPD calculation finished (done) | time=140ms
build	17-Nov-2021 14:08:59	INFO: Load New Code definition
build	17-Nov-2021 14:08:59	INFO: Load New Code definition (done) | time=116ms
build	17-Nov-2021 14:09:00	INFO: Analysis report generated in 526ms, dir size=44 MB
build	17-Nov-2021 14:09:02	INFO: Analysis report compressed in 2073ms, zip size=8 MB
build	17-Nov-2021 14:09:02	INFO: Analysis report uploaded in 635ms
build	17-Nov-2021 14:09:02	INFO: ANALYSIS SUCCESSFUL, you can browse https://***removed***/dashboard?id=***removed***&branch=master
build	17-Nov-2021 14:09:02	INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
build	17-Nov-2021 14:09:02	INFO: More about the report processing at https://***removed***/api/ce/task?id=AX0uOsEtszmMbEJdlc5_
build	17-Nov-2021 14:09:02	INFO: Analysis total time: 6:58.545 s
build	17-Nov-2021 14:09:02	INFO: ------------------------------------------------------------------------
build	17-Nov-2021 14:09:02	INFO: EXECUTION SUCCESS
build	17-Nov-2021 14:09:02	INFO: ------------------------------------------------------------------------
build	17-Nov-2021 14:09:02	INFO: Total time: 7:05.136s
build	17-Nov-2021 14:09:02	INFO: Final Memory: 19M/114M
build	17-Nov-2021 14:09:02	INFO: ------------------------------------------------------------------------
build	17-Nov-2021 14:09:03	Built target sonar_scan
build	17-Nov-2021 14:09:05	Info: Script finished. Exit docker now.
simple	17-Nov-2021 14:09:05	Finished task 'Script' with result: Success
simple	17-Nov-2021 14:09:05	Starting task 'JUnit Parser' of type 'com.atlassian.bamboo.plugins.testresultparser:task.testresultparser.junit'
simple	17-Nov-2021 14:09:05	Parsing test results under /home/bamboo/bamboo-agent-home/xml-data/build-dir/***removed***...
simple	17-Nov-2021 14:09:05	Finished task 'JUnit Parser' with result: Success
simple	17-Nov-2021 14:09:05	Running post build plugin 'NCover Results Collector'
simple	17-Nov-2021 14:09:05	Running post build plugin 'Clover Results Collector'
simple	17-Nov-2021 14:09:05	Running post build plugin 'Sauce OnDemand Post Runner'
simple	17-Nov-2021 14:09:05	Running post build plugin 'Docker Container Cleanup'
simple	17-Nov-2021 14:09:05	Running post build plugin 'npm Cache Cleanup'
simple	17-Nov-2021 14:09:05	Running post build plugin 'Artifact Copier'
simple	17-Nov-2021 14:09:05	Finalising the build...
simple	17-Nov-2021 14:09:05	Stopping timer.
simple	17-Nov-2021 14:09:05	Build ***removed***-220 completed.
simple	17-Nov-2021 14:09:05	Running on server: post build plugin 'NCover Results Collector'
simple	17-Nov-2021 14:09:05	Running on server: post build plugin 'Build Hanging Detection Configuration'
simple	17-Nov-2021 14:09:05	Running on server: post build plugin 'Bamboo project link creator'
simple	17-Nov-2021 14:09:05	Running on server: post build plugin 'Clover Delta Calculator'
simple	17-Nov-2021 14:09:05	Running on server: post build plugin 'Maven Dependencies Postprocessor'
simple	17-Nov-2021 14:09:05	All post build plugins have finished

Hello @max.sch,

For some historical reasons sonar.tests isn’t supported by the C++ analyzer yet.

We analyze the entries in build-wrapper or compilation database JSON file that matches with the sonar.sources entries.
We don’t analyze entries in sonar.tests. Unless you don’t want the analysis to run on your tests you should use:
sonar.sources and sonar.coverage.exclusions

Thanks,

1 Like

Hi @Abbas,

Thanks for clarification. Then we simply can’t use that feature. You should mention that in your documentation, would have saved me some hours of trying and searching :wink:
However not a big deal. Would have been nice feature, but it’s not a blocker for us.

Thanks,
Max

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.