JenkinsPipelineUnit helper can intercept text param call but not string param call

306 Views Asked by At

How can I get JenkinsPipelineUnit to intercept both text() and string() param calls? I have code that triggers a build with a few params. I want to write a nice unit test to check that it does what it should. However, the string() calls are not intercepted so I cannot test them. I can test the text() call.

Yes, it is possible to write unit tests for Jenkins pipeline code vs testing via jenkins production jobs. This project + shared libraries makes jenkins into a much better tool.

Any ideas on how I could do this? I looked in the JenkinsPipelineUnit project but didn't find an example that fit and couldn't figure out where to look in the runtime objects.

I do see that the project's BasePipelineTest.groovy links string to its stringInterceptor which seems to just eat it the string. Maybe, I can unregister theirs...

Example

def triggeringParams = [:]
....
for (def param in ['text', 'string']) {
    helper.registerAllowedMethod(param, [LinkedHashMap],
            { LinkedHashMap data ->
                triggeringParams << data
            }
    )
}


thisScript = helper.loadScript('''
    package resources
    return this''')

def params = []
params << thisScript.text(name: 'MULTILINE_PARAM', value: '\nline1\nline2')
params << thisScript.string(name: 'STRING_PARAM', value: 'a string')

thisScript.build(job: 'myJob', parameters: params)

println triggeringParams

Results

[
    [name:JOB_PROPERTIES, value:
        line1
        line2]
]
1

There are 1 best solutions below

0
On BEST ANSWER

Wrong type was the problem. The project's BasePipelineTest.groovy links string to its stringInterceptor which seems to just eat it the string and uses register Map not LinkedHashMap. So, the first is found before mine and boom, the string doesn't show in my collector.

If I modify my code to use the more generic map it works

void addParameterHelpers() {
    for (def param in ['text', 'string']) {
        helper.registerAllowedMethod(param, [Map],
                { LinkedHashMap data ->
                    triggeringParams << data
                }
        )
    }
}