JenkinsPipelineUnit
JenkinsPipelineUnit copied to clipboard
thisObject inside pipeline script referes to Declaration class
In actual jenkins the this object in the pipeline can be used to pass the pipeline script.
I workaround this by adding a line: Script pipelineScript = this
Above the pipeline closure
Would this also "help" for actual call stack output for target
in MethodCall
?
E.g. based upon: https://github.com/kamilszymanski/jenkins-pipeline-automation-starter
- For pipeline:
timestamps {
node {
stage('Generate data') {
progressNotices.displayProgressNotice('Generating data')
try {
generateData('data')
} catch (exception) {
cleanWs()
throw exception
}
}
}
}
- And (new shared library) tests case:
@Test
void 'sharedlib-minimum-for-ticket'() throws Exception {
String jenkinsfileFileName = 'SharedLibraryBasedSampleScriptedJenkinsfile'
helper.registerAllowedMethod('ansiColor', [String, Closure], null)
helper.registerAllowedMethod('timestamps', [Closure], null)
// According to https://github.com/jenkinsci/JenkinsPipelineUnit/issues/318:
def progressNoticesScript = loadScript('vars/progressNotices.groovy')
// Like before:
helper.registerAllowedMethod('generateData', [String], null)
// Instead of:
//runScript(jenkinsfilePath(jenkinsfileFileName))
// ... inject the 'progressNotices' var and execute it:
def jenkinsfileScript = loadScript(jenkinsfilePath(jenkinsfileFileName))
jenkinsfileScript.progressNotices = progressNoticesScript
jenkinsfileScript.run()
printCallStack()
// Must not contain: SharedLibraryBasedSampleScriptedJenkinsfile.cleanWs()
assertCallStack().doesNotContain('cleanWs')
}
- The resulting call stack is:
SharedLibraryBasedSampleScriptedJenkinsfile.run()
SharedLibraryBasedSampleScriptedJenkinsfile.timestamps(groovy.lang.Closure)
SharedLibraryBasedSampleScriptedJenkinsfile.node(groovy.lang.Closure)
SharedLibraryBasedSampleScriptedJenkinsfile.stage(Generate data, groovy.lang.Closure)
progressNotices.displayProgressNotice(Generating data)
progressNotices.ansiColor(xterm, groovy.lang.Closure)
progressNotices.sh(echo -e '\e[32mGenerating data\e[0m')
SharedLibraryBasedSampleScriptedJenkinsfile.generateData(data)
Which (I think) I would rather expect to be:
SharedLibraryBasedSampleScriptedJenkinsfile.run()
SharedLibraryBasedSampleScriptedJenkinsfile.timestamps(groovy.lang.Closure)
SharedLibraryBasedSampleScriptedJenkinsfile.node(groovy.lang.Closure)
SharedLibraryBasedSampleScriptedJenkinsfile.stage(Generate data, groovy.lang.Closure)
progressNotices.displayProgressNotice(Generating data)
SharedLibraryBasedSampleScriptedJenkinsfile.ansiColor(xterm, groovy.lang.Closure)
SharedLibraryBasedSampleScriptedJenkinsfile.sh(echo -e '\e[32mGenerating data\e[0m')
SharedLibraryBasedSampleScriptedJenkinsfile.generateData(data)