]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-21195 Fetch publishAllFiles value from the languages registered by the plugins
authorMatteo Mara <matteo.mara@sonarsource.com>
Thu, 21 Dec 2023 13:24:42 +0000 (14:24 +0100)
committersonartech <sonartech@sonarsource.com>
Thu, 4 Jan 2024 20:02:48 +0000 (20:02 +0000)
sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesLoader.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesProvider.java [new file with mode: 0644]
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java

index 861f1b1d2074b212cab34da8673c946cde31a849..41d49b205e057648a61ac985f6a62b7e356c73c3 100644 (file)
@@ -107,6 +107,7 @@ public class ScannerMediumTester extends ExternalResource {
   private final FakeSonarRuntime sonarRuntime = new FakeSonarRuntime();
   private final CeTaskReportDataHolder reportMetadataHolder = new CeTaskReportDataHolderExt();
   private final FakeLanguagesLoader languagesLoader = new FakeLanguagesLoader();
+  private final FakeLanguagesProvider languagesProvider = new FakeLanguagesProvider();
   private LogOutput logOutput = null;
 
   private static void createWorkingDirs() throws IOException {
@@ -287,10 +288,12 @@ public class ScannerMediumTester extends ExternalResource {
 
   public void addLanguage(String key, String name, String... suffixes) {
     languagesLoader.addLanguage(key, name, suffixes, new String[0]);
+    languagesProvider.addLanguage(key, name, true);
   }
 
   public void addLanguage(String key, String name, boolean publishAllFiles, String... suffixes) {
-    languagesLoader.addLanguage(key, name, suffixes, new String[0], publishAllFiles);
+    languagesLoader.addLanguage(key, name, suffixes, new String[0]);
+    languagesProvider.addLanguage(key, name, publishAllFiles);
   }
 
   public static class AnalysisBuilder {
@@ -325,6 +328,7 @@ public class ScannerMediumTester extends ExternalResource {
           tester.sonarRuntime,
           tester.reportMetadataHolder,
           tester.languagesLoader,
+          tester.languagesProvider,
           result);
       if (tester.logOutput != null) {
         builder.setLogOutput(tester.logOutput);
index d7b481a1113e28a9c1235aaed41568219bf80db2..f2e2f48d55629677099fe73b56cfc7e32593a6a8 100644 (file)
@@ -33,12 +33,12 @@ public class FakeLanguagesLoader implements LanguagesLoader {
   private final Map<String, Language> languageMap = new HashMap<>();
 
   public FakeLanguagesLoader() {
-    languageMap.put("xoo", new Language(new FakeLanguage("xoo", "xoo", new String[] { ".xoo" }, new String[0], true)));
+    languageMap.put("xoo", new Language(new SupportedLanguageDto("xoo", "xoo", new String[] { ".xoo" }, new String[0])));
   }
 
   public FakeLanguagesLoader(Languages languages) {
     for (org.sonar.api.resources.Language language : languages.all()) {
-      languageMap.put(language.getKey(), new Language(new FakeLanguage(language.getKey(), language.getName(), language.getFileSuffixes(), language.filenamePatterns(), true)));
+      languageMap.put(language.getKey(), new Language(new SupportedLanguageDto(language.getKey(), language.getName(), language.getFileSuffixes(), language.filenamePatterns())));
     }
   }
   @Override
@@ -47,25 +47,7 @@ public class FakeLanguagesLoader implements LanguagesLoader {
   }
 
   public void addLanguage(String key, String name, String[] suffixes, String[] filenamePatterns) {
-    languageMap.put(key, new Language(new FakeLanguage(key, name, suffixes, filenamePatterns, true)));
+    languageMap.put(key, new Language(new SupportedLanguageDto(key, name, suffixes, filenamePatterns)));
   }
 
-  public void addLanguage(String key, String name, String[] suffixes, String[] filenamePatterns, boolean publishAllFiles) {
-    languageMap.put(key, new Language(new FakeLanguage(key, name, suffixes, filenamePatterns, publishAllFiles)));
-  }
-
-  private static class FakeLanguage extends SupportedLanguageDto {
-
-    private final boolean publishAllFiles;
-
-    public FakeLanguage(String key, String name, String[] fileSuffixes, String[] filenamePatterns, boolean publishAllFiles) {
-      super(key, name, fileSuffixes, filenamePatterns);
-      this.publishAllFiles = publishAllFiles;
-    }
-
-    @Override
-    public boolean publishAllFiles() {
-      return publishAllFiles;
-    }
-  }
 }
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesProvider.java
new file mode 100644 (file)
index 0000000..6009746
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * 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;
+
+import javax.annotation.Priority;
+import org.sonar.api.resources.Language;
+import org.sonar.api.resources.Languages;
+import org.springframework.context.annotation.Bean;
+
+@Priority(1)
+public class FakeLanguagesProvider {
+
+  private Languages languages = new Languages();
+
+  @Bean("Languages")
+  public Languages provide() {
+    return this.languages;
+  }
+
+  public void addLanguage(String key, String name, boolean publishAllFiles) {
+    this.languages.add(new FakeLanguage(key, name, publishAllFiles));
+  }
+
+  private static class FakeLanguage implements Language {
+
+    private final String name;
+    private final String key;
+    private final boolean publishAllFiles;
+
+    public FakeLanguage(String key, String name, boolean publishAllFiles) {
+      this.name = name;
+      this.key = key;
+      this.publishAllFiles = publishAllFiles;
+    }
+
+    @Override
+    public String getKey() {
+      return this.key;
+    }
+
+    @Override
+    public String getName() {
+      return this.name;
+    }
+
+    @Override
+    public String[] getFileSuffixes() {
+      return new String[0];
+    }
+
+    @Override
+    public boolean publishAllFiles() {
+      return this.publishAllFiles;
+    }
+  }
+
+
+}
index 30687416d14aa5042a6afb5723727beb52041f85..9b84a7195207dbda5bb4dc4755b669ea8475bdf8 100644 (file)
@@ -32,6 +32,7 @@ import org.sonar.api.batch.fs.internal.DefaultInputFile;
 import org.sonar.api.batch.fs.internal.DefaultInputModule;
 import org.sonar.api.batch.fs.internal.DefaultInputProject;
 import org.sonar.api.batch.fs.internal.SensorStrategy;
+import org.sonar.api.resources.Languages;
 import org.sonar.api.utils.MessageException;
 import org.sonar.scanner.issue.ignore.scanner.IssueExclusionsLoader;
 import org.sonar.scanner.repository.language.Language;
@@ -59,14 +60,15 @@ public class FileIndexer {
   private final LanguageDetection langDetection;
   private final StatusDetection statusDetection;
   private final ScmChangedFiles scmChangedFiles;
-
   private final ModuleRelativePathWarner moduleRelativePathWarner;
   private final InputFileFilterRepository inputFileFilterRepository;
+  private final Languages languages;
 
   public FileIndexer(DefaultInputProject project, ScannerComponentIdGenerator scannerComponentIdGenerator, InputComponentStore componentStore,
     ProjectCoverageAndDuplicationExclusions projectCoverageAndDuplicationExclusions, IssueExclusionsLoader issueExclusionsLoader,
     MetadataGenerator metadataGenerator, SensorStrategy sensorStrategy, LanguageDetection languageDetection, ScanProperties properties,
-    ScmChangedFiles scmChangedFiles, StatusDetection statusDetection, ModuleRelativePathWarner moduleRelativePathWarner, InputFileFilterRepository inputFileFilterRepository) {
+    ScmChangedFiles scmChangedFiles, StatusDetection statusDetection, ModuleRelativePathWarner moduleRelativePathWarner,
+    InputFileFilterRepository inputFileFilterRepository, Languages languages) {
     this.project = project;
     this.scannerComponentIdGenerator = scannerComponentIdGenerator;
     this.componentStore = componentStore;
@@ -80,6 +82,7 @@ public class FileIndexer {
     this.statusDetection = statusDetection;
     this.moduleRelativePathWarner = moduleRelativePathWarner;
     this.inputFileFilterRepository = inputFileFilterRepository;
+    this.languages = languages;
   }
 
   void indexFile(DefaultInputModule module, ModuleCoverageAndDuplicationExclusions moduleCoverageAndDuplicationExclusions, Path sourceFile,
@@ -104,7 +107,7 @@ public class FileIndexer {
 
     DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> metadataGenerator.setMetadata(module.key(), f, module.getEncoding()),
       f -> f.setStatus(statusDetection.findStatusFromScm(f)));
-    if (language != null && language.isPublishAllFiles()) {
+    if (language != null && isPublishAllFiles(language.key())) {
       inputFile.setPublished(true);
     }
     if (!accept(inputFile)) {
@@ -126,6 +129,13 @@ public class FileIndexer {
     progressReport.message(count + " " + pluralizeFiles(count) + " indexed...  (last one was " + inputFile.getProjectRelativePath() + ")");
   }
 
+  private boolean isPublishAllFiles(String languageKey) {
+    if (languages.get(languageKey) != null) {
+      return languages.get(languageKey).publishAllFiles();
+    }
+    return false;
+  }
+
   private void checkIfAlreadyIndexed(DefaultInputFile inputFile) {
     if (componentStore.inputFile(inputFile.getProjectRelativePath()) != null) {
       throw MessageException.of("File " + inputFile + " can't be indexed twice. Please check that inclusion/exclusion patterns produce "