Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-41751

Groovy PowerAssertions don't show a useful message when being CPS transformed

XMLWordPrintable

      PowerAssertions are a really useful feature of the groovy programming language, as they give you the full information on what is not as it is expected in the error message - the full assert statement with all information about the values.

      However, when running CPS transformed, the output is less useful, as it only states what the assertion was that failed, without the values.
      Take for example the following pipeline script:

      node {
          def a = 4
          stage('without cps') {
              nonCps(a)
          }
      
          stage('with cps') {
              def b = [1, 2, 3]
              try {
                  assert a == b[0]
              } catch (Throwable e) {
                  echo e.class.toString()
                  echo e.message
                  echo e.stackTrace.join('\n')
              }
          }
      }
      
      @NonCPS
      def nonCps(a) {
          def b = [1, 2, 3]
          try {
              assert a == b[0]
          } catch (Throwable e) {
              echo e.class.toString()
              echo e.message
              echo e.stackTrace.join('\n')
          }
      }
      

      Although the code is the same for NonCPS and with CPS,

      NonCPS prints the following:

      assert a == b[0]
             |    |
             |    1
             false
      

      (I.e. the value of the whole expression and the value of b[0],

      while the CPS transformed version prints no information whatsoever:

      assert a == b[0]
      

      N.B.: running the same code in a groovysh actually gives even more information:

      assert a == b[0]
             | |  ||
             4 |  |1
               |  [1, 2, 3]
               false
      

      That one can be worked around by liberally throwing parentheses at the problem, but that again only works in the NonCPS case:

      assert (a == (b[0]))
             ||    ||
             |4    |[1, 2, 3]
             false 1
      

      This makes the assert statement almost useless for writing test jobs for your global library methods, so it would be a really nice addition if the information could be preserved and be added to the message of the PowerAssertionError.

            jglick Jesse Glick
            0x89 Martin Sander
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: