diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2017-07-31 18:44:48 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2017-08-04 14:58:16 +0200 |
commit | 25aaeb2880043aeb8c219bcc2d178041e76062b1 (patch) | |
tree | d99c993fd7657d0dd10023d1447e84f6c77ff185 /sonar-plugin-api | |
parent | 34b0fd70f5c0e5b4298cba0b2fefe4a1688f7622 (diff) | |
download | sonarqube-25aaeb2880043aeb8c219bcc2d178041e76062b1.tar.gz sonarqube-25aaeb2880043aeb8c219bcc2d178041e76062b1.zip |
SONAR-9561 File inclusions/exclusions should be applied before language detection
Diffstat (limited to 'sonar-plugin-api')
4 files changed, 33 insertions, 29 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/PathPattern.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/PathPattern.java index 5ec259f0021..d91829c5404 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/PathPattern.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/PathPattern.java @@ -19,29 +19,31 @@ */ package org.sonar.api.batch.fs.internal; +import java.nio.file.Path; import javax.annotation.concurrent.ThreadSafe; - import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang.StringUtils; +import org.sonar.api.utils.PathUtils; import org.sonar.api.utils.WildcardPattern; @ThreadSafe public abstract class PathPattern { + private static final String ABSOLUTE_PATH_PATTERN_PREFIX = "file:"; final WildcardPattern pattern; PathPattern(String pattern) { this.pattern = WildcardPattern.create(pattern); } - public abstract boolean match(String absolutePath, String relativePath); + public abstract boolean match(Path absolutePath, Path relativePath); - public abstract boolean match(String absolutePath, String relativePath, boolean caseSensitiveFileExtension); + public abstract boolean match(Path absolutePath, Path relativePath, boolean caseSensitiveFileExtension); public static PathPattern create(String s) { String trimmed = StringUtils.trim(s); - if (StringUtils.startsWithIgnoreCase(trimmed, "file:")) { - return new AbsolutePathPattern(StringUtils.substring(trimmed, "file:".length())); + if (StringUtils.startsWithIgnoreCase(trimmed, ABSOLUTE_PATH_PATTERN_PREFIX)) { + return new AbsolutePathPattern(StringUtils.substring(trimmed, ABSOLUTE_PATH_PATTERN_PREFIX.length())); } return new RelativePathPattern(trimmed); } @@ -60,15 +62,15 @@ public abstract class PathPattern { } @Override - public boolean match(String absolutePath, String relativePath) { + public boolean match(Path absolutePath, Path relativePath) { return match(absolutePath, relativePath, true); } @Override - public boolean match(String absolutePath, String relativePath, boolean caseSensitiveFileExtension) { - String path = absolutePath; + public boolean match(Path absolutePath, Path relativePath, boolean caseSensitiveFileExtension) { + String path = PathUtils.sanitize(absolutePath.toString()); if (!caseSensitiveFileExtension) { - String extension = sanitizeExtension(FilenameUtils.getExtension(relativePath)); + String extension = sanitizeExtension(FilenameUtils.getExtension(path)); if (StringUtils.isNotBlank(extension)) { path = StringUtils.removeEndIgnoreCase(path, extension); path = path + extension; @@ -79,7 +81,7 @@ public abstract class PathPattern { @Override public String toString() { - return "file:" + pattern.toString(); + return ABSOLUTE_PATH_PATTERN_PREFIX + pattern.toString(); } } @@ -92,15 +94,15 @@ public abstract class PathPattern { } @Override - public boolean match(String absolutePath, String relativePath) { + public boolean match(Path absolutePath, Path relativePath) { return match(absolutePath, relativePath, true); } @Override - public boolean match(String absolutePath, String relativePath, boolean caseSensitiveFileExtension) { - String path = relativePath; + public boolean match(Path absolutePath, Path relativePath, boolean caseSensitiveFileExtension) { + String path = PathUtils.sanitize(relativePath.toString()); if (!caseSensitiveFileExtension) { - String extension = sanitizeExtension(FilenameUtils.getExtension(relativePath)); + String extension = sanitizeExtension(FilenameUtils.getExtension(path)); if (StringUtils.isNotBlank(extension)) { path = StringUtils.removeEndIgnoreCase(path, extension); path = path + extension; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/PathPatternPredicate.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/PathPatternPredicate.java index e6b4a9246e3..7a5a7eb839f 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/PathPatternPredicate.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/PathPatternPredicate.java @@ -19,6 +19,7 @@ */ package org.sonar.api.batch.fs.internal; +import java.nio.file.Paths; import org.sonar.api.batch.fs.InputFile; /** @@ -34,7 +35,7 @@ class PathPatternPredicate extends AbstractFilePredicate { @Override public boolean apply(InputFile f) { - return pattern.match(f.absolutePath(), f.relativePath()); + return pattern.match(f.path(), Paths.get(f.relativePath())); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokens.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokens.java index 88e9c1d6b13..57a5340f600 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokens.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokens.java @@ -19,6 +19,7 @@ */ package org.sonar.api.batch.sensor.cpd.internal; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import org.sonar.api.CoreProperties; @@ -57,7 +58,7 @@ public class DefaultCpdTokens extends DefaultStorable implements NewCpdTokens { this.inputFile = requireNonNull(inputFile, "file can't be null"); String[] cpdExclusions = config.getStringArray(CoreProperties.CPD_EXCLUSIONS); for (PathPattern cpdExclusion : PathPattern.create(cpdExclusions)) { - if (cpdExclusion.match(inputFile.absolutePath(), inputFile.relativePath())) { + if (cpdExclusion.match(inputFile.path(), Paths.get(inputFile.relativePath()))) { this.excluded = true; } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/PathPatternTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/PathPatternTest.java index fac72032858..8331630e672 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/PathPatternTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/PathPatternTest.java @@ -19,15 +19,15 @@ */ package org.sonar.api.batch.fs.internal; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.api.batch.fs.IndexedFile; -import java.io.IOException; -import java.nio.file.Path; - import static org.assertj.core.api.Assertions.assertThat; public class PathPatternTest { @@ -46,14 +46,14 @@ public class PathPatternTest { assertThat(pattern.toString()).isEqualTo("**/*Foo.java"); IndexedFile indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/MyFoo.java", null); - assertThat(pattern.match(indexedFile.absolutePath(), indexedFile.relativePath())).isTrue(); + assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()))).isTrue(); // case sensitive by default indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/MyFoo.JAVA", null); - assertThat(pattern.match(indexedFile.absolutePath(), indexedFile.relativePath())).isFalse(); + assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()))).isFalse(); indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/Other.java", null); - assertThat(pattern.match(indexedFile.absolutePath(), indexedFile.relativePath())).isFalse(); + assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()))).isFalse(); } @Test @@ -61,10 +61,10 @@ public class PathPatternTest { PathPattern pattern = PathPattern.create("**/*Foo.java"); IndexedFile indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/MyFoo.JAVA", null); - assertThat(pattern.match(indexedFile.absolutePath(), indexedFile.relativePath(), false)).isTrue(); + assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()), false)).isTrue(); indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/Other.java", null); - assertThat(pattern.match(indexedFile.absolutePath(), indexedFile.relativePath(), false)).isFalse(); + assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()), false)).isFalse(); } @Test @@ -73,14 +73,14 @@ public class PathPatternTest { assertThat(pattern.toString()).isEqualTo("file:**/src/main/**Foo.java"); IndexedFile indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/MyFoo.java", null); - assertThat(pattern.match(indexedFile.absolutePath(), indexedFile.relativePath())).isTrue(); + assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()))).isTrue(); // case sensitive by default indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/MyFoo.JAVA", null); - assertThat(pattern.match(indexedFile.absolutePath(), indexedFile.relativePath())).isFalse(); + assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()))).isFalse(); indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/Other.java", null); - assertThat(pattern.match(indexedFile.absolutePath(), indexedFile.relativePath())).isFalse(); + assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()))).isFalse(); } @Test @@ -89,10 +89,10 @@ public class PathPatternTest { assertThat(pattern.toString()).isEqualTo("file:**/src/main/**Foo.java"); IndexedFile indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/MyFoo.JAVA", null); - assertThat(pattern.match(indexedFile.absolutePath(), indexedFile.relativePath(), false)).isTrue(); + assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()), false)).isTrue(); indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/Other.JAVA", null); - assertThat(pattern.match(indexedFile.absolutePath(), indexedFile.relativePath(), false)).isFalse(); + assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()), false)).isFalse(); } @Test |