github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/util/JenkinsLockRule.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  import static org.hamcrest.Matchers.containsString
     9  import static org.junit.Assert.assertThat
    10  
    11  class JenkinsLockRule implements TestRule {
    12  
    13      final BasePipelineTest testInstance
    14      final List lockResources = []
    15  
    16      JenkinsLockRule(BasePipelineTest testInstance) {
    17          this.testInstance = testInstance
    18      }
    19  
    20      @Override
    21      Statement apply(Statement base, Description description) {
    22          return statement(base)
    23      }
    24  
    25      private Statement statement(final Statement base) {
    26          return new Statement() {
    27              @Override
    28              void evaluate() throws Throwable {
    29  
    30                  testInstance.helper.registerAllowedMethod("lock", [String.class, Closure.class], {
    31                      resource, body ->
    32                          lockResources.add(resource)
    33                          body()
    34                  })
    35  
    36                  base.evaluate()
    37              }
    38          }
    39      }
    40  }