Skip to content

Commit

Permalink
SONARGRADL-133 Fix Exception when adding Gradle Kotlin scripts if cus…
Browse files Browse the repository at this point in the history
…tom 'sonar.sources' value is defined (#215)
  • Loading branch information
leveretka authored Mar 22, 2024
1 parent 1367c64 commit 9c2cc39
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/main/java/org/sonarqube/gradle/SonarPropertyComputer.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,13 @@ private void computeSonarProperties(Project project, Map<String, Object> propert
if (isAndroidProject(project)) {
AndroidUtils.configureForAndroid(project, SonarQubePlugin.getConfiguredAndroidVariant(project), rawProperties);
}

ActionBroadcast<SonarProperties> actionBroadcast = actionBroadcastMap.get(project.getPath());
if (actionBroadcast != null) {
evaluateSonarPropertiesBlocks(actionBroadcast, rawProperties);
}
if (project.equals(targetProject)) {
addEnvironmentProperties(rawProperties);
addSystemProperties(rawProperties);
if (isRootProject(project)) {
addKotlinBuildScriptsToSources(project, rawProperties);
}

overrideWithUserDefinedProperties(project, rawProperties);

// This is required if "sonar.sources" are neither found nor defined by user
rawProperties.putIfAbsent(SONAR_SOURCES_PROP, "");

if (project.equals(targetProject)) {
Expand Down Expand Up @@ -156,6 +152,20 @@ private void computeSonarProperties(Project project, Map<String, Object> propert
properties.put(convertKey("sonar.modules", prefix), String.join(",", moduleIds));
}

private void overrideWithUserDefinedProperties(Project project, Map<String, Object> rawProperties) {
ActionBroadcast<SonarProperties> actionBroadcast = actionBroadcastMap.get(project.getPath());
if (actionBroadcast != null) {
evaluateSonarPropertiesBlocks(actionBroadcast, rawProperties);
}
if (isRootProject(project)) {
addEnvironmentProperties(rawProperties);
addSystemProperties(rawProperties);
}
}

private boolean isRootProject(Project project) {
return project.equals(targetProject);
}

private static void evaluateSonarPropertiesBlocks(ActionBroadcast<? super SonarProperties> propertiesActions, Map<String, Object> properties) {
SonarProperties sqProperties = new SonarProperties(properties);
Expand Down
37 changes: 37 additions & 0 deletions src/test/groovy/org/sonarqube/gradle/GradleKtsTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,27 @@ class GradleKtsTests extends Specification {

}

def "don't add .gradle.kts files if sources are overridden"() {
given:
addBuildKtsWithCustomSources()
addSettingsKts()

when:
GradleRunner.create()
.withProjectDir(testProjectDir.toFile())
.forwardOutput()
.withArguments('sonarqube', '--info', '-Dsonar.scanner.dumpToFile=' + outFile.toAbsolutePath())
.withPluginClasspath()
.build()

def props = new Properties()
props.load(outFile.newDataInputStream())

then:
props["sonar.sources"] == "src/main/custom"

}

private def addSettings() {
settingsFile = testProjectDir.resolve('settings.gradle')
settingsFile << "rootProject.name = 'java-task-toolchains'"
Expand Down Expand Up @@ -230,6 +251,22 @@ class GradleKtsTests extends Specification {
"""
}

private def addBuildKtsWithCustomSources() {
buildFile = testProjectDir.resolve('build.gradle.kts')
buildFile << """
plugins {
java
id("org.sonarqube")
}
sonar {
properties {
property("sonar.sources", "src/main/custom")
}
}
"""
}

private def addSubProject() {
settingsFile << """
include("subproject")
Expand Down

0 comments on commit 9c2cc39

Please sign in to comment.