Use sonar.tests with wildcards

Hi,

I’m using a Vue CLI-generated project structure and use Jest for unit testing (https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-unit-jest). By default the Jest configuration offers two alternatives where Jest is looking for tests <rootDir>/(tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx))

  • inside a dedicated structure /tests/unit/**/*
  • or inside any subdirectory **/__tests__

I want to use the second approach as the test code is located closer to the production code. This leads to a directory structure like

  • production code /src/components/hello-world
  • test code /src/components/hello-world/__tests__

As Sonarqube supports the usage of wildcards in other analysis parameters (https://docs.sonarqube.org/latest/analysis/coverage/) I gave this configuration a try to mark these files as test code and to exclude them from analysis:

sonar.sources=src
sonar.tests=**/__tests__/*

The scan fails with
[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.7.0.1746:sonar (default-cli) on project hello-world: The directory '/var/lib/tomcat8/data/jenkins/jobs/hello-world/jobs/sonar/workspace/gui/**/__tests__/*' does not exist for Maven module [...]. Please check the property sonar.tests -> [Help 1]

Regarding the SonarQube documentation, this seems to be the expected result. The parameter sonar.tests is defined as “Comma-separated paths to directories containing test source files.”. No word about the option to use wildcards here.

But what would be the correct way to configure the scanner in this scenario?

Environment
sonar-maven-plugin: 3.7.0.1746
SonarQube version: 7.9.1

4 Likes

Hello,

Indeed SonarQube is not designed to seamlessly cope with situations where test code is not gathered in a dedicated directory structure. Probably an historical Java Maven bias…

Yet it’s possible to support your case with bit more configuration settings. The generally idea is that:

  1. You point a the same directories for the production and test code
  2. You use production / test code exclusion system (that support glob pattern wildcards) to point at 2 disjoint set of files for production and test code.

Here’s how you would do it for your case:

# src or src/components or src/components/hello-world depending on where is the root directory for your production and test code
sonar.sources=src 
sonar.tests=src
sonar.test.inclusions=**/__test__/**
sonar.exclusions=**/__test__/**

This should work. Let me know.

Olivier

5 Likes

Hi Olivier,
perfect!
This did the trick. Thank you very much.
Felix

Glad to read this; Enjoy!