Skip to content

Commit

Permalink
Merge pull request #542 from jenkinsci/nre/master/528-unmodifiable-pa…
Browse files Browse the repository at this point in the history
…rams

Make params map immutable
  • Loading branch information
nre-ableton authored Jul 11, 2022
2 parents 9ecdf79 + a34b220 commit a191fbf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ abstract class BasePipelineTest {

binding.setVariable('env', [:])
binding.setVariable('scm', [:])
binding.setVariable('params', [:].asImmutable())
}

/**
Expand Down Expand Up @@ -506,13 +507,12 @@ abstract class BasePipelineTest {
* Helper for adding a params value in tests
*/
void addParam(String name, Object val, Boolean overWrite = false) {
if (!binding.hasVariable('params')) {
binding.setVariable('params', [:])
}

Map params = binding.getVariable('params') as Map
if (params[name] == null || overWrite) {
params[name] = val
Map immutableParams = binding.getVariable('params') as Map
if (immutableParams[name] == null || overWrite) {
Map mutableParams = [:]
immutableParams.each { k, v -> mutableParams[k] = v }
mutableParams[name] = val
binding.setVariable('params', mutableParams.asImmutable())
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/test/groovy/com/lesfurets/jenkins/TestAddParam.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,21 @@ class TestAddParam extends BasePipelineTest {
super.setUp()
addParam('FOO', 'BAR')
}

@Test(expected = UnsupportedOperationException)
void addParamImmutable() throws Exception {
super.setUp()
addParam('FOO', 'BAR')

// We should not be able to modify existing parameters. This would not work on Jenkins.
binding.getVariable('params')['FOO'] = 'NOT-BAR'
}

@Test(expected = UnsupportedOperationException)
void addNewParamImmutable() throws Exception {
super.setUp()

// It also is not permitted to add new parameters directly. Instead, addParam must be used.
binding.getVariable('params')['BAZ'] = 'QUX'
}
}

0 comments on commit a191fbf

Please sign in to comment.