1919 */
2020package org .sonar .python .caching ;
2121
22+ import java .util .Optional ;
2223import org .junit .Test ;
2324import org .sonar .api .SonarProduct ;
2425import org .sonar .api .SonarRuntime ;
2526import org .sonar .api .batch .sensor .SensorContext ;
27+ import org .sonar .api .config .Configuration ;
2628import org .sonar .api .utils .Version ;
29+ import org .sonar .api .utils .log .LogTester ;
30+ import org .sonar .api .utils .log .LoggerLevel ;
2731import org .sonar .plugins .python .api .caching .CacheContext ;
2832
2933import 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 }
0 commit comments