Coverage reporting in Emacs with Buttercup, Undercover, Coverage, and a Makefile
| emacs, elisp, subedOne of the things that I always wanted to get back to was the practice of having good test coverage. That way, I can have all these tests catch me in case I break something in my sleep-deprived late-night hacking sessions, and I can see where I may have missed a spot.
Fortunately, subed-mode included lots of tests using the Buttercup testing framework. They look like this:
(describe "SRT" (describe "Getting" (describe "the subtitle ID" (it "returns the subtitle ID if it can be found." (with-temp-srt-buffer (insert mock-srt-data) (subed-jump-to-subtitle-text 2) (expect (subed-subtitle-id) :to-equal 2))) (it "returns nil if no subtitle ID can be found." (with-temp-srt-buffer (expect (subed-subtitle-id) :to-equal nil)))) ...))
and I can run them with make test
, which the Makefile defines as
emacs -batch -f package-initialize -L . -f buttercup-run-discover
.
I don't have Cask set up for subed. I should probably learn how to use Cask. In the meantime, I needed to figure out how to get my Makefile to get the buttercup tests to capture the coverage data and report it in a nice way.
It turns out that the undercover coverage recording library works well
with buttercup. It took me a little fiddling (and some reference to
undercover.el-buttercup-integration-example) to figure out exactly how
to invoke it so that undercover instrumented libraries that I was
loading, since the subed files were in one subdirectory and the tests
were in another. This is what I eventually came up with for
tests/undercover-init.el
:
(add-to-list 'load-path "./subed") (when (require 'undercover nil t) (undercover "./subed/*.el" (:report-format 'simplecov) (:send-report nil)))
Then the tests files could start with:
(load-file "./tests/undercover-init.el") (require 'subed-srt)
and my Makefile target for running tests with coverage reporting could be:
test-coverage: mkdir -p coverage UNDERCOVER_FORCE=true emacs -batch -L . -f package-initialize -f buttercup-run-discover
Displaying the coverage information in code buffers was easy with the coverage package. It looks in the git root directory for the coverage results, so I didn't need to tell it where the results were. This is what it looks like:
There are a few other options for displaying coverage info. cov uses the fringe and coverlay focuses on highlighting missed lines.
So now I can actually see how things are going, and I can start writing tests for some of those gaps. At some point I may even do the badge thing mentioned in my blog post from 2015 on continuous integration and code coverage for Emacs packages. There are a lot of things I'm slowly remembering how to do… =)