diff options
author | Michal Duda <michal.duda@sonarsource.com> | 2020-09-11 14:30:31 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2020-09-29 20:07:41 +0000 |
commit | 8a94f2ffc23369e675fceb34c03c98e1535d49d6 (patch) | |
tree | c5eb21cccc85b7d73af13e201387e52d51659156 /sonar-scanner-engine/src/test/java/org/sonar | |
parent | 4b3d914bf578d1eb88fc9ca79efef00843511865 (diff) | |
download | sonarqube-8a94f2ffc23369e675fceb34c03c98e1535d49d6.tar.gz sonarqube-8a94f2ffc23369e675fceb34c03c98e1535d49d6.zip |
SONAR-13867 Collect number of indexed C/C++ files in the scanner
Diffstat (limited to 'sonar-scanner-engine/src/test/java/org/sonar')
8 files changed, 131 insertions, 39 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdExecutorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdExecutorTest.java index 7598a64e485..f8614ce4fcc 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdExecutorTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdExecutorTest.java @@ -36,6 +36,7 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.mockito.ArgumentMatchers; +import org.sonar.api.SonarRuntime; import org.sonar.api.utils.log.LogTester; import org.sonar.api.utils.log.LoggerLevel; import org.sonar.core.util.CloseableIterator; @@ -72,10 +73,11 @@ public class CpdExecutorTest { public ExpectedException thrown = ExpectedException.none(); private CpdExecutor executor; - private ExecutorService executorService = mock(ExecutorService.class); - private CpdSettings settings = mock(CpdSettings.class); - private ReportPublisher publisher = mock(ReportPublisher.class); - private SonarCpdBlockIndex index = new SonarCpdBlockIndex(publisher, settings); + private final ExecutorService executorService = mock(ExecutorService.class); + private final CpdSettings settings = mock(CpdSettings.class); + private final ReportPublisher publisher = mock(ReportPublisher.class); + private final SonarRuntime sonarRuntime = mock(SonarRuntime.class); + private final SonarCpdBlockIndex index = new SonarCpdBlockIndex(publisher, settings); private ScannerReportReader reader; private DefaultInputFile batchComponent1; private DefaultInputFile batchComponent2; @@ -90,7 +92,7 @@ public class CpdExecutorTest { when(publisher.getWriter()).thenReturn(new ScannerReportWriter(outputDir)); DefaultInputProject project = TestInputFileBuilder.newDefaultInputProject("foo", baseDir); - componentStore = new InputComponentStore(mock(BranchConfiguration.class)); + componentStore = new InputComponentStore(mock(BranchConfiguration.class), sonarRuntime); executor = new CpdExecutor(settings, index, publisher, componentStore, executorService); reader = new ScannerReportReader(outputDir); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java index c1458524e51..74bc606295f 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java @@ -27,6 +27,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.api.CoreProperties; +import org.sonar.api.SonarRuntime; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputFile.Type; @@ -49,6 +50,9 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class ComponentsPublisherTest { + + private final SonarRuntime sonarRuntime = mock(SonarRuntime.class); + @Rule public TemporaryFolder temp = new TemporaryFolder(); @@ -77,7 +81,7 @@ public class ComponentsPublisherTest { .setWorkDir(temp.newFolder()); DefaultInputProject project = new DefaultInputProject(rootDef, 1); - InputComponentStore store = new InputComponentStore(branchConfiguration); + InputComponentStore store = new InputComponentStore(branchConfiguration, sonarRuntime); Path moduleBaseDir = temp.newFolder().toPath(); ProjectDefinition module1Def = ProjectDefinition.create() @@ -143,7 +147,7 @@ public class ComponentsPublisherTest { .setWorkDir(temp.newFolder()); DefaultInputProject project = new DefaultInputProject(rootDef, 1); - InputComponentStore store = new InputComponentStore(branchConfiguration); + InputComponentStore store = new InputComponentStore(branchConfiguration, sonarRuntime); DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.java", 5) .setLines(2) @@ -180,7 +184,7 @@ public class ComponentsPublisherTest { .setWorkDir(temp.newFolder()); DefaultInputProject project = new DefaultInputProject(rootDef, 1); - InputComponentStore store = new InputComponentStore(branchConfiguration); + InputComponentStore store = new InputComponentStore(branchConfiguration, sonarRuntime); ComponentsPublisher publisher = new ComponentsPublisher(project, store); publisher.publish(writer); @@ -210,7 +214,7 @@ public class ComponentsPublisherTest { .setWorkDir(temp.newFolder()); DefaultInputProject project = new DefaultInputProject(rootDef, 1); - InputComponentStore store = new InputComponentStore(branchConfiguration); + InputComponentStore store = new InputComponentStore(branchConfiguration, sonarRuntime); ComponentsPublisher publisher = new ComponentsPublisher(project, store); publisher.publish(writer); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java index 53d37e99363..f6f176d8cff 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java @@ -56,6 +56,7 @@ import org.sonar.scanner.rule.QualityProfiles; import org.sonar.scanner.scan.ScanProperties; import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.scan.branch.BranchType; +import org.sonar.scanner.scan.filesystem.InputComponentStore; import org.sonar.scanner.scm.ScmConfiguration; import org.sonar.scanner.scm.ScmRevision; @@ -73,19 +74,18 @@ public class MetadataPublisherTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); - private DefaultInputModule rootModule; private MetadataPublisher underTest; - private ScanProperties properties = mock(ScanProperties.class); - private QualityProfiles qProfiles = mock(QualityProfiles.class); - private ProjectInfo projectInfo = mock(ProjectInfo.class); - private CpdSettings cpdSettings = mock(CpdSettings.class); - private InputModuleHierarchy inputModuleHierarchy; - private ForkDateSupplier forkDateSupplier = mock(ForkDateSupplier.class); - private ScannerPluginRepository pluginRepository = mock(ScannerPluginRepository.class); + private final ScanProperties properties = mock(ScanProperties.class); + private final QualityProfiles qProfiles = mock(QualityProfiles.class); + private final ProjectInfo projectInfo = mock(ProjectInfo.class); + private final CpdSettings cpdSettings = mock(CpdSettings.class); + private final ForkDateSupplier forkDateSupplier = mock(ForkDateSupplier.class); + private final ScannerPluginRepository pluginRepository = mock(ScannerPluginRepository.class); private BranchConfiguration branches; private ScmConfiguration scmConfiguration; - private ScmProvider scmProvider = mock(ScmProvider.class); - private ScmRevision scmRevision = mock(ScmRevision.class); + private final ScmProvider scmProvider = mock(ScmProvider.class); + private final ScmRevision scmRevision = mock(ScmRevision.class); + private final InputComponentStore componentStore = mock(InputComponentStore.class); @Before public void prepare() throws IOException { @@ -101,11 +101,11 @@ public class MetadataPublisherTest { Path rootBaseDir = temp.newFolder().toPath(); Path moduleBaseDir = rootBaseDir.resolve("moduleDir"); Files.createDirectory(moduleBaseDir); - rootModule = new DefaultInputModule(def + DefaultInputModule rootModule = new DefaultInputModule(def .setBaseDir(rootBaseDir.toFile()) .setKey("root") .setWorkDir(temp.newFolder()), TestInputFileBuilder.nextBatchId()); - inputModuleHierarchy = mock(InputModuleHierarchy.class); + InputModuleHierarchy inputModuleHierarchy = mock(InputModuleHierarchy.class); when(inputModuleHierarchy.root()).thenReturn(rootModule); DefaultInputModule child = new DefaultInputModule(ProjectDefinition.create() .setKey("module") @@ -118,7 +118,7 @@ public class MetadataPublisherTest { scmConfiguration = mock(ScmConfiguration.class); when(scmConfiguration.provider()).thenReturn(scmProvider); underTest = new MetadataPublisher(projectInfo, inputModuleHierarchy, properties, qProfiles, cpdSettings, - pluginRepository, branches, scmRevision, forkDateSupplier, scmConfiguration); + pluginRepository, branches, scmRevision, forkDateSupplier, componentStore, scmConfiguration); } @Test @@ -141,6 +141,7 @@ public class MetadataPublisherTest { assertThat(metadata.getProjectKey()).isEqualTo("root"); assertThat(metadata.getModulesProjectRelativePathByKeyMap()).containsOnly(entry("module", "modulePath"), entry("root", "")); assertThat(metadata.getProjectVersion()).isEmpty(); + assertThat(metadata.getNotAnalyzedFilesByLanguageCount()).isZero(); assertThat(metadata.getQprofilesPerLanguageMap()).containsOnly(entry("java", org.sonar.scanner.protocol.output.ScannerReport.Metadata.QProfile.newBuilder() .setKey("q1") .setName("Q1") @@ -148,9 +149,9 @@ public class MetadataPublisherTest { .setRulesUpdatedAt(date.getTime()) .build())); assertThat(metadata.getPluginsByKey()).containsOnly(entry("java", org.sonar.scanner.protocol.output.ScannerReport.Metadata.Plugin.newBuilder() - .setKey("java") - .setUpdatedAt(12345) - .build()), + .setKey("java") + .setUpdatedAt(12345) + .build()), entry("php", org.sonar.scanner.protocol.output.ScannerReport.Metadata.Plugin.newBuilder() .setKey("php") .setUpdatedAt(45678) @@ -158,6 +159,20 @@ public class MetadataPublisherTest { } @Test + public void write_not_analysed_file_counts() throws Exception { + when(componentStore.getNotAnalysedFilesByLanguage()).thenReturn(ImmutableMap.of("c", 10, "cpp", 20)); + + File outputDir = temp.newFolder(); + ScannerReportWriter writer = new ScannerReportWriter(outputDir); + + underTest.publish(writer); + + ScannerReportReader reader = new ScannerReportReader(outputDir); + ScannerReport.Metadata metadata = reader.readMetadata(); + assertThat(metadata.getNotAnalyzedFilesByLanguageMap()).contains(entry("c", 10), entry("cpp", 20)); + } + + @Test public void write_project_organization() throws Exception { when(properties.organizationKey()).thenReturn(Optional.of("SonarSource")); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/SourcePublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/SourcePublisherTest.java index 4de345c222f..de69cdaea76 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/SourcePublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/SourcePublisherTest.java @@ -27,6 +27,7 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.sonar.api.SonarRuntime; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DefaultInputProject; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; @@ -58,7 +59,7 @@ public class SourcePublisherTest { .build(); DefaultInputProject rootProject = TestInputFileBuilder.newDefaultInputProject(moduleKey, baseDir); - InputComponentStore componentStore = new InputComponentStore(mock(BranchConfiguration.class)); + InputComponentStore componentStore = new InputComponentStore(mock(BranchConfiguration.class), mock(SonarRuntime.class)); componentStore.put(moduleKey, inputFile); publisher = new SourcePublisher(componentStore); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/QProfileVerifierTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/QProfileVerifierTest.java index c427e5c9ccf..4720972d391 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/QProfileVerifierTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/QProfileVerifierTest.java @@ -24,6 +24,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.slf4j.Logger; +import org.sonar.api.SonarRuntime; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.scan.filesystem.InputComponentStore; @@ -42,7 +43,7 @@ public class QProfileVerifierTest { @Before public void before() { - store = new InputComponentStore(mock(BranchConfiguration.class)); + store = new InputComponentStore(mock(BranchConfiguration.class), mock(SonarRuntime.class)); profiles = mock(QualityProfiles.class); QProfile javaProfile = new QProfile("p1", "My Java profile", "java", null); when(profiles.findByLanguage("java")).thenReturn(javaProfile); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ModuleIndexerTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ModuleIndexerTest.java index 2a581f0c5ee..b3786854552 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ModuleIndexerTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ModuleIndexerTest.java @@ -21,6 +21,7 @@ package org.sonar.scanner.scan; import java.util.Arrays; import org.junit.Test; +import org.sonar.api.SonarRuntime; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.scanner.scan.branch.BranchConfiguration; @@ -35,7 +36,7 @@ public class ModuleIndexerTest { private DefaultInputModuleHierarchy moduleHierarchy; public void createIndexer() { - InputComponentStore componentStore = new InputComponentStore(mock(BranchConfiguration.class)); + InputComponentStore componentStore = new InputComponentStore(mock(BranchConfiguration.class), mock(SonarRuntime.class)); moduleHierarchy = mock(DefaultInputModuleHierarchy.class); indexer = new ModuleIndexer(componentStore, moduleHierarchy); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputComponentStoreTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputComponentStoreTest.java index abfbabcce30..f435cef1532 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputComponentStoreTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputComponentStoreTest.java @@ -24,9 +24,12 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.LinkedList; import java.util.List; +import javax.annotation.Nullable; import org.junit.ClassRule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.sonar.api.SonarEdition; +import org.sonar.api.SonarRuntime; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputFile.Status; @@ -38,10 +41,16 @@ import org.sonar.api.batch.fs.internal.DefaultInputProject; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import org.sonar.scanner.scan.branch.BranchConfiguration; +import static java.util.Optional.ofNullable; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class InputComponentStoreTest { + + private final SonarRuntime sonarRuntime = mock(SonarRuntime.class); + @ClassRule public static TemporaryFolder temp = new TemporaryFolder(); @@ -60,7 +69,7 @@ public class InputComponentStoreTest { DefaultInputProject rootProject = TestInputFileBuilder.newDefaultInputProject(rootDef); DefaultInputModule subModule = TestInputFileBuilder.newDefaultInputModule(moduleDef); - InputComponentStore store = new InputComponentStore(mock(BranchConfiguration.class)); + InputComponentStore store = new InputComponentStore(mock(BranchConfiguration.class), sonarRuntime); store.put(subModule); DefaultInputFile fooFile = new TestInputFileBuilder(rootModuleKey, "src/main/java/Foo.java") @@ -95,13 +104,20 @@ public class InputComponentStoreTest { } static class InputComponentStoreTester extends InputComponentStore { - InputComponentStoreTester() { - super(mock(BranchConfiguration.class)); + InputComponentStoreTester(SonarRuntime sonarRuntime) { + super(mock(BranchConfiguration.class), sonarRuntime); + } + + InputFile addFile(String moduleKey, String relpath, @Nullable String language) { + TestInputFileBuilder fileBuilder = new TestInputFileBuilder(moduleKey, relpath); + ofNullable(language).ifPresent(fileBuilder::setLanguage); + DefaultInputFile file = fileBuilder.build(); + put(moduleKey, file); + return file; } - InputFile addFile(String moduleKey, String relpath, String language) { - DefaultInputFile file = new TestInputFileBuilder(moduleKey, relpath) - .setLanguage(language) + InputFile addFile(String moduleKey, String relPath) { + DefaultInputFile file = new TestInputFileBuilder(moduleKey, relPath) .build(); put(moduleKey, file); return file; @@ -109,8 +125,8 @@ public class InputComponentStoreTest { } @Test - public void should_add_languages_per_module_and_globally() throws IOException { - InputComponentStoreTester tester = new InputComponentStoreTester(); + public void should_add_languages_per_module_and_globally() { + InputComponentStoreTester tester = new InputComponentStoreTester(sonarRuntime); String mod1Key = "mod1"; tester.addFile(mod1Key, "src/main/java/Foo.java", "java"); @@ -124,8 +140,8 @@ public class InputComponentStoreTest { } @Test - public void should_find_files_per_module_and_globally() throws IOException { - InputComponentStoreTester tester = new InputComponentStoreTester(); + public void should_find_files_per_module_and_globally() { + InputComponentStoreTester tester = new InputComponentStoreTester(sonarRuntime); String mod1Key = "mod1"; InputFile mod1File = tester.addFile(mod1Key, "src/main/java/Foo.java", "java"); @@ -137,4 +153,55 @@ public class InputComponentStoreTest { assertThat(tester.filesByModule(mod2Key)).containsExactly(mod2File); assertThat(tester.inputFiles()).containsExactlyInAnyOrder(mod1File, mod2File); } + + @Test + public void stores_not_analysed_c_file_count_in_sq_community_edition() { + when(sonarRuntime.getEdition()).thenReturn(SonarEdition.COMMUNITY); + InputComponentStoreTester underTest = new InputComponentStoreTester(sonarRuntime); + String mod1Key = "mod1"; + underTest.addFile(mod1Key, "src/main/java/Foo.java", "java"); + underTest.addFile(mod1Key, "src/main/c/file1.c"); + underTest.addFile(mod1Key, "src/main/c/file2.c"); + String mod2Key = "mod2"; + underTest.addFile(mod2Key, "src/main/groovy/Foo.groovy", "groovy"); + underTest.addFile(mod2Key, "src/main/c/file3.c"); + + assertThat(underTest.getNotAnalysedFilesByLanguage()).hasSize(1); + assertThat(underTest.getNotAnalysedFilesByLanguage()).containsEntry("C", 3); + } + + @Test + public void stores_not_analysed_cpp_file_count_in_sq_community_edition() { + when(sonarRuntime.getEdition()).thenReturn(SonarEdition.COMMUNITY); + InputComponentStoreTester underTest = new InputComponentStoreTester(sonarRuntime); + String mod1Key = "mod1"; + underTest.addFile(mod1Key, "src/main/java/Foo.java", "java"); + underTest.addFile(mod1Key, "src/main/c/file1.c"); + underTest.addFile(mod1Key, "src/main/c/file2.cpp"); + underTest.addFile(mod1Key, "src/main/c/file3.cxx"); + underTest.addFile(mod1Key, "src/main/c/file4.c++"); + underTest.addFile(mod1Key, "src/main/c/file5.cc"); + underTest.addFile(mod1Key, "src/main/c/file6.CPP"); + String mod2Key = "mod2"; + underTest.addFile(mod2Key, "src/main/groovy/Foo.groovy", "groovy"); + underTest.addFile(mod2Key, "src/main/c/file3.cpp"); + + assertThat(underTest.getNotAnalysedFilesByLanguage()).hasSize(2); + assertThat(underTest.getNotAnalysedFilesByLanguage()).containsEntry("C++", 6); + } + + @Test + public void does_not_store_not_analysed_file_counts_in_sq_non_community_editions() { + when(sonarRuntime.getEdition()).thenReturn(SonarEdition.DEVELOPER); + InputComponentStoreTester underTest = new InputComponentStoreTester(sonarRuntime); + String mod1Key = "mod1"; + underTest.addFile(mod1Key, "src/main/java/Foo.java", "java"); + underTest.addFile(mod1Key, "src/main/java/file1.c"); + underTest.addFile(mod1Key, "src/main/java/file2.c"); + String mod2Key = "mod2"; + underTest.addFile(mod2Key, "src/main/groovy/Foo.groovy", "groovy"); + underTest.addFile(mod2Key, "src/main/groovy/file4.c"); + + assertThat(underTest.getNotAnalysedFilesByLanguage()).isEmpty(); + } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java index 0b9e8b2571a..522172db47f 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java @@ -24,6 +24,7 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.sonar.api.SonarRuntime; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputModule; import org.sonar.api.batch.fs.internal.SensorStrategy; @@ -49,7 +50,7 @@ public class ModuleInputComponentStoreTest { @Before public void setUp() throws IOException { DefaultInputProject root = TestInputFileBuilder.newDefaultInputProject(projectKey, temp.newFolder()); - componentStore = new InputComponentStore(mock(BranchConfiguration.class)); + componentStore = new InputComponentStore(mock(BranchConfiguration.class), mock(SonarRuntime.class)); } @Test |