orchestrator.getServer().associateProjectToQualityProfile("com.sonarsource.it.samples:multi-modules-exclusions", "xoo", "with-many-rules");
SonarScanner scan = SonarScanner.create(ItUtils.projectDir(PROJECT_DIR))
- .setProperties("sonar.cpd.skip", "true")
+ .setProperty("sonar.cpd.exclusions", "**/*")
.setProperties(properties)
- .setProperties("sonar.verbose", "true");
+ .setProperty("sonar.verbose", "true");
return orchestrator.executeBuildQuietly(scan);
}
private void scanSample(@Nullable String date, @Nullable String profile) {
SonarScanner scan = SonarScanner.create(projectDir("shared/xoo-sample"))
- .setProperties("sonar.cpd.skip", "true");
+ .setProperty("sonar.cpd.exclusions", "**/*");
if (date != null) {
scan.setProperty("sonar.projectDate", date);
}
private void scanSample(@Nullable String date, @Nullable String profile) {
SonarScanner scan = SonarScanner.create(projectDir("shared/xoo-sample"))
- .setProperties("sonar.cpd.skip", "true");
+ .setProperty("sonar.cpd.exclusions", "**/*");
if (date != null) {
scan.setProperty("sonar.projectDate", date);
}
/* CPD */
String CPD_PLUGIN = "cpd";
- /**
- * @deprecated in 5.0
- * @see <a href="https://jira.sonarsource.com/browse/SONAR-5339">SONAR-5339</a>
- */
- @Deprecated
- String CPD_SKIP_PROPERTY = "sonar.cpd.skip";
-
/**
* @since 2.11
*/
public DefaultCpdTokens onFile(InputFile inputFile) {
Preconditions.checkNotNull(inputFile, "file can't be null");
this.inputFile = (DefaultInputFile) inputFile;
- String language = inputFile.language();
- if (language != null && isSkipped(language)) {
- this.excluded = true;
- } else {
- String[] cpdExclusions = settings.getStringArray(CoreProperties.CPD_EXCLUSIONS);
- for (PathPattern cpdExclusion : PathPattern.create(cpdExclusions)) {
- if (cpdExclusion.match(inputFile)) {
- this.excluded = true;
- }
+ String[] cpdExclusions = settings.getStringArray(CoreProperties.CPD_EXCLUSIONS);
+ for (PathPattern cpdExclusion : PathPattern.create(cpdExclusions)) {
+ if (cpdExclusion.match(inputFile)) {
+ this.excluded = true;
}
}
return this;
}
- boolean isSkipped(String language) {
- String key = "sonar.cpd." + language + ".skip";
- if (settings.hasKey(key)) {
- return settings.getBoolean(key);
- }
- return settings.getBoolean(CoreProperties.CPD_SKIP_PROPERTY);
- }
-
public InputFile inputFile() {
return inputFile;
}
assertThat(tokens.getTokenLines()).isEmpty();
}
- @Test
- public void handle_exclusions_by_language() {
- SensorStorage sensorStorage = mock(SensorStorage.class);
- Settings settings = new Settings();
- settings.setProperty("sonar.cpd.java.skip", "true");
- DefaultCpdTokens tokens = new DefaultCpdTokens(settings, sensorStorage)
- .onFile(INPUT_FILE)
- .addToken(INPUT_FILE.newRange(1, 2, 1, 5), "foo");
-
- tokens.save();
-
- verifyZeroInteractions(sensorStorage);
-
- assertThat(tokens.getTokenLines()).isEmpty();
- }
-
- @Test
- public void handle_exclusions() {
- SensorStorage sensorStorage = mock(SensorStorage.class);
- Settings settings = new Settings();
- settings.setProperty("sonar.cpd.skip", "true");
- DefaultCpdTokens tokens = new DefaultCpdTokens(settings, sensorStorage)
- .onFile(INPUT_FILE)
- .addToken(INPUT_FILE.newRange(1, 2, 1, 5), "foo");
-
- tokens.save();
-
- verifyZeroInteractions(sensorStorage);
-
- assertThat(tokens.getTokenLines()).isEmpty();
- }
-
@Test
public void save_many_tokens() {
SensorStorage sensorStorage = mock(SensorStorage.class);
import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.sonar.api.CoreProperties;
import org.sonar.api.batch.CpdMapping;
import org.sonar.api.batch.Phase;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
-import org.sonar.api.config.Settings;
/**
* Feed block index using deprecated {@link CpdMapping} extension point if not already
private CpdBlockIndexer javaCpdBlockIndexer;
private CpdBlockIndexer defaultCpdBlockIndexer;
- private Settings settings;
private FileSystem fs;
- public DeprecatedCpdBlockIndexerSensor(JavaCpdBlockIndexer javaCpdBlockIndexer, DefaultCpdBlockIndexer defaultCpdBlockIndexer, Settings settings, FileSystem fs) {
+ public DeprecatedCpdBlockIndexerSensor(JavaCpdBlockIndexer javaCpdBlockIndexer, DefaultCpdBlockIndexer defaultCpdBlockIndexer, FileSystem fs) {
this.javaCpdBlockIndexer = javaCpdBlockIndexer;
this.defaultCpdBlockIndexer = defaultCpdBlockIndexer;
- this.settings = settings;
this.fs = fs;
}
return defaultCpdBlockIndexer;
}
- @VisibleForTesting
- boolean isSkipped(String language) {
- String key = "sonar.cpd." + language + ".skip";
- if (settings.hasKey(key)) {
- return settings.getBoolean(key);
- }
- return settings.getBoolean(CoreProperties.CPD_SKIP_PROPERTY);
- }
-
@Override
public void execute(SensorContext context) {
- if (settings.hasKey(CoreProperties.CPD_SKIP_PROPERTY)) {
- LOG.warn("\"sonar.cpd.skip\" property is deprecated and will be removed. Please set \"sonar.cpd.exclusions=**\" instead to disable duplication mechanism.");
- }
for (String language : fs.languages()) {
- if (settings.hasKey("sonar.cpd." + language + ".skip")) {
- LOG
- .warn("\"sonar.cpd." + language + ".skip\" property is deprecated and will be removed. Please set \"sonar.cpd.exclusions=**\" instead to disable duplication mechanism.");
- }
-
- if (isSkipped(language)) {
- LOG.info("Detection of duplicated code is skipped for {}", language);
- continue;
- }
-
CpdBlockIndexer blockIndexer = getBlockIndexer(language);
if (!blockIndexer.isLanguageSupported(language)) {
LOG.debug("Detection of duplicated code is not supported for {}", language);
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
-import org.sonar.api.config.PropertyDefinitions;
-import org.sonar.api.config.Settings;
import org.sonar.scanner.FakeJava;
-import org.sonar.scanner.cpd.CpdComponents;
import static org.assertj.core.api.Assertions.assertThat;
JavaCpdBlockIndexer sonarEngine;
DefaultCpdBlockIndexer sonarBridgeEngine;
DeprecatedCpdBlockIndexerSensor sensor;
- Settings settings;
@Before
public void setUp() throws IOException {
sonarEngine = new JavaCpdBlockIndexer(null, null, null);
sonarBridgeEngine = new DefaultCpdBlockIndexer(new CpdMappings(), null, null, null);
- settings = new Settings(new PropertyDefinitions(CpdComponents.class));
DefaultFileSystem fs = new DefaultFileSystem(temp.newFolder().toPath());
- sensor = new DeprecatedCpdBlockIndexerSensor(sonarEngine, sonarBridgeEngine, settings, fs);
- }
-
- @Test
- public void test_global_skip() {
- settings.setProperty("sonar.cpd.skip", true);
- assertThat(sensor.isSkipped(FakeJava.KEY)).isTrue();
- }
-
- @Test
- public void should_not_skip_by_default() {
- assertThat(sensor.isSkipped(FakeJava.KEY)).isFalse();
- }
-
- @Test
- public void should_skip_by_language() {
- settings.setProperty("sonar.cpd.skip", false);
- settings.setProperty("sonar.cpd.php.skip", true);
-
- assertThat(sensor.isSkipped("php")).isTrue();
- assertThat(sensor.isSkipped(FakeJava.KEY)).isFalse();
+ sensor = new DeprecatedCpdBlockIndexerSensor(sonarEngine, sonarBridgeEngine, fs);
}
@Test