Skip to content

Commit

Permalink
SONARJAVA-4022 FP S5960 when analyzing package containing (#4875)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardo-pilastri-sonarsource authored Sep 20, 2024
1 parent bb0b8f6 commit 9bfb7e6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.regex.Pattern;
import org.sonar.check.Rule;
import org.sonar.java.annotations.VisibleForTesting;
import org.sonar.java.checks.helpers.ExpressionsHelper;
import org.sonar.java.checks.helpers.UnitTestUtils;
import org.sonar.java.checks.methods.AbstractMethodDetection;
Expand All @@ -37,7 +38,8 @@
@Rule(key = "S5960")
public class AssertionsInProductionCodeCheck extends AbstractMethodDetection {

private static final Pattern TEST_PACKAGE_REGEX = Pattern.compile("test|junit|assert");
@VisibleForTesting
static final Pattern TEST_PACKAGE_REGEX = Pattern.compile("test|junit|assert|\\.it(?:\\.|$)");
private final List<Tree> assertions = new ArrayList<>();
private boolean packageNameNotRelatedToTests = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
*/
package org.sonar.java.checks;

import java.util.List;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;

import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.java.checks.verifier.TestUtils.mainCodeSourcesPath;
import static org.sonar.java.checks.verifier.TestUtils.testCodeSourcesPath;

Expand Down Expand Up @@ -52,4 +54,26 @@ void test_without_semantic() {
.verifyNoIssues();
}

@Test
void test_package_name_regex() {
var productionPackageNames = List.of(
"org.sonar.java.checks",
"it.sonar.java.checks");
var testPackageNames = List.of(
"org.sonar.java.checks.test",
"org.sonar.test.checks",
"org.sonar.java.checks.junit",
"assert.org.sonar.java",
"org.sonar.java.it",
"assert",
"test",
"junit");
for (String packageName : productionPackageNames) {
assertThat(AssertionsInProductionCodeCheck.TEST_PACKAGE_REGEX.matcher(packageName).find()).isFalse();
}
for (String packageName : testPackageNames) {
assertThat(AssertionsInProductionCodeCheck.TEST_PACKAGE_REGEX.matcher(packageName).find()).isTrue();
}
}

}

0 comments on commit 9bfb7e6

Please sign in to comment.