github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/util/JenkinsWithEnvRule.groovy (about)

     1  package util
     2  
     3  import com.lesfurets.jenkins.unit.BasePipelineTest
     4  import org.junit.rules.TestRule
     5  import org.junit.runner.Description
     6  import org.junit.runners.model.Statement
     7  
     8  class JenkinsWithEnvRule implements TestRule {
     9      final BasePipelineTest testInstance
    10  
    11      JenkinsWithEnvRule(BasePipelineTest testInstance) {
    12          this.testInstance = testInstance
    13      }
    14  
    15      @Override
    16      Statement apply(Statement base, Description description) {
    17          return statement(base)
    18      }
    19  
    20      private Statement statement(final Statement base) {
    21          return new Statement() {
    22              @Override
    23              void evaluate() throws Throwable {
    24                  testInstance.helper.registerAllowedMethod('withEnv', [List, Closure], {envs, body -> body()})
    25                  base.evaluate()
    26              }
    27          }
    28      }
    29  }