Skip to content

Commit

Permalink
Allow access to overloaded TemplatePrimitives from the JTE namespaces (
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-terrana authored Apr 27, 2021
1 parent 2dc14a4 commit ce8cbb0
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ abstract class TemplatePrimitive extends GlobalVariable implements Serializable{
String name
TemplatePrimitiveNamespace parent

@Override
@SuppressWarnings("UnusedMethodParameter")
@NonCPS
Object getValue(@Nonnull CpsScript script) throws Exception {
isOverloaded()
Object getValue(@Nonnull CpsScript script, Boolean skipOverloaded = false) throws Exception {
if(! skipOverloaded){
isOverloaded()
}
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ class TemplatePrimitiveNamespace implements Serializable {
Object getProperty(String property){
TemplatePrimitive primitive = primitives.find{ p -> p.getName() == property }
if(primitive){
return primitive.getValue(null)
return primitive.getValue(null, true)
}
throw new JTEException("Primitive ${property} not found in ${name}")
}

Object methodMissing(String methodName, Object args){
TemplatePrimitive primitive = primitives.find{ p -> p.getName() == methodName }
if(primitive){
return primitive.getValue(null).call(args)
return primitive.getValue(null, true).call(args)
}
throw new JTEException("Primitive ${methodName} not found in ${name}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ class Keyword extends TemplatePrimitive{

@Override String getName(){ return name }
@Override String toString(){ return "Keyword '${name}'" }
@Override Object getValue(CpsScript script){
isOverloaded()
@SuppressWarnings("UnusedMethodParameter")
Object getValue(CpsScript script, Boolean skipOverloaded = false){
if(! skipOverloaded){
isOverloaded()
}
return value
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ class Stage extends TemplatePrimitive{
return "Stage '${name}'"
}

@Override Object getValue(CpsScript script){
isOverloaded()
@SuppressWarnings("UnusedMethodParameter")
Object getValue(CpsScript script, Boolean skipOverloaded = false){
if(! skipOverloaded){
isOverloaded()
}
return getCPSClass().newInstance(name: name, steps: steps)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,12 @@ class StepWrapper extends TemplatePrimitive implements Serializable, Cloneable{
@Override String getName(){ return name }
String getLibrary(){ return library }

@Override
Object getValue(CpsScript script){
isOverloaded()
@SuppressWarnings("UnusedMethodParameter")
Object getValue(CpsScript script, Boolean skipOverloaded = false){
// if permissive_initialization is true, overloaded is okay
if(! skipOverloaded){
isOverloaded()
}
Class stepwrapperCPS = getPrimitiveClass()
def s = stepwrapperCPS.newInstance(
name: this.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class StageCPS extends Stage{
}
StepWrapper clone = s.first().clone()
clone.setStageContext(stageContext)
clone.getValue().call()
clone.getValue(null).call()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class TemplateBindingSpec extends Specification {
jenkins.assertBuildStatus(Result.FAILURE, run)
jenkins.assertLogContains("Failed to set variable 'someStep'", run)
}

def "Access an overloaded step results in exception"() {
given:
StepWrapper step = Spy()
Expand All @@ -137,6 +138,39 @@ class TemplateBindingSpec extends Specification {
1 * step.isOverloaded()
}

def "Invoking overloaded step via namespace works when permissive_initialization is true"(){
given:
TestLibraryProvider libProvider = new TestLibraryProvider()
libProvider.addStep('maven', 'build', 'void call(){ println "build from maven" }')
libProvider.addStep('gradle', 'build', 'void call(){ println "build from gradle" }')
libProvider.addGlobally()

def run
WorkflowJob job = TestUtil.createAdHoc(jenkins,
config: '''
libraries{
maven
gradle
}
jte{
permissive_initialization = true
}
''',
template: '''
jte.libraries.maven.build()
jte.libraries.gradle.build()
'''
)

when:
run = job.scheduleBuild2(0).get()

then:
jenkins.assertBuildStatus(Result.SUCCESS, run)
jenkins.assertLogContains("build from maven", run)
jenkins.assertLogContains("build from gradle", run)
}

/****************************
* Stage overriding
****************************/
Expand Down

0 comments on commit ce8cbb0

Please sign in to comment.