diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2022-07-15 14:26:34 -0500 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-07-23 20:02:53 +0000 |
commit | b0058ce4110d6311c9fe3f81252da28883de1c43 (patch) | |
tree | 6a973770da91350bc5bc4011de8dd28f925b4a44 /sonar-scanner-engine/src/test/java/org/sonar/scanner | |
parent | 0fb5e45d935ad212aa3fe32202c33dd8395078c5 (diff) | |
download | sonarqube-b0058ce4110d6311c9fe3f81252da28883de1c43.tar.gz sonarqube-b0058ce4110d6311c9fe3f81252da28883de1c43.zip |
SONAR-17044 SONAR-17043 SONAR-17042 Optimize Compute Engine issue tracking and persisting of measures where file is marked as unchanged
Diffstat (limited to 'sonar-scanner-engine/src/test/java/org/sonar/scanner')
3 files changed, 204 insertions, 24 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java index fb719656262..5a3061e9c8f 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java @@ -28,13 +28,12 @@ import org.sonar.api.SonarQubeSide; import org.sonar.api.SonarRuntime; import org.sonar.api.batch.fs.InputModule; import org.sonar.api.batch.fs.internal.DefaultFileSystem; +import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DefaultInputProject; -import org.sonar.api.batch.measure.MetricFinder; import org.sonar.api.batch.rule.ActiveRules; import org.sonar.api.batch.rule.internal.ActiveRulesBuilder; import org.sonar.api.config.internal.MapSettings; import org.sonar.api.internal.SonarRuntimeImpl; -import org.sonar.api.measures.CoreMetrics; import org.sonar.api.utils.Version; import org.sonar.scanner.cache.AnalysisCacheEnabled; import org.sonar.scanner.cache.ReadCacheImpl; @@ -43,6 +42,7 @@ import org.sonar.scanner.scan.branch.BranchConfiguration; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class ModuleSensorContextTest { @@ -50,37 +50,27 @@ public class ModuleSensorContextTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); - private ActiveRules activeRules; + private final ActiveRules activeRules = new ActiveRulesBuilder().build(); + private final MapSettings settings = new MapSettings(); + private final DefaultSensorStorage sensorStorage = mock(DefaultSensorStorage.class); + private final BranchConfiguration branchConfiguration = mock(BranchConfiguration.class); + private final WriteCacheImpl writeCache = mock(WriteCacheImpl.class); + private final ReadCacheImpl readCache = mock(ReadCacheImpl.class); + private final AnalysisCacheEnabled analysisCacheEnabled = mock(AnalysisCacheEnabled.class); + private final UnchangedFilesHandler unchangedFilesHandler = mock(UnchangedFilesHandler.class); + private final SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.parse("5.5"), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY); private DefaultFileSystem fs; private ModuleSensorContext adaptor; - private MapSettings settings; - private DefaultSensorStorage sensorStorage; - private SonarRuntime runtime; - private BranchConfiguration branchConfiguration; - private WriteCacheImpl writeCache; - private ReadCacheImpl readCache; - private AnalysisCacheEnabled analysisCacheEnabled; @Before public void prepare() throws Exception { - activeRules = new ActiveRulesBuilder().build(); fs = new DefaultFileSystem(temp.newFolder().toPath()); - MetricFinder metricFinder = mock(MetricFinder.class); - when(metricFinder.<Integer>findByKey(CoreMetrics.NCLOC_KEY)).thenReturn(CoreMetrics.NCLOC); - when(metricFinder.<String>findByKey(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION); - settings = new MapSettings(); - sensorStorage = mock(DefaultSensorStorage.class); - branchConfiguration = mock(BranchConfiguration.class); - writeCache = mock(WriteCacheImpl.class); - readCache = mock(ReadCacheImpl.class); - analysisCacheEnabled = mock(AnalysisCacheEnabled.class); - runtime = SonarRuntimeImpl.forSonarQube(Version.parse("5.5"), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY); + adaptor = new ModuleSensorContext(mock(DefaultInputProject.class), mock(InputModule.class), settings.asConfig(), settings, fs, activeRules, sensorStorage, runtime, + branchConfiguration, writeCache, readCache, analysisCacheEnabled, unchangedFilesHandler); } @Test public void shouldProvideComponents() { - adaptor = new ModuleSensorContext(mock(DefaultInputProject.class), mock(InputModule.class), settings.asConfig(), settings, fs, activeRules, sensorStorage, runtime, - branchConfiguration, writeCache, readCache, analysisCacheEnabled); assertThat(adaptor.activeRules()).isEqualTo(activeRules); assertThat(adaptor.fileSystem()).isEqualTo(fs); assertThat(adaptor.getSonarQubeVersion()).isEqualTo(Version.parse("5.5")); @@ -100,10 +90,18 @@ public class ModuleSensorContextTest { } @Test + public void should_delegate_to_unchanged_files_handler() { + DefaultInputFile defaultInputFile = mock(DefaultInputFile.class); + adaptor.markAsUnchanged(defaultInputFile); + + verify(unchangedFilesHandler).markAsUnchanged(defaultInputFile); + } + + @Test public void pull_request_can_skip_unchanged_files() { when(branchConfiguration.isPullRequest()).thenReturn(true); adaptor = new ModuleSensorContext(mock(DefaultInputProject.class), mock(InputModule.class), settings.asConfig(), settings, fs, activeRules, sensorStorage, runtime, - branchConfiguration, writeCache, readCache, analysisCacheEnabled); + branchConfiguration, writeCache, readCache, analysisCacheEnabled, unchangedFilesHandler); assertThat(adaptor.canSkipUnchangedFiles()).isTrue(); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/SensorIdTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/SensorIdTest.java new file mode 100644 index 00000000000..2c7053879ef --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/SensorIdTest.java @@ -0,0 +1,63 @@ +/* + * SonarQube + * Copyright (C) 2009-2022 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.sensor; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class SensorIdTest { + @Test + public void equals_and_hashCode_depend_on_all_fields() { + SensorId s1 = new SensorId("a", "b"); + SensorId s2 = new SensorId("a", "b"); + SensorId s3 = new SensorId(null, "b"); + SensorId s4 = new SensorId("a", "a"); + + assertThat(s1) + .isEqualTo(s1) + .isEqualTo(s2) + .isNotEqualTo(s3) + .isNotEqualTo(s4) + .hasSameHashCodeAs(s2); + } + + @Test + public void constructor_fails_if_sensorName_is_null() { + assertThatThrownBy(() -> new SensorId("p1", null)).isInstanceOf(NullPointerException.class); + } + + @Test + public void getters_are_correct() { + SensorId s1 = new SensorId("a", "b"); + assertThat(s1.getSensorName()).isEqualTo("b"); + assertThat(s1.getPluginKey()).isEqualTo("a"); + } + + @Test + public void toString_supports_all_values() { + SensorId s = new SensorId(null, "a"); + assertThat(s).hasToString("a"); + + s = new SensorId("a", "b"); + assertThat(s).hasToString("b [a]"); + } +} diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/UnchangedFilesHandlerTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/UnchangedFilesHandlerTest.java new file mode 100644 index 00000000000..243ab9f3b76 --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/UnchangedFilesHandlerTest.java @@ -0,0 +1,119 @@ +/* + * SonarQube + * Copyright (C) 2009-2022 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.sensor; + +import javax.annotation.Nullable; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.internal.DefaultInputFile; +import org.sonar.api.config.Configuration; +import org.sonar.api.config.internal.MapSettings; +import org.sonar.api.utils.log.LogTester; +import org.sonar.scanner.scan.branch.BranchConfiguration; +import org.sonar.scanner.scan.branch.DefaultBranchConfiguration; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; + +public class UnchangedFilesHandlerTest { + private final ExecutingSensorContext executingSensorContext = new ExecutingSensorContext(); + private final Configuration enabledConfig = new MapSettings().setProperty("sonar.unchangedFiles.optimize", true).asConfig(); + private final DefaultInputFile file = mock(DefaultInputFile.class); + private final BranchConfiguration defaultBranchConfig = new DefaultBranchConfiguration(); + + @Rule + public LogTester logTester = new LogTester(); + + @Test + public void log_if_active() { + new UnchangedFilesHandler(enabledConfig, defaultBranchConfig, executingSensorContext); + assertThat(logTester.logs()).contains("Optimization for unchanged files enabled"); + } + + @Test + public void not_active_if_its_pr() { + BranchConfiguration prConfig = branchConfiguration(null, null, true); + UnchangedFilesHandler handler = new UnchangedFilesHandler(enabledConfig, prConfig, executingSensorContext); + assertThat(logTester.logs()).contains("Optimization for unchanged files not enabled because it's not an analysis of a branch with a previous analysis"); + + handler.markAsUnchanged(file); + verifyNoInteractions(file); + } + + @Test + public void not_active_if_using_different_reference() { + BranchConfiguration differentRefConfig = branchConfiguration("a", "b", false); + UnchangedFilesHandler handler = new UnchangedFilesHandler(enabledConfig, differentRefConfig, executingSensorContext); + assertThat(logTester.logs()).contains("Optimization for unchanged files not enabled because it's not an analysis of a branch with a previous analysis"); + + handler.markAsUnchanged(file); + verifyNoInteractions(file); + } + + @Test + public void not_active_if_property_not_defined() { + UnchangedFilesHandler handler = new UnchangedFilesHandler(new MapSettings().asConfig(), defaultBranchConfig, executingSensorContext); + handler.markAsUnchanged(file); + verifyNoInteractions(file); + assertThat(logTester.logs()).isEmpty(); + } + + @Test + public void mark_file_if_sensor_is_enabled() { + when(file.status()).thenReturn(InputFile.Status.SAME); + executingSensorContext.setSensorExecuting(new SensorId("cpp", "CFamily")); + UnchangedFilesHandler handler = new UnchangedFilesHandler(enabledConfig, defaultBranchConfig, executingSensorContext); + + handler.markAsUnchanged(file); + verify(file).setMarkedAsUnchanged(true); + } + + @Test + public void dont_mark_file_if_sensor_is_not_enabled() { + executingSensorContext.setSensorExecuting(new SensorId("cpp", "other")); + UnchangedFilesHandler handler = new UnchangedFilesHandler(enabledConfig, defaultBranchConfig, executingSensorContext); + + handler.markAsUnchanged(file); + verifyNoInteractions(file); + } + + @Test + public void dont_mark_file_is_status_is_not_same() { + when(file.status()).thenReturn(InputFile.Status.CHANGED); + executingSensorContext.setSensorExecuting(new SensorId("cpp", "CFamily")); + UnchangedFilesHandler handler = new UnchangedFilesHandler(enabledConfig, defaultBranchConfig, executingSensorContext); + + handler.markAsUnchanged(file); + verify(file, never()).setMarkedAsUnchanged(true); + } + + private BranchConfiguration branchConfiguration(@Nullable String branchName, @Nullable String referenceName, boolean isPullRequest) { + BranchConfiguration branchConfiguration = mock(BranchConfiguration.class); + when(branchConfiguration.referenceBranchName()).thenReturn(referenceName); + when(branchConfiguration.branchName()).thenReturn(branchName); + when(branchConfiguration.isPullRequest()).thenReturn(isPullRequest); + return branchConfiguration; + } +} |