diff options
author | Matteo Mara <matteo.mara@sonarsource.com> | 2023-12-22 10:28:20 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-12-22 20:03:01 +0000 |
commit | 48705fd9366d3cfae6c4642084d0400ee73313ad (patch) | |
tree | 1932ef20c1ff00118d8b83a6879e16d62a788381 /sonar-scanner-engine/src/test | |
parent | b6367ef978efd8f342dd70c4427e7570320818a4 (diff) | |
download | sonarqube-48705fd9366d3cfae6c4642084d0400ee73313ad.tar.gz sonarqube-48705fd9366d3cfae6c4642084d0400ee73313ad.zip |
Revert "SONAR-21195 Refactor file indexing into two distinct steps"
This reverts commit 42d16dafde01b19bc7fcb99084e087fdc9b8e95e.
Diffstat (limited to 'sonar-scanner-engine/src/test')
3 files changed, 7 insertions, 160 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/DirectoryFileVisitorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/DirectoryFileVisitorTest.java deleted file mode 100644 index fd79238f65d..00000000000 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/DirectoryFileVisitorTest.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.scanner.scan.filesystem; - -import java.io.File; -import java.io.IOException; -import java.nio.file.FileSystemLoopException; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.LinkOption; -import java.nio.file.Path; -import java.nio.file.attribute.BasicFileAttributes; -import org.apache.commons.lang.SystemUtils; -import org.junit.ClassRule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.DefaultInputModule; -import org.sonar.scanner.fs.InputModuleHierarchy; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertThrows; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; - -public class DirectoryFileVisitorTest { - - @ClassRule - public static TemporaryFolder temp = new TemporaryFolder(); - - private final DefaultInputModule module = mock(); - private final ModuleExclusionFilters moduleExclusionFilters = mock(); - private final InputModuleHierarchy inputModuleHierarchy = mock(); - private final InputFile.Type type = mock(); - - @Test - public void visit_hidden_file() throws IOException { - DirectoryFileVisitor.FileVisitAction action = mock(DirectoryFileVisitor.FileVisitAction.class); - - File hidden = temp.newFile(".hidden"); - if (SystemUtils.IS_OS_WINDOWS) { - Files.setAttribute(hidden.toPath(), "dos:hidden", true, LinkOption.NOFOLLOW_LINKS); - } - - - DirectoryFileVisitor underTest = new DirectoryFileVisitor(action, module, moduleExclusionFilters, inputModuleHierarchy, type); - underTest.visitFile(hidden.toPath(), Files.readAttributes(hidden.toPath(), BasicFileAttributes.class)); - - verify(action, never()).execute(any(Path.class)); - } - - @Test - public void test_visit_file_failed_generic_io_exception() throws IOException { - DirectoryFileVisitor.FileVisitAction action = mock(DirectoryFileVisitor.FileVisitAction.class); - - File file = temp.newFile("failed"); - - DirectoryFileVisitor underTest = new DirectoryFileVisitor(action, module, moduleExclusionFilters, inputModuleHierarchy, type); - assertThrows(IOException.class, () -> underTest.visitFileFailed(file.toPath(), new IOException())); - } - - @Test - public void test_visit_file_failed_file_system_loop_exception() throws IOException { - DirectoryFileVisitor.FileVisitAction action = mock(DirectoryFileVisitor.FileVisitAction.class); - - File file = temp.newFile("symlink"); - - DirectoryFileVisitor underTest = new DirectoryFileVisitor(action, module, moduleExclusionFilters, inputModuleHierarchy, type); - FileVisitResult result = underTest.visitFileFailed(file.toPath(), new FileSystemLoopException(file.getPath())); - - assertThat(result).isEqualTo(FileVisitResult.CONTINUE); - } - -} diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputFileFilterRepositoryTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputFileFilterRepositoryTest.java deleted file mode 100644 index 12f1cb0c63a..00000000000 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputFileFilterRepositoryTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.scanner.scan.filesystem; - -import org.junit.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -public class InputFileFilterRepositoryTest { - - @Test - public void should_not_return_null_if_initialized_with_no_filters() { - InputFileFilterRepository underTest = new InputFileFilterRepository(); - assertThat(underTest.getInputFileFilters()).isNotNull(); - } - - @Test - public void should_return_filters_from_initialization() { - InputFileFilterRepository underTest = new InputFileFilterRepository(f -> true); - assertThat(underTest.getInputFileFilters()).isNotNull(); - assertThat(underTest.getInputFileFilters()).hasSize(1); - } - -} 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 18408451557..e5810baba5a 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 @@ -24,8 +24,6 @@ import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; import java.io.File; import java.nio.file.Paths; -import java.util.HashMap; -import java.util.Map; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -40,11 +38,7 @@ import org.sonar.scanner.repository.language.LanguagesRepository; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.endsWith; import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; @RunWith(DataProviderRunner.class) public class LanguageDetectionTest { @@ -78,7 +72,7 @@ public class LanguageDetectionTest { @Test public void detectLanguageKey_shouldDetectByFileExtension() { LanguagesRepository languages = new FakeLanguagesRepository(new Languages(new MockLanguage("java", "java", "jav"), new MockLanguage("cobol", "cbl", "cob"))); - LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, new HashMap<>()); + LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages); assertThat(detectLanguageKey(detection, "Foo.java")).isEqualTo("java"); assertThat(detectLanguageKey(detection, "src/Foo.java")).isEqualTo("java"); @@ -100,7 +94,7 @@ public class LanguageDetectionTest { new MockLanguage("docker", new String[0], new String[] {"*.dockerfile", "*.Dockerfile", "Dockerfile", "Dockerfile.*"}), new MockLanguage("terraform", new String[] {"tf"}, new String[] {".tf"}), new MockLanguage("java", new String[0], new String[] {"**/*Test.java"}))); - LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, new HashMap<>()); + LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages); assertThat(detectLanguageKey(detection, fileName)).isEqualTo(expectedLanguageKey); } @@ -123,7 +117,7 @@ public class LanguageDetectionTest { @Test public void detectLanguageKey_shouldNotFailIfNoLanguage() { - LanguageDetection detection = spy(new LanguageDetection(settings.asConfig(), new FakeLanguagesRepository(new Languages()), new HashMap<>())); + LanguageDetection detection = spy(new LanguageDetection(settings.asConfig(), new FakeLanguagesRepository(new Languages()))); assertThat(detectLanguageKey(detection, "Foo.java")).isNull(); } @@ -131,14 +125,14 @@ public class LanguageDetectionTest { public void detectLanguageKey_shouldAllowPluginsToDeclareFileExtensionTwiceForCaseSensitivity() { LanguagesRepository languages = new FakeLanguagesRepository(new Languages(new MockLanguage("abap", "abap", "ABAP"))); - LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, new HashMap<>()); + LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages); assertThat(detectLanguageKey(detection, "abc.abap")).isEqualTo("abap"); } @Test public void detectLanguageKey_shouldFailIfConflictingLanguageSuffix() { LanguagesRepository languages = new FakeLanguagesRepository(new Languages(new MockLanguage("xml", "xhtml"), new MockLanguage("web", "xhtml"))); - LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, new HashMap<>()); + LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages); assertThatThrownBy(() -> detectLanguageKey(detection, "abc.xhtml")) .isInstanceOf(MessageException.class) .hasMessageContaining("Language of file 'abc.xhtml' can not be decided as the file matches patterns of both ") @@ -152,7 +146,7 @@ public class LanguageDetectionTest { settings.setProperty("sonar.lang.patterns.xml", "xml/**"); settings.setProperty("sonar.lang.patterns.web", "web/**"); - LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, new HashMap<>()); + LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages); assertThat(detectLanguageKey(detection, "xml/abc.xhtml")).isEqualTo("xml"); assertThat(detectLanguageKey(detection, "web/abc.xhtml")).isEqualTo("web"); } @@ -163,7 +157,7 @@ public class LanguageDetectionTest { settings.setProperty("sonar.lang.patterns.abap", "*.abap,*.txt"); settings.setProperty("sonar.lang.patterns.cobol", "*.cobol,*.txt"); - LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, new HashMap<>()); + LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages); assertThat(detectLanguageKey(detection, "abc.abap")).isEqualTo("abap"); assertThat(detectLanguageKey(detection, "abc.cobol")).isEqualTo("cobol"); @@ -174,19 +168,6 @@ public class LanguageDetectionTest { .hasMessageContaining("sonar.lang.patterns.cobol : *.cobol,*.txt"); } - @Test - public void should_cache_detected_language_by_file_path() { - Map<String, org.sonar.scanner.repository.language.Language> languageCacheSpy = spy(new HashMap<>()); - LanguagesRepository languages = new FakeLanguagesRepository(new Languages( - new MockLanguage("java", "java", "jav"), new MockLanguage("cobol", "cbl", "cob"))); - LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, languageCacheSpy); - - assertThat(detectLanguageKey(detection, "Foo.java")).isEqualTo("java"); - assertThat(detectLanguageKey(detection, "Foo.java")).isEqualTo("java"); - verify(languageCacheSpy, times(1)).put(endsWith("/Foo.java"), any(org.sonar.scanner.repository.language.Language.class)); - verify(languageCacheSpy, times(2)).get(endsWith("/Foo.java")); - } - private String detectLanguageKey(LanguageDetection detection, String path) { org.sonar.scanner.repository.language.Language language = detection.language(new File(temp.getRoot(), path).toPath(), Paths.get(path)); return language != null ? language.key() : null; |