Skip to content

Commit

Permalink
Add mechanism to enable checks on main and test code at the same time (
Browse files Browse the repository at this point in the history
  • Loading branch information
johann-beleites-sonarsource authored Apr 16, 2024
1 parent b0515a4 commit 92f27d6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.sonar.plugins.java;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -1380,8 +1381,12 @@ public final class CheckList {
TooManyAssertionsCheck.class,
UnusedTestRuleCheck.class);

private static final List<Class<?>> ALL_CHECKS = Stream.of(JAVA_MAIN_CHECKS, JAVA_TEST_CHECKS)
.flatMap(List::stream).collect(Collectors.toList());
private static final List<Class<? extends JavaCheck>> JAVA_MAIN_AND_TEST_CHECKS = Arrays.asList();

private static final List<Class<?>> ALL_CHECKS = Stream.of(JAVA_MAIN_CHECKS, JAVA_MAIN_AND_TEST_CHECKS, JAVA_TEST_CHECKS)
.flatMap(List::stream)
.sorted(Comparator.comparing(Class::getSimpleName))
.collect(Collectors.toList());

private static final Set<Class<? extends JavaCheck>> JAVA_CHECKS_NOT_WORKING_FOR_AUTOSCAN = Set.of(
// Symbolic executions rules are not in this list because they are dynamically excluded
Expand Down Expand Up @@ -1439,15 +1444,22 @@ public static List<Class<?>> getChecks() {
}

public static List<Class<? extends JavaCheck>> getJavaChecks() {
return JAVA_MAIN_CHECKS;
return sortedJoin(JAVA_MAIN_CHECKS, JAVA_MAIN_AND_TEST_CHECKS);
}

public static List<Class<? extends JavaCheck>> getJavaTestChecks() {
return JAVA_TEST_CHECKS;
return sortedJoin(JAVA_MAIN_AND_TEST_CHECKS, JAVA_TEST_CHECKS);
}

public static Set<Class<? extends JavaCheck>> getJavaChecksNotWorkingForAutoScan() {
return JAVA_CHECKS_NOT_WORKING_FOR_AUTOSCAN;
}

@SafeVarargs
private static List<Class<? extends JavaCheck>> sortedJoin(List<Class<? extends JavaCheck>>... lists) {
return Arrays.stream(lists)
.flatMap(List::stream)
.sorted(Comparator.comparing(Class::getSimpleName))
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ void enforce_CheckList_registration() {
@Test
void rules_targeting_tests_should_have_tests_tag() throws Exception {
Set<Class<? extends JavaCheck>> testChecks = new HashSet<>(CheckList.getJavaTestChecks());
Set<Class<? extends JavaCheck>> mainChecks = new HashSet<>(CheckList.getJavaChecks());

for (Class<?> cls : CheckList.getChecks()) {
String key = AnnotationUtils.getAnnotation(cls, Rule.class).key();
Expand All @@ -205,7 +206,7 @@ void rules_targeting_tests_should_have_tests_tag() throws Exception {

if (!"deprecated".equals(metadata.status)) {
// deprecated rules usually have no tags
if (testChecks.contains(cls) || "S3414".equals(key)) {
if ((testChecks.contains(cls) && !mainChecks.contains(cls)) || "S3414".equals(key)) {
assertThat(metadata.tags)
.as("Rule " + key + " is targeting tests sources and should contain the 'tests' tag.")
.contains("tests");
Expand Down

0 comments on commit 92f27d6

Please sign in to comment.