From 071bb12a6f070cb3a85a009e1cfde197f680110c Mon Sep 17 00:00:00 2001 From: Lucas Persson Date: Fri, 16 Aug 2024 16:20:41 +0200 Subject: [PATCH] add unit test for issue 9, java source code version --- .../sonarlint/impl/SonarlintAction.java | 11 +++----- .../se/solrike/sonarlint/IntegrationTest.java | 25 +++++++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/main/java/se/solrike/sonarlint/impl/SonarlintAction.java b/src/main/java/se/solrike/sonarlint/impl/SonarlintAction.java index b228d77..7cfa754 100644 --- a/src/main/java/se/solrike/sonarlint/impl/SonarlintAction.java +++ b/src/main/java/se/solrike/sonarlint/impl/SonarlintAction.java @@ -61,23 +61,20 @@ public SonarlintAction(Sonarlint task) { * @return list of sonarlint issues */ public List run(Sonarlint task, SetProperty plugins, ProjectLayout layout) { - - Logger logger = task.getLogger(); - - return analyze(task, logger, plugins, layout); - + return analyze(task, task.getLogger(), plugins, layout); } @SuppressWarnings({ "java:S1874", "deprecation" }) protected List analyze(Sonarlint task, Logger logger, SetProperty plugins, ProjectLayout layout) { - Map sonarProperties = new HashMap<>(); + Map sonarProperties = new HashMap<>(); Project project = task.getProject(); - // Java sourceCompatibility + // Java sourceCompatibility needs to be read so project is actually configured if (project.getProperties().containsKey("sourceCompatibility")) { String sourceCompatibility = project.getProperties().get("sourceCompatibility").toString(); sonarProperties.put("sonar.java.source", sourceCompatibility); } + Set compileClasspath = Collections.emptySet(); if (task.getCompileClasspath() != null) { compileClasspath = task.getCompileClasspath().getFiles(); diff --git a/src/test/java/se/solrike/sonarlint/IntegrationTest.java b/src/test/java/se/solrike/sonarlint/IntegrationTest.java index 8256593..a810c38 100644 --- a/src/test/java/se/solrike/sonarlint/IntegrationTest.java +++ b/src/test/java/se/solrike/sonarlint/IntegrationTest.java @@ -58,6 +58,9 @@ void setup() throws IOException { + " sarif.enabled = true\n" + " }\n" + "}\n" + + "java {\n" + + " sourceCompatibility = '1.8'\n" + + "}" ); // @formatter:on } @@ -113,6 +116,28 @@ void testSonarlintListRules() throws IOException { // CHECKSTYLE:ON } + @Test + void testJavaVersion() throws IOException { + // given the project has source comparability set to 1.8 the sonarlist component shall be invokded with that. + // and given that default java runtime is java11 + + createJavaFile(Files.createFile(mProjectDir.resolve("src/main/java/Hello.java"))); + + // when sonarlintMain is run with debug printouts + BuildResult buildResult = runGradle(false, List.of("--debug", "sonarlintMain")); + + // then java source shall be 1.8 + assertThat(buildResult.getOutput()).contains("extraProperties: {sonar.java.source=1.8"); + + // CHECKSTYLE:OFF + System.err.println(buildResult.getOutput()); + // CHECKSTYLE:ON + } + + + + + BuildResult runGradle(List args) { return runGradle(true, args); }