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

Be more flexible with JenkinsReadYaml rule

When we register a closure as file, the closure will be exectutd.
Otherwise we return what is registered.

This gives us a maximum level of flexibility. We can throw exceptions (e.g. FileNotFound) as
as test setup requires this, in simple cases we provide the yaml as a string.
This commit is contained in:
Marcus Holl 2018-09-04 09:44:34 +02:00
parent fb425a4c70
commit 6b7dc48c44
4 changed files with 14 additions and 12 deletions

View File

@ -183,7 +183,7 @@ class CloudFoundryDeployTest extends BasePiperTest {
@Test
void testCfNativeAppNameFromManifest() {
helper.registerAllowedMethod('fileExists', [String.class], { s -> return true })
jryr.registerYaml('test.yml', {"[applications: [[name: 'manifestAppName']]]"})
jryr.registerYaml('test.yml', "[applications: [[name: 'manifestAppName']]]")
jsr.step.cloudFoundryDeploy([
script: nullScript,
@ -202,7 +202,7 @@ class CloudFoundryDeployTest extends BasePiperTest {
@Test
void testCfNativeWithoutAppName() {
helper.registerAllowedMethod('fileExists', [String.class], { s -> return true })
jryr.registerYaml('test.yml', { "applications: [[]]"} )
jryr.registerYaml('test.yml', "applications: [[]]")
thrown.expect(hudson.AbortException)
thrown.expectMessage('[cloudFoundryDeploy] ERROR: No appName available in manifest test.yml.')

View File

@ -29,7 +29,7 @@ public class MtaBuildTest extends BasePiperTest {
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
private JenkinsShellCallRule jscr = new JenkinsShellCallRule(this)
private JenkinsStepRule jsr = new JenkinsStepRule(this)
private JenkinsReadYamlRule jryr = new JenkinsReadYamlRule(this).registerYaml('mta.yaml', { defaultMtaYaml() })
private JenkinsReadYamlRule jryr = new JenkinsReadYamlRule(this).registerYaml('mta.yaml', defaultMtaYaml() )
@Rule
public RuleChain ruleChain = Rules
@ -112,7 +112,7 @@ public class MtaBuildTest extends BasePiperTest {
thrown.expect(ParserException)
thrown.expectMessage('while parsing a block mapping')
jryr.registerYaml('mta.yaml', { badMtaYaml() })
jryr.registerYaml('mta.yaml', badMtaYaml())
jsr.step.call(buildTarget: 'NEO')
}

View File

@ -17,7 +17,7 @@ class MtaArtifactVersioningTest extends BasePiperTest{
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(new JenkinsReadYamlRule(this).registerYaml('mta.yaml', {"version: '1.2.3'"}))
.around(new JenkinsReadYamlRule(this).registerYaml('mta.yaml', "version: '1.2.3'"))
.around(jscr)
@Test

View File

@ -18,8 +18,8 @@ class JenkinsReadYamlRule implements TestRule {
this.testInstance = testInstance
}
JenkinsReadYamlRule registerYaml(fileName, closure) {
ymls.put(fileName, closure)
JenkinsReadYamlRule registerYaml(fileName, yaml) {
ymls.put(fileName, yaml)
return this
}
@Override
@ -32,15 +32,17 @@ class JenkinsReadYamlRule implements TestRule {
@Override
void evaluate() throws Throwable {
testInstance.helper.registerAllowedMethod("readYaml", [Map], { Map m ->
def yml
if(m.text) {
return new Yaml().load(m.text)
yml = m.text
} else if(m.file) {
def closure = ymls.get(m.file)
if(!closure) throw new NullPointerException("yaml file '${m.file}' not registered.")
return new Yaml().load(closure())
yml = ymls.get(m.file)
if(yml == null) throw new NullPointerException("yaml file '${m.file}' not registered.")
if(yml instanceof Closure) yml = yml()
} else {
throw new IllegalArgumentException("Key 'text' is missing in map ${m}.")
throw new IllegalArgumentException("Key 'text' and 'file' are both missing in map ${m}.")
}
return new Yaml().load(yml)
})
base.evaluate()