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

Make bindings accessible in SharedLibrary classes. #519

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
stchar marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}

}
14 changes: 14 additions & 0 deletions src/test/jenkins/job/library/params_not_accessible.jenkins
Original file line number Diff line number Diff line change
@@ -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}"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.test

class LibClass {
def getX() {
return testVar
}
}