I don’t know whether this is better posted here, or in the Spock forum.
I have a plugin and task skeleton, along with a Spock spec with a single test that I’ve verified on the command line (fails in Eclipse, but that’s a different problem).
My plugin defines a task, but I expect to completely configure and run the task just from the extension object in the build script. My simple test applies the plugin and verifies that the task is registered. Now, I want to write a test that verifies that the plugin initializes its data and task with data from properties in the extension object that I create.
I’ll post some brief code samples. Feel free to point out other issues with what I’m doing here.
Here is my plugin “apply” method:
public void apply(Project project) {
project.plugins.apply(JavaPlugin)
YangExtension yang = project.extensions.create("$YANG_PLUGIN", YangExtension)
project.task("$YANG_GENERATE_TASK", type: YangGenerateTask) {
sourceDir = yang.yangFilesRootDir
generators = yang.generators
}
}
Here’s my test so far:
class YangPluginSpec extends Specification {
Project project = ProjectBuilder.builder().build()
def "Yang plugin registers YangGenerate task and no others"() {
when: "plugin is applied to project"
project.apply(plugin: YangPlugin)
then: "Yang task is registered"
project.tasks.withType(YangGenerateTask).collect { it.name }.sort() == ["yangGenerate"]
}
}
Update:
The next thing I’m trying to do is test that the plugin and task are initialized with correct data based on the contents of a simulated extension block.
For instance, I was trying something like this:
def "plugin copies data from extension object"() {
given:
Project project = ProjectBuilder.builder().build()
when: "plugin is applied to project"
project.apply(plugin: YangPlugin)
//project.yang = [yangFilesRootDir : "/abc"]
then: "Yang task gets sourcedir set from extension object"
project.yangGenerate.sourceDir == "/abcx"
}
I’m comparing with “abcx” initially just to make sure the test fails, if everything else works. I needn’t have worried about that, as this has bigger problems. If I uncomment the line to set “project.yang”, it fails with:
java.lang.IllegalArgumentException: There's an extension registered with name 'yang'. You should not reassign it via a property setter.
at org.gradle.api.internal.plugins.ExtensionsStorage.checkExtensionIsNotReassigned(ExtensionsStorage.java:57)
at org.gradle.api.internal.plugins.DefaultConvention$ExtensionsDynamicObject.setProperty(DefaultConvention.java:190)
at org.gradle.api.internal.CompositeDynamicObject.setProperty(CompositeDynamicObject.java:101)
at com.att.opnfv.yang.gradle.YangPluginSpec.plugin copies data from extension object(YangPluginSpec.groovy:26)
What are some reasonable strategies for doing this?