Include only Specific pattern files in code coverage

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
    We are using sonarqube from Azure devops
    I want to include only **/*transform.py pattern files in code coverage of each folder on Sonarqube .
  • what have you tried so far to achieve this
    I am able to achieve this using sonar.exclusions property but code smells and other metrics are not visible for my other code. Please let me if there is any way with which i can get code coverage for **/*transform.py files and all other metrics for all files.

In Sonarqube project, coverage of folder is reduced due to other files. Could you please tell me a way so that coverage includes only /*transform.py files? Below is code that i am using
extraProperties: |
sonar.python.coverage.reportPaths=combined-coverage.xml
sonar.sources=DataEngineering/
sonar.inclusions=
/transform.py
sonar.tests=DataEngineering/
sonar.test.inclusions=DataEngineering/Test/
*
sonar.text.excluded.file.suffixes=.csv, .xml
sonar.dynamicAnalysis=reuseReports
sonar.core.codeCoveragePlugin=cobertura
sonar.sourceEncoding=UTF-8
sonar.python.version=3.8
sonar.qualitygate.wait=true
sonar.qualitygate.timeout=300

My purpose is to get code coverage on files with pattern *transform.py and other metrics like code smells must be visible for whole codebase. Currently using above code, i am not getting other metrics for whole codebase.

i have used below property but it is not working
sonar.coverage.exclusions=!DataEngineering/**/*transform.py

Hey there.

To be perfectly honest, including files for code coverage is something we haven’t ever considered, since these should really be an exception.

You could consider having two separate SonarQube projects – one with everything except your transform.py files (and excluding all files from code coverage), and one with just your transform.py files.

Hello Colin,

Can we introduce ! flag in wildcard file patterns which we can use in sonar.coverage.exclusions to exclude required files which are not matching *transform.py pattern?

sonar.coverage.exclusions=!DataEngineering/**/*transform.py

Hi @Anish_Gupta,

We have recorded your suggestion for consideration.
Thank you very much for your input, we deeply appreciate it!

Denis

Hi Team,

Can you please confirm incremental analysis on Sonar?(incremental analysis means if we publish report for incremental changes then will sonar analysis of other unchanged files will be removed or not)

Hi @Anish_Gupta,

I am not sure I understand what you mean. Is it related to the current question on coverage?
Would you mind explaining a bit more?

Denis

my scenario is as below -

Suppose there are folder structure as mentioned below

Folder1
→ Folder2
→ Folder3
→ Folder4
In my first SonarQube analysis on Folder1, all metrics are visible for above folders.
If i do SonarQube analysis on path Folder1/Folder2 only then will it delete older analysis of other folder or it will update analysis of path Folder1/Folder2 only??

Is Incremental Analysis possible in sonarqube?

Hi Anish,

This is not possible, no. To achieve something like this you would probably have to create two projects as a monorepo (each excluding the folders from the other) and then aggregate the two under an application.

Note that monorepos are only available from Entreprise edition.

Hello Denis, Sonar analysis task is completed successfully. I am facing below issue while publishing report on sonar.

Error: Unable to load component class org.sonar.ce.task.projectanalysis.step.ExecuteVisitorsStep

Could you please suggest how should i debug?

Hi Anish,
To do this, we would need a bit more information:

  • What product you use (SonarQube, SonarCloud)
  • Which version and edition (for SonarQube)
  • Which language(s) you are trying to analyze
  • The (redacted) contents of your pipeline, so we knoàw the steps you took
  • the analysis log

I can then refer your post to the proper team

Product: SonarQube
Version: 10.4.1
Language: Python
I am using azure devops pipeline and in azure devops pipeline, below task are being used
SonarQubePrepare@5
SonarQubeAnalyze@5
SonarQubePublish@5

Logs:

May I ask what version of Java is installed on your agent?

Below are inputs for analyze and publish task.

   - task: SonarQubeAnalyze@5
      displayName: "Vulnerability scan of ${{ parameters.repo }}"
      inputs:
        jdkversion: 'JAVA_HOME_17_X64'
        
    - task: SonarQubePublish@5
      displayName: "Vulnerability scan of ${{ parameters.repo }}"
      inputs:
        pollingTimeoutSec: '300'

What about the inputs for prepare task?

   - task: SonarQubePrepare@5
      displayName: "Vulnerability scan of ${{ parameters.repo }}"
      inputs:
        SonarQube: ''
        scannerMode: 'CLI'
        configMode: 'manual'
        cliProjectKey: '$(System.TeamProjectId)_allreposcan_${{ parameters.repo }}'
        cliProjectName: allreposcan_${{ parameters.repo }}
        cliProjectVersion: '$(Build.BuildNumber)'
        cliSources: '$(Agent.BuildDirectory)/${{ parameters.repo }}'
        extraProperties: |
          sonar.exclusions= $(exclusions)

Hello Denis,

I got the issue.
In analyze scope settings, i am trying to deactivate python:S1192 rule to ignore code smells and second thing, i am trying to ignore code lines starting with # MAGIC and # MAGIC %md in code smells. These settings were not set properly due to which it is giving issue.

Could you please guide me how can i achieve above things?

To disable a rule you should modify your Quality Profile in SonarQube (or create a specific one if you use the default).

As for ignoring a line based on its contents, I do not think that is possible.