aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2014-03-17 20:02:39 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2014-03-17 20:03:16 +0100
commite7c7e73b8e6c55715c762ba3d9e8c79c7b0295c0 (patch)
tree42b3077c7350e156bbee06fbf6257f0b9c3809f0
parent7448dc86eb31d3d5c3cd37b6a1e43a86aee3bb69 (diff)
downloadsonarqube-e7c7e73b8e6c55715c762ba3d9e8c79c7b0295c0.tar.gz
sonarqube-e7c7e73b8e6c55715c762ba3d9e8c79c7b0295c0.zip
SONAR-926 apply inclusions and exclusions to source directories
If sonar.sources is not set (blank), then no files are analyzed
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ExclusionFilters.java42
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java2
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ExclusionFiltersTest.java109
3 files changed, 19 insertions, 134 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ExclusionFilters.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ExclusionFilters.java
index 2bc3c709985..f2e1e428cc6 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ExclusionFilters.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ExclusionFilters.java
@@ -26,12 +26,6 @@ import org.sonar.api.BatchComponent;
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;
@@ -45,10 +39,10 @@ public class ExclusionFilters implements BatchComponent {
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);
@@ -95,41 +89,29 @@ public class ExclusionFilters implements BatchComponent {
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);
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java
index 430c9ea27e0..eb9b3f0abae 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java
@@ -69,7 +69,7 @@ public class FileIndexer implements BatchComponent {
return;
}
logger.info("Index files");
- exclusionFilters.prepare(fileSystem);
+ exclusionFilters.prepare();
Progress progress = new Progress(fileCache.byModule(fileSystem.moduleKey()));
diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ExclusionFiltersTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ExclusionFiltersTest.java
index e23359c5369..75a0c8cb5d9 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ExclusionFiltersTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ExclusionFiltersTest.java
@@ -19,7 +19,6 @@
*/
package org.sonar.batch.scan.filesystem;
-import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@@ -28,36 +27,22 @@ import org.sonar.api.batch.fs.InputFile;
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);
@@ -73,45 +58,6 @@ public class ExclusionFiltersTest {
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 {
@@ -120,7 +66,7 @@ public class ExclusionFiltersTest {
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();
@@ -134,55 +80,12 @@ public class ExclusionFiltersTest {
}
@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();
@@ -201,7 +104,7 @@ public class ExclusionFiltersTest {
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);
@@ -225,7 +128,7 @@ public class ExclusionFiltersTest {
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();
@@ -240,7 +143,7 @@ public class ExclusionFiltersTest {
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");
}
}