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

Safer lazy initialization of variables #332

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
26 changes: 12 additions & 14 deletions src/main/groovy/com/lesfurets/jenkins/unit/BasePipelineTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/groovy/com/lesfurets/jenkins/TestAddCredential.groovy
Original file line number Diff line number Diff line change
@@ -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')
}
}
13 changes: 13 additions & 0 deletions src/test/groovy/com/lesfurets/jenkins/TestAddEnvVar.groovy
Original file line number Diff line number Diff line change
@@ -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')
}
}
13 changes: 13 additions & 0 deletions src/test/groovy/com/lesfurets/jenkins/TestAddParam.groovy
Original file line number Diff line number Diff line change
@@ -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')
}
}