I asked about this on the Gradle forum, but perhaps this forum will have a better POV.
I'm writing my first Spock spec and Gradle plugin, and am pretty new to Groovy, so I'm still just trying to figure things out.
My Gradle plugin skeleton creates a task, and attempts to set properties in the task from the plugin's extension block. I have a simple test that verifies the task is registered, but now I want to write a test showing that the task is initialized with expected data from an extension block.
My first stab at this looks 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"
}
If I uncomment the "project.yang" line, 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)
I also thought to put the "project.yang" line before the "apply" line, but that fails, saying that property "yang" does not exist.
This is, of course, all in Gradle code, but I'm wondering if there are any common Spock conventions for doing this kind of thing.