jenkinsci / JenkinsPipelineUnit

Framework for unit testing Jenkins pipelines
MIT License
1.55k stars 396 forks source link

How to test steps enclosed in a `withEnv` block #663

Closed axieum closed 4 months ago

axieum commented 4 months ago

Given the following Jenkins script -

withEnv(['MYTOOL_HOME=/usr/local/mytool']) {
  sh label: 'lorem', script: '$MYTOOL_HOME/bin/start'
}

And the following unit test groovy -

MethodCall call = helper.callStack.find { call -> call.methodName = 'sh' }
assertEquals('$MYTOOL_HOME/bin/start', call.args[0]['script'])

Given environment variables are evaluated by the OS, and the environment variables only live within the withEnv { ... } block, how can these be asserted, to make sure the sh script is running with the correct environment variables?

axieum commented 4 months ago

I've found that testing against the entire callStack is probably easier. https://github.com/jenkinsci/JenkinsPipelineUnit?tab=readme-ov-file#compare-the-callstack-with-a-previous-implementation