You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ModuleSensorsExecutorTest.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.phases;
  21. import com.tngtech.java.junit.dataprovider.DataProvider;
  22. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  23. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  24. import java.io.IOException;
  25. import java.util.Collections;
  26. import org.junit.Before;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.junit.rules.TemporaryFolder;
  30. import org.junit.runner.RunWith;
  31. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  32. import org.sonar.api.batch.fs.internal.DefaultInputModule;
  33. import org.sonar.api.batch.fs.internal.SensorStrategy;
  34. import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
  35. import org.sonar.api.batch.sensor.Sensor;
  36. import org.sonar.api.batch.sensor.SensorContext;
  37. import org.sonar.api.batch.sensor.SensorDescriptor;
  38. import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
  39. import org.sonar.api.utils.log.LogTester;
  40. import org.sonar.scanner.bootstrap.ScannerPluginRepository;
  41. import org.sonar.scanner.fs.InputModuleHierarchy;
  42. import org.sonar.scanner.scan.branch.BranchConfiguration;
  43. import org.sonar.scanner.scan.branch.BranchType;
  44. import org.sonar.scanner.scan.filesystem.MutableFileSystem;
  45. import org.sonar.scanner.sensor.ModuleSensorContext;
  46. import org.sonar.scanner.sensor.ModuleSensorExtensionDictionary;
  47. import org.sonar.scanner.sensor.ModuleSensorOptimizer;
  48. import org.sonar.scanner.sensor.ModuleSensorWrapper;
  49. import org.sonar.scanner.sensor.ModuleSensorsExecutor;
  50. import static org.assertj.core.api.Assertions.assertThat;
  51. import static org.mockito.Mockito.mock;
  52. import static org.mockito.Mockito.times;
  53. import static org.mockito.Mockito.verify;
  54. import static org.mockito.Mockito.verifyNoMoreInteractions;
  55. import static org.mockito.Mockito.verifyZeroInteractions;
  56. import static org.mockito.Mockito.when;
  57. @RunWith(DataProviderRunner.class)
  58. public class ModuleSensorsExecutorTest {
  59. @Rule
  60. public TemporaryFolder temp = new TemporaryFolder();
  61. @Rule
  62. public LogTester logTester = new LogTester();
  63. private ModuleSensorsExecutor rootModuleExecutor;
  64. private ModuleSensorsExecutor subModuleExecutor;
  65. private final SensorStrategy strategy = new SensorStrategy();
  66. private final ModuleSensorWrapper perModuleSensor = mock(ModuleSensorWrapper.class);
  67. private final ModuleSensorWrapper globalSensor = mock(ModuleSensorWrapper.class);
  68. private final ScannerPluginRepository pluginRepository = mock(ScannerPluginRepository.class);
  69. private final ModuleSensorContext context = mock(ModuleSensorContext.class);
  70. private final ModuleSensorOptimizer optimizer = mock(ModuleSensorOptimizer.class);
  71. private final ScannerPluginRepository pluginRepo = mock(ScannerPluginRepository.class);
  72. private final MutableFileSystem fileSystem = mock(MutableFileSystem.class);
  73. private final BranchConfiguration branchConfiguration = mock(BranchConfiguration.class);
  74. @Before
  75. public void setUp() throws IOException {
  76. when(perModuleSensor.isGlobal()).thenReturn(false);
  77. when(perModuleSensor.shouldExecute()).thenReturn(true);
  78. when(perModuleSensor.wrappedSensor()).thenReturn(mock(Sensor.class));
  79. when(globalSensor.isGlobal()).thenReturn(true);
  80. when(globalSensor.shouldExecute()).thenReturn(true);
  81. when(globalSensor.wrappedSensor()).thenReturn(mock(Sensor.class));
  82. ModuleSensorExtensionDictionary selector = mock(ModuleSensorExtensionDictionary.class);
  83. when(selector.selectSensors(false)).thenReturn(Collections.singleton(perModuleSensor));
  84. when(selector.selectSensors(true)).thenReturn(Collections.singleton(globalSensor));
  85. ProjectDefinition childDef = ProjectDefinition.create().setKey("sub").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder());
  86. ProjectDefinition rootDef = ProjectDefinition.create().setKey("root").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder());
  87. DefaultInputModule rootModule = TestInputFileBuilder.newDefaultInputModule(rootDef);
  88. DefaultInputModule subModule = TestInputFileBuilder.newDefaultInputModule(childDef);
  89. InputModuleHierarchy hierarchy = mock(InputModuleHierarchy.class);
  90. when(hierarchy.isRoot(rootModule)).thenReturn(true);
  91. rootModuleExecutor = new ModuleSensorsExecutor(selector, rootModule, hierarchy, strategy, pluginRepository);
  92. subModuleExecutor = new ModuleSensorsExecutor(selector, subModule, hierarchy, strategy, pluginRepository);
  93. }
  94. @Test
  95. public void should_not_execute_global_sensor_for_submodule() {
  96. subModuleExecutor.execute();
  97. verify(perModuleSensor).analyse();
  98. verify(perModuleSensor).wrappedSensor();
  99. verifyZeroInteractions(globalSensor);
  100. verifyNoMoreInteractions(perModuleSensor, globalSensor);
  101. }
  102. @Test
  103. public void should_execute_all_sensors_for_root_module() {
  104. rootModuleExecutor.execute();
  105. verify(globalSensor).wrappedSensor();
  106. verify(perModuleSensor).wrappedSensor();
  107. verify(globalSensor).analyse();
  108. verify(perModuleSensor).analyse();
  109. verifyNoMoreInteractions(perModuleSensor, globalSensor);
  110. }
  111. @Test
  112. @UseDataProvider("sensorsOnlyChangedInPR")
  113. public void should_restrict_filesystem_when_pull_request_and_expected_sensor(String sensorName) throws IOException {
  114. when(branchConfiguration.branchType()).thenReturn(BranchType.PULL_REQUEST);
  115. ModuleSensorsExecutor executor = createModuleExecutor(sensorName);
  116. executor.execute();
  117. verify(fileSystem, times(2)).setRestrictToChangedFiles(true);
  118. assertThat(logTester.logs().stream().anyMatch(
  119. p -> p.contains(String.format("Sensor %s is restricted to changed files only", sensorName)))
  120. ).isTrue();
  121. }
  122. @Test
  123. @UseDataProvider("sensorsOnlyChangedInPR")
  124. public void should_not_restrict_filesystem_when_branch(String sensorName) throws IOException {
  125. when(branchConfiguration.branchType()).thenReturn(BranchType.BRANCH);
  126. ModuleSensorsExecutor executor = createModuleExecutor(sensorName);
  127. executor.execute();
  128. verify(fileSystem, times(2)).setRestrictToChangedFiles(false);
  129. assertThat(logTester.logs().stream().anyMatch(
  130. p -> p.contains(String.format("Sensor %s is restricted to changed files only", sensorName)))
  131. ).isFalse();
  132. }
  133. @Test
  134. public void should_not_restrict_filesystem_when_pull_request_and_non_expected_sensor() throws IOException {
  135. String sensorName = "NonRestrictedSensor";
  136. when(branchConfiguration.branchType()).thenReturn(BranchType.PULL_REQUEST);
  137. ModuleSensorsExecutor executor = createModuleExecutor(sensorName);
  138. executor.execute();
  139. verify(fileSystem, times(2)).setRestrictToChangedFiles(false);
  140. assertThat(logTester.logs().stream().anyMatch(
  141. p -> p.contains(String.format("Sensor %s is restricted to changed files only", sensorName)))
  142. ).isFalse();
  143. }
  144. @DataProvider
  145. public static Object[][] sensorsOnlyChangedInPR() {
  146. return new Object[][] {DefaultSensorDescriptor.HARDCODED_INDEPENDENT_FILE_SENSORS.toArray()};
  147. }
  148. private ModuleSensorsExecutor createModuleExecutor(String sensorName) throws IOException {
  149. Sensor sensor = new TestSensor(sensorName);
  150. ModuleSensorWrapper sensorWrapper = new ModuleSensorWrapper(sensor, context, optimizer, fileSystem, branchConfiguration);
  151. ModuleSensorExtensionDictionary selector = mock(ModuleSensorExtensionDictionary.class);
  152. when(selector.selectSensors(false)).thenReturn(Collections.singleton(sensorWrapper));
  153. when(selector.selectSensors(true)).thenReturn(Collections.singleton(sensorWrapper));
  154. ProjectDefinition rootDef = ProjectDefinition.create().setKey("root").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder());
  155. DefaultInputModule rootModule = TestInputFileBuilder.newDefaultInputModule(rootDef);
  156. InputModuleHierarchy hierarchy = mock(InputModuleHierarchy.class);
  157. when(hierarchy.isRoot(rootModule)).thenReturn(true);
  158. return new ModuleSensorsExecutor(selector, rootModule, hierarchy, strategy, pluginRepo);
  159. }
  160. private static class TestSensor implements Sensor {
  161. private final String name;
  162. public TestSensor(String name) {
  163. this.name = name;
  164. }
  165. @Override
  166. public void describe(SensorDescriptor descriptor) {
  167. descriptor.name(name);
  168. }
  169. @Override
  170. public void execute(SensorContext context) {
  171. }
  172. }
  173. }