import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.PathPattern;
import org.sonar.api.scan.filesystem.FileExclusions;
-import org.sonar.api.scan.filesystem.ModuleFileSystem;
-import org.sonar.api.scan.filesystem.PathResolver;
-
-import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
public class ExclusionFilters implements BatchComponent {
private final FileExclusions exclusionSettings;
this.exclusionSettings = exclusions;
}
- public void prepare(ModuleFileSystem fs) {
- mainInclusions = prepareMainInclusions(fs);
- mainExclusions = prepareMainExclusions(fs);
- testInclusions = prepareTestInclusions(fs);
+ public void prepare() {
+ mainInclusions = prepareMainInclusions();
+ mainExclusions = prepareMainExclusions();
+ testInclusions = prepareTestInclusions();
testExclusions = prepareTestExclusions();
log("Included sources: ", mainInclusions);
log("Excluded sources: ", mainExclusions);
return matchInclusion;
}
- PathPattern[] prepareMainInclusions(ModuleFileSystem fs) {
+ PathPattern[] prepareMainInclusions() {
if (exclusionSettings.sourceInclusions().length > 0) {
// User defined params
return PathPattern.create(exclusionSettings.sourceInclusions());
}
- // Convert source directories to inclusions
- List<String> sourcePattern = new ArrayList<String>();
- for (File src : fs.sourceDirs()) {
- String path = new PathResolver().relativePath(fs.baseDir(), src);
- sourcePattern.add(path + "/**");
- }
- return PathPattern.create(sourcePattern.toArray(new String[sourcePattern.size()]));
+ return new PathPattern[0];
}
- PathPattern[] prepareTestInclusions(ModuleFileSystem fs) {
- return PathPattern.create(computeTestInclusions(fs));
+ PathPattern[] prepareTestInclusions() {
+ return PathPattern.create(computeTestInclusions());
}
- private String[] computeTestInclusions(ModuleFileSystem fs) {
+ private String[] computeTestInclusions() {
if (exclusionSettings.testInclusions().length > 0) {
// User defined params
return exclusionSettings.testInclusions();
}
- // Convert source directories to inclusions
- List<String> testPatterns = new ArrayList<String>();
- for (File test : fs.testDirs()) {
- String path = new PathResolver().relativePath(fs.baseDir(), test);
- testPatterns.add(path + "/**");
- }
- return testPatterns.toArray(new String[testPatterns.size()]);
+ return ArrayUtils.EMPTY_STRING_ARRAY;
}
- PathPattern[] prepareMainExclusions(ModuleFileSystem fs) {
+ PathPattern[] prepareMainExclusions() {
String[] patterns = (String[]) ArrayUtils.addAll(
- exclusionSettings.sourceExclusions(), computeTestInclusions(fs));
+ exclusionSettings.sourceExclusions(), computeTestInclusions());
return PathPattern.create(patterns);
}
*/
package org.sonar.batch.scan.filesystem;
-import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.config.Settings;
import org.sonar.api.scan.filesystem.FileExclusions;
-import org.sonar.api.scan.filesystem.ModuleFileSystem;
-import java.io.File;
import java.io.IOException;
-import java.util.Arrays;
import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public class ExclusionFiltersTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
- private ModuleFileSystem fs;
- private File basedir;
-
- @Before
- public void prepare() throws IOException {
- basedir = temp.newFolder();
- fs = mock(ModuleFileSystem.class);
- when(fs.baseDir()).thenReturn(basedir);
- }
@Test
public void match_main_inclusion() throws IOException {
Settings settings = new Settings();
settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "**/*Dao.java");
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
- filter.prepare(fs);
+ filter.prepare();
java.io.File file = temp.newFile();
DefaultInputFile inputFile = new DefaultInputFile("src/main/java/com/mycompany/FooDao.java").setFile(file);
assertThat(filter.accept(inputFile, InputFile.Type.TEST)).isFalse();
}
- @Test
- public void include_main_folders_by_default() throws IOException {
- Settings settings = new Settings();
- ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
- when(fs.sourceDirs()).thenReturn(Arrays.asList(new File(basedir, "src/main/java")));
-
- filter.prepare(fs);
-
- java.io.File file = temp.newFile();
-
- DefaultInputFile inputFile = new DefaultInputFile("src/main/java/com/mycompany/Foo.java").setFile(file);
- assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isTrue();
-
- inputFile = new DefaultInputFile("src/main/java2/com/mycompany/FooDao.java").setFile(file);
- assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isFalse();
-
- // source inclusions do not apply to tests
- inputFile = new DefaultInputFile("src/main/java/com/mycompany/FooDao.java").setFile(file);
- assertThat(filter.accept(inputFile, InputFile.Type.TEST)).isFalse();
- }
-
- @Test
- public void include_main_and_test_folders_by_default() throws IOException {
- Settings settings = new Settings();
- ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
- when(fs.sourceDirs()).thenReturn(Arrays.asList(new File(basedir, "src/main/java")));
- when(fs.testDirs()).thenReturn(Arrays.asList(new File(basedir, "src/test/java")));
-
- filter.prepare(fs);
-
- java.io.File file = temp.newFile();
-
- DefaultInputFile inputFile = new DefaultInputFile("src/main/java/com/mycompany/Foo.java").setFile(file);
- assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isTrue();
- assertThat(filter.accept(inputFile, InputFile.Type.TEST)).isFalse();
-
- inputFile = new DefaultInputFile("src/test/java/com/mycompany/Foo.java").setFile(file);
- assertThat(filter.accept(inputFile, InputFile.Type.TEST)).isTrue();
- }
@Test
public void exclude_test_inclusions_from_main_sources() throws IOException {
settings.setProperty(CoreProperties.PROJECT_TEST_INCLUSIONS_PROPERTY, "src/test/java/**/*.java");
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
- filter.prepare(fs);
+ filter.prepare();
java.io.File file = temp.newFile();
assertThat(filter.accept(inputFile, InputFile.Type.TEST)).isTrue();
}
- @Test
- public void ignore_main_folders_if_inclusion_defined() throws IOException {
- Settings settings = new Settings();
- settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "src/main/java2/**/*");
- ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
- when(fs.sourceDirs()).thenReturn(Arrays.asList(new File(basedir, "src/main/java")));
-
- filter.prepare(fs);
-
- java.io.File file = temp.newFile();
-
- DefaultInputFile inputFile = new DefaultInputFile("src/main/java/com/mycompany/Foo.java").setFile(file);
- assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isFalse();
-
- inputFile = new DefaultInputFile("src/main/java2/com/mycompany/FooDao.java").setFile(file);
- assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isTrue();
-
- // source inclusions do not apply to tests
- inputFile = new DefaultInputFile("src/main/java/com/mycompany/Foo.java").setFile(file);
- assertThat(filter.accept(inputFile, InputFile.Type.TEST)).isFalse();
- }
-
- @Test
- public void include_test_folders_by_default() throws IOException {
- Settings settings = new Settings();
- ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
- when(fs.testDirs()).thenReturn(Arrays.asList(new File(basedir, "src/test/java")));
-
- filter.prepare(fs);
-
- java.io.File file = temp.newFile();
-
- // test inclusions do not apply to main code
- DefaultInputFile inputFile = new DefaultInputFile("src/test/java/com/mycompany/Foo.java").setFile(file);
- assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isFalse();
-
- inputFile = new DefaultInputFile("src/test2/java/com/mycompany/FooTest.java").setFile(file);
- assertThat(filter.accept(inputFile, InputFile.Type.TEST)).isFalse();
-
- inputFile = new DefaultInputFile("src/test/java/com/mycompany/Foo.java").setFile(file);
- assertThat(filter.accept(inputFile, InputFile.Type.TEST)).isTrue();
- }
-
@Test
public void match_at_least_one_source_inclusion() throws IOException {
Settings settings = new Settings();
settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "**/*Dao.java,**/*Dto.java");
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
- filter.prepare(fs);
+ filter.prepare();
java.io.File file = temp.newFile();
settings.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/*Dao.java");
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
- filter.prepare(fs);
+ filter.prepare();
java.io.File file = temp.newFile();
DefaultInputFile inputFile = new DefaultInputFile("src/main/java/com/mycompany/FooDao.java").setFile(file);
settings.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "file:" + excludedFile.getCanonicalPath());
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
- filter.prepare(fs);
+ filter.prepare();
DefaultInputFile inputFile = new DefaultInputFile("src/main/java/org/bar/Foo.java").setFile(includedFile);
assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isTrue();
settings.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, " **/*Dao.java ");
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
- assertThat(filter.prepareMainExclusions(fs)[0].toString()).isEqualTo("**/*Dao.java");
+ assertThat(filter.prepareMainExclusions()[0].toString()).isEqualTo("**/*Dao.java");
}
}