aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api-impl
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2021-12-03 10:56:25 +0100
committersonartech <sonartech@sonarsource.com>2021-12-03 20:03:32 +0000
commit1ffcc9a85c962e716b3c7e0dd73e0a955dd36c69 (patch)
tree345e052939548cd2fa8592755080e7acfe070eaf /sonar-plugin-api-impl
parent0c2ad3d5d708b3d34049767d5b37e7f5d7939325 (diff)
downloadsonarqube-1ffcc9a85c962e716b3c7e0dd73e0a955dd36c69.tar.gz
sonarqube-1ffcc9a85c962e716b3c7e0dd73e0a955dd36c69.zip
SONAR-15761 new API to identify Sensors processing files independently (#5091)
Diffstat (limited to 'sonar-plugin-api-impl')
-rw-r--r--sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptor.java28
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptorTest.java29
2 files changed, 39 insertions, 18 deletions
diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptor.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptor.java
index e0819050190..558dfe17c1f 100644
--- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptor.java
+++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptor.java
@@ -27,16 +27,17 @@ import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
+
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.config.Configuration;
public class DefaultSensorDescriptor implements SensorDescriptor {
- public static final Set<String> SENSORS_ONLY_CHANGED_IN_PR = Collections.unmodifiableSet(Stream.of(
- "CSS Metrics",
- "CSS Rules",
- "HTML",
- "XML Sensor"
+ public static final Set<String> HARDCODED_INDEPENDENT_FILE_SENSORS = Collections.unmodifiableSet(Stream.of(
+ "CSS Metrics",
+ "CSS Rules",
+ "HTML",
+ "XML Sensor"
).collect(Collectors.toSet()));
private String name;
@@ -45,7 +46,7 @@ public class DefaultSensorDescriptor implements SensorDescriptor {
private String[] ruleRepositories = new String[0];
private boolean global = false;
private Predicate<Configuration> configurationPredicate;
- private boolean onlyChangedFilesInPullRequests = false;
+ private boolean processesFilesIndependently = false;
public String name() {
return name;
@@ -72,15 +73,15 @@ public class DefaultSensorDescriptor implements SensorDescriptor {
return global;
}
- public boolean onlyChangedFilesInPullRequest() {
- return onlyChangedFilesInPullRequests;
+ public boolean isProcessesFilesIndependently() {
+ return processesFilesIndependently;
}
@Override
public DefaultSensorDescriptor name(String name) {
- // TODO: Add onlyChangedFilesInPullRequest into API and implement it at sensors
- if (SENSORS_ONLY_CHANGED_IN_PR.contains(name)) {
- onlyChangedFilesInPullRequests = true;
+ // TODO: Remove this hardcoded list once all plugins will implement the new API "processFilesIndependently"
+ if (HARDCODED_INDEPENDENT_FILE_SENSORS.contains(name)) {
+ processesFilesIndependently = true;
}
this.name = name;
return this;
@@ -126,4 +127,9 @@ public class DefaultSensorDescriptor implements SensorDescriptor {
return this;
}
+ @Override
+ public SensorDescriptor processesFilesIndependently() {
+ this.processesFilesIndependently = true;
+ return this;
+ }
}
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptorTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptorTest.java
index e34c5385cad..a5cd716a04e 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptorTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptorTest.java
@@ -33,6 +33,19 @@ import static org.assertj.core.api.Assertions.assertThat;
public class DefaultSensorDescriptorTest {
@Test
+ public void describe_defaults() {
+ DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor();
+ descriptor
+ .name("Foo");
+
+ assertThat(descriptor.name()).isEqualTo("Foo");
+ assertThat(descriptor.languages()).isEmpty();
+ assertThat(descriptor.type()).isNull();
+ assertThat(descriptor.ruleRepositories()).isEmpty();
+ assertThat(descriptor.isProcessesFilesIndependently()).isFalse();
+ }
+
+ @Test
public void describe() {
DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor();
descriptor
@@ -40,7 +53,8 @@ public class DefaultSensorDescriptorTest {
.onlyOnLanguage("java")
.onlyOnFileType(InputFile.Type.MAIN)
.onlyWhenConfiguration(c -> c.hasKey("sonar.foo.reportPath2") && c.hasKey("sonar.foo.reportPath"))
- .createIssuesForRuleRepository("squid-java");
+ .createIssuesForRuleRepository("squid-java")
+ .processesFilesIndependently();
assertThat(descriptor.name()).isEqualTo("Foo");
assertThat(descriptor.languages()).containsOnly("java");
@@ -51,31 +65,32 @@ public class DefaultSensorDescriptorTest {
settings.setProperty("sonar.foo.reportPath2", "foo");
assertThat(descriptor.configurationPredicate().test(settings.asConfig())).isTrue();
assertThat(descriptor.ruleRepositories()).containsOnly("squid-java");
+ assertThat(descriptor.isProcessesFilesIndependently()).isTrue();
}
@Test
- @UseDataProvider("sensorsOnlyChangedInPR")
+ @UseDataProvider("independentFilesSensors")
public void describe_with_restricted_sensor(String sensorName) {
DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor();
descriptor
.name(sensorName);
- assertThat(descriptor.onlyChangedFilesInPullRequest()).isTrue();
+ assertThat(descriptor.isProcessesFilesIndependently()).isTrue();
}
@Test
- @UseDataProvider("sensorsOnlyChangedInPR")
+ @UseDataProvider("independentFilesSensors")
public void describe_with_non_restricted_sensor(String sensorName) {
DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor();
descriptor
.name(sensorName + "other");
- assertThat(descriptor.onlyChangedFilesInPullRequest()).isFalse();
+ assertThat(descriptor.isProcessesFilesIndependently()).isFalse();
}
@DataProvider
- public static Object[][] sensorsOnlyChangedInPR() {
- return new Object[][] {DefaultSensorDescriptor.SENSORS_ONLY_CHANGED_IN_PR.toArray()};
+ public static Object[][] independentFilesSensors() {
+ return new Object[][] {DefaultSensorDescriptor.HARDCODED_INDEPENDENT_FILE_SENSORS.toArray()};
}
}