Search in sources :

Example 1 with PathPattern

use of org.sonar.api.batch.fs.internal.PathPattern in project sonarqube by SonarSource.

the class DefaultCpdTokens method onFile.

@Override
public DefaultCpdTokens onFile(InputFile inputFile) {
    this.inputFile = requireNonNull(inputFile, "file can't be null");
    String[] cpdExclusions = settings.getStringArray(CoreProperties.CPD_EXCLUSIONS);
    for (PathPattern cpdExclusion : PathPattern.create(cpdExclusions)) {
        if (cpdExclusion.match(inputFile)) {
            this.excluded = true;
        }
    }
    return this;
}
Also used : PathPattern(org.sonar.api.batch.fs.internal.PathPattern)

Example 2 with PathPattern

use of org.sonar.api.batch.fs.internal.PathPattern in project sonarqube by SonarSource.

the class ExclusionFilters method accept.

public boolean accept(IndexedFile indexedFile, InputFile.Type type) {
    PathPattern[] inclusionPatterns;
    PathPattern[] exclusionPatterns;
    if (InputFile.Type.MAIN == type) {
        inclusionPatterns = mainInclusions;
        exclusionPatterns = mainExclusions;
    } else if (InputFile.Type.TEST == type) {
        inclusionPatterns = testInclusions;
        exclusionPatterns = testExclusions;
    } else {
        throw new IllegalArgumentException("Unknown file type: " + type);
    }
    if (inclusionPatterns.length > 0) {
        boolean matchInclusion = false;
        for (PathPattern pattern : inclusionPatterns) {
            matchInclusion |= pattern.match(indexedFile);
        }
        if (!matchInclusion) {
            return false;
        }
    }
    if (exclusionPatterns.length > 0) {
        for (PathPattern pattern : exclusionPatterns) {
            if (pattern.match(indexedFile)) {
                return false;
            }
        }
    }
    return true;
}
Also used : PathPattern(org.sonar.api.batch.fs.internal.PathPattern)

Example 3 with PathPattern

use of org.sonar.api.batch.fs.internal.PathPattern in project sonarqube by SonarSource.

the class PathPatternTest method match_absolute_path.

@Test
public void match_absolute_path() {
    PathPattern pattern = PathPattern.create("file:**/src/main/**Foo.java");
    assertThat(pattern.toString()).isEqualTo("file:**/src/main/**Foo.java");
    IndexedFile indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/main/java/org/MyFoo.java", null);
    assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()))).isTrue();
    // case sensitive by default
    indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/main/java/org/MyFoo.JAVA", null);
    assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()))).isFalse();
    indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/main/java/org/Other.java", null);
    assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()))).isFalse();
}
Also used : PathPattern(org.sonar.api.batch.fs.internal.PathPattern) IndexedFile(org.sonar.api.batch.fs.IndexedFile) DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) Test(org.junit.Test)

Example 4 with PathPattern

use of org.sonar.api.batch.fs.internal.PathPattern in project sonarqube by SonarSource.

the class PathPatternTest method match_relative_path_and_insensitive_file_extension.

@Test
public void match_relative_path_and_insensitive_file_extension() {
    PathPattern pattern = PathPattern.create("**/*Foo.java");
    IndexedFile indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/main/java/org/MyFoo.JAVA", null);
    assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()), false)).isTrue();
    indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/main/java/org/Other.java", null);
    assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()), false)).isFalse();
}
Also used : PathPattern(org.sonar.api.batch.fs.internal.PathPattern) IndexedFile(org.sonar.api.batch.fs.IndexedFile) DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) Test(org.junit.Test)

Example 5 with PathPattern

use of org.sonar.api.batch.fs.internal.PathPattern in project sonarqube by SonarSource.

the class PathPatternTest method match_relative_path.

@Test
public void match_relative_path() {
    PathPattern pattern = PathPattern.create("**/*Foo.java");
    assertThat(pattern.toString()).isEqualTo("**/*Foo.java");
    IndexedFile indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/main/java/org/MyFoo.java", null);
    assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()))).isTrue();
    // case sensitive by default
    indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/main/java/org/MyFoo.JAVA", null);
    assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()))).isFalse();
    indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/main/java/org/Other.java", null);
    assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()))).isFalse();
}
Also used : PathPattern(org.sonar.api.batch.fs.internal.PathPattern) IndexedFile(org.sonar.api.batch.fs.IndexedFile) DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) Test(org.junit.Test)

Aggregations

PathPattern (org.sonar.api.batch.fs.internal.PathPattern)6 Test (org.junit.Test)4 IndexedFile (org.sonar.api.batch.fs.IndexedFile)4 DefaultIndexedFile (org.sonar.api.batch.fs.internal.DefaultIndexedFile)4