Skip to content

Commit 227bdbb

Browse files
SONARPY-1234 Disable cache when sonar.modules is used (#1326)
1 parent c61d672 commit 227bdbb

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

python-frontend/src/main/java/org/sonar/python/caching/CacheContextImpl.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@
2222
import org.sonar.api.SonarProduct;
2323
import org.sonar.api.batch.sensor.SensorContext;
2424
import org.sonar.api.utils.Version;
25+
import org.sonar.api.utils.log.Logger;
26+
import org.sonar.api.utils.log.Loggers;
2527
import org.sonar.plugins.python.api.caching.CacheContext;
2628
import org.sonar.plugins.python.api.caching.PythonReadCache;
2729
import org.sonar.plugins.python.api.caching.PythonWriteCache;
2830

2931
public class CacheContextImpl implements CacheContext {
3032

33+
private static final Logger LOG = Loggers.get(CacheContextImpl.class);
34+
private static final Version MINIMUM_RUNTIME_VERSION = Version.create(9, 7);
35+
3136
private final boolean isCacheEnabled;
3237
private final PythonWriteCache writeCache;
3338
private final PythonReadCache readCache;
@@ -54,7 +59,15 @@ public PythonWriteCache getWriteCache() {
5459
}
5560

5661
public static CacheContextImpl of(SensorContext context) {
57-
if (!context.runtime().getProduct().equals(SonarProduct.SONARLINT) && context.runtime().getApiVersion().isGreaterThanOrEqual(Version.create(9, 7))) {
62+
String sonarModules = context.config().get("sonar.modules").orElse("");
63+
boolean isUsingSonarModules = !sonarModules.isEmpty();
64+
if (isUsingSonarModules && context.isCacheEnabled()) {
65+
LOG.info("Caching will be disabled for this analysis due to the use of the \"sonar.modules\" property.");
66+
}
67+
if (!context.runtime().getProduct().equals(SonarProduct.SONARLINT)
68+
&& context.runtime().getApiVersion().isGreaterThanOrEqual(MINIMUM_RUNTIME_VERSION)
69+
&& !isUsingSonarModules
70+
) {
5871
return new CacheContextImpl(context.isCacheEnabled(), new PythonWriteCacheImpl(context.nextCache()), new PythonReadCacheImpl(context.previousCache()));
5972
}
6073
return new CacheContextImpl(false, new DummyCache(), new DummyCache());

python-frontend/src/test/java/org/sonar/python/caching/CacheContextImplTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@
1919
*/
2020
package org.sonar.python.caching;
2121

22+
import java.util.Optional;
2223
import org.junit.Test;
2324
import org.sonar.api.SonarProduct;
2425
import org.sonar.api.SonarRuntime;
2526
import org.sonar.api.batch.sensor.SensorContext;
27+
import org.sonar.api.config.Configuration;
2628
import org.sonar.api.utils.Version;
29+
import org.sonar.api.utils.log.LogTester;
30+
import org.sonar.api.utils.log.LoggerLevel;
2731
import org.sonar.plugins.python.api.caching.CacheContext;
2832

2933
import static org.assertj.core.api.Assertions.assertThat;
@@ -34,6 +38,10 @@ public class CacheContextImplTest {
3438

3539
private static final Version VERSION_WITH_CACHING = Version.create(9, 7);
3640
private static final Version VERSION_WITHOUT_CACHING = Version.create(9, 6);
41+
private static final String EXPECTED_SONAR_MODULE_LOG = "Caching will be disabled for this analysis due to the use of the \"sonar.modules\" property.";
42+
43+
@org.junit.Rule
44+
public LogTester logTester = new LogTester();
3745

3846
@Test
3947
public void cache_context_of_enabled_cache() {
@@ -67,6 +75,30 @@ public void cache_context_on_old_version() {
6775
assertThat(cacheContext.isCacheEnabled()).isFalse();
6876
}
6977

78+
@Test
79+
public void cache_context_with_sonar_modules_property() {
80+
SensorContext sensorContext = sensorContext(SonarProduct.SONARQUBE, VERSION_WITH_CACHING, true);
81+
Configuration configuration = mock(Configuration.class);
82+
when(configuration.get("sonar.modules")).thenReturn(Optional.of("module1, module2"));
83+
when(sensorContext.config()).thenReturn(configuration);
84+
85+
CacheContext cacheContext = CacheContextImpl.of(sensorContext);
86+
assertThat(cacheContext.isCacheEnabled()).isFalse();
87+
assertThat(logTester.logs(LoggerLevel.INFO)).contains(EXPECTED_SONAR_MODULE_LOG);
88+
}
89+
90+
@Test
91+
public void cache_context_when_cache_disabled_no_sonar_module_logs() {
92+
SensorContext sensorContext = sensorContext(SonarProduct.SONARQUBE, VERSION_WITH_CACHING, false);
93+
Configuration configuration = mock(Configuration.class);
94+
when(configuration.get("sonar.modules")).thenReturn(Optional.of("module1, module2"));
95+
when(sensorContext.config()).thenReturn(configuration);
96+
97+
CacheContext cacheContext = CacheContextImpl.of(sensorContext);
98+
assertThat(cacheContext.isCacheEnabled()).isFalse();
99+
assertThat(logTester.logs(LoggerLevel.INFO)).doesNotContain(EXPECTED_SONAR_MODULE_LOG);
100+
}
101+
70102
@Test
71103
public void dummy_cache() {
72104
CacheContext dummyCache = CacheContextImpl.dummyCache();
@@ -81,6 +113,8 @@ private static SensorContext sensorContext(SonarProduct product, Version version
81113
SensorContext sensorContext = mock(SensorContext.class);
82114
when(sensorContext.runtime()).thenReturn(runtime);
83115
when(sensorContext.isCacheEnabled()).thenReturn(isCacheEnabled);
116+
Configuration configuration = mock(Configuration.class);
117+
when(sensorContext.config()).thenReturn(configuration);
84118

85119
return sensorContext;
86120
}

sonar-python-plugin/src/test/java/org/sonar/plugins/python/indexer/SonarQubePythonIndexerTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import org.sonar.python.index.VariableDescriptor;
4848
import org.sonar.python.types.TypeShed;
4949

50-
import static org.assertj.core.api.Assertions.as;
5150
import static org.mockito.ArgumentMatchers.any;
5251
import static org.mockito.Mockito.spy;
5352
import static org.mockito.Mockito.when;

0 commit comments

Comments
 (0)