Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: mock existing local function. #465

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}