I am building a Gradle plugin that requires a username and password for the access to an external resource; to avoid clear text passwords, I use the plugin nu.studer.credentials
.
To test my plugin, I use Spock; the following test file CredentialsFunctionTest.groovy
was stripped down to only the relevant parts, to show the problem:
package my.gradle.plugin
import org.gradle.testkit.runner.GradleRunner
import spock.lang.Specification
import spock.lang.TempDir
/**
* A simple functional test for the credentials acquisition.
*/
class CredentialsFunctionalTest extends Specification {
@TempDir
private File projectDir
private getBuildFile() {
new File( projectDir, "build.gradle" )
}
private getSettingsFile() {
new File( projectDir, "settings.gradle" )
}
def "can run task"() {
given:
//---> Begin of settings file <------------------------------------------------
settingsFile << """
plugins {
id 'nu.studer.credentials' version '3.0'
}
"""
//---> End of settings file <--------------------------------------------------
//---> Begin of build file <---------------------------------------------------
buildFile << """
plugins {
id 'nu.studer.credentials'
}
String username = credentials.forKey( 'myUser' )
System.out.printf( 'Username: %s%n', username )
String password = credentials.forKey( 'myPassword' )
System.out.printf( 'Password: %s%n', password )
task doSomething {
}
"""
//---> End of build file <-----------------------------------------------------
when:
def runner = GradleRunner.create()
runner.forwardOutput()
runner.withPluginClasspath()
runner.withArguments("doSomething" )
runner.withProjectDir( projectDir )
def result = runner.build()
then:
result.output.contains( "Alive!" )
}
}
The output for "Username" and "Password" is null
– that the test as such fails is expected, as there is no output for the String "Alive!"
anywhere.
When I place the files settings.gradle
and build.gradle
(content below) to an empty folder, and call gradle with gradle doSomething
, I get the expected output.
settings.gradle
:
plugins {
id 'nu.studer.credentials' version '3.0'
}
build.gradle
:
plugins {
id 'nu.studer.credentials'
}
String username = credentials.forKey( 'myUser' )
System.out.printf( 'Username: %s%n', username )
String password = credentials.forKey( 'myPassword' )
System.out.printf( 'Password: %s%n', password )
task doSomething {
}
The nu.studer.credentials
plugin stores the values in $GRADLE_USER_HOME/gradle.encrypted.properties
, so one assumption is that Spock is using a different, temporary user and/or home folder.