Testing specific extension properties for Gradle plugin

498 views
Skip to first unread message

David Karr

unread,
Mar 5, 2015, 4:31:30 PM3/5/15
to spockfr...@googlegroups.com
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.

Marcin Grzejszczak

unread,
Mar 7, 2015, 6:51:26 AM3/7/15
to spockfr...@googlegroups.com
Hi!

Did you write in the implementation of your plugin

project.extensions.create('yang', YangExtension)

BR,
Marcin

David Karr

unread,
Mar 7, 2015, 12:31:27 PM3/7/15
to spockfr...@googlegroups.com
Thanks for replying, Marcin. I'm not making much progress with this,but I think my main problems lie with the Gradle API, not with Spock. I think I'd have similar problems with a plain JUnit test. However, I appreciate anyone's help on this.

Yes, I have a line similar to that in the plugin “apply” method.

Here is my current plugin body (with some other attempted variations commented out):
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
       
}
//        project.afterEvaluate {
//            task.sourceDir    = yang.yangFilesRootDir
//            task.generators    = yang.generators
//        }
       
//project.afterEvaluate {
//            project.task(YANG_GENERATE_TASK, type: YangGenerateTask) {
//                it.sourceDir    = yang.yangFilesRootDir
//                it.generators    = yang.generators
//            }
       
//}
   
}
}


Note that I'm also asking about this on the Gradle forum.

Marcin Grzejszczak

unread,
Mar 8, 2015, 8:35:00 AM3/8/15
to spockfr...@googlegroups.com
David I replied to you at the Gradle forum cause I don't think it's relared to Spock at all.

David Karr

unread,
Mar 8, 2015, 2:54:52 PM3/8/15
to spockfr...@googlegroups.com


On Sunday, March 8, 2015 at 5:35:00 AM UTC-7, Marcin Grzejszczak wrote:
David I replied to you at the Gradle forum cause I don't think it's relared to Spock at all.

Thanks, Marcin.

If anyone is tracking this, I managed to resolve this.  As was obvious, this wasn't a Spock problem, although you might run into this while trying to implement a Spock test (or JUnit, or whatever) of a Gradle plugin.

You can see the details on the Gradle forum thread.
Reply all
Reply to author
Forward
0 new messages