Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script under test doesn't correctly handle field annotated @Field in script's methods #120

Open
PavelKa opened this issue May 5, 2022 · 3 comments

Comments

@PavelKa
Copy link

PavelKa commented May 5, 2022

Expected Behavior

The tested script correctly handles fields annotated @field in all methods

Actual Behavior

Methods access the wrong instance of the field

Steps to Reproduce

define script :
vars/testFiled.groovy

import groovy.transform.Field
@Field f = "test"
def call(Map _args) {
    node {
        stage("set f") {
            echo(f)
            f = "test2"
            setF()
        }
    }
}
def setF() {
   // field f should contains value "test2" but contains "test" instead
    echo (f)
}

define test:

import com.homeaway.devtools.jenkins.testing.JenkinsPipelineSpecification

class TestFieldSpec extends JenkinsPipelineSpecification {
    def "should echo test test2"() {
        given:
        def testField = loadPipelineScriptForTest("vars/testField.groovy")
        when:
        testField()
        then:
        1 * getPipelineMock("echo")('test')
        1 * getPipelineMock("echo")('test2')
    }
}

Test fails:
Too many invocations for:

1 * getPipelineMock("echo")('test') (2 invocations)

Additional Information

Tested with Java 1.8 and 11 and jenkins-spock.version 2.1.5

Script works as expected in Jenkins

@mikedld
Copy link

mikedld commented Mar 26, 2023

You seem to be overriding the setter for f with setF method declaration, so f = "test2" leads to setF method invocation which doesn't assign a new value to the field. Doesn't look like an issue with this library.

@PavelKa
Copy link
Author

PavelKa commented Mar 27, 2023

You seem to be overriding the setter for f with setF method declaration, so f = "test2" leads to setF method invocation which doesn't assign a new value to the field. Doesn't look like an issue with this library.

Even if I rename setF to echoF then test fails and echoF under test prints test instead of test2, althought skript works well in jenkins.

@mikedld
Copy link

mikedld commented Mar 27, 2023

Hmm, true. Actually, I was wrong and setF can't be used as a setter because it doesn't even accept a new value as a parameter :)

Doing the assignment directly inside call (outside of node and stage blocks) works, so something weird is going on indeed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants