Skip to content

Commit

Permalink
test: pass values via system property instead of unsafe string injection
Browse files Browse the repository at this point in the history
Pass values using JSON encapsulation via system property.

- This removes the need to escape/transform strings
- This allows the test to work under Windows, which uses the \ backslash
  character as the directory separator
- This also make the test work in case other special characters are in
  the path such as ' (quote)
  • Loading branch information
Fiouz committed Aug 2, 2022
1 parent 80fd874 commit 0f88f7f
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/test/groovy/com/gorylenko/BuildSrcFunctionalTest.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gorylenko

import com.gorylenko.properties.GitRepositoryBuilder
import groovy.json.JsonOutput
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Rule
Expand All @@ -19,24 +20,21 @@ public class BuildSrcFunctionalTest {
def projectDir = temporaryFolder.newFolder()
def runner = GradleRunner.create()
.withPluginClasspath()
.withArguments("check")
.withProjectDir(projectDir)

def classpathString = runner.pluginClasspath
.collect { "'$it'" }
.join(", ")
runner.withArguments('check', "-Dcom.gorylenko.gradle-git-properties.unit-test-classpath=${JsonOutput.toJson(runner.pluginClasspath*.path)}")

def buildSrcDir = new File(projectDir, "buildSrc")
buildSrcDir.mkdirs()
new File(buildSrcDir, "build.gradle") << """\
import groovy.json.JsonSlurper
plugins {
id("groovy-gradle-plugin")
}
repositories {
gradlePluginPortal()
}
dependencies {
runtimeClasspath files($classpathString)
runtimeClasspath files(new JsonSlurper().parseText(System.getProperty('com.gorylenko.gradle-git-properties.unit-test-classpath')))
}
"""
def pluginSourceDir = buildSrcDir.toPath().resolve("src").resolve("main").resolve("groovy").toFile()
Expand Down Expand Up @@ -77,24 +75,22 @@ public class BuildSrcFunctionalTest {
.withPluginClasspath()
.withArguments("check")
.withProjectDir(projectDir)

def classpathString = runner.pluginClasspath
.collect { "'$it'" }
.join(", ")
runner.withArguments('check', "-Dcom.gorylenko.gradle-git-properties.unit-test-classpath=${JsonOutput.toJson(runner.pluginClasspath*.path)}")

def buildSrcDir = new File(projectDir, "buildSrc")
buildSrcDir.mkdirs()
def gitPropDir = new File(projectDir, "gitProp")
gitPropDir.mkdirs()
new File(buildSrcDir, "build.gradle") << """\
import groovy.json.JsonSlurper
plugins {
id("groovy-gradle-plugin")
}
repositories {
gradlePluginPortal()
}
dependencies {
runtimeClasspath files($classpathString)
runtimeClasspath files(new JsonSlurper().parseText(System.getProperty('com.gorylenko.gradle-git-properties.unit-test-classpath')))
}
"""
def pluginSourceDir = buildSrcDir.toPath().resolve("src").resolve("main").resolve("groovy").toFile()
Expand Down

0 comments on commit 0f88f7f

Please sign in to comment.