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-scanner-engine/src/test | |
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-scanner-engine/src/test')
4 files changed, 64 insertions, 23 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java index 016e8c8e554..a61330e3657 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java @@ -61,6 +61,7 @@ public class FileSystemMediumTest { public ScannerMediumTester tester = new ScannerMediumTester() .registerPlugin("xoo", new XooPlugin()) .addDefaultQProfile("xoo", "Sonar Way") + .addDefaultQProfile("xoo2", "Sonar Way") .setLogOutput(logs); private File baseDir; @@ -670,4 +671,45 @@ public class FileSystemMediumTest { assertThat(result.inputDirs()).hasSize(3); } + @Test + public void twoLanguagesWithSameExtension() throws IOException { + File srcDir = new File(baseDir, "src"); + srcDir.mkdir(); + + File xooFile = new File(srcDir, "sample.xoo"); + FileUtils.write(xooFile, "Sample xoo\ncontent"); + + File xooFile2 = new File(srcDir, "sample.xoo2"); + FileUtils.write(xooFile2, "Sample xoo 2\ncontent"); + + TaskResult result = tester.newTask() + .properties(builder + .put("sonar.sources", "src") + .build()) + .execute(); + + assertThat(result.inputFiles()).hasSize(2); + + try { + result = tester.newTask() + .properties(builder + .put("sonar.lang.patterns.xoo2", "**/*.xoo") + .build()) + .execute(); + } catch (Exception e) { + assertThat(e) + .isInstanceOf(MessageException.class) + .hasMessage( + "Language of file 'src/sample.xoo' can not be decided as the file matches patterns of both sonar.lang.patterns.xoo : **/*.xoo and sonar.lang.patterns.xoo2 : **/*.xoo"); + } + + // SONAR-9561 + result = tester.newTask() + .properties(builder + .put("sonar.exclusions", "**/sample.xoo") + .build()) + .execute(); + + assertThat(result.inputFiles()).hasSize(1); + } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ExclusionFiltersTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ExclusionFiltersTest.java index f64a6729750..186f40189ee 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ExclusionFiltersTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ExclusionFiltersTest.java @@ -19,12 +19,10 @@ */ package org.sonar.scanner.scan.filesystem; -import static org.assertj.core.api.Assertions.assertThat; - import java.io.File; 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; @@ -36,6 +34,8 @@ import org.sonar.api.batch.fs.internal.DefaultIndexedFile; import org.sonar.api.config.internal.MapSettings; import org.sonar.api.scan.filesystem.FileExclusions; +import static org.assertj.core.api.Assertions.assertThat; + public class ExclusionFiltersTest { @Rule @@ -56,8 +56,8 @@ public class ExclusionFiltersTest { filter.prepare(); IndexedFile indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/com/mycompany/FooDao.java", null); - assertThat(filter.accept(indexedFile, InputFile.Type.MAIN)).isTrue(); - assertThat(filter.accept(indexedFile, InputFile.Type.TEST)).isTrue(); + assertThat(filter.accept(indexedFile.path(), Paths.get(indexedFile.relativePath()), InputFile.Type.MAIN)).isTrue(); + assertThat(filter.accept(indexedFile.path(), Paths.get(indexedFile.relativePath()), InputFile.Type.TEST)).isTrue(); } @Test @@ -66,10 +66,10 @@ public class ExclusionFiltersTest { filter.prepare(); IndexedFile indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/com/mycompany/FooDao.java", null); - assertThat(filter.accept(indexedFile, InputFile.Type.MAIN)).isTrue(); + assertThat(filter.accept(indexedFile.path(), Paths.get(indexedFile.relativePath()), InputFile.Type.MAIN)).isTrue(); indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/com/mycompany/Foo.java", null); - assertThat(filter.accept(indexedFile, InputFile.Type.MAIN)).isFalse(); + assertThat(filter.accept(indexedFile.path(), Paths.get(indexedFile.relativePath()), InputFile.Type.MAIN)).isFalse(); } @Test @@ -78,10 +78,10 @@ public class ExclusionFiltersTest { filter.prepare(); IndexedFile indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/com/mycompany/Foo.java", null); - assertThat(filter.accept(indexedFile, InputFile.Type.MAIN)).isFalse(); + assertThat(filter.accept(indexedFile.path(), Paths.get(indexedFile.relativePath()), InputFile.Type.MAIN)).isFalse(); indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/com/mycompany/FooDto.java", null); - assertThat(filter.accept(indexedFile, InputFile.Type.MAIN)).isTrue(); + assertThat(filter.accept(indexedFile.path(), Paths.get(indexedFile.relativePath()), InputFile.Type.MAIN)).isTrue(); } @Test @@ -92,14 +92,14 @@ public class ExclusionFiltersTest { filter.prepare(); IndexedFile indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/com/mycompany/FooDao.java", null); - assertThat(filter.accept(indexedFile, InputFile.Type.MAIN)).isFalse(); + assertThat(filter.accept(indexedFile.path(), Paths.get(indexedFile.relativePath()), InputFile.Type.MAIN)).isFalse(); indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/com/mycompany/Foo.java", null); - assertThat(filter.accept(indexedFile, InputFile.Type.MAIN)).isTrue(); + assertThat(filter.accept(indexedFile.path(), Paths.get(indexedFile.relativePath()), InputFile.Type.MAIN)).isTrue(); // source exclusions do not apply to tests indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/test/java/com/mycompany/FooDao.java", null); - assertThat(filter.accept(indexedFile, InputFile.Type.TEST)).isTrue(); + assertThat(filter.accept(indexedFile.path(), Paths.get(indexedFile.relativePath()), InputFile.Type.TEST)).isTrue(); } @Test @@ -111,10 +111,10 @@ public class ExclusionFiltersTest { filter.prepare(); IndexedFile indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/org/bar/Foo.java", null); - assertThat(filter.accept(indexedFile, InputFile.Type.MAIN)).isTrue(); + assertThat(filter.accept(indexedFile.path(), Paths.get(indexedFile.relativePath()), InputFile.Type.MAIN)).isTrue(); indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/org/bar/Bar.java", null); - assertThat(filter.accept(indexedFile, InputFile.Type.MAIN)).isFalse(); + assertThat(filter.accept(indexedFile.path(), Paths.get(indexedFile.relativePath()), InputFile.Type.MAIN)).isFalse(); } @Test diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputFileBuilderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputFileBuilderTest.java index a387635db29..a4c6a32b6dd 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputFileBuilderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputFileBuilderTest.java @@ -53,19 +53,18 @@ public class InputFileBuilderTest { .setWorkDir(workDir.toFile()) .setKey("module1"), 0); - LanguageDetection langDetection = mock(LanguageDetection.class); MetadataGenerator metadataGenerator = mock(MetadataGenerator.class); BatchIdGenerator idGenerator = new BatchIdGenerator(); MapSettings settings = new MapSettings(); ModuleFileSystemInitializer moduleFileSystemInitializer = mock(ModuleFileSystemInitializer.class); when(moduleFileSystemInitializer.defaultEncoding()).thenReturn(StandardCharsets.UTF_8); - builder = new InputFileBuilder(module, langDetection, metadataGenerator, idGenerator, settings.asConfig(), moduleFileSystemInitializer); + builder = new InputFileBuilder(module, metadataGenerator, idGenerator, settings.asConfig(), moduleFileSystemInitializer); } @Test public void testBuild() { Path filePath = baseDir.resolve("src/File1.xoo"); - DefaultInputFile inputFile = builder.create(filePath, Type.MAIN); + DefaultInputFile inputFile = builder.create(Type.MAIN, "src/File1.xoo", null); assertThat(inputFile.moduleKey()).isEqualTo("module1"); assertThat(inputFile.absolutePath()).isEqualTo(filePath.toString().replaceAll("\\\\", "/")); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/LanguageDetectionTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/LanguageDetectionTest.java index 1d8f553d1f5..1e516523acf 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/LanguageDetectionTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/LanguageDetectionTest.java @@ -19,13 +19,9 @@ */ package org.sonar.scanner.scan.filesystem; -import static junit.framework.Assert.fail; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.spy; - import java.io.File; import java.io.IOException; - +import java.nio.file.Paths; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -39,6 +35,10 @@ import org.sonar.api.utils.MessageException; import org.sonar.scanner.repository.language.DefaultLanguagesRepository; import org.sonar.scanner.repository.language.LanguagesRepository; +import static junit.framework.Assert.fail; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.spy; + public class LanguageDetectionTest { @Rule @@ -183,7 +183,7 @@ public class LanguageDetectionTest { } private String detectLanguage(LanguageDetection detection, String path) { - return detection.language(new File(temp.getRoot(), path).getAbsolutePath(), path); + return detection.language(new File(temp.getRoot(), path).toPath(), Paths.get(path)); } static class MockLanguage implements Language { |