index rss mastodon twitter github linkedin email
Álvaro Ramírez
sponsor

Álvaro Ramírez

29 December 2020 Swift package code coverage (plus Emacs overlay)

While playing around with Swift package manager, I had a quick look into code coverage options. Luckily, coverage reporting and exporting are supported out of the box (via llvm-cov).

Ensure tests are invoked as follows:

swift test --enable-code-coverage

A high level report can be generated with:

xcrun llvm-cov report .build/x86_64-apple-macosx/debug/FooPackageTests.xctest/Contents/MacOS/FooPackageTests \
      -instr-profile=.build/x86_64-apple-macosx/debug/codecov/default.profdata -ignore-filename-regex=".build|Tests"
Filename                                   Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/tmp/Foo/Sources/Foo/Foo.swift                   2                 1    50.00%           2                 1    50.00%           6                 3    50.00%
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                            2                 1    50.00%           2                 1    50.00%           6                 3    50.00%

llvm-cov can export as lcov format:

xcrun llvm-cov export -format="lcov" .build/x86_64-apple-macosx/debug/FooPackageTests.xctest/Contents/MacOS/FooPackageTests -instr-profile=.build/x86_64-apple-macosx/debug/codecov/default.profdata -ignore-filename-regex=".build|Tests" > coverage.lcov

With the report in lcov format, we can look for an Emacs package to visualize coverage in source files. Found coverlay.el to require minimal setup. I was interested in highlighting only untested areas, so I set tested-line-background-color to nil:

(use-package coverlay
  :ensure t
  :config
  (setq coverlay:tested-line-background-color nil))

After installing coverlay, I enabled the minor mode via M-x coverlay-minor-mode, invoked M-x coverlay-watch-file to watch coverage.lcov for changes, and voilà!

coverage.png