Skip to content

Commit

Permalink
Merge pull request #546 from jenkinsci/nre/master/immutable-params-de…
Browse files Browse the repository at this point in the history
…clarative

Make params immutable for declarative piplines
  • Loading branch information
nre-ableton authored Jul 18, 2022
2 parents 5fda3ae + 8f9bb3d commit 2bbd00b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ abstract class DeclarativePipelineTest extends BasePipelineTest {
helper.registerAllowedMethod('timeout', [Integer, Closure])
helper.registerAllowedMethod('timestamps')
binding.setVariable('credentials', [:])
binding.setVariable('params', [:])
binding.setVariable('params', [:].asImmutable())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package com.lesfurets.jenkins.unit.declarative

class ParametersDeclaration extends GenericPipelineDeclaration {

void setParams(String key, Object val) {
Map params = this.params
if (params != null && (!params.containsKey(key))) {
params[key] = val
void setParams(String name, Object val) {
Map immutableParams = binding.getVariable('params') as Map
if (immutableParams[name] == null) {
Map mutableParams = [:]
immutableParams.each { k, v -> mutableParams[k] = v }
mutableParams[name] = val
binding.setVariable('params', mutableParams.asImmutable())
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.lesfurets.jenkins

import com.lesfurets.jenkins.unit.declarative.DeclarativePipelineTest
import org.junit.Before
import org.junit.Test

import static org.junit.Assert.assertEquals

class TestDeclarativeImmutableParams extends DeclarativePipelineTest {

@Override
@Before
void setUp() throws Exception {
scriptRoots += 'src/test/jenkins'

super.setUp()
}

@Test(expected = UnsupportedOperationException)
void "test immutable params in declarative pipeline"() {
runScript("job/library/test_params_immutable_declarative.jenkins")
assertEquals('original', binding.params['new'].toString())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ class TestSharedLibraryEnvVariable extends DeclarativePipelineTest {

@Test
void "test params not defined in env"() {
def version = "1.2.0"
addParam("VERSION", version)

runScript("job/library/test_params_not_defined_in_env.jenkins")

def versionFromEnv = binding.env["VERSION"]
Assert.assertEquals(version, versionFromEnv.toString())
def versionFromParams = binding.params["VERSION"]
Assert.assertEquals(versionFromParams.toString(), versionFromEnv.toString())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -721,8 +721,7 @@ class TestDeclarativePipeline extends DeclarativePipelineTest {
}

@Test void agent_with_mock_param_label() throws Exception {
def params = binding.getVariable('params')
params['AGENT'] = 'mockSlave'
addParam('AGENT', 'mockSlave')
runScript('AgentParam_Jenkinsfile')
printCallStack()
assertCallStack().contains('mockSlave')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pipeline {
parameters {
string(
name: 'TEST_STRING',
defaultValue: 'original'
)
}

stages {
stage('Test declarative parameters') {
steps {
script {
params.TEST_STRING = 'new'
}
}
}
}
}

0 comments on commit 2bbd00b

Please sign in to comment.