Skip to content

Commit

Permalink
Bugfix: mock existing local function.
Browse files Browse the repository at this point in the history
The mock was not applicable in case the function already exists and the specified closure is null.
  • Loading branch information
Nestor Acuna-Blanco committed Jan 7, 2022
1 parent 640a2a9 commit bb1a3e4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class InterceptingGCL extends GroovyClassLoader {
Map.Entry<MethodSignature, Closure> matchingMethod = helper.allowedMethodCallbacks.find { k, v -> k == signature }
if (matchingMethod) {
// a matching method was registered, replace script method execution call with the registered closure (mock)
metaClazz."$scriptMethod.name" = matchingMethod.value
metaClazz."$scriptMethod.name" = matchingMethod.value ?: {}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.lesfurets.jenkins.unit.declarative

import org.junit.Before
import org.junit.Test

class TestMockLocalFunction extends DeclarativePipelineTest {

@Before
@Override
void setUp() throws Exception {
scriptRoots += 'src/test/jenkins/jenkinsfiles'
helper.registerAllowedMethod("githubNotify", [Map])
super.setUp()
}

@Test(expected=MissingMethodException.class)
void should_execute_with_errors() {
runScript("Mock_existing_function_Jenkinsfile")
}

@Test
void should_execute_without_errors() {
helper.registerAllowedMethod("runFunc")
runScript("Mock_existing_function_Jenkinsfile")
}
}
16 changes: 16 additions & 0 deletions src/test/jenkins/jenkinsfiles/Mock_existing_function_Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
pipeline {
agent { node { label 'test1' } }
stages {
stage('sdfsdf') {
steps {
githubNotify status: 'PENDING', context: 'Pipeline'
runFunc()
}
}
}
}

def runFunc(){
echo "inside runFunc"
doSomeStuff()
}

0 comments on commit bb1a3e4

Please sign in to comment.