-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONARGRADL-120 Add .gradle.kts files to the sonar.sources (#182)
- Loading branch information
Showing
2 changed files
with
229 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
202 changes: 202 additions & 0 deletions
202
src/test/groovy/org/sonarqube/gradle/GradleKtsTests.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
/* | ||
* SonarQube Scanner for Gradle | ||
* Copyright (C) 2015-2023 SonarSource | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | ||
*/ | ||
package org.sonarqube.gradle | ||
|
||
import org.assertj.core.api.Assertions | ||
import org.gradle.testkit.runner.GradleRunner | ||
import spock.lang.Specification | ||
import spock.lang.TempDir | ||
|
||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import java.nio.file.StandardCopyOption | ||
|
||
class GradleKtsTests extends Specification { | ||
@TempDir | ||
Path testProjectDir | ||
Path settingsFile | ||
Path buildFile | ||
Path outFile | ||
|
||
def setup() { | ||
outFile = testProjectDir.resolve('out.properties') | ||
// For JaCoCo coverage, see https://github.com/koral--/jacoco-gradle-testkit-plugin | ||
InputStream is = GradleKtsTests.class.getClassLoader().getResourceAsStream('testkit-gradle.properties') | ||
Files.copy(is, testProjectDir.resolve('gradle.properties'), StandardCopyOption.REPLACE_EXISTING) | ||
} | ||
|
||
def "add build and settings gradle files to sources when both are in kotlin dsl"() { | ||
given: | ||
addBuildKts() | ||
addSettingsKts() | ||
|
||
when: | ||
def result = 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: | ||
def sonarSources = props["sonar.sources"].split(",") | ||
Assertions.assertThat(sonarSources) | ||
.containsExactlyInAnyOrder(settingsFile.toRealPath().toString(), buildFile.toRealPath().toString()) | ||
|
||
} | ||
|
||
def "add only build and file to sources when only build is in kotlin dsl"() { | ||
given: | ||
addBuildKts() | ||
addSettings() | ||
|
||
when: | ||
def result = 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"] == buildFile.toRealPath().toString() | ||
|
||
} | ||
|
||
def "add only settings file to sources when only settings file is in kotlin dsl"() { | ||
given: | ||
addBuild() | ||
addSettingsKts() | ||
|
||
when: | ||
def result = 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"] == settingsFile.toRealPath().toString() | ||
|
||
} | ||
|
||
def "add only build file to sources when no settings found"() { | ||
given: | ||
addBuildKts() | ||
|
||
when: | ||
def result = 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"] == buildFile.toRealPath().toString() | ||
|
||
} | ||
|
||
def "add nothing to sources when Groovy dsl is used"() { | ||
given: | ||
addBuild() | ||
addSettings() | ||
|
||
when: | ||
def result = 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"] == "" | ||
|
||
} | ||
|
||
def "add nothing to sources when Groovy dsl is used and no settings"() { | ||
given: | ||
addBuild() | ||
|
||
when: | ||
def result = 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"] == "" | ||
|
||
} | ||
|
||
private def addSettings() { | ||
settingsFile = testProjectDir.resolve('settings.gradle') | ||
settingsFile << "rootProject.name = 'java-task-toolchains'" | ||
} | ||
|
||
private def addSettingsKts() { | ||
settingsFile = testProjectDir.resolve('settings.gradle.kts') | ||
settingsFile << """rootProject.name = "java-task-toolchains" | ||
""" | ||
} | ||
|
||
private def addBuild() { | ||
buildFile = testProjectDir.resolve('build.gradle') | ||
buildFile << """ | ||
plugins { | ||
id 'java' | ||
id 'org.sonarqube' | ||
} | ||
""" | ||
} | ||
|
||
private def addBuildKts() { | ||
buildFile = testProjectDir.resolve('build.gradle.kts') | ||
buildFile << """ | ||
plugins { | ||
java | ||
id("org.sonarqube") | ||
} | ||
""" | ||
} | ||
} | ||
|