Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ghislainpiot committed Dec 17, 2024
1 parent 26c941b commit 4acefce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,25 @@ public final class PythonSensor implements Sensor {
*/
public PythonSensor(FileLinesContextFactory fileLinesContextFactory, CheckFactory checkFactory,
NoSonarFilter noSonarFilter, AnalysisWarningsWrapper analysisWarnings) {
this(fileLinesContextFactory, checkFactory, noSonarFilter, null, null, null, analysisWarnings, new SensorTelemetryStorage());
this(fileLinesContextFactory, checkFactory, noSonarFilter, null, null, null, analysisWarnings);
}

public PythonSensor(FileLinesContextFactory fileLinesContextFactory, CheckFactory checkFactory, NoSonarFilter noSonarFilter,
PythonCustomRuleRepository[] customRuleRepositories, AnalysisWarningsWrapper analysisWarnings) {
this(fileLinesContextFactory, checkFactory, noSonarFilter, customRuleRepositories, null, null, analysisWarnings);
}

public PythonSensor(FileLinesContextFactory fileLinesContextFactory, CheckFactory checkFactory, NoSonarFilter noSonarFilter,
PythonIndexer indexer, SonarLintCache sonarLintCache, AnalysisWarningsWrapper analysisWarnings) {
// ^^ This constructor implicitly assumes that a PythonIndexer and a SonarLintCache are always available at the same time.
// In practice, this is currently the case, since both are provided by PythonPlugin under the same conditions.
// See also PythonPlugin::SonarLintPluginAPIManager::addSonarlintPythonIndexer.
this(fileLinesContextFactory, checkFactory, noSonarFilter, null, indexer, sonarLintCache, analysisWarnings, new SensorTelemetryStorage());
this(fileLinesContextFactory, checkFactory, noSonarFilter, null, indexer, sonarLintCache, analysisWarnings);
}

public PythonSensor(FileLinesContextFactory fileLinesContextFactory, CheckFactory checkFactory, NoSonarFilter noSonarFilter,
@Nullable PythonCustomRuleRepository[] customRuleRepositories, @Nullable PythonIndexer indexer,
@Nullable SonarLintCache sonarLintCache, AnalysisWarningsWrapper analysisWarnings) {
this(fileLinesContextFactory, checkFactory, noSonarFilter, customRuleRepositories, indexer, sonarLintCache, analysisWarnings, new SensorTelemetryStorage());
}

public PythonSensor(FileLinesContextFactory fileLinesContextFactory, CheckFactory checkFactory, NoSonarFilter noSonarFilter,
@Nullable PythonCustomRuleRepository[] customRuleRepositories, @Nullable PythonIndexer indexer,
@Nullable SonarLintCache sonarLintCache, AnalysisWarningsWrapper analysisWarnings, SensorTelemetryStorage sensorTelemetryStorage) {
this.checks = new PythonChecks(checkFactory)
.addChecks(CheckList.REPOSITORY_KEY, CheckList.getChecks())
.addCustomChecks(customRuleRepositories);
Expand All @@ -103,12 +102,7 @@ public PythonSensor(FileLinesContextFactory fileLinesContextFactory, CheckFactor
this.indexer = indexer;
this.sonarLintCache = sonarLintCache;
this.analysisWarnings = analysisWarnings;
this.sensorTelemetryStorage = sensorTelemetryStorage;
}

public PythonSensor(FileLinesContextFactory fileLinesContextFactory, CheckFactory checkFactory, NoSonarFilter noSonarFilter, PythonCustomRuleRepository[] customRuleRepositories,
AnalysisWarningsWrapper analysisWarnings, SensorTelemetryStorage sensorTelemetryStorage) {
this(fileLinesContextFactory, checkFactory, noSonarFilter, customRuleRepositories, null, null, analysisWarnings, sensorTelemetryStorage);
this.sensorTelemetryStorage = new SensorTelemetryStorage();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -1418,13 +1417,11 @@ void send_telemetry_with_version() {
.build();

context.setSettings(new MapSettings().setProperty("sonar.python.version", "3.10,3.13"));

SensorTelemetryStorage sensorTelemetryStorage = spy(new SensorTelemetryStorage());
PythonSensor sensor = sensor(sensorTelemetryStorage);
sensor.execute(context);
verify(sensorTelemetryStorage, times(1)).send(context);
assertThat(sensorTelemetryStorage.data()).containsExactlyInAnyOrderEntriesOf(Map.of(TelemetryMetricKey.PYTHON_VERSION_KEY, "3.10,3.13",
TelemetryMetricKey.PYTHON_VERSION_SET_KEY, "1"));
var contextSpy = spy(context);
PythonSensor sensor = sensor();
sensor.execute(contextSpy);
verify(contextSpy, times(1)).addTelemetryProperty(TelemetryMetricKey.PYTHON_VERSION_KEY.key(), "3.10,3.13");
verify(contextSpy, times(1)).addTelemetryProperty(TelemetryMetricKey.PYTHON_VERSION_SET_KEY.key(), "1");
}

@Test
Expand All @@ -1435,12 +1432,10 @@ void send_telemetry_no_version() {
.build())
.build();

SensorTelemetryStorage sensorTelemetryStorage = spy(new SensorTelemetryStorage());
PythonSensor sensor = sensor(sensorTelemetryStorage);
sensor.execute(context);
verify(sensorTelemetryStorage, times(1)).send(context);
assertThat(sensorTelemetryStorage.data()).containsExactlyInAnyOrderEntriesOf(Map.of(TelemetryMetricKey.PYTHON_VERSION_SET_KEY, "0"));

PythonSensor sensor = sensor();
var contextSpy = spy(context);
sensor.execute(contextSpy);
verify(contextSpy, times(1)).addTelemetryProperty(TelemetryMetricKey.PYTHON_VERSION_SET_KEY.key(), "0");
}

private com.sonar.sslr.api.Token passToken(URI uri) {
Expand All @@ -1457,16 +1452,7 @@ private PythonSensor sensor() {
return sensor(CUSTOM_RULES, null, analysisWarning);
}

private PythonSensor sensor(SensorTelemetryStorage sensorTelemetryStorage) {
return sensor(CUSTOM_RULES, null, analysisWarning, sensorTelemetryStorage);
}

private PythonSensor sensor(@Nullable PythonCustomRuleRepository[] customRuleRepositories, @Nullable PythonIndexer indexer, AnalysisWarningsWrapper analysisWarnings) {
return sensor(customRuleRepositories, indexer, analysisWarnings, new SensorTelemetryStorage());
}

private PythonSensor sensor(@Nullable PythonCustomRuleRepository[] customRuleRepositories, @Nullable PythonIndexer indexer, AnalysisWarningsWrapper analysisWarnings,
SensorTelemetryStorage sensorTelemetryStorage) {
FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
FileLinesContext fileLinesContext = mock(FileLinesContext.class);
when(fileLinesContextFactory.createFor(Mockito.any(InputFile.class))).thenReturn(fileLinesContext);
Expand All @@ -1475,7 +1461,7 @@ private PythonSensor sensor(@Nullable PythonCustomRuleRepository[] customRuleRep
return new PythonSensor(fileLinesContextFactory, checkFactory, mock(NoSonarFilter.class), analysisWarnings);
}
if (indexer == null) {
return new PythonSensor(fileLinesContextFactory, checkFactory, mock(NoSonarFilter.class), customRuleRepositories, analysisWarnings, sensorTelemetryStorage);
return new PythonSensor(fileLinesContextFactory, checkFactory, mock(NoSonarFilter.class), customRuleRepositories, analysisWarnings);
}
if (customRuleRepositories == null) {
return new PythonSensor(fileLinesContextFactory, checkFactory, mock(NoSonarFilter.class), indexer, new SonarLintCache(), analysisWarnings);
Expand Down

0 comments on commit 4acefce

Please sign in to comment.