emacs/var/elfeed/db/data/9f/9fc380205ce3c4c696daa980eb1b1369962bec98
2022-01-03 12:49:32 -06:00

101 lines
4.6 KiB
Plaintext

<p>
One 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.
</p>
<p>
Fortunately, subed-mode included lots of tests using the <a href="https://github.com/jorgenschaefer/emacs-buttercup">Buttercup</a>
testing framework. They look like this:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span class="org-keyword">describe</span> <span class="org-string">"SRT"</span>
(<span class="org-keyword">describe</span> <span class="org-string">"Getting"</span>
(<span class="org-keyword">describe</span> <span class="org-string">"the subtitle ID"</span>
(<span class="org-keyword">it</span> <span class="org-string">"returns the subtitle ID if it can be found."</span>
(with-temp-srt-buffer
(insert mock-srt-data)
(subed-jump-to-subtitle-text 2)
(<span class="org-keyword">expect</span> (subed-subtitle-id) <span class="org-builtin">:to-equal</span> 2)))
(<span class="org-keyword">it</span> <span class="org-string">"returns nil if no subtitle ID can be found."</span>
(with-temp-srt-buffer
(<span class="org-keyword">expect</span> (subed-subtitle-id) <span class="org-builtin">:to-equal</span> nil))))
...))
</pre>
</div>
<p>
and I can run them with <code>make test</code>, which the Makefile defines as
<code>emacs -batch -f package-initialize -L . -f buttercup-run-discover</code>.
</p>
<p>
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.
</p>
<p>
It turns out that the <a href="https://github.com/undercover-el/undercover.el">undercover</a> coverage recording library <a href="https://github.com/jorgenschaefer/emacs-buttercup/issues/9">works well</a>
with buttercup. It took me a little fiddling (and some reference to
<a href="https://github.com/undercover-el/undercover.el-buttercup-integration-example">undercover.el-buttercup-integration-example</a>) 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
<code>tests/undercover-init.el</code>:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(add-to-list 'load-path <span class="org-string">"./subed"</span>)
(<span class="org-keyword">when</span> (<span class="org-keyword">require</span> '<span class="org-constant">undercover</span> nil t)
(<span class="org-keyword">undercover</span> <span class="org-string">"./subed/*.el"</span> (<span class="org-builtin">:report-format</span> 'simplecov) (<span class="org-builtin">:send-report</span> nil)))
</pre>
</div>
<p>
Then the tests files could start with:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(load-file <span class="org-string">"./tests/undercover-init.el"</span>)
(<span class="org-keyword">require</span> '<span class="org-constant">subed-srt</span>)
</pre>
</div>
<p>
and my Makefile target for running tests with coverage reporting could be:
</p>
<pre class="example" id="org3357d5c">test-coverage:
mkdir -p coverage
UNDERCOVER_FORCE=true emacs -batch -L . -f package-initialize -f buttercup-run-discover
</pre>
<p>
Displaying the coverage information in code buffers was easy with the
<a href="https://github.com/trezona-lecomte/coverage">coverage</a> 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:
</p>
<div class="figure" id="org89ad410">
<p><img alt="2022-01-02-19-00-28.svg" src="https://sachachua.com/blog/2022/01/coverage-reporting-in-emacs-with-buttercup-undercover-coverage-and-a-makefile/2022-01-02-19-00-28.svg" />
</p>
</div>
<p>
There are a few other options for displaying coverage info. <a href="https://github.com/AdamNiederer/cov">cov</a> uses
the fringe and <a href="https://github.com/twada/coverlay.el">coverlay</a> focuses on highlighting missed lines.
</p>
<p>
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 <a href="https://sachachua.com/blog/2015/02/continuous-integration-code-coverage-emacs-packages-travis-coveralls/">blog post from 2015 on continuous
integration and code coverage for Emacs packages</a>. There are a lot of
things I'm slowly remembering how to do… =)
</p>