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

     1  import org.junit.After
     2  import org.junit.Before
     3  import org.junit.Ignore
     4  import org.junit.Rule
     5  import org.junit.Test
     6  import org.junit.rules.RuleChain
     7  import org.junit.rules.ExpectedException
     8  
     9  import util.BasePiperTest
    10  import util.JenkinsReadYamlRule
    11  import util.JenkinsStepRule
    12  import static org.junit.Assert.assertEquals
    13  import static org.junit.Assert.assertTrue
    14  
    15  import com.sap.piper.Utils
    16  
    17  import util.Rules
    18  import minimatch.Minimatch
    19  
    20  class TestsPublishResultsTest extends BasePiperTest {
    21      Map publisherStepOptions
    22      List archiveStepPatterns
    23  
    24      private ExpectedException thrown = ExpectedException.none()
    25      private JenkinsStepRule stepRule = new JenkinsStepRule(this)
    26  
    27      @Rule
    28      public RuleChain ruleChain = Rules
    29          .getCommonRules(this)
    30          .around(new JenkinsReadYamlRule(this))
    31          .around(thrown)
    32          .around(stepRule)
    33  
    34      @Before
    35      void init() {
    36          publisherStepOptions = [:]
    37          archiveStepPatterns = []
    38          // prepare checkResultsPublish step
    39          helper.registerAllowedMethod('junit', [Map.class], {
    40              parameters -> publisherStepOptions['junit'] = parameters
    41          })
    42          helper.registerAllowedMethod('jacoco', [Map.class], {
    43              parameters -> publisherStepOptions['jacoco'] = parameters
    44          })
    45          helper.registerAllowedMethod('cobertura', [Map.class], {
    46              parameters -> publisherStepOptions['cobertura'] = parameters
    47          })
    48          helper.registerAllowedMethod('perfReport', [Map.class], {
    49              parameters -> publisherStepOptions['jmeter'] = parameters
    50          })
    51          helper.registerAllowedMethod('cucumber', [Map.class], {
    52              parameters -> publisherStepOptions['cucumber'] = parameters
    53          })
    54          helper.registerAllowedMethod('publishHTML', [Map.class], {
    55              parameters -> publisherStepOptions['htmlPublisher'] = parameters
    56          })
    57          helper.registerAllowedMethod('archiveArtifacts', [Map.class], {
    58              parameters -> archiveStepPatterns.push(parameters.artifacts)
    59          })
    60  
    61          Utils.metaClass.echo = { def m -> }
    62      }
    63  
    64      @After
    65      public void tearDown() {
    66          Utils.metaClass = null
    67      }
    68  
    69      @Test
    70      void testPublishNothingWithDefaultSettings() throws Exception {
    71          stepRule.step.testsPublishResults(script: nullScript)
    72  
    73          // ensure nothing is published
    74          assertTrue('WarningsPublisher options not empty', publisherStepOptions.junit == null)
    75          assertTrue('PmdPublisher options not empty', publisherStepOptions.jacoco == null)
    76          assertTrue('DryPublisher options not empty', publisherStepOptions.cobertura == null)
    77          assertTrue('FindBugsPublisher options not empty', publisherStepOptions.jmeter == null)
    78          assertTrue('Cucumber options not empty', publisherStepOptions.cucumber == null)
    79          assertTrue('HtmlPublisher options not empty', publisherStepOptions.htmlPublisher == null)
    80      }
    81  
    82      @Test
    83      void testPublishNothingWithAllDisabled() throws Exception {
    84          stepRule.step.testsPublishResults(script: nullScript, junit: false, jacoco: false, cobertura: false, jmeter: false, cucumber: false, htmlPublisher: false)
    85  
    86          // ensure nothing is published
    87          assertTrue('WarningsPublisher options not empty', publisherStepOptions.junit == null)
    88          assertTrue('PmdPublisher options not empty', publisherStepOptions.jacoco == null)
    89          assertTrue('DryPublisher options not empty', publisherStepOptions.cobertura == null)
    90          assertTrue('FindBugsPublisher options not empty', publisherStepOptions.jmeter == null)
    91          assertTrue('Cucumber options not empty', publisherStepOptions.cucumber == null)
    92          assertTrue('HtmlPublisher options not empty', publisherStepOptions.htmlPublisher == null)
    93      }
    94  
    95      @Test
    96      void testPublishUnitTestsWithDefaultSettings() throws Exception {
    97          stepRule.step.testsPublishResults(script: nullScript, junit: true)
    98  
    99          assertTrue('JUnit options are empty', publisherStepOptions.junit != null)
   100          // ensure default patterns are set
   101          assertEquals('JUnit default pattern not set correct',
   102              '**/TEST-*.xml', publisherStepOptions.junit.testResults)
   103          // ensure nothing else is published
   104          assertTrue('JaCoCo options are not empty', publisherStepOptions.jacoco == null)
   105          assertTrue('Cobertura options are not empty', publisherStepOptions.cobertura == null)
   106          assertTrue('JMeter options are not empty', publisherStepOptions.jmeter == null)
   107          assertTrue('Cucumber options not empty', publisherStepOptions.cucumber == null)
   108          assertTrue('HtmlPublisher options not empty', publisherStepOptions.htmlPublisher == null)
   109      }
   110  
   111      @Test
   112      void testPublishCoverageWithDefaultSettings() throws Exception {
   113          stepRule.step.testsPublishResults(script: nullScript, jacoco: true, cobertura: true)
   114  
   115          assertTrue('JaCoCo options are empty', publisherStepOptions.jacoco != null)
   116          assertEquals('JaCoCo default pattern not set correct',
   117              '**/target/*.exec', publisherStepOptions.jacoco.execPattern)
   118          // ensure nothing else is published
   119          assertTrue('JUnit options are not empty', publisherStepOptions.junit == null)
   120          assertTrue('JMeter options are not empty', publisherStepOptions.jmeter == null)
   121          assertTrue('Cucumber options not empty', publisherStepOptions.cucumber == null)
   122          assertTrue('HtmlPublisher options not empty', publisherStepOptions.htmlPublisher == null)
   123  
   124          assertTrue('Cobertura options are empty', publisherStepOptions.cobertura != null)
   125          assertTrue('Cobertura default pattern is empty', publisherStepOptions.cobertura.coberturaReportFile != null)
   126          String sampleCoberturaPathForJava = 'my/workspace/my/project/target/coverage/cobertura-coverage.xml'
   127          assertTrue('Cobertura default pattern does not match files at target/coverage/cobertura-coverage.xml for Java projects',
   128              Minimatch.minimatch(sampleCoberturaPathForJava, publisherStepOptions.cobertura.coberturaReportFile))
   129          String sampleCoberturaPathForKarma = 'my/workspace/my/project/target/coverage/Chrome 78.0.3904 (Mac OS X 10.14.6)/cobertura-coverage.xml'
   130          assertTrue('Cobertura default pattern does not match files at target/coverage/<browser>/cobertura-coverage.xml for UI5 projects',
   131              Minimatch.minimatch(sampleCoberturaPathForKarma, publisherStepOptions.cobertura.coberturaReportFile))
   132      }
   133  
   134      @Test
   135      void testPublishJMeterWithDefaultSettings() throws Exception {
   136          stepRule.step.testsPublishResults(script: nullScript, jmeter: true)
   137  
   138          assertTrue('JMeter options are empty', publisherStepOptions.jmeter != null)
   139          assertEquals('JMeter default pattern not set',
   140              '**/*.jtl', publisherStepOptions.jmeter.sourceDataFiles)
   141  
   142          // ensure nothing else is published
   143          assertTrue('JUnit options are not empty', publisherStepOptions.junit == null)
   144          assertTrue('JaCoCo options are not empty', publisherStepOptions.jacoco == null)
   145          assertTrue('Cobertura options are not empty', publisherStepOptions.cobertura == null)
   146          assertTrue('Cucumber options not empty', publisherStepOptions.cucumber == null)
   147          assertTrue('HtmlPublisher options not empty', publisherStepOptions.htmlPublisher == null)
   148      }
   149  
   150      @Test
   151      void testPublishUnitTestsWithCustomSettings() throws Exception {
   152          stepRule.step.testsPublishResults(script: nullScript, junit: [pattern: 'fancy/file/path', archive: true, active: true])
   153  
   154          assertTrue('JUnit options are empty', publisherStepOptions.junit != null)
   155          // ensure default patterns are set
   156          assertEquals('JUnit pattern not set correct',
   157              'fancy/file/path', publisherStepOptions.junit.testResults)
   158          assertEquals('JUnit default pattern not set correct',
   159              'fancy/file/path', publisherStepOptions.junit.testResults)
   160          // ensure nothing else is published
   161          assertTrue('JaCoCo options are not empty', publisherStepOptions.jacoco == null)
   162          assertTrue('Cobertura options are not empty', publisherStepOptions.cobertura == null)
   163          assertTrue('JMeter options are not empty', publisherStepOptions.jmeter == null)
   164          assertTrue('Cucumber options not empty', publisherStepOptions.cucumber == null)
   165          assertTrue('HtmlPublisher options not empty', publisherStepOptions.htmlPublisher == null)
   166      }
   167  
   168      @Test
   169      void testPublishCucumberResults() throws Exception {
   170          stepRule.step.testsPublishResults(script: nullScript, cucumber: [pattern: 'fancy/file/path', archive: true, active: true])
   171  
   172          assertTrue('Cucumber options are empty', publisherStepOptions.cucumber != null)
   173          assertEquals('Cucumber pattern not set correct',
   174              'fancy/file/path', publisherStepOptions.cucumber.testResults)
   175  
   176          assertTrue('JUnit options are not empty', publisherStepOptions.junit == null)
   177          assertTrue('JaCoCo options are not empty', publisherStepOptions.jacoco == null)
   178          assertTrue('Cobertura options are not empty', publisherStepOptions.cobertura == null)
   179          assertTrue('JMeter options are not empty', publisherStepOptions.jmeter == null)
   180          assertTrue('HtmlPublisher options not empty', publisherStepOptions.htmlPublisher == null)
   181      }
   182  
   183      @Test
   184      void testPublishHtmlResults() throws Exception {
   185          stepRule.step.testsPublishResults(script: nullScript, htmlPublisher: [pattern: 'fancy/file/path', active: true])
   186  
   187          assertTrue('HtmlPublisher options are empty', publisherStepOptions.htmlPublisher != null)
   188          assertEquals('HtmlPublisher pattern not set correct',
   189              'fancy/file/path', publisherStepOptions.htmlPublisher.target.reportFiles)
   190  
   191          assertTrue('JUnit options are not empty', publisherStepOptions.junit == null)
   192          assertTrue('JaCoCo options are not empty', publisherStepOptions.jacoco == null)
   193          assertTrue('Cobertura options are not empty', publisherStepOptions.cobertura == null)
   194          assertTrue('JMeter options are not empty', publisherStepOptions.jmeter == null)
   195          assertTrue('Cucumber options not empty', publisherStepOptions.cucumber == null)
   196      }
   197  
   198      @Test
   199      void testBuildResultStatus() throws Exception {
   200          stepRule.step.testsPublishResults(script: nullScript)
   201          assertJobStatusSuccess()
   202      }
   203  
   204      @Test
   205      void testBuildWithTestFailuresAndWithoutFailOnError() throws Exception {
   206          nullScript.currentBuild.getRawBuild = {
   207              return [getAction: { type ->
   208                  return [getFailCount: {
   209                      return 6
   210                  }]
   211              }]
   212          }
   213  
   214          stepRule.step.testsPublishResults(script: nullScript)
   215          assertJobStatusSuccess()
   216      }
   217  
   218      @Test
   219      void testBuildWithTestFailuresAndWithFailOnError() throws Exception {
   220          nullScript.currentBuild.getRawBuild = {
   221              return [getAction: { type ->
   222                  return [getFailCount: {
   223                      return 6
   224                  }]
   225              }]
   226          }
   227  
   228          thrown.expect(hudson.AbortException)
   229          thrown.expectMessage('[testsPublishResults] Some tests failed!')
   230  
   231          stepRule.step.testsPublishResults(script: nullScript, failOnError: true)
   232      }
   233  }