diff options
author | Matteo Mara <matteo.mara@sonarsource.com> | 2023-12-06 16:58:03 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-01-04 20:02:48 +0000 |
commit | 7013e543f07fca1831fd1efee29997981b6ec19b (patch) | |
tree | 658cc82afdefacdfa1e1454cc78fd9c3dbf5d652 /sonar-scanner-engine/src/it | |
parent | 8fd8c030e7dda6eb03c83eb4e59474f5e2d4e401 (diff) | |
download | sonarqube-7013e543f07fca1831fd1efee29997981b6ec19b.tar.gz sonarqube-7013e543f07fca1831fd1efee29997981b6ec19b.zip |
SONAR-21195 Enhance scanner engine to download only required plugins
Diffstat (limited to 'sonar-scanner-engine/src/it')
4 files changed, 119 insertions, 4 deletions
diff --git a/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java b/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java index a4d4b9915cd..94f1ac31b97 100644 --- a/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java +++ b/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java @@ -105,6 +105,7 @@ public class ScannerMediumTester extends ExternalResource { private final FakeActiveRulesLoader activeRules = new FakeActiveRulesLoader(); private final FakeSonarRuntime sonarRuntime = new FakeSonarRuntime(); private final CeTaskReportDataHolder reportMetadataHolder = new CeTaskReportDataHolderExt(); + private final FakeLanguagesRepository languagesRepository = new FakeLanguagesRepository(); private LogOutput logOutput = null; private static void createWorkingDirs() throws IOException { @@ -282,6 +283,14 @@ public class ScannerMediumTester extends ExternalResource { return builder; } + public void addLanguage(String key, String name, String... suffixes) { + languagesRepository.addLanguage(key, name, suffixes, new String[0]); + } + + public void addLanguage(String key, String name, boolean publishAllFiles, String... suffixes) { + languagesRepository.addLanguage(key, name, suffixes, new String[0], publishAllFiles); + } + public static class AnalysisBuilder { private final Map<String, String> taskProperties = new HashMap<>(); private final ScannerMediumTester tester; @@ -313,7 +322,8 @@ public class ScannerMediumTester extends ExternalResource { tester.analysisCacheLoader, tester.sonarRuntime, tester.reportMetadataHolder, - result); + result, + tester.languagesRepository); if (tester.logOutput != null) { builder.setLogOutput(tester.logOutput); } else { diff --git a/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumIT.java b/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumIT.java index 864e073db2f..e06c524d457 100644 --- a/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumIT.java +++ b/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumIT.java @@ -866,6 +866,8 @@ public class FileSystemMediumIT { File srcDir = new File(baseDir, "src"); srcDir.mkdir(); + tester.addLanguage("xoo3", "xoo3",false, ".xoo3"); + writeFile(srcDir, "sample.xoo3", "Sample xoo\ncontent"); writeFile(srcDir, "sample2.xoo3", "Sample xoo 2\ncontent"); @@ -897,6 +899,7 @@ public class FileSystemMediumIT { assertThat(result.inputFiles()).hasSize(2); + tester.addLanguage("xoo2", "xoo2", ".xoo"); AnalysisBuilder analysisBuilder = tester.newAnalysis() .properties(builder .put("sonar.lang.patterns.xoo2", "**/*.xoo") diff --git a/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/fs/NoLanguagesPluginsMediumIT.java b/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/fs/NoLanguagesPluginsMediumIT.java index 609eb38e616..d575c39d0e5 100644 --- a/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/fs/NoLanguagesPluginsMediumIT.java +++ b/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/fs/NoLanguagesPluginsMediumIT.java @@ -26,7 +26,6 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.scanner.mediumtest.ScannerMediumTester; -import org.springframework.beans.factory.BeanCreationException; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -44,8 +43,8 @@ public class NoLanguagesPluginsMediumIT { assertThatThrownBy(() -> tester .newAnalysis(new File(projectDir, "sonar-project.properties")) .execute()) - .isInstanceOf(BeanCreationException.class) - .hasRootCauseMessage("No language plugins are installed."); + .isInstanceOf(IllegalStateException.class) + .hasMessage("No language plugins are installed."); } private File copyProject(String path) throws Exception { diff --git a/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/properties/PropertiesMediumIT.java b/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/properties/PropertiesMediumIT.java new file mode 100644 index 00000000000..fe54129e805 --- /dev/null +++ b/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/properties/PropertiesMediumIT.java @@ -0,0 +1,103 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.mediumtest.properties; + +import com.google.common.collect.ImmutableMap; +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; +import org.apache.commons.io.FileUtils; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.SonarEdition; +import org.sonar.api.testfixtures.log.LogTester; +import org.sonar.scanner.mediumtest.ScannerMediumTester; +import org.sonar.scanner.protocol.output.FileStructure; +import org.sonar.scanner.protocol.output.ScannerReportReader; +import org.sonar.xoo.XooPlugin; +import org.sonar.xoo.rule.XooRulesDefinition; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PropertiesMediumIT { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Rule + public LogTester logTester = new LogTester(); + + @Rule + public ScannerMediumTester tester = new ScannerMediumTester() + .setEdition(SonarEdition.ENTERPRISE) + .registerPlugin("xoo", new XooPlugin()) + .addDefaultQProfile("xoo", "Sonar Way") + .addRules(new XooRulesDefinition()) + // active a rule just to be sure that xoo files are published + .addActiveRule("xoo", "xoo:OneIssuePerFile", null, "One Issue Per File", null, null, null); + + @Test + public void testProperties() throws IOException { + File baseDir = prepareProject(); + + tester.newAnalysis() + .properties(ImmutableMap.<String, String>builder() + .put("sonar.projectBaseDir", baseDir.getAbsolutePath()) + .put("sonar.projectKey", "com.foo.project") + .put("sonar.sources", "src") + .put("sonar.analysis.property", "value") + .build()) + .execute(); + + var properties = getProperties(baseDir); + + //We focus on the specific property that we would like to get added to the report + assertThat(properties).containsEntry("sonar.analysis.property", "value"); + } + + private Map<String, String> getProperties(File baseDir) { + File reportDir = new File(baseDir, ".sonar/scanner-report"); + FileStructure fileStructure = new FileStructure(reportDir); + ScannerReportReader reader = new ScannerReportReader(fileStructure); + + Map<String, String> properties = new HashMap<>(); + + try (var iterator = reader.readContextProperties()) { + iterator.forEachRemaining(p -> properties.put(p.getKey(), p.getValue())); + } + + return properties; + } + + private File prepareProject() throws IOException { + File baseDir = temp.getRoot(); + File srcDir = new File(baseDir, "src"); + srcDir.mkdir(); + + File xooFile1 = new File(srcDir, "sample.xoo"); + FileUtils.write(xooFile1, "Sample xoo\ncontent\n3\n4\n5", StandardCharsets.UTF_8); + + return baseDir; + } + +} |