diff options
author | Jacek <jacek.poreda@sonarsource.com> | 2022-10-26 16:17:06 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-10-26 20:03:10 +0000 |
commit | dc592db7bb160b5c43fae1fcfd7e222ed0c875a1 (patch) | |
tree | 70715bcd8c0432c19edceb3f9ba523fcb9117ee0 /sonar-scanner-engine/src | |
parent | d4c9abe2a0a04d6a974c12d6ad06a17eb1ace4a8 (diff) | |
download | sonarqube-dc592db7bb160b5c43fae1fcfd7e222ed0c875a1.tar.gz sonarqube-dc592db7bb160b5c43fae1fcfd7e222ed0c875a1.zip |
SONAR-11096 Discard files from analysis scope greater 20MB
SONAR-11096 Discard files from analysis scope greater when than 20 MB
- introduce property 'sonar.filesize.limit'
Diffstat (limited to 'sonar-scanner-engine/src')
3 files changed, 49 insertions, 6 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ScanProperties.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ScanProperties.java index 250d9d22482..095887f8159 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ScanProperties.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ScanProperties.java @@ -27,6 +27,7 @@ import org.sonar.api.config.Configuration; import org.sonar.api.utils.MessageException; import static org.sonar.core.config.ScannerProperties.BRANCH_NAME; +import static org.sonar.core.config.ScannerProperties.FILE_SIZE_LIMIT; /** * Properties that can be passed to the scanners and are not exposed in SonarQube. @@ -93,6 +94,10 @@ public class ScanProperties { return configuration.getInt(REPORT_PUBLISH_TIMEOUT_IN_SEC).orElse(60); } + public long fileSizeLimit() { + return configuration.getInt(FILE_SIZE_LIMIT).orElse(20); + } + /** * This should be called in the beginning of the analysis to fail fast */ diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java index f62617d166a..caf0ad0d76f 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java @@ -20,6 +20,7 @@ package org.sonar.scanner.scan.filesystem; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.Path; import java.util.Arrays; @@ -119,6 +120,12 @@ public class FileIndexer { return; } + long maxFileSize = properties.fileSizeLimit(); + if (Files.size(realAbsoluteFile) > maxFileSize * 1024L * 1024L) { + LOG.warn("File '{}' is bigger than {}MB and as consequence is removed from the analysis scope.", realAbsoluteFile.toAbsolutePath(), maxFileSize); + return; + } + Language language = langDetection.language(realAbsoluteFile, projectRelativePath); if (ignoreCommand != null && ignoreCommand.isIgnored(realAbsoluteFile)) { @@ -141,7 +148,7 @@ public class FileIndexer { DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> metadataGenerator.setMetadata(module.key(), f, module.getEncoding())); if (language != null && language.isPublishAllFiles()) { - inputFile.setPublished(true); + inputFile.setPublished(true); } if (!accept(inputFile)) { return; 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 f6daaed28b1..687d65a1017 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 @@ -22,6 +22,7 @@ package org.sonar.scanner.mediumtest.fs; import com.google.common.collect.ImmutableMap; import java.io.File; import java.io.IOException; +import java.io.RandomAccessFile; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.LinkOption; @@ -50,6 +51,7 @@ import org.sonar.xoo.global.DeprecatedGlobalSensor; import org.sonar.xoo.global.GlobalProjectSensor; import org.sonar.xoo.rule.XooRulesDefinition; +import static java.lang.String.format; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.tuple; @@ -121,7 +123,7 @@ public class FileSystemMediumTest { .put("sonar.sources", "src") .build()) .execute()) - .isInstanceOf(MessageException.class); + .isInstanceOf(MessageException.class); } @Test @@ -417,6 +419,26 @@ public class FileSystemMediumTest { } @Test + public void ignoreFilesWhenGreaterThanDefinedSize() throws IOException { + File srcDir = new File(baseDir, "src"); + srcDir.mkdir(); + + File fileGreaterThanLimit = writeFile(srcDir, "sample.xoo", 1024 * 1024 + 1); + writeFile(srcDir, "another.xoo", "Sample xoo 2\ncontent"); + + AnalysisResult result = tester.newAnalysis() + .properties(builder + // set limit to 1MB + .put("sonar.filesize.limit", "1") + .build()) + .execute(); + + assertThat(result.inputFiles()).hasSize(1); + assertThat(logTester.logs()) + .contains(format("File '%s' is bigger than 1MB and as consequence is removed from the analysis scope.", fileGreaterThanLimit.getAbsolutePath())); + } + + @Test public void test_inclusions_on_multi_modules() throws IOException { File baseDir = temp.getRoot(); File baseDirModuleA = new File(baseDir, "moduleA"); @@ -644,8 +666,8 @@ public class FileSystemMediumTest { .put("sonar.sources", "src,src/sample.xoo") .build()) .execute()) - .isInstanceOf(MessageException.class) - .hasMessage("File src/sample.xoo can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files"); + .isInstanceOf(MessageException.class) + .hasMessage("File src/sample.xoo can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files"); } // SONAR-9574 @@ -663,8 +685,8 @@ public class FileSystemMediumTest { .put("module1.sonar.sources", "src") .build()) .execute()) - .isInstanceOf(MessageException.class) - .hasMessage("File module1/src/sample.xoo can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files"); + .isInstanceOf(MessageException.class) + .hasMessage("File module1/src/sample.xoo can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files"); } // SONAR-5330 @@ -1048,4 +1070,13 @@ public class FileSystemMediumTest { FileUtils.write(file, content, StandardCharsets.UTF_8); return file; } + + private File writeFile(File parent, String name, long size) throws IOException { + File file = new File(parent, name); + RandomAccessFile raf = new RandomAccessFile(file, "rw"); + raf.setLength(size); + raf.close(); + return file; + } + } |