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

Can't access 'env' properties in nested bindings #566

Open
YoniSegal opened this issue Nov 24, 2022 · 0 comments
Open

Can't access 'env' properties in nested bindings #566

YoniSegal opened this issue Nov 24, 2022 · 0 comments

Comments

@YoniSegal
Copy link

YoniSegal commented Nov 24, 2022

If I have a jenkins shared library which looks like this for example:

└── project
    ├── src
    │   ├── helper.groovy
    │   └── mainFunc.groovy
    ├── test
    │   └── mainFuncTest.groovy
    └── vars
        └── myStep.groovy

src/helper.groovy

def call() {
    println(env.BUILD_URL)
}

src/mainFunc.groovy:

def call() {
    println(env.BUILD_URL)
    def helper = new helper()
    helper()
}

vars/myStep.groovy

def call() {
    def mainFunc = new mainFunc()
    return mainFunc
}

test/mainFuncTest.groovy:

import com.lesfurets.jenkins.unit.BasePipelineTest
import org.junit.Before
import org.junit.Test

class mainFuncTest extends BasePipelineTest {

    @Override
    @Before
    void setUp() throws Exception {
        baseScriptRoot = 'project'
        scriptRoots += 'src'
        scriptExtension = 'groovy'
        super.setUp()
    }

    @Test
    void test1() {
        Script mainFunc = loadScript("mainFunc.groovy")
        addEnvVar("BUILD_URL", "http://something")
        mainFunc()
    }
}

When running test1, I get the expected print: http://something, however, when mainFunc() calls helper(), I get an error message: No such property: env for class: helper

I understand that when running addEnvVar("BUILD_URL", "http://something") in test1, it adds env.BUILD_URL as a property to mainFunc's binding.

Is there anyway to make the env property truly global? Such that it would be accessible in helper's binding?

If it's not possible, can someone please explain how to mock helper.call?

Thank you

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

No branches or pull requests

1 participant