From 71644e3440acba655c1b94043cd6c1655a4d9d6e Mon Sep 17 00:00:00 2001 From: Steven Terrana Date: Mon, 7 Mar 2022 20:25:53 -0500 Subject: [PATCH 1/2] update TemplatePrimitiveNamespace to use spread operator for call methods --- .../TemplatePrimitiveNamespace.groovy | 2 +- .../TemplatePrimitiveNamespaceSpec.groovy | 104 ++++++++++++++++++ 2 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 src/test/groovy/org/boozallen/plugins/jte/init/primitives/TemplatePrimitiveNamespaceSpec.groovy diff --git a/src/main/groovy/org/boozallen/plugins/jte/init/primitives/TemplatePrimitiveNamespace.groovy b/src/main/groovy/org/boozallen/plugins/jte/init/primitives/TemplatePrimitiveNamespace.groovy index e33d290bc..5e42c44b2 100644 --- a/src/main/groovy/org/boozallen/plugins/jte/init/primitives/TemplatePrimitiveNamespace.groovy +++ b/src/main/groovy/org/boozallen/plugins/jte/init/primitives/TemplatePrimitiveNamespace.groovy @@ -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}") } diff --git a/src/test/groovy/org/boozallen/plugins/jte/init/primitives/TemplatePrimitiveNamespaceSpec.groovy b/src/test/groovy/org/boozallen/plugins/jte/init/primitives/TemplatePrimitiveNamespaceSpec.groovy new file mode 100644 index 000000000..c7b3545d9 --- /dev/null +++ b/src/test/groovy/org/boozallen/plugins/jte/init/primitives/TemplatePrimitiveNamespaceSpec.groovy @@ -0,0 +1,104 @@ +/* + 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 hudson.model.Result +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) + } + +} From c4318b84eb7f850b601fdb1a26a314a7a7060eec Mon Sep 17 00:00:00 2001 From: Steven Terrana Date: Mon, 7 Mar 2022 20:34:19 -0500 Subject: [PATCH 2/2] lint --- .../jte/init/primitives/TemplatePrimitiveNamespaceSpec.groovy | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/groovy/org/boozallen/plugins/jte/init/primitives/TemplatePrimitiveNamespaceSpec.groovy b/src/test/groovy/org/boozallen/plugins/jte/init/primitives/TemplatePrimitiveNamespaceSpec.groovy index c7b3545d9..4846fe423 100644 --- a/src/test/groovy/org/boozallen/plugins/jte/init/primitives/TemplatePrimitiveNamespaceSpec.groovy +++ b/src/test/groovy/org/boozallen/plugins/jte/init/primitives/TemplatePrimitiveNamespaceSpec.groovy @@ -15,7 +15,6 @@ */ package org.boozallen.plugins.jte.init.primitives -import hudson.model.Result import org.boozallen.plugins.jte.init.governance.libs.TestLibraryProvider import org.boozallen.plugins.jte.util.TestUtil import org.jenkinsci.plugins.workflow.job.WorkflowJob