From 6b7dc48c440b21a9651c946da612951639710734 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Tue, 4 Sep 2018 09:44:34 +0200 Subject: [PATCH] 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. --- test/groovy/CloudFoundryDeployTest.groovy | 4 ++-- test/groovy/MTABuildTest.groovy | 4 ++-- .../versioning/MtaArtifactVersioningTest.groovy | 2 +- test/groovy/util/JenkinsReadYamlRule.groovy | 16 +++++++++------- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/test/groovy/CloudFoundryDeployTest.groovy b/test/groovy/CloudFoundryDeployTest.groovy index 64cdd31fb..afabbd3af 100644 --- a/test/groovy/CloudFoundryDeployTest.groovy +++ b/test/groovy/CloudFoundryDeployTest.groovy @@ -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.') diff --git a/test/groovy/MTABuildTest.groovy b/test/groovy/MTABuildTest.groovy index 46fa9de6f..1aa5f228c 100644 --- a/test/groovy/MTABuildTest.groovy +++ b/test/groovy/MTABuildTest.groovy @@ -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') } diff --git a/test/groovy/com/sap/piper/versioning/MtaArtifactVersioningTest.groovy b/test/groovy/com/sap/piper/versioning/MtaArtifactVersioningTest.groovy index a40a15f0e..f6a98ed3e 100644 --- a/test/groovy/com/sap/piper/versioning/MtaArtifactVersioningTest.groovy +++ b/test/groovy/com/sap/piper/versioning/MtaArtifactVersioningTest.groovy @@ -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 diff --git a/test/groovy/util/JenkinsReadYamlRule.groovy b/test/groovy/util/JenkinsReadYamlRule.groovy index ed99027cb..30e74f460 100644 --- a/test/groovy/util/JenkinsReadYamlRule.groovy +++ b/test/groovy/util/JenkinsReadYamlRule.groovy @@ -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()