Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

unit testing - JenkinsPipelineUnit helper can intercept text param call but not string param call

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: '
line1
line2')
params << thisScript.string(name: 'STRING_PARAM', value: 'a string')

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

println triggeringParams

Results

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
                }
        )
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...