Skip to content

Commit

Permalink
fix: fixed the calculation of picnic rules key (#11)
Browse files Browse the repository at this point in the history
* fix: fixed the calculation of picnic rules key

Look for the picnic doc url in the error message so we can locate the
rule from the correct repository.
Updated the integration tests to look for picnic issues
  • Loading branch information
gtoison authored Dec 15, 2022
1 parent 6c198f5 commit 45a20f5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
String message = diagnostic.getMessage(Locale.ENGLISH);
String rule = parseRule(diagnostic, message);

RuleKey ruleKey = RuleKey.of(findRepository(rule), rule);
RuleKey ruleKey = RuleKey.of(findRepository(rule, message), rule);

int startLine = (int) diagnostic.getLineNumber();

Expand Down Expand Up @@ -148,10 +148,14 @@ private String parseRule(Diagnostic<? extends JavaFileObject> diagnostic, String
}
}

private String findRepository(String rule) {
private String findRepository(String rule, String message) {
if (rule.startsWith("Slf4j")) {
return ErrorAwayRulesDefinition.ERRORPRONE_SLF4J_REPOSITORY;
}

if (message.contains("see https://error-prone.picnic.tech/bugpatterns/")) {
return ErrorAwayRulesDefinition.PICNIC_REPOSITORY;
}

switch (rule) {
case "NullAway":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,22 +229,25 @@ void analyzeWithErrorProneSlf4j() {
setConfigurationStringArray(ErrorAwayPlugin.MAVEN_REPOSITORIES, new String[] {"https://repo1.maven.org/maven2/"});
setup(Path.of("com/bug/Slf4jSamples.java"));

enableRule(RuleKey.of("errorprone-slf4j", "Slf4jPlaceholderMismatch"));
RuleKey ruleKey = RuleKey.of("errorprone-slf4j", "Slf4jPlaceholderMismatch");
enableRule(ruleKey);
setConfigurationStringArray(ErrorAwayPlugin.CLASS_PATH_MAVEN_COORDINATES, new String[]{"org.slf4j:slf4j-api:1.7.36"});

// Call the sensor
ErrorAwaySensor sensor = new ErrorAwaySensor(javaResourceLocator, dependencyManager, tempFolder);
sensor.execute(context);

verify(context, times(1)).newIssue();
verify(newIssue, times(1)).forRule(ruleKey);
}

@Test
void analyzeWithAutodispose2() {
setConfigurationStringArray(ErrorAwayPlugin.MAVEN_REPOSITORIES, new String[] {"https://repo1.maven.org/maven2/"});
setup(Path.of("com/bug/AndroidActivity.java"));

enableRule(RuleKey.of("autodispose2", "AutoDispose"));
RuleKey ruleKey = RuleKey.of("autodispose2", "AutoDispose");
enableRule(ruleKey);
setConfigurationStringArray(ErrorAwayPlugin.CLASS_PATH_MAVEN_COORDINATES, new String[]{
"com.google.android:android:4.1.1.4",
"io.reactivex.rxjava3:rxjava:3.1.4"
Expand All @@ -255,8 +258,28 @@ void analyzeWithAutodispose2() {
sensor.execute(context);

verify(context, times(1)).newIssue();
verify(newIssue, times(1)).forRule(ruleKey);
}


@Test
void analyzeWithPicnicErrorProneSupport() {
setConfigurationStringArray(ErrorAwayPlugin.MAVEN_REPOSITORIES, new String[]{"https://repo1.maven.org/maven2/"});
setup(Path.of("com/bug/PicnicErrorProneSupportSample.java"));

RuleKey ruleKey = RuleKey.of(ErrorAwayRulesDefinition.PICNIC_REPOSITORY, "IdentityConversion");
enableRule(ruleKey);
setConfigurationStringArray(
ErrorAwayPlugin.CLASS_PATH_MAVEN_COORDINATES,
new String[]{"com.google.guava:guava:31.1-jre"});

// Call the sensor
ErrorAwaySensor sensor = new ErrorAwaySensor(javaResourceLocator, dependencyManager, tempFolder);
sensor.execute(context);

verify(context, times(1)).newIssue();
verify(newIssue, times(1)).forRule(ruleKey);
}

@Test
void analyzeManyBugs() {
setup(Path.of("com/bug/ManyBugs.java"));
Expand Down
30 changes: 19 additions & 11 deletions src/test/java/com/github/erroraway/sonarqube/it/ErrorAwayIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ public class ErrorAwayIT {
@BeforeAll
public static void startOrchestrator() {
String sonarVersion = System.getProperty("sonar.server.version", "9.5");
OrchestratorBuilder orchestratorBuilder = Orchestrator.builderEnv()
OrchestratorBuilder orchestratorBuilder = Orchestrator.builderEnv()
.addPlugin(FileLocation.of("./target/sonar-erroraway-plugin.jar"))
.keepBundledPlugins()
.keepBundledPlugins()
.setServerProperty("sonar.web.port", "9000")
.setSonarVersion("LATEST_RELEASE[" + sonarVersion + "]");

ORCHESTRATOR = orchestratorBuilder.build();
Expand Down Expand Up @@ -145,15 +146,16 @@ private void checkIssues(String projectKey) {
issueRequest.setProjects(Collections.singletonList(projectKey));
List<Issue> issues = ISSUES_SERVICES.search(issueRequest).getIssuesList();

assertThat(issues.size(), is(23));
assertThat(issues.size(), is(24));

assertSimpleIssues(issues, projectKey);
assertAndroidActivityIssues(issues, projectKey);
assertApplicationSimpleIssues(issues, projectKey);
assertBugsSamplesIssues(issues, projectKey);
assertHibernateEntityIssues(issues, projectKey);
assertAutoValueSamplesIssues(issues, projectKey);
assertGrammarListenerIssues(issues, projectKey);
assertPicnicSamplesIssues(issues, projectKey);
assertGrammarListenerIssues(issues, projectKey);
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -202,18 +204,24 @@ private void assertHibernateEntityIssues(List<Issue> issues, String projectKey)
assertThat(issues, containsIssueMatching(applicationSimpleJavaPredicate, rule("nullaway:NullAway"), startLine(32)));
}

@SuppressWarnings("unchecked")
private void assertAutoValueSamplesIssues(List<Issue> issues, String projectKey) {
Predicate<Issue> applicationSimpleJavaPredicate = component(projectKey, "src/main/java/application/AutoValueSamples.java");
assertThat(issues, containsIssueMatching(applicationSimpleJavaPredicate, rule("errorprone:DurationTemporalUnit"), startLine(13)));
}

@SuppressWarnings("unchecked")
private void assertPicnicSamplesIssues(List<Issue> issues, String projectKey) {
Predicate<Issue> applicationSimpleJavaPredicate = component(projectKey, "src/main/java/application/GrammarListener.java");
assertThat(issues, containsIssueMatching(applicationSimpleJavaPredicate, rule("picnic-errorprone:EmptyMethod"), startLine(12)));
}

@SuppressWarnings("unchecked")
private void assertGrammarListenerIssues(List<Issue> issues, String projectKey) {
Predicate<Issue> applicationSimpleJavaPredicate = component(projectKey, "src/main/java/application/GrammarListener.java");
assertThat(issues, containsIssueMatching(applicationSimpleJavaPredicate, rule("errorprone:MissingOverride"), startLine(8)));
}

@SuppressWarnings("unchecked")
private void assertAutoValueSamplesIssues(List<Issue> issues, String projectKey) {
Predicate<Issue> applicationSimpleJavaPredicate = component(projectKey, "src/main/java/application/AutoValueSamples.java");
assertThat(issues, containsIssueMatching(applicationSimpleJavaPredicate, rule("errorprone:DurationTemporalUnit"), startLine(13)));
}

@SuppressWarnings("unchecked")
private Matcher<List<Issue>> containsIssueMatching(Predicate<Issue>... issuePredicates) {
Predicate<Issue> issuePredicate = i -> Stream.of(issuePredicates).allMatch(p -> p.test(i));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.bug;

import com.google.common.collect.ImmutableSet;
import java.math.BigDecimal;

public class PicnicErrorProneSupportSample {
static BigDecimal getNumber() {
return BigDecimal.valueOf(0);
}

public ImmutableSet<Integer> getSet() {
ImmutableSet<Integer> set = ImmutableSet.of(1);
return ImmutableSet.copyOf(set);
}
}

0 comments on commit 45a20f5

Please sign in to comment.