Skip to content

Instantly share code, notes, and snippets.

@paul-hammant
Created May 11, 2019 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paul-hammant/7bd8a42adbb8b74eff5b75b772a9b7ed to your computer and use it in GitHub Desktop.
Save paul-hammant/7bd8a42adbb8b74eff5b75b772a9b7ed to your computer and use it in GitHub Desktop.
Showing JaCoCo coverage percentages inline in GitHub READMEs (Multi module Maven Projects)
#!/bin/bash
# You will have needed to have installed 'xmlstarlet' first
#
# Each time you use this, you will have had to run tests "mvn clean install" first.
#
# In your README, have a line like so, to display the coverage.
#
# Code Coverage: ![](coverage-percentage.svg?raw=true&sanitize=true)%
#
# You'll have to check this in of course. Perhaps daily is enough. I'd prefer NN.n percentages
# but I can't see how to configure that for JaCoCo.
template=`cat <<EOF
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xl="http://www.w3.org/1999/xlink" version="1.1" viewBox="314.084 115 32.832 30" width="32.832" height="30">
<defs>
<font-face font-family="Helvetica Neue" font-size="16" panose-1="2 0 5 3 0 0 0 2 0 4" units-per-em="1000" underline-position="-100" underline-thickness="50" slope="0" x-height="517" cap-height="714" ascent="951.9958" descent="-212.99744" font-weight="400">
<font-face-src>
<font-face-name name="HelveticaNeue"/>
</font-face-src>
</font-face>
</defs>
<g id="Canvas_1" fill-opacity="1" stroke="none" stroke-dasharray="none" fill="none" stroke-opacity="1">
<title>Canvas 1</title>
<g id="Canvas_1: Layer 1">
<title>Layer 1</title>
<g id="Graphic_2">
<text transform="translate(319.084 120)" fill="black">
<tspan font-family="Helvetica Neue" font-size="16" font-weight="400" fill="black" x="0" y="15">NN</tspan>
</text>
</g>
</g>
</g>
</svg>
EOF
`
doit ()
{
if [ -e target/site/jacoco/index.html ]
then
pct=$(cat target/site/jacoco/index.html | xmlstarlet format --indent-tab | xmlstarlet sel -N x="http://www.w3.org/1999/xhtml" -t -m "//x:tfoot" -v . 2>&1 | grep % | head -1 | xargs | tr -d '%')
echo "$template" | sed "s/NN/${pct}/g" > coverage-percentage.svg
fi
}
dirs=$(find . -d | grep target | grep "target$" | sed "s#/target##")
while read -r line; do
cd $line
doit
cd - > /dev/null
done <<< "$dirs"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment