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

Ensure arguments are passed correctly to steps when invoked via primitive namespace #261

Merged
merged 2 commits into from
Mar 8, 2022
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 @@ -46,7 +46,7 @@ class TemplatePrimitiveNamespace implements Serializable {
TemplatePrimitive primitive = primitives.find{ p -> p.getName() == methodName }
if(primitive){
Object value = primitive.getValue(null, true)
return args.size() > 0 ? value.call(args) : value.call()
return args.size() > 0 ? value.call(*args) : value.call()
}
throw new JTEException("Primitive ${methodName} not found in ${name}")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2018 Booz Allen Hamilton

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.boozallen.plugins.jte.init.primitives

import org.boozallen.plugins.jte.init.governance.libs.TestLibraryProvider
import org.boozallen.plugins.jte.util.TestUtil
import org.jenkinsci.plugins.workflow.job.WorkflowJob
import org.junit.ClassRule
import org.jvnet.hudson.test.JenkinsRule
import spock.lang.Issue
import spock.lang.Shared
import spock.lang.Specification

class TemplatePrimitiveNamespaceSpec extends Specification {

@Shared @ClassRule JenkinsRule jenkins = new JenkinsRule()

@Issue("https://github.com/jenkinsci/templating-engine-plugin/issues/255")
def "Namespaced Step call passes named arguments as map"(){
given:
TestLibraryProvider libProvider = new TestLibraryProvider()
libProvider.addStep('lib1', 'someStep', '''
void call(def opts = [:]){
println opts.toString()
assert opts.foo == "bar"
}
''')
libProvider.addGlobally()

def run
WorkflowJob job = TestUtil.createAdHoc(jenkins,
config: 'libraries{ lib1 }',
template: 'jte.libraries.lib1.someStep(foo: "bar")'
)

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

then:
jenkins.assertBuildStatusSuccess(run)
}

def "Namespaced Step call passes single individual parameters correctly"(){
given:
TestLibraryProvider libProvider = new TestLibraryProvider()
libProvider.addStep('lib2', 'someStep', '''
void call(String foo){
assert foo == "bar"
}
''')
libProvider.addGlobally()

def run
WorkflowJob job = TestUtil.createAdHoc(jenkins,
config: 'libraries{ lib2 }',
template: 'jte.libraries.lib2.someStep("bar")'
)

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

then:
jenkins.assertBuildStatusSuccess(run)
}

def "Namespaced Step call passes multiple individual parameters correctly"(){
given:
TestLibraryProvider libProvider = new TestLibraryProvider()
libProvider.addStep('lib3', 'someStep', '''
void call(String foo, def x){
assert foo == "bar"
assert x == 11
}
''')
libProvider.addGlobally()

def run
WorkflowJob job = TestUtil.createAdHoc(jenkins,
config: 'libraries{ lib3 }',
template: 'jte.libraries.lib3.someStep("bar", 11)'
)

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

then:
jenkins.assertBuildStatusSuccess(run)
}

}