Skip to content

Commit

Permalink
SONARJAVA-4267 Fix skipping of generated files (#4119)
Browse files Browse the repository at this point in the history
  • Loading branch information
dorian-burihabwa-sonarsource authored Jun 16, 2022
1 parent e749ee9 commit cc15204
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
6 changes: 2 additions & 4 deletions java-frontend/src/main/java/org/sonar/java/JavaFrontend.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import javax.annotation.Nullable;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.utils.log.Logger;
Expand Down Expand Up @@ -142,10 +143,7 @@ public void scan(Iterable<InputFile> sourceFiles, Iterable<InputFile> testFiles,
successfullyScanned += testFilesScannedWithoutParsing.get(true).size();
total += testFilesScannedWithoutParsing.get(true).size() + testFilesScannedWithoutParsing.get(false).size();

Map<Boolean, List<InputFile>> generatedFilesScannedWithoutParsing = astScannerForGeneratedFiles.scanWithoutParsing(generatedFiles);
generatedFiles = generatedFilesScannedWithoutParsing.get(false);
successfullyScanned += generatedFilesScannedWithoutParsing.get(true).size();
total += generatedFilesScannedWithoutParsing.get(true).size() + generatedFilesScannedWithoutParsing.get(false).size();
total += StreamSupport.stream(generatedFiles.spliterator(), false).count();

LOG.info(
"Server-side caching is enabled. The Java analyzer was able to leverage cached data from previous analyses for {} out of {} files. These files will not be parsed.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.sonar.java.classpath.ClasspathForMain;
import org.sonar.java.classpath.ClasspathForTest;
import org.sonar.java.exceptions.ApiMismatchException;
import org.sonar.java.model.GeneratedFile;
import org.sonar.java.model.JProblem;
import org.sonar.java.model.LineUtils;
import org.sonar.java.reporting.AnalyzerMessage;
Expand Down Expand Up @@ -451,6 +452,10 @@ public boolean canSkipUnchangedFiles() throws ApiMismatchException {


public boolean fileCanBeSkipped(InputFile inputFile) {
if (inputFile instanceof GeneratedFile) {
// Generated files should not be skipped as we cannot assess the change status of the source file
return false;
}
boolean canSkipInContext;
try {
canSkipInContext = canSkipUnchangedFiles();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -77,6 +78,7 @@
import org.sonar.java.classpath.ClasspathForMain;
import org.sonar.java.classpath.ClasspathForTest;
import org.sonar.java.exceptions.ApiMismatchException;
import org.sonar.java.model.GeneratedFile;
import org.sonar.java.model.JParserTestUtils;
import org.sonar.java.model.JavaTree;
import org.sonar.java.reporting.AnalyzerMessage;
Expand Down Expand Up @@ -714,6 +716,23 @@ void skipUnchangedFiles_throws_a_NoSuchMethodError_when_canSkipUnchangedFiles_no
assertThat(error).hasCause(new NoSuchMethodError("API version mismatch :-("));
}

@Test
void fileCanBeSkipped_returns_false_when_the_file_is_a_generated_file() throws ApiMismatchException {
SensorContextTester sensorContextTester = SensorContextTester.create(new File(""));
SonarComponents sonarComponents = spy(
new SonarComponents(
fileLinesContextFactory,
sensorContextTester.fileSystem(),
mock(ClasspathForMain.class),
mock(ClasspathForTest.class),
checkFactory
)
);
InputFile inputFile = new GeneratedFile(Path.of("non-existing-generated-file.java"));

assertThat(sonarComponents.fileCanBeSkipped(inputFile)).isFalse();
}

@Test
void fileCanBeSkipped_always_returns_false_when_skipUnchangedFiles_is_false() throws ApiMismatchException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.sonar.java.model;

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -38,7 +39,6 @@
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.java.AnalysisException;
import org.sonar.java.CheckFailureException;
import org.sonar.plugins.java.api.internal.EndOfAnalysis;
import org.sonar.java.JavaVersionAwareVisitor;
import org.sonar.java.SonarComponents;
import org.sonar.java.TestUtils;
Expand All @@ -54,6 +54,7 @@
import org.sonar.plugins.java.api.JavaVersion;
import org.sonar.plugins.java.api.ModuleScannerContext;
import org.sonar.plugins.java.api.caching.CacheContext;
import org.sonar.plugins.java.api.internal.EndOfAnalysis;
import org.sonar.plugins.java.api.tree.CompilationUnitTree;
import org.sonar.plugins.java.api.tree.SyntaxToken;
import org.sonar.plugins.java.api.tree.SyntaxTrivia;
Expand Down Expand Up @@ -491,6 +492,21 @@ void scanWithoutParsing_returns_false_when_the_file_cannot_be_skipped() throws A
assertThat(visitorsBridge.scanWithoutParsing(inputFile)).isFalse();
}

@Test
void scanWithoutParsing_returns_false_when_the_file_is_a_generated_file() throws ApiMismatchException {
InputFile inputFile = new GeneratedFile(Path.of("non-existing-generated-file.java"));

SonarComponents sonarComponents = spy(new SonarComponents(null, null, null, null, null));
doReturn(true).when(sonarComponents).canSkipUnchangedFiles();
VisitorsBridge visitorsBridge = new VisitorsBridge(
Collections.singletonList(new EndOfAnalysisVisitor()),
Collections.emptyList(),
sonarComponents
);

assertThat(visitorsBridge.scanWithoutParsing(inputFile)).isFalse();
}

@Test
void scanWithoutParsing_returns_true_for_scanners_that_do_not_override_scanWithoutParsing() throws ApiMismatchException {
JavaFileScanner scanner = new DefaultEndOfAnalysisCheck();
Expand Down

0 comments on commit cc15204

Please sign in to comment.