Skip to content

Commit

Permalink
add unit test for issue 9, java source code version
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas3oo committed Aug 16, 2024
1 parent 675392c commit 071bb12
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/main/java/se/solrike/sonarlint/impl/SonarlintAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,20 @@ public SonarlintAction(Sonarlint task) {
* @return list of sonarlint issues
*/
public List<IssueEx> run(Sonarlint task, SetProperty<File> 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<IssueEx> analyze(Sonarlint task, Logger logger, SetProperty<File> plugins, ProjectLayout layout) {
Map<String, String> sonarProperties = new HashMap<>();
Map<String, String> 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<File> compileClasspath = Collections.emptySet();
if (task.getCompileClasspath() != null) {
compileClasspath = task.getCompileClasspath().getFiles();
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/se/solrike/sonarlint/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ void setup() throws IOException {
+ " sarif.enabled = true\n"
+ " }\n"
+ "}\n"
+ "java {\n"
+ " sourceCompatibility = '1.8'\n"
+ "}"
);
// @formatter:on
}
Expand Down Expand Up @@ -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<String> args) {
return runGradle(true, args);
}
Expand Down

0 comments on commit 071bb12

Please sign in to comment.