Destroyed checkunit2text (markdown)

Olliver Schinagl
2023-11-14 21:28:45 +01:00
parent 621c9ecfa5
commit cc8251d119

@@ -1,81 +0,0 @@
Since shellcheck can only produce output in a single format, producing machine readable formats (such as `XML`), posses a problem when also wanting to get the text output.
Producing checkstyle output we have something machine processable, but not user readable.
If we want both outputs, without the overhead of re-processing shellcheck, `sed` can be used to somewhat reproduce the original shellcheck _tty_ format. Since checkstyle does not include column length, we can only indicate the start of the issue. Also severity colors and shellcheck summary links are lost, where the intend was to remain similar to the original output.
Using the [xslt](#checkstyle2jtext.xslt) at the end of this article, combined with the following `sed` magic, we generate machine/human parsable output and then translate that to human readable output. This is needed because neither checkstyle, nor xslt allow us to show the content of the failure.
```
xmlstarlet tr 'checkstyle2log.xslt] '/path/to/checkstyle.xml' | \
sed -n \
-e "s|^\(.*\): ShellCheck.\(SC\d\+\) (\(.*\)) line \(\d\+\), column \(\d\+\) - \(.*\)$|echo 'In \1 line \4:'\nsed -n '\4p' \1\nprintf \"%\$((\5 - 1\))s^\\\n\"\necho '\2 (\3): \6'\necho\n|p" | \
sh | \
tee -a 'shellcheck.log'
```
Producing an incompatible output, but with ShellCheck URL's and a simple file header
```
xmlstarlet tr "/tmp/checkstyle2text.xslt" "/path/to/file" | \
sed 's|"|\\"|g' | \
sed -n \
-e "s|^\(.*\) - \([0-9]\+\) failure.*$|echo '========='\n \
echo 'Found \2 failure(s) in \1'\n \
echo '---------'|p" \
-e "s|^\(.*\): \(ShellCheck\.\)\?\(\(..\)[0-9]\{4\}\) (\(.*\)) line \([0-9]\+\), column \([0-9]\+\) - \(.*\)$|echo 'In \1 line \6:'\n \
sed -n '\6p' \1\n \
printf \"%\$((\7 - 1\))s^ \"\n \
printf 'https://github.com/hadolint/hadolint/wiki/' \
echo \"\3 (\5): \8\"\n \
echo\n|p" | \
sh | \
tee -a "${_file%%.checkstyle.xml}.log"
```
### `checkstyle2text.xslt`
```
<?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="text"/>
<xsl:template match="/">
<xsl:apply-templates select="//file" />
</xsl:template>
<xsl:template match="file">
<xsl:variable name="filename" select="@name" />
<xsl:variable name="errorcount" select="count(error)" />
<xsl:value-of select="$filename" />
<xsl:text> - </xsl:text>
<xsl:value-of select="$errorcount" />
<xsl:text> failure(s)</xsl:text>
<xsl:text>&#10;</xsl:text>
<xsl:apply-templates select="error">
<xsl:with-param name="filename" select="$filename"/>
</xsl:apply-templates>
<xsl:text>&#10;&#10;</xsl:text>
</xsl:template>
<xsl:template match="error">
<xsl:param name="filename"/>
<xsl:value-of select="$filename" />
<xsl:text>&#58; </xsl:text>
<xsl:value-of select="@source" />
<xsl:text> (</xsl:text>
<xsl:value-of select="@severity" />
<xsl:text>) line </xsl:text>
<xsl:value-of select="@line" />
<xsl:text>, column </xsl:text>
<xsl:value-of select="@column" />
<xsl:text> - </xsl:text>
<xsl:value-of select="@message" />
<xsl:text>&#10;</xsl:text>
<xsl:text> - </xsl:text>
<xsl:value-of select="@message" />
<xsl:text>&#10;</xsl:text>
</xsl:template>
</xsl:stylesheet>
```