github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/util/JenkinsPropertiesRule.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 JenkinsPropertiesRule implements TestRule {
     9  
    10      final BasePipelineTest testInstance
    11  
    12      final String propertyPath
    13  
    14      final Properties configProperties
    15  
    16      JenkinsPropertiesRule(BasePipelineTest testInstance, String propertyPath) {
    17          this.testInstance = testInstance
    18          this.propertyPath = propertyPath
    19          configProperties = loadProperties(propertyPath)
    20      }
    21  
    22      JenkinsPropertiesRule(BasePipelineTest testInstance, String propertyPath, Properties properties) {
    23          this.testInstance = testInstance
    24          this.propertyPath = propertyPath
    25          configProperties = properties
    26      }
    27  
    28  
    29      @Override
    30      Statement apply(Statement base, Description description) {
    31          return statement(base)
    32      }
    33  
    34      private Statement statement(final Statement base) {
    35          return new Statement() {
    36              @Override
    37              void evaluate() throws Throwable {
    38  
    39                  testInstance.helper.registerAllowedMethod("readProperties", [Map.class], {
    40                      readPropertyType ->
    41                          if(readPropertyType.file){
    42                              if (JenkinsPropertiesRule.this.propertyPath.contains(readPropertyType.file)) {
    43                                  return JenkinsPropertiesRule.this.configProperties
    44                              }
    45                              throw new Exception("Could not find the properties with path $readPropertyType")
    46                          }
    47                          else if (readPropertyType.text){
    48                              // Multiline properties are not supported.
    49                              def propertiesMap = [:]
    50                              for (def line : new StringReader(readPropertyType.text)) {
    51                                  if (! line.trim()) continue
    52                                  entry = line.split('=')
    53                                  if(entry.length != 2) {
    54                                      throw new RuntimeException("Invalid properties: ${readPropertyType.text}. Line '${line}' does not contain a valid key value pair ")
    55                                  }
    56                                  propertiesMap.put(entry[0], entry[1])
    57  
    58                              }
    59                              return propertiesMap
    60                          }
    61                          throw new Exception("neither 'text' nor 'file' argument was provided to 'readProperties'")
    62                  })
    63  
    64                  base.evaluate()
    65              }
    66          }
    67      }
    68  
    69      static Properties loadProperties(String path) {
    70          def inputStream = new File(path).newInputStream()
    71          def properties = new Properties()
    72          properties.load(inputStream)
    73          inputStream.close()
    74          return properties
    75      }
    76  
    77  }