Last active
August 29, 2015 14:06
-
-
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)
This file contains hidden or 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
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