1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +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 @Test
void testCfNativeAppNameFromManifest() { void testCfNativeAppNameFromManifest() {
helper.registerAllowedMethod('fileExists', [String.class], { s -> return true }) 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([ jsr.step.cloudFoundryDeploy([
script: nullScript, script: nullScript,
@ -202,7 +202,7 @@ class CloudFoundryDeployTest extends BasePiperTest {
@Test @Test
void testCfNativeWithoutAppName() { void testCfNativeWithoutAppName() {
helper.registerAllowedMethod('fileExists', [String.class], { s -> return true }) helper.registerAllowedMethod('fileExists', [String.class], { s -> return true })
jryr.registerYaml('test.yml', { "applications: [[]]"} ) jryr.registerYaml('test.yml', "applications: [[]]")
thrown.expect(hudson.AbortException) thrown.expect(hudson.AbortException)
thrown.expectMessage('[cloudFoundryDeploy] ERROR: No appName available in manifest test.yml.') 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 JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
private JenkinsShellCallRule jscr = new JenkinsShellCallRule(this) private JenkinsShellCallRule jscr = new JenkinsShellCallRule(this)
private JenkinsStepRule jsr = new JenkinsStepRule(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 @Rule
public RuleChain ruleChain = Rules public RuleChain ruleChain = Rules
@ -112,7 +112,7 @@ public class MtaBuildTest extends BasePiperTest {
thrown.expect(ParserException) thrown.expect(ParserException)
thrown.expectMessage('while parsing a block mapping') thrown.expectMessage('while parsing a block mapping')
jryr.registerYaml('mta.yaml', { badMtaYaml() }) jryr.registerYaml('mta.yaml', badMtaYaml())
jsr.step.call(buildTarget: 'NEO') jsr.step.call(buildTarget: 'NEO')
} }

View File

@ -17,7 +17,7 @@ class MtaArtifactVersioningTest extends BasePiperTest{
@Rule @Rule
public RuleChain ruleChain = Rules public RuleChain ruleChain = Rules
.getCommonRules(this) .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) .around(jscr)
@Test @Test

View File

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