1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
/*
* SonarQube
* Copyright (C) 2009-2025 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.phases;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.io.IOException;
import java.util.Collections;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.fs.internal.SensorStrategy;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.testfixtures.log.LogTester;
import org.sonar.scanner.bootstrap.ScannerPluginRepository;
import org.sonar.scanner.fs.InputModuleHierarchy;
import org.sonar.scanner.scan.branch.BranchConfiguration;
import org.sonar.scanner.scan.branch.BranchType;
import org.sonar.scanner.scan.filesystem.MutableFileSystem;
import org.sonar.scanner.sensor.ExecutingSensorContext;
import org.sonar.scanner.sensor.ModuleSensorContext;
import org.sonar.scanner.sensor.ModuleSensorExtensionDictionary;
import org.sonar.scanner.sensor.ModuleSensorOptimizer;
import org.sonar.scanner.sensor.ModuleSensorWrapper;
import org.sonar.scanner.sensor.ModuleSensorsExecutor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
@RunWith(DataProviderRunner.class)
public class ModuleSensorsExecutorTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
@Rule
public LogTester logTester = new LogTester();
private ModuleSensorsExecutor rootModuleExecutor;
private ModuleSensorsExecutor subModuleExecutor;
private final SensorStrategy strategy = new SensorStrategy();
private final ModuleSensorWrapper perModuleSensor = mock(ModuleSensorWrapper.class);
private final ModuleSensorWrapper globalSensor = mock(ModuleSensorWrapper.class);
private final ScannerPluginRepository pluginRepository = mock(ScannerPluginRepository.class);
private final ModuleSensorContext context = mock(ModuleSensorContext.class);
private final ModuleSensorOptimizer optimizer = mock(ModuleSensorOptimizer.class);
private final ScannerPluginRepository pluginRepo = mock(ScannerPluginRepository.class);
private final MutableFileSystem fileSystem = mock(MutableFileSystem.class);
private final BranchConfiguration branchConfiguration = mock(BranchConfiguration.class);
private final ExecutingSensorContext executingSensorContext = mock(ExecutingSensorContext.class);
@Before
public void setUp() throws IOException {
when(perModuleSensor.isGlobal()).thenReturn(false);
when(perModuleSensor.shouldExecute()).thenReturn(true);
when(perModuleSensor.wrappedSensor()).thenReturn(mock(Sensor.class));
when(globalSensor.isGlobal()).thenReturn(true);
when(globalSensor.shouldExecute()).thenReturn(true);
when(globalSensor.wrappedSensor()).thenReturn(mock(Sensor.class));
ModuleSensorExtensionDictionary selector = mock(ModuleSensorExtensionDictionary.class);
when(selector.selectSensors(false)).thenReturn(Collections.singleton(perModuleSensor));
when(selector.selectSensors(true)).thenReturn(Collections.singleton(globalSensor));
ProjectDefinition childDef = ProjectDefinition.create().setKey("sub").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder());
ProjectDefinition rootDef = ProjectDefinition.create().setKey("root").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder());
DefaultInputModule rootModule = TestInputFileBuilder.newDefaultInputModule(rootDef);
DefaultInputModule subModule = TestInputFileBuilder.newDefaultInputModule(childDef);
InputModuleHierarchy hierarchy = mock(InputModuleHierarchy.class);
when(hierarchy.isRoot(rootModule)).thenReturn(true);
rootModuleExecutor = new ModuleSensorsExecutor(selector, rootModule, hierarchy, strategy, pluginRepository, executingSensorContext);
subModuleExecutor = new ModuleSensorsExecutor(selector, subModule, hierarchy, strategy, pluginRepository, executingSensorContext);
}
@Test
public void should_not_execute_global_sensor_for_submodule() {
subModuleExecutor.execute();
verify(perModuleSensor).analyse();
verify(perModuleSensor).wrappedSensor();
verifyNoInteractions(globalSensor);
verifyNoMoreInteractions(perModuleSensor, globalSensor);
}
@Test
public void should_execute_all_sensors_for_root_module() {
rootModuleExecutor.execute();
verify(globalSensor).wrappedSensor();
verify(perModuleSensor).wrappedSensor();
verify(globalSensor).analyse();
verify(perModuleSensor).analyse();
verify(executingSensorContext, times(2)).setSensorExecuting(any());
verify(executingSensorContext, times(2)).clearExecutingSensor();
verifyNoMoreInteractions(perModuleSensor, globalSensor, executingSensorContext);
}
@Test
@UseDataProvider("sensorsOnlyChangedInPR")
public void should_restrict_filesystem_when_pull_request_and_expected_sensor(String sensorName) throws IOException {
when(branchConfiguration.branchType()).thenReturn(BranchType.PULL_REQUEST);
ModuleSensorsExecutor executor = createModuleExecutor(sensorName);
executor.execute();
verify(fileSystem, times(2)).setRestrictToChangedFiles(true);
assertThat(logTester.logs().stream().anyMatch(
p -> p.contains(String.format("Sensor %s is restricted to changed files only", sensorName)))
).isTrue();
}
@Test
@UseDataProvider("sensorsOnlyChangedInPR")
public void should_not_restrict_filesystem_when_branch(String sensorName) throws IOException {
when(branchConfiguration.branchType()).thenReturn(BranchType.BRANCH);
ModuleSensorsExecutor executor = createModuleExecutor(sensorName);
executor.execute();
verify(fileSystem, times(2)).setRestrictToChangedFiles(false);
assertThat(logTester.logs().stream().anyMatch(
p -> p.contains(String.format("Sensor %s is restricted to changed files only", sensorName)))
).isFalse();
}
@Test
public void should_not_restrict_filesystem_when_pull_request_and_non_expected_sensor() throws IOException {
String sensorName = "NonRestrictedSensor";
when(branchConfiguration.branchType()).thenReturn(BranchType.PULL_REQUEST);
ModuleSensorsExecutor executor = createModuleExecutor(sensorName);
executor.execute();
verify(fileSystem, times(2)).setRestrictToChangedFiles(false);
assertThat(logTester.logs().stream().anyMatch(
p -> p.contains(String.format("Sensor %s is restricted to changed files only", sensorName)))
).isFalse();
}
@DataProvider
public static Object[][] sensorsOnlyChangedInPR() {
return new Object[][] {DefaultSensorDescriptor.HARDCODED_INDEPENDENT_FILE_SENSORS.toArray()};
}
private ModuleSensorsExecutor createModuleExecutor(String sensorName) throws IOException {
Sensor sensor = new TestSensor(sensorName);
ModuleSensorWrapper sensorWrapper = new ModuleSensorWrapper(sensor, context, optimizer, fileSystem, branchConfiguration);
ModuleSensorExtensionDictionary selector = mock(ModuleSensorExtensionDictionary.class);
when(selector.selectSensors(false)).thenReturn(Collections.singleton(sensorWrapper));
when(selector.selectSensors(true)).thenReturn(Collections.singleton(sensorWrapper));
ProjectDefinition rootDef = ProjectDefinition.create().setKey("root").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder());
DefaultInputModule rootModule = TestInputFileBuilder.newDefaultInputModule(rootDef);
InputModuleHierarchy hierarchy = mock(InputModuleHierarchy.class);
when(hierarchy.isRoot(rootModule)).thenReturn(true);
return new ModuleSensorsExecutor(selector, rootModule, hierarchy, strategy, pluginRepo, executingSensorContext);
}
private static class TestSensor implements Sensor {
private final String name;
public TestSensor(String name) {
this.name = name;
}
@Override
public void describe(SensorDescriptor descriptor) {
descriptor.name(name);
}
@Override
public void execute(SensorContext context) {
}
}
}
|