I posted about this [earlier] (http://forums.gradle.org/gradle/topics/spock-test-for-plugin-verify-plugin-initializes-data-from-specific-extension-object), but I didn’t get any responses.
I’m writing my first plugin, along with my first Spock tests.
For background, the plugin will define a single task, which will read a set of input files from a defined location and use a defined set of “code generator” classes to generate some java code that will be written to defined locations and then eventually compiled.
I expect a build script to apply the plugin and define the extension block with property settings, and the plugin should do everything else.
Up to this point, my plugin is a skeleton, defining an empty task with some instance variables, and an extension object for configuration. I have a single spec method that successfully verifies the task is registered with the project.
I then wanted to write a spec method that verifies that for a particular set of properties set in my extension object, that the task is created with expected values. This is where I’m starting to get confused.
This is my plugin class so far:
class YangPlugin implements Plugin<Project> {
public static final YANG_PLUGIN
= 'yang'
public static final YANG_GENERATE_TASK = 'yangGenerate'
@Override
public void apply(Project project) {
project.plugins.apply(JavaPlugin)
YangExtension yang = project.extensions.create(YANG_PLUGIN, YangExtension)
project.task(YANG_GENERATE_TASK, type: YangGenerateTask) {
it.sourceDir = yang.yangFilesRootDir
it.generators = yang.generators
}
}
}
My extension object, along with one other POGO:
class YangExtension {
CodeGenerator[] generators
String
yangFilesRootDir
String[]
excludeFiles
boolean
inspectDependencies
}
class CodeGenerator {
String generatorClassName
File outputBaseDir
Map
additionalConfiguration
}
The task class:
class YangGenerateTask extends DefaultTask {
CodeGenerator[] generators
@InputDirectory
File sourceDir
@Input
Class<?>[] getClasses() {
generators.collect { it.generatorClass }
}
@OutputDirectories
File[] getOutputDirectories() {
generators.collect { it.outputBaseDir }
}
@TaskAction
void generate() {
}
}
And finally, my Spec:
class YangPluginSpec extends Specification {
def "Yang plugin registers YangGenerate task and no others"() {
given:
Project project = ProjectBuilder.builder().build()
when: "plugin is applied to project"
project.apply(plugin: YangPlugin)
then: "Yang task is registered"
project.tasks.withType(YangGenerateTask).collect { it.name }.sort() == ["yangGenerate"]
}
def "plugin copies data from extension object"() {
given:
Project project = ProjectBuilder.builder().build()
YangPlugin yangPlugin = new YangPlugin()
when: "plugin is applied to project"
YangExtension yang = project.extensions.create("yang", YangExtension)
yangPlugin.apply(project)
//project.apply(plugin: YangPlugin)
then: "Yang task gets sourcedir set from extension object"
project.yangGenerate.sourceDir == "/abcx"
}
}
The first method is fine, and the second method is where I’m stuck. It’s current state is probably horribly wrong, but I’m not sure what to do here. My assertion at the end is intended to verify the property in the task is the same as what I would set in the extension object. My intention is to define the extension object that would be used and then somehow apply that to the plugin.