Deactivate Rules based on directory pattern

Hello, I’m trying to deactivate a set of rules for our test files. However, the rules should still be applicable for the rest of the project. Ideally, I would want to set in a quality gate something like, “trigger if file path is/ is not ” or something similar.

Is something like this possible?

I’m using v10.0.0.68432
Thank you.

Hi,

There are a couple ways to approach this.

You don’t mention your language. For most languages, “normal” rules don’t run on test code. So it sounds like your test files aren’t properly identified as such for analysis. You can tackle that by defining the sonar.tests analysis parameter. You’ll want to make sure it doesn’t overlap with the set described by your sonar.sources property. Depending on how your project is structured, exclusions may be needed for that.

As a side note, lines of test code aren’t counted toward your license usage, so if you’re in a commercial edition of SonarQube, correctly identifying your test files as such will have additional benefits.

So that’s the “proper” way of handling this. The quick and dirty way would be to define a multi-criteria issue exclusion. That allows you to say “Don’t apply these specific rules to those specific files.”

 
HTH,
Ann

Hi,
Thanks for the response.

We are using PHP, my bad for not mentioning this.
I’ve tried in the past using the sonar.tests property but it would overlap with the sonar.source, and sonar.exclusion also didn’t seem to help.
What seems to be working is instead of setting sonar.source=. and then exclude all sort of directories, test as well, we just set multiple directories as the source (sonar.source=dir1,dir2), the test directory as sonar.tests and got rid of exclusions.

With the proper sonar.source and sonar.tests configured, it looks like our problem with the rules is gone.

Thank you!

2 Likes

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