diff --git a/src/main/groovy/com/lesfurets/jenkins/unit/BasePipelineTest.groovy b/src/main/groovy/com/lesfurets/jenkins/unit/BasePipelineTest.groovy index 18f8d334..efcfd9c4 100644 --- a/src/main/groovy/com/lesfurets/jenkins/unit/BasePipelineTest.groovy +++ b/src/main/groovy/com/lesfurets/jenkins/unit/BasePipelineTest.groovy @@ -429,40 +429,38 @@ abstract class BasePipelineTest { assertCallStack().contains(text) } - /** * Helper for adding a params value in tests */ void addParam(String name, Object val, Boolean overWrite = false) { - Map params = binding.getVariable('params') as Map - if (params == null) { - params = [:] - binding.setVariable('params', params) + if (!binding.hasVariable('params')) { + binding.setVariable('params', [:]) } + + Map params = binding.getVariable('params') as Map if (params[name] == null || overWrite) { params[name] = val } } - /** * Helper for adding a environment value in tests */ void addEnvVar(String name, String val) { - Map env = binding.getVariable('env') as Map - if (env == null) { - env = [:] - binding.setVariable('env', env) + if (!binding.hasVariable('env')) { + binding.setVariable('env', [:]) } + + Map env = binding.getVariable('env') as Map env[name] = val } void addCredential(String key, String val) { - Map credentials = binding.getVariable('credentials') as Map - if (credentials == null) { - credentials = [:] - binding.setVariable('credentials', credentials) + if (!binding.hasVariable('credentials')) { + binding.setVariable('credentials', [:]) } + + Map credentials = binding.getVariable('credentials') as Map credentials[key] = val } } diff --git a/src/main/groovy/com/lesfurets/jenkins/unit/declarative/DeclarativePipelineTest.groovy b/src/main/groovy/com/lesfurets/jenkins/unit/declarative/DeclarativePipelineTest.groovy index 6d76c993..73523805 100644 --- a/src/main/groovy/com/lesfurets/jenkins/unit/declarative/DeclarativePipelineTest.groovy +++ b/src/main/groovy/com/lesfurets/jenkins/unit/declarative/DeclarativePipelineTest.groovy @@ -18,11 +18,11 @@ abstract class DeclarativePipelineTest extends BasePipelineTest { def stringInterceptor = { Map desc-> if (desc) { - // we are in context of paremetes { string(...)} + // we are in context of parameters { string(...)} if (desc.name) { addParam(desc.name, desc.defaultValue, false) } - // we are in context of withCredentaials([string()..]) { } + // we are in context of withCredentials([string()..]) { } if(desc.variable) { return desc.variable } diff --git a/src/test/groovy/com/lesfurets/jenkins/TestAddCredential.groovy b/src/test/groovy/com/lesfurets/jenkins/TestAddCredential.groovy new file mode 100644 index 00000000..54443d1a --- /dev/null +++ b/src/test/groovy/com/lesfurets/jenkins/TestAddCredential.groovy @@ -0,0 +1,13 @@ +package com.lesfurets.jenkins + +import com.lesfurets.jenkins.unit.BasePipelineTest +import org.junit.Test + +class TestAddCredential extends BasePipelineTest { + + @Test + void readUndefinedParamNoException() throws Exception { + super.setUp() + addCredential('FOO', 'BAR') + } +} diff --git a/src/test/groovy/com/lesfurets/jenkins/TestAddEnvVar.groovy b/src/test/groovy/com/lesfurets/jenkins/TestAddEnvVar.groovy new file mode 100644 index 00000000..d6e13bb1 --- /dev/null +++ b/src/test/groovy/com/lesfurets/jenkins/TestAddEnvVar.groovy @@ -0,0 +1,13 @@ +package com.lesfurets.jenkins + +import com.lesfurets.jenkins.unit.BasePipelineTest +import org.junit.Test + +class TestAddEnvVar extends BasePipelineTest { + + @Test + void readUndefinedParamNoException() throws Exception { + super.setUp() + addEnvVar('FOO', 'BAR') + } +} diff --git a/src/test/groovy/com/lesfurets/jenkins/TestAddParam.groovy b/src/test/groovy/com/lesfurets/jenkins/TestAddParam.groovy new file mode 100644 index 00000000..349d7b18 --- /dev/null +++ b/src/test/groovy/com/lesfurets/jenkins/TestAddParam.groovy @@ -0,0 +1,13 @@ +package com.lesfurets.jenkins + +import com.lesfurets.jenkins.unit.BasePipelineTest +import org.junit.Test + +class TestAddParam extends BasePipelineTest { + + @Test + void readUndefinedParamNoException() throws Exception { + super.setUp() + addParam('FOO', 'BAR') + } +}