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

     1  package util
     2  
     3  import com.lesfurets.jenkins.unit.BasePipelineTest
     4  
     5  import static org.junit.Assert.assertNotNull
     6  import static org.junit.Assert.assertTrue
     7  import org.junit.rules.TestRule
     8  import org.junit.runner.Description
     9  import org.junit.runners.model.Statement
    10  
    11  class JenkinsWriteFileRule implements TestRule {
    12  
    13      final BasePipelineTest testInstance
    14  
    15      Map files = [:]
    16  
    17      JenkinsWriteFileRule(BasePipelineTest testInstance) {
    18          this.testInstance = testInstance
    19      }
    20  
    21      @Override
    22      Statement apply(Statement base, Description description) {
    23          return statement(base)
    24      }
    25  
    26      private Statement statement(final Statement base) {
    27          return new Statement() {
    28              @Override
    29              void evaluate() throws Throwable {
    30  
    31                  testInstance.helper.registerAllowedMethod( 'writeFile', [Map.class], { m ->
    32                      assertNotNull(m.file)
    33                      assertTrue(m.file instanceof CharSequence)
    34                      assertNotNull(m.text)
    35                      assertTrue(m.text instanceof CharSequence)
    36                      if (m.encoding) {
    37                          assertTrue(m.encoding instanceof CharSequence)
    38                          // Would be nice to actually handle encoding
    39                      }
    40                      files[m.file] = m.text
    41                  })
    42  
    43                  base.evaluate()
    44              }
    45          }
    46      }
    47  }