shellcheck

nicolaw 14th August 2018 at 2:12pm
Jenkins
shellcheck -f checkstyle foo.bash bar.bash \
  | xmlstarlet tr checkstyle2junit.xslt
  stage('Shellcheck') {
    // https://github.com/koalaman/shellcheck/wiki/JUnit
    sh '''
shellcheck --version
shellcheck -f checkstyle $(find . \
  -type f \
  -size -40k \
  -not -path '*/.git/*' \
  -not -path '*/.virtualenv/*' \
  -print0 \
    | xargs -0 file \
    | grep 'shell script' \
    | sed -E 's/:[[:space:]]+.*//' \
    | cut -b3-) \
  | xmlstarlet tr ~/checkstyle2junit.xslt \
  | tee "junit-reports/shellcheck.xml"
'''
    // https://jenkins.io/doc/pipeline/steps/junit/
    junit testResults: 'junit-reports/*.xml', healthScaleFactor: 0.0, allowEmptyResults: true
  }
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output encoding="UTF-8" method="xml"></xsl:output>

  <xsl:template match="/">
    <testsuite>
      <xsl:attribute name="tests">
        <xsl:value-of select="count(.//file)" />
      </xsl:attribute>
      <xsl:attribute name="failures">
        <xsl:value-of select="count(.//error)" />
      </xsl:attribute>
      <xsl:for-each select="//checkstyle">
        <xsl:apply-templates />
      </xsl:for-each>
    </testsuite>
  </xsl:template>

  <xsl:template match="file">
    <testcase>
      <xsl:attribute name="classname">
        <xsl:value-of select="@name" />
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:value-of select="@name" />
      </xsl:attribute>
      <xsl:apply-templates select="node()" />
    </testcase>
  </xsl:template>

  <xsl:template match="error">
    <failure>
      <xsl:attribute name="type">
        <xsl:value-of select="@source" />
      </xsl:attribute>
      <xsl:text>Line </xsl:text>
      <xsl:value-of select="@line" />
      <xsl:text>: </xsl:text>
      <xsl:value-of select="@message" />
    </failure>
  </xsl:template>
</xsl:stylesheet>