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

     1  package util
     2  
     3  import com.lesfurets.jenkins.unit.BasePipelineTest
     4  
     5  import org.junit.Assert
     6  import org.junit.rules.TestRule
     7  import org.junit.runner.Description
     8  import org.junit.runners.model.Statement
     9  
    10  import static org.hamcrest.Matchers.containsString
    11  import static org.hamcrest.Matchers.not
    12  import static org.junit.Assert.assertThat
    13  
    14  import org.hamcrest.Matchers
    15  
    16  class JenkinsLoggingRule implements TestRule {
    17  
    18      final BasePipelineTest testInstance
    19  
    20      def expected = []
    21      def notExpected = []
    22  
    23      String log = ""
    24  
    25      JenkinsLoggingRule(BasePipelineTest testInstance) {
    26          this.testInstance = testInstance
    27      }
    28  
    29      public JenkinsLoggingRule expect(String substring) {
    30          expected.add(substring)
    31          return this
    32      }
    33  
    34      public JenkinsLoggingRule notExpect(String substring) {
    35          notExpected.add(substring)
    36          return this
    37      }
    38  
    39      @Override
    40      Statement apply(Statement base, Description description) {
    41          return statement(base)
    42      }
    43  
    44      private Statement statement(final Statement base) {
    45          return new Statement() {
    46              @Override
    47              void evaluate() throws Throwable {
    48  
    49                  testInstance.helper.registerAllowedMethod("echo", [String.class], {
    50                      echoInput ->
    51                          log += "$echoInput \n"
    52                  })
    53  
    54                  Throwable caught
    55  
    56                  try {
    57                      base.evaluate()
    58                  } catch(Throwable thr) {
    59                      caught = thr
    60                  } finally {
    61                      if(caught instanceof AssertionError) {
    62                          // Be polite, give other rules the advantage.
    63                          // We expect other rules located closer to the test case
    64                          // to throw an AssertionError in case of a violation.
    65                          throw caught
    66                      }
    67  
    68                      expected.each { substring -> assertThat("Substring '${substring}' not contained in log.",
    69                                                              log,
    70                                                              containsString(substring)) }
    71  
    72                      notExpected.each { substring -> assertThat("Substring '${substring}' is present in log.",
    73                                                              log,
    74                                                              not(containsString(substring))) }
    75  
    76                      if(caught != null) {
    77                          // do not swallow, so that other rules located farer away
    78                          // to the test case can react
    79                          throw caught
    80                      }
    81                  }
    82              }
    83          }
    84      }
    85  }