Search code examples
groovyspockjenkins-groovy

How is spock calling this function in this test?


I'm going through this example but something about it is very confusing to me: https://www.testcookbook.com/book/groovy/jenkins/intro-testing-job-dsl.html

In this test, how/what is executing getJobFiles()? I don't see it being called anywhere. Is there some magic with jobFiles? Is specifying jobFiles somehow calling getJobFiles?

import javaposse.jobdsl.dsl.DslScriptLoader
import javaposse.jobdsl.plugin.JenkinsJobManagement
import org.junit.ClassRule
import org.jvnet.hudson.test.JenkinsRule
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll

class JobScriptsSpec extends Specification {
    @Shared
    @ClassRule
    JenkinsRule jenkinsRule = new JenkinsRule()

    @Unroll
    def 'test script #file.name'(File file) {
        given:
        def jobManagement = new JenkinsJobManagement(System.out, [:], new File('.'))

        when:
        new DslScriptLoader(jobManagement).runScript(file.text)

        then:
        noExceptionThrown()

        where:
        file << jobFiles
    }

    static List<File> getJobFiles() {
        List<File> files = []
        new File('jobs').eachFileRecurse {
            if (it.name.endsWith('.groovy')) {
                files << it
            }
        }
        files
    }
}

Edit

It seems like jobFiles does call getJobFiles() but I don't understand how. Is this a groovy or spock feature? I've been trying to research this but can finding anything explaining this in detail.


Solution

  • This is standard Groovy functionality. You can abbreviate any getter call like

    def file = new File("sarek-test-parent/sarek-test-common/src/main/java")
    println file.name          // getName()
    println file.parent        // getParent()
    println file.absolutePath  // getAbsolutePath()
    println file.directory     // isDirectory()
    
    java
    sarek-test-parent\sarek-test-common\src\main
    C:\Users\alexa\Documents\java-src\Sarek\sarek-test-parent\sarek-test-common\src\main\java
    true
    

    The same works for setters:

    new Person().name = "John"      // setName("John")
    new Person().zipCode = "12345"  // setZipCode("12345")
    

    Actually the second link provided by jaco0646 explains it, just his mixing up this simple fact with data providers clouds the explanation.