versions used (SonarQube, Scanner, language analyzer)
SonarQube : 8.7.0.41497
SonarLint : 4.14.2.28348
Intellij IDEA : 2020.3.3
Scanner : 3.1.1
How can I define the “scope” for my custom java rules?
We are using SonarQube as a main source for our code quality. As we maintain a quite large code base with a main application consisting of several modules, we also try to make sure, that our unit tests are of high quality.
Because of this, I implemented a custom rule for making sure, that @Ignore in JUnit tests is only used according to our coding rules. I implemented this rule just the same way, the other rules I did. But then I found out, the the rule was only appied to the main java source sets (defined in the sonar runner property sonar.sources) and not the test source sets (defined in the property sonar.tests).
Searching for the reason of the behavior, I figured out that the built-in java rules use a json file for defining the rules (rule name, type, status, tags etc.). And within these json configuration of a rule, there is also the property “scope” with the possible values “All”, “Main” and “Tests”. Looking at the issues in our code base I came to the conclusion, that only rules having the scope “All” or “Tests” are applied to the java files in the test source set.
{
"title": "JUnit5 inner test classes should be annotated with @Nested",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "2min"
},
"tags": [
"junit",
"tests"
],
"defaultSeverity": "Critical",
"ruleSpecification": "RSPEC-5790",
"sqKey": "S5790",
"scope": "Tests"
}
Obviously my custom rules are only applied on the “Main” java source set and not on the test source set. And the @Rule Annotation does not offer a property for defining a “scope” of a custom rule:
package org.sonar.check;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Rule {
String key() default "";
String name() default "";
String description() default "";
Priority priority() default Priority.MAJOR;
Cardinality cardinality() default Cardinality.SINGLE;
String status() default "READY";
String[] tags() default {};
}
My Question
Is there a way to define the scope of a custom rule so that the rule is applied to the test source set?