Finding uncovered lines in a feature branch with git+Jacoco
At my current workplace, we use jacoco to compute code coverage, and we have a minimum coverage threshold to satisfy in order for our CI pipeline to pass. Sometimes the building process fails because code that has been introduced or removed causes some lines to be uncovered. Consulting jacoco reports is painful, because they are scattered across a huge codebase and the html interface offers poor search capabilities. On top of that, it is often hard to remember all the files that have changed on the current feature branch when compared to master
or develop
.
Thus I created a simple script to quickly help me find those lines and analyze them. Notice that this is just a small hack, rather than a fully functional program. Use it at your discretion to speed up developing and testing :)
As a premise, note that it’s possible to get a list of all files on the current branch that differ from a target branch with a compact git command:
git diff --name-status target-branch
Given that we know what files changed, it’s now sufficient to cross that information with the uncovered lines in those files, which can be retrieved directly from jacoco reports. The following script requires two paths in input:
- the path of a text file containing the output of the git command above;
- the path of the directory containing jacoco reports in html format. That is the artefact we currently use, but of course the script can be adapted to parse any valid jacoco report.
1 | const fs = require('fs'); |
Finally, saving the script above in a file named retrieve-changed-uncovered-lines.js
, we put everything together in a tiny bash script, to be executed in the project root (where you have your pom.xml
):
1 | FILE_DIFFS_PATH="./file-diffs.txt" |
I used the default jacoco reporting directory. Yours may be different. In that case, remember to change it in the script!