Skip to content

Instantly share code, notes, and snippets.

@breskeby
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save breskeby/eb0a43caa0722b3ec8a7 to your computer and use it in GitHub Desktop.
Save breskeby/eb0a43caa0722b3ec8a7 to your computer and use it in GitHub Desktop.
sample test spec for testing with gradle toolingapi and task options (tested with 2.1)
package org.acme
import org.gradle.tooling.BuildLauncher
import org.gradle.tooling.GradleConnector
import org.gradle.tooling.ProjectConnection
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Specification
/**
* Created by Rene on 11/09/14.
*/
class SamplePluginTest extends Specification{
@Rule
TemporaryFolder testFolder = new TemporaryFolder();
File projectDir;
String output;
def setup(){
projectDir = testFolder.newFolder();
}
def "can test build with parameter"(){
given:
buildFile << """
import org.gradle.api.DefaultTask
import org.gradle.api.internal.tasks.options.Option
import org.gradle.api.tasks.TaskAction
/**
* Created by Rene on 11/09/14.
*/
class SampleTask extends DefaultTask{
String param
@Option(option = "param", description = "Some parameter")
public SampleTask setParameter(String para) {
this.param = para;
return this;
}
@TaskAction void printValue(){
println "task parameter: '\$param'"
}
}
task sampleTask(type:SampleTask)
"""
when:
succeed("sampleTask", "--param", "someValue")
then:
output.contains("task parameter: 'someValue'")
}
File getBuildFile() {
projectDir.mkdirs()
new File(projectDir, "build.gradle")
}
def succeed(String... args) {
// Configure the connector and create the connection
GradleConnector connector = GradleConnector.newConnector();
connector.forProjectDirectory(projectDir);
ProjectConnection connection = connector.connect();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
// Configure the build
BuildLauncher launcher = connection.newBuild();
launcher.withArguments(args)
launcher.setStandardOutput(outputStream);
launcher.setStandardError(outputStream);
// Run the build
launcher.run();
} finally {
// Clean up
output = outputStream.toString()
connection.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment