From c3092b3cc87723ed0a75f6edb782724d109cb120 Mon Sep 17 00:00:00 2001 From: Alban Auzeill Date: Fri, 17 Nov 2023 17:58:10 +0100 Subject: [PATCH] [NO JIRA] Improve CI performance, change SanityTest to use batch mode instead of file by file analysis (#4555) --- .../org/sonar/plugins/java/SanityTest.java | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/sonar-java-plugin/src/test/java/org/sonar/plugins/java/SanityTest.java b/sonar-java-plugin/src/test/java/org/sonar/plugins/java/SanityTest.java index e2f0747480b..00e08171d26 100644 --- a/sonar-java-plugin/src/test/java/org/sonar/plugins/java/SanityTest.java +++ b/sonar-java-plugin/src/test/java/org/sonar/plugins/java/SanityTest.java @@ -43,12 +43,12 @@ import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import org.sonar.api.batch.sensor.internal.SensorContextTester; -import org.sonar.api.config.internal.MapSettings; import org.sonar.api.internal.SonarRuntimeImpl; import org.sonar.api.testfixtures.log.LogAndArguments; import org.sonar.api.testfixtures.log.LogTesterJUnit5; import org.sonar.api.utils.AnnotationUtils; import org.sonar.api.utils.Version; +import org.sonar.java.AnalysisProgress; import org.sonar.java.SonarComponents; import org.sonar.java.ast.JavaAstScanner; import org.sonar.java.checks.verifier.FilesUtils; @@ -60,7 +60,11 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; -import static org.sonar.java.checks.verifier.TestUtils.*; +import static org.sonar.java.checks.verifier.TestUtils.mainCodeSourcesPath; +import static org.sonar.java.checks.verifier.TestUtils.mainCodeSourcesPathInModule; +import static org.sonar.java.checks.verifier.TestUtils.nonCompilingTestSourcesPath; +import static org.sonar.java.checks.verifier.TestUtils.nonCompilingTestSourcesPathInModule; +import static org.sonar.java.checks.verifier.TestUtils.testCodeSourcesPath; class SanityTest { @@ -255,16 +259,20 @@ private static List getClassPathFromModule(String module) { private static List scanFiles(File moduleBaseDir, List inputFiles, List checks, List classpath) { SonarComponents sonarComponents = sonarComponents(moduleBaseDir, inputFiles); + JavaAstScanner scanner = new JavaAstScanner(sonarComponents); JavaVersion javaVersion = JParserConfig.MAXIMUM_SUPPORTED_JAVA_VERSION; - VisitorsBridgeForTests visitorsBridge = new VisitorsBridgeForTests(checks, classpath, sonarComponents, javaVersion); + scanner.setVisitorBridge(new VisitorsBridgeForTests(checks, classpath, sonarComponents, javaVersion)); List exceptions = new ArrayList<>(); - for (InputFile inputFile : inputFiles) { - try { - JavaAstScanner.scanSingleFileForTests(inputFile, visitorsBridge, null); - } catch (Throwable e) { - exceptions.add(new SanityCheckException(inputFile, e)); - } - } + AnalysisProgress analysisProgress = new AnalysisProgress(inputFiles.size()); + JParserConfig.Mode.BATCH + .create(javaVersion, classpath) + .parse(inputFiles, () -> false, analysisProgress, (input, result) -> { + try { + scanner.simpleScan(input, result, ast -> {}); + } catch (Throwable e) { + exceptions.add(new SanityCheckException(input, e)); + } + }); return exceptions; } @@ -303,9 +311,6 @@ private static String parseStackForCheck(@Nullable Throwable realCause) { private static SonarComponents sonarComponents(File moduleBaseDir, List inputFiles) { SensorContextTester context = SensorContextTester.create(moduleBaseDir).setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7))); - context.setSettings(new MapSettings() - .setProperty(SonarComponents.FAIL_ON_EXCEPTION_KEY, true) - .setProperty(SonarComponents.SONAR_BATCH_MODE_KEY, true)); DefaultFileSystem fileSystem = context.fileSystem(); SonarComponents sonarComponents = new SonarComponents(null, fileSystem, null, null, null, null) { @Override