From e954e3b62988a9f4055e7c82e4dd8375dd06ace2 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Fri, 2 Aug 2019 17:05:49 +0200 Subject: [PATCH] unified behaviour for shell call rule (#794) * Ensure closure gets called when neither returnStdout nor returnStatus are set In this case we do not have a return value, but in case we execute a closure we should execute the closure. With that it is possible to raise an exception from the closure. * [refactoring] unify usage of unify method call * Remove dead code. Coding after uncondition throw exception statement does not get executed. * Ensure script rule behaves the same whan called with string and with map. --- test/groovy/util/JenkinsShellCallRule.groovy | 83 +++++++++----------- 1 file changed, 39 insertions(+), 44 deletions(-) diff --git a/test/groovy/util/JenkinsShellCallRule.groovy b/test/groovy/util/JenkinsShellCallRule.groovy index 6b65baccc..807ef66c8 100644 --- a/test/groovy/util/JenkinsShellCallRule.groovy +++ b/test/groovy/util/JenkinsShellCallRule.groovy @@ -60,6 +60,39 @@ class JenkinsShellCallRule implements TestRule { failingCommands.add(new Command(type, script)) } + def handleShellCall(Map parameters) { + + def unifiedScript = unify(parameters.script) + + shell.add(unifiedScript) + + for (Command failingCommand: failingCommands){ + if(failingCommand.type == Type.REGEX && unifiedScript =~ failingCommand.script) { + throw new Exception("Script execution failed!") + } else if(failingCommand.type == Type.PLAIN && unifiedScript.equals(failingCommand.script)) { + throw new Exception("Script execution failed!") + } + } + + + def result = null + + for(def e : returnValues.entrySet()) { + if(e.key.type == Type.REGEX && unifiedScript =~ e.key.script) { + result = e.value + break + } else if(e.key.type == Type.PLAIN && unifiedScript.equals(e.key.script)) { + result = e.value + break + } + } + if(result instanceof Closure) result = result() + if (!result && parameters.returnStatus) result = 0 + + if(! parameters.returnStdout && ! parameters.returnStatus) return + return result + } + @Override Statement apply(Statement base, Description description) { return statement(base) @@ -71,53 +104,15 @@ class JenkinsShellCallRule implements TestRule { void evaluate() throws Throwable { testInstance.helper.registerAllowedMethod("sh", [String.class], { - command -> - def unifiedScript = unify(command) - - shell.add(unifiedScript) - - for (Command failingCommand: failingCommands){ - if(failingCommand.type == Type.REGEX && unifiedScript =~ failingCommand.script) { - throw new Exception("Script execution failed!") - break - } else if(failingCommand.type == Type.PLAIN && unifiedScript.equals(failingCommand.script)) { - throw new Exception("Script execution failed!") - break - } - } + command -> handleShellCall([ + script: command, + returnStdout: false, + returnStatus: false + ]) }) testInstance.helper.registerAllowedMethod("sh", [Map.class], { - m -> - shell.add(m.script.replaceAll(/\s+/," ").trim()) - - def unifiedScript = unify(m.script) - for (Command failingCommand: failingCommands){ - if(failingCommand.type == Type.REGEX && unifiedScript =~ failingCommand.script) { - throw new Exception("Script execution failed!") - break - } else if(failingCommand.type == Type.PLAIN && unifiedScript.equals(failingCommand.script)) { - throw new Exception("Script execution failed!") - break - } - } - - if (m.returnStdout || m.returnStatus) { - def result = null - - for(def e : returnValues.entrySet()) { - if(e.key.type == Type.REGEX && unifiedScript =~ e.key.script) { - result = e.value - break - } else if(e.key.type == Type.PLAIN && unifiedScript.equals(e.key.script)) { - result = e.value - break - } - } - if(result instanceof Closure) result = result() - if (!result && m.returnStatus) result = 0 - return result - } + m -> handleShellCall(m) }) base.evaluate()