Skip to content

Commit

Permalink
Apply magic fix proposed in the PR
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieulaude committed Jun 10, 2020
1 parent 0927715 commit ca6c4ff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
17 changes: 6 additions & 11 deletions src/main/groovy/com/lesfurets/jenkins/unit/BasePipelineTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.lesfurets.jenkins.unit

import static java.util.stream.Collectors.joining
import static org.assertj.core.api.Assertions.assertThat
import static groovy.lang.Closure.DELEGATE_ONLY

import org.assertj.core.api.AbstractCharSequenceAssert

Expand Down Expand Up @@ -289,17 +290,11 @@ abstract class BasePipelineTest {
* @return the return value of the script
*/
Object runScript(Closure scriptRun) {
final Script script = new Script(binding) {
@Override
Object run() {
scriptRun.call(this)
return null
}
}
script.metaClass.invokeMethod = helper.getMethodInterceptor()
script.metaClass.static.invokeMethod = helper.getMethodInterceptor()
script.metaClass.methodMissing = helper.getMethodMissingInterceptor()
return runScript(script)
File file = File.createTempFile("temp",".groovy")
def script = loadScript(file.absolutePath)
def rehydrate = scriptRun.rehydrate(script, script, script)
rehydrate.resolveStrategy = DELEGATE_ONLY
rehydrate.call()
}

void printCallStack() {
Expand Down
9 changes: 4 additions & 5 deletions src/test/groovy/com/lesfurets/jenkins/TestRunClosure.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ class TestRunClosure extends BasePipelineTest {
@Test
void should_execute_without_errors() throws Exception {
runScript({ script ->
script.echo 'Test'
echo 'Test'
})
printCallStack()
}

@Test
void should_print_property_value() {
runScript({ script ->
script.println 'value'
println 'value'
})

def value = 'value'
Expand All @@ -43,7 +43,7 @@ class TestRunClosure extends BasePipelineTest {
void should_use_registered_method() {
helper.registerAllowedMethod("customMethod", [Map.class], null)
runScript({ script ->
script.customMethod test: 'value'
customMethod test: 'value'
})
}

Expand All @@ -58,8 +58,7 @@ class TestRunClosure extends BasePipelineTest {
.build()
helper.registerSharedLibrary(library)
runScript({ script ->

script.sayHello()
sayHello()
})
}
}

0 comments on commit ca6c4ff

Please sign in to comment.