1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00

Merge remote-tracking branch 'github/master' into HEAD

This commit is contained in:
Marcus Holl 2019-05-16 09:13:51 +02:00
commit 1a907a834d
6 changed files with 122 additions and 65 deletions

View File

@ -6,6 +6,8 @@ import com.sap.piper.GenerateDocumentation
import java.util.regex.Matcher
import groovy.text.StreamingTemplateEngine
import com.sap.piper.MapUtils
//
// Collects helper functions for rendering the documentation
//
@ -375,13 +377,6 @@ class Helper {
return mappings
}
static getValue(Map config, def pPath) {
def p =config[pPath.head()]
if(pPath.size() == 1) return p // there is no tail
if(p in Map) getValue(p, pPath.tail())
else return p
}
static resolveDocuRelevantSteps(GroovyScriptEngine gse, File stepsDir) {
def docuRelevantSteps = []
@ -640,7 +635,7 @@ def handleStep(stepName, prepareDefaultValuesStep, gse, customDefaults) {
it ->
def defaultValue = Helper.getValue(defaultConfig, it.split('/'))
def defaultValue = MapUtils.getByPath(defaultConfig, it)
def parameterProperties = [
defaultValue: defaultValue,
@ -675,7 +670,7 @@ def handleStep(stepName, prepareDefaultValuesStep, gse, customDefaults) {
[
dependentParameterKey: dependentParameterKey,
key: possibleValue,
value: Helper.getValue(defaultConfig.get(possibleValue), k.split('/'))
value: MapUtils.getByPath(defaultConfig.get(possibleValue), k)
]
}
}

View File

@ -127,9 +127,11 @@ class ConfigurationHelper implements Serializable {
handleValidationFailures()
MapUtils.traverse(config, { v -> (v instanceof GString) ? v.toString() : v })
if(config.verbose) step.echo "[${name}] Configuration: ${config}"
return config
return MapUtils.deepCopy(config)
}
/* private */ def getConfigPropertyNested(key) {
return getConfigPropertyNested(config, key)
}

View File

@ -62,4 +62,56 @@ class MapUtils implements Serializable {
}
m.putAll(updates)
}
static private def getByPath(Map m, def key) {
List path = key in CharSequence ? key.tokenize('/') : key
def value = m.get(path.head())
if (path.size() == 1) return value
if (value in Map) return getByPath(value, path.tail())
return null
}
/*
* Provides a new map with the same content like the original map.
* Nested Collections and Maps are copied. Values with are not
* Collections/Maps are not copied/cloned.
* <paranoia>&/ltThe keys are also not copied/cloned, even if they are
* Maps or Collections;paranoia>
*/
static deepCopy(Map original) {
Map copy = [:]
for (def e : original.entrySet()) {
if(e.value == null) {
copy.put(e.key, e.value)
} else {
copy.put(e.key, deepCopy(e.value))
}
}
copy
}
/* private */ static deepCopy(Set original) {
Set copy = []
for(def e : original)
copy << deepCopy(e)
copy
}
/* private */ static deepCopy(List original) {
List copy = []
for(def e : original)
copy << deepCopy(e)
copy
}
/*
* In fact not a copy, but a catch all for everything not matching
* with the other signatures
*/
/* private */ static deepCopy(def original) {
original
}
}

View File

@ -3,6 +3,9 @@ package com.sap.piper
import org.junit.Assert
import org.junit.Test
import static org.hamcrest.Matchers.is
import static org.junit.Assert.assertThat
class MapUtilsTest {
@Test
@ -50,4 +53,44 @@ class MapUtilsTest {
MapUtils.traverse(m, { s -> (s.startsWith('x')) ? "replaced" : s})
assert m == [a: 'replaced', m: [b: 'replaced', c: 'otherString']]
}
@Test
void testGetByPath() {
Map m = [trees: [oak: 5, beech :1], flowers:[rose: 23]]
assertThat(MapUtils.getByPath(m, 'flowers'), is([rose: 23]))
assertThat(MapUtils.getByPath(m, 'trees/oak'), is(5))
assertThat(MapUtils.getByPath(m, 'trees/palm'), is(null))
}
@Test
void testDeepCopy() {
List l = ['a', 'b', 'c']
def original = [
list: l,
set: (Set)['1', '2'],
nextLevel: [
list: ['x', 'y'],
duplicate: l,
set: (Set)[9, 8, 7]
]
]
def copy = MapUtils.deepCopy(original)
assert ! copy.is(original)
assert ! copy.list.is(original.list)
assert ! copy.set.is(original.set)
assert ! copy.nextLevel.list.is(original.nextLevel.list)
assert ! copy.nextLevel.set.is(original.nextLevel.set)
assert ! copy.nextLevel.duplicate.is(original.nextLevel.duplicate)
// Within the original identical list is used twice, but the
// assuption is that there are different lists in the copy.
assert ! copy.nextLevel.duplicate.is(copy.list)
assert copy == original
}
}

View File

@ -451,27 +451,4 @@ steps: {}
assertThat(nullScript.commonPipelineEnvironment.configuration.runStage.Acceptance, is(true))
}
@Test
void testGetConfigValue() {
def config = [
invalidKey: 'invalidValue',
stringKey: 'stringValue',
listKey: [
'listValue1',
'listValue2'
],
nested: [
key: 'nestedValue'
]
]
assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'stringKey'), is('stringValue'))
assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'listKey'), is(['listValue1','listValue2']))
assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'nested/key'), is('nestedValue'))
assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'invalidKey/key'), is(nullValue()))
//assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'nested/key'), is('nestedValue'))
}
}

View File

@ -3,6 +3,7 @@ import com.sap.piper.ConfigurationLoader
import static com.sap.piper.Prerequisites.checkScript
import com.sap.piper.ConfigurationHelper
import com.sap.piper.MapUtils
import groovy.transform.Field
@Field String STEP_NAME = getClass().getName()
@ -65,27 +66,27 @@ void call(Map parameters = [:]) {
case 'config':
if (condition.getValue() instanceof Map) {
condition.getValue().each {configCondition ->
if (getConfigValue(stepConfig, configCondition.getKey()) in configCondition.getValue()) {
if (MapUtils.getByPath(stepConfig, configCondition.getKey()) in configCondition.getValue()) {
stepActive = true
}
}
} else if (getConfigValue(stepConfig, condition.getValue())) {
} else if (MapUtils.getByPath(stepConfig, condition.getValue())) {
stepActive = true
}
break
case 'configKeys':
if (condition.getValue() instanceof List) {
condition.getValue().each {configKey ->
if (getConfigValue(stepConfig, configKey)) {
if (MapUtils.getByPath(stepConfig, configKey)) {
stepActive = true
}
}
} else if (getConfigValue(stepConfig, condition.getValue())) {
} else if (MapUtils.getByPath(stepConfig, condition.getValue())) {
stepActive = true
}
break
case 'filePatternFromConfig':
def conditionValue = getConfigValue(stepConfig, condition.getValue())
def conditionValue = MapUtils.getByPath(stepConfig, condition.getValue())
if (conditionValue && findFiles(glob: conditionValue)) {
stepActive = true
}
@ -110,16 +111,3 @@ void call(Map parameters = [:]) {
echo "[${STEP_NAME}] Debug - Run Step Configuration: ${script.commonPipelineEnvironment.configuration.runStep}"
}
}
private def getConfigValue(Map stepConfig, def configKey) {
if (stepConfig == null) return null
List configPath = configKey instanceof String ? configKey.tokenize('/') : configKey
def configValue = stepConfig[configPath.head()]
if (configPath.size() == 1) return configValue
if (configValue in Map) return getConfigValue(configValue, configPath.tail())
return null
}