I have installed SQ community edition, and I wonder if it’s possible to use a negative lookahead in a RegEx within the sonar.coverage.exclude so that certain classes or directories are not excluded? Long story short, what I am trying to achieve is exclude all from coverage but this and that.
Furthermore, sonar.java.skipUnchanged, does that also apply to coverage within PR analysis?
sonar.coverage.exclusions can use file path patterns, which are similar to but not exactly the same as regular expressions.
The file path patterns are more like glob patterns than traditional regex.
e.g. **/test/** - Excludes all files in test directories **/*.test.ts,**/*.test.js,**/*.test.jsx,**/*.test.tsx - Excludes test files with specific extensions
The inclusion/exclusion rules use Ant style patterns. Something you can do to make configuration a little simpler on your side is to pass environment variables to sonar-project.properties and have a wrapper expand the variables.
For example in your case your properties files could have:
And a shell wrapper (bash syntax, allows for lists):
extra_excludes_ext=( ts js jsx tsx )
SONAR_EXTRA_EXCLUDES=''
for ext in "${extra_excludes_ext[@]}"; do
SONAR_EXTRA_EXCLUDES="${SONAR_EXTRA_EXCLUDES},**/*.test.${ext}"
done
sonar-scanner ...
We have custom shell libraries for joining shell strings to avoid the extra comma, but that’s not required, AFAIK sonar will ignore the empty entries… You can have the logic in whatever language you want as long as you can export the environment variables.