Skip to content

Commit 9bb9526

Browse files
guillaume-dequennesonartech
authored andcommitted
SONARPY-4194 Fix failing caching tests (#1121)
GitOrigin-RevId: 505a4f3f290d02cf533f05a5fe36152bd410aa8f
1 parent 850af07 commit 9bb9526

4 files changed

Lines changed: 20 additions & 8 deletions

File tree

python-commons/src/main/java/org/sonar/plugins/python/PythonScanner.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,11 @@ public boolean scanFileWithoutParsing(PythonInputFile inputFile) {
290290
// We must avoid pushing measures twice due to the risk of duplicate cache key error.
291291
return false;
292292
}
293-
return restoreAndPushMeasuresIfApplicable(inputFile);
293+
boolean success = restoreAndPushMeasuresIfApplicable(inputFile);
294+
if (success && !testSourcesConfigured && fileType == InputFile.Type.MAIN) {
295+
indexer.writeEffectiveFileType(inputFile.wrappedFile().key(), effectiveTypeForRules);
296+
}
297+
return success;
294298
}
295299

296300
private boolean scanFileWithoutParsingNotSonarPython(PythonInputFile inputFile, PythonCheck check, InputFile.Type fileType,

python-commons/src/main/java/org/sonar/plugins/python/caching/Caching.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ public void copyFromPrevious(String fileKey) {
9292
cacheContext.getWriteCache().copyFromPrevious(importsMapCacheKey(fileKey));
9393
cacheContext.getWriteCache().copyFromPrevious(projectSymbolTableCacheKey(fileKey));
9494
cacheContext.getWriteCache().copyFromPrevious(fileContentHashCacheKey(fileKey));
95-
if (cacheContext.getReadCache().contains(effectiveFileTypeCacheKey(fileKey))) {
96-
cacheContext.getWriteCache().copyFromPrevious(effectiveFileTypeCacheKey(fileKey));
97-
}
9895
}
9996

10097
public void writeEffectiveFileType(String fileKey, InputFile.Type type) {

python-commons/src/test/java/org/sonar/plugins/python/PythonSensorTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,8 @@ void test_using_cache() throws IOException {
10931093
assertThat(context.allIssues()).isEmpty();
10941094
assertThat(logTester.logs(Level.INFO))
10951095
.contains("The Python analyzer was able to leverage cached data from previous analyses for 1 out of 1 files. These files were not parsed.");
1096+
assertThat(writeCache.getData())
1097+
.containsEntry(effectiveFileTypeCacheKey(inputFile.wrappedFile().key()), "MAIN".getBytes(UTF_8));
10961098
}
10971099

10981100
@Test
@@ -1202,6 +1204,8 @@ void test_typeshed_stub_cache_information_is_propagated() throws IOException {
12021204
byte[] bytes = writeCache.getData().get(TYPESHED_MODULES_KEY);
12031205
Set<String> resolvedTypeshedModules = new HashSet<>(Arrays.asList(new String(bytes, StandardCharsets.UTF_8).split(";")));
12041206
assertThat(resolvedTypeshedModules).containsExactlyInAnyOrder("math");
1207+
assertThat(writeCache.getData())
1208+
.containsEntry(effectiveFileTypeCacheKey(inputFile.wrappedFile().key()), "MAIN".getBytes(UTF_8));
12051209
}
12061210

12071211
@Test
@@ -1324,6 +1328,7 @@ void test_scan_without_parsing_fails_does_not_reexecute_successful_checks() {
13241328
readCache.put(importsMapCacheKey(inputFile.wrappedFile().key()), String.join(";", Collections.emptyList()).getBytes(StandardCharsets.UTF_8));
13251329
readCache.put(projectSymbolTableCacheKey(inputFile.wrappedFile().key()), serializedSymbolTable);
13261330
readCache.put(fileContentHashCacheKey(inputFile.wrappedFile().key()), inputFile.wrappedFile().md5Hash().getBytes(UTF_8));
1331+
readCache.put(effectiveFileTypeCacheKey(inputFile.wrappedFile().key()), "MAIN".getBytes(UTF_8));
13271332
context.setPreviousCache(readCache);
13281333
context.setNextCache(writeCache);
13291334
context.setCacheEnabled(true);
@@ -1356,6 +1361,7 @@ void test_partial_scan_without_parsing() {
13561361
readCache.put(importsMapCacheKey(inputFile2.wrappedFile().key()), String.join(";", List.of("file1.py")).getBytes(StandardCharsets.UTF_8));
13571362
readCache.put(projectSymbolTableCacheKey(inputFile2.wrappedFile().key()), serializedSymbolTable);
13581363
readCache.put(fileContentHashCacheKey(inputFile2.wrappedFile().key()), inputFile2.wrappedFile().md5Hash().getBytes(UTF_8));
1364+
readCache.put(effectiveFileTypeCacheKey(inputFile2.wrappedFile().key()), "MAIN".getBytes(UTF_8));
13591365
context.setPreviousCache(readCache);
13601366
context.setNextCache(writeCache);
13611367
context.setCacheEnabled(true);
@@ -1509,6 +1515,7 @@ void read_cpd_tokens_from_cache() throws IOException {
15091515
readCache.put(importsMapCacheKey(inputFile.wrappedFile().key()), String.join(";", Collections.emptyList()).getBytes(StandardCharsets.UTF_8));
15101516
readCache.put(projectSymbolTableCacheKey(inputFile.wrappedFile().key()), serializedSymbolTable);
15111517
readCache.put(fileContentHashCacheKey(inputFile.wrappedFile().key()), inputFile.wrappedFile().md5Hash().getBytes(UTF_8));
1518+
readCache.put(effectiveFileTypeCacheKey(inputFile.wrappedFile().key()), "MAIN".getBytes(UTF_8));
15121519

15131520
TestWriteCache writeCache = new TestWriteCache();
15141521
writeCache.bind(readCache);
@@ -1597,6 +1604,7 @@ void read_cpd_tokens_from_cache_corrupted_format() throws IOException {
15971604
readCache.put(importsMapCacheKey(inputFile.wrappedFile().key()), String.join(";", Collections.emptyList()).getBytes(StandardCharsets.UTF_8));
15981605
readCache.put(projectSymbolTableCacheKey(inputFile.wrappedFile().key()), serializedSymbolTable);
15991606
readCache.put(fileContentHashCacheKey(inputFile.wrappedFile().key()), inputFile.wrappedFile().md5Hash().getBytes(UTF_8));
1607+
readCache.put(effectiveFileTypeCacheKey(inputFile.wrappedFile().key()), "MAIN".getBytes(UTF_8));
16001608

16011609
TestWriteCache writeCache = new TestWriteCache();
16021610
writeCache.bind(readCache);
@@ -1670,6 +1678,7 @@ void cpd_tokens_failure_does_not_execute_checks_multiple_times() throws IOExcept
16701678
readCache.put(importsMapCacheKey(inputFile.wrappedFile().key()), String.join(";", Collections.emptyList()).getBytes(StandardCharsets.UTF_8));
16711679
readCache.put(projectSymbolTableCacheKey(inputFile.wrappedFile().key()), serializedSymbolTable);
16721680
readCache.put(fileContentHashCacheKey(inputFile.wrappedFile().key()), inputFile.wrappedFile().md5Hash().getBytes(UTF_8));
1681+
readCache.put(effectiveFileTypeCacheKey(inputFile.wrappedFile().key()), "MAIN".getBytes(UTF_8));
16731682

16741683
TestWriteCache writeCache = new TestWriteCache();
16751684
writeCache.bind(readCache);

python-commons/src/test/java/org/sonar/plugins/python/caching/CachingTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,22 +246,24 @@ void readEffectiveFileTypeReturnsNullForInvalidCachedValue() {
246246
}
247247

248248
@Test
249-
void copyFromPreviousIncludesEffectiveFileType() {
249+
void copyFromPreviousCopiesThreeIndexingKeys() {
250250
TestWriteCache writeCache = new TestWriteCache();
251251
TestReadCache readCache = new TestReadCache();
252252
writeCache.bind(readCache);
253253
CacheContextImpl cacheContext = new CacheContextImpl(true, new PythonWriteCacheImpl(writeCache), new PythonReadCacheImpl(readCache));
254254

255255
Caching caching = new Caching(cacheContext, CACHE_VERSION);
256256
String fileKey = "module:src/helper.py";
257-
// copyFromPrevious copies all 4 per-file keys — all must be present in the read cache
258257
readCache.put(Caching.importsMapCacheKey(fileKey), new byte[0]);
259258
readCache.put(Caching.projectSymbolTableCacheKey(fileKey), new byte[0]);
260259
readCache.put(Caching.fileContentHashCacheKey(fileKey), new byte[0]);
261-
readCache.put(effectiveFileTypeCacheKey(fileKey), "TEST".getBytes(StandardCharsets.UTF_8));
262260

263261
caching.copyFromPrevious(fileKey);
264262

265-
assertThat(writeCache.getData()).containsKey(effectiveFileTypeCacheKey(fileKey));
263+
assertThat(writeCache.getData())
264+
.containsKey(Caching.importsMapCacheKey(fileKey))
265+
.containsKey(Caching.projectSymbolTableCacheKey(fileKey))
266+
.containsKey(Caching.fileContentHashCacheKey(fileKey))
267+
.doesNotContainKey(effectiveFileTypeCacheKey(fileKey));
266268
}
267269
}

0 commit comments

Comments
 (0)