diff --git a/src/main/groovy/com/lesfurets/jenkins/unit/InterceptingGCL.groovy b/src/main/groovy/com/lesfurets/jenkins/unit/InterceptingGCL.groovy index 3e04af06..a80d4a50 100644 --- a/src/main/groovy/com/lesfurets/jenkins/unit/InterceptingGCL.groovy +++ b/src/main/groovy/com/lesfurets/jenkins/unit/InterceptingGCL.groovy @@ -12,6 +12,9 @@ class InterceptingGCL extends GroovyClassLoader { metaClazz.static.invokeMethod = helper.getMethodInterceptor() metaClazz.methodMissing = helper.getMethodMissingInterceptor() metaClazz.getEnv = {return binding.env} + binding.variables.forEach { String property, Object value -> + metaClazz."$property" = metaClazz."$property" ?: value + } // find and replace script method closure with any matching allowed method closure metaClazz.methods.forEach { scriptMethod -> def signature = method(scriptMethod.name, scriptMethod.nativeParameterTypes) diff --git a/src/test/groovy/com/lesfurets/jenkins/TestSharedLibraryAccessibleParams.groovy b/src/test/groovy/com/lesfurets/jenkins/TestSharedLibraryAccessibleParams.groovy new file mode 100644 index 00000000..2de29ae8 --- /dev/null +++ b/src/test/groovy/com/lesfurets/jenkins/TestSharedLibraryAccessibleParams.groovy @@ -0,0 +1,55 @@ +package com.lesfurets.jenkins + +import com.lesfurets.jenkins.unit.declarative.DeclarativePipelineTest +import org.junit.Before +import org.junit.Test + +import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library +import static com.lesfurets.jenkins.unit.global.lib.ProjectSource.projectSource +import static org.assertj.core.api.Assertions.assertThat + +class TestSharedLibraryAccessibleParams extends DeclarativePipelineTest { + + private final String JOB_NAME = "params_not_accessible" + private final String JOB_PATH = "job/library/${JOB_NAME}.jenkins" + private final String LIB_DIR = this.class.getResource("/libs/$JOB_NAME").getFile() + private final String BINDING_VAR = "testVar" + private final String BINDING_VAL = "notBroken" + + @Override + @Before + void setUp() throws Exception { + scriptRoots += 'src/test/jenkins' + super.setUp() + def library = library().name(JOB_NAME) + .retriever(projectSource(LIB_DIR)) + .defaultVersion("master") + .targetPath(LIB_DIR) + .allowOverride(true) + .implicit(false) + .build() + helper.registerSharedLibrary(library) + } + + @Test + void accessible_params_test() { + run_test_with_bindings {assertJobStatusSuccess()} + } + + @Test + void change_binding_test() { + run_test_with_bindings {assertThat(binding.getVariable(BINDING_VAR)).isNotEqualTo(BINDING_VAL)} + } + + @Test(expected = MissingPropertyException.class) + void not_accessible_params_test() { + runScript(JOB_PATH) + } + + private void run_test_with_bindings(Closure assertion) { + binding.setVariable(BINDING_VAR, BINDING_VAL) + runScript(JOB_PATH) + assertion() + } + +} diff --git a/src/test/jenkins/job/library/params_not_accessible.jenkins b/src/test/jenkins/job/library/params_not_accessible.jenkins new file mode 100644 index 00000000..c411260b --- /dev/null +++ b/src/test/jenkins/job/library/params_not_accessible.jenkins @@ -0,0 +1,14 @@ +@Library('params_not_accessible') +import org.test.LibClass + +pipeline { + stages { + stage('Test stage'){ + steps { + out = new LibClass().getX() + testVar = 'changedValue' + echo "Printing ${out}" + } + } + } +} diff --git a/src/test/resources/libs/params_not_accessible/src/org/test/LibClass.groovy b/src/test/resources/libs/params_not_accessible/src/org/test/LibClass.groovy new file mode 100644 index 00000000..3c3b801b --- /dev/null +++ b/src/test/resources/libs/params_not_accessible/src/org/test/LibClass.groovy @@ -0,0 +1,7 @@ +package org.test + +class LibClass { + def getX() { + return testVar + } +}