Repository navigation
Expand file tree
/
Copy pathhow_it_works.Rmd
More file actions
352 lines (275 loc) · 16.9 KB
/
Copy pathhow_it_works.Rmd
File metadata and controls
352 lines (275 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
---
title: "How does covr work anyway?"
author: "Jim Hester"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{How does covr work anyway}
%\VignetteEngine{knitr::rmarkdown}
\usepackage[utf8]{inputenc}
---
```{r setup}
#| include = FALSE
library(covr)
```
# Introduction #
The **covr** package provides a framework for measuring unit test coverage.
Unit testing is one of the cornerstones of software development.
Any piece of R code can be thought of as a software application with a certain set of behaviors.
Unit testing means creating examples of how the code should behave _with a definition of the expected output_.
This could include normal use, edge cases, and expected error cases.
Unit testing is commonly facilitated by frameworks such as **testthat** and **RUnit**.
Test _coverage_ is the _proportion_ of the source code that is executed when running these tests.
Code coverage consists of:
* instrumenting the source code so that it reports when it is run,
* executing the unit test code to exercise the source code.
Measuring code coverage allows developers to asses their progress in quality checking their own (or their collaborators) code.
Measuring code coverage allows code consumers to have confidence in the measures taken by the package authors to verify high code quality.
**covr** provides several functions to calculate test coverage.
- `package_coverage()` performs coverage calculation on an R package. (Unit tests must be contained in the `"tests"` directory.)
- `file_coverage()` performs coverage calculation on one or more R scripts by executing one or more R scripts.
- `function_coverage()` performs coverage calculation on a single named function, using an expression provided.
- `code_coverage()` performs coverage calculation on a string of R source code, using an expression provided.
- `environment_coverage()` performs coverage calculation on the functions in an environment, e.g. the namespace of a package loaded with `pkgload::load_all()`, using an expression provided.
- `with_coverage()` is the lower level function the others build on: it runs an expression while the functions in an environment are traced.
- `shiny_app_coverage()` performs coverage calculation on a Shiny app, including tests that run the app in a separate process, e.g. with **shinytest2**.
In addition to providing an objective metric of test suite extensiveness, it is often advantageous for developers to have a code level view of their unit tests.
An interface for visually marking code with test coverage results allows a clear box view of the unit test suite.
The clear box view can be accessed using online tools or a local report can be generated using `report()`.
# Instrumenting R Source Code #
## Modifying the call tree ##
The core function in **covr** is `trace_calls()`.
This function was adapted from ideas in [_Advanced R - Walking the Abstract Syntax Tree with
recursive functions_](https://adv-r.had.co.nz/Expressions.html#ast-funs).
This recursive function modifies each of the leaves (atomic or name objects) of
an R expression by applying a given function to them.
If the expression is not a leaf the walker function calls itself recursively on elements of the expression instead.
We can use this same framework to instead insert a trace statement before each
call by replacing each call with a call to a counting function followed by the previous call.
Braces (`{`) in R may seem like language syntax, but
they are actually a Primitive function and you can call them like any other
function.
```{r}
identical(x = { 1 + 2; 3 + 4 },
y = `{`(1 + 2, 3 + 4))
```
Remembering that braces always return the value of the last evaluated expression, we can call a counting function followed by the previous function
substituting `as.call(recurse(x))` in our function above with.
```{r}
#| eval = FALSE
`{`(count(), as.call(recurse(x)))
```
## Source References ##
Now that we have a way to add a counting function to any call in the Abstract Syntax Tree
without changing the output we need a way to determine where in the code source
that function came from.
Luckily R has a built-in method to provide this
information in the form of source references.
When `option(keep.source = TRUE)` (the default for interactive sessions), a reference to the source code
for functions is stored along with the function definition.
This reference is used to provide the original formatting and comments for the given function source.
In particular each call in a function contains a `srcref` attribute, which can then be used as a key to count just that call.
The actual source for `trace_calls` is slightly more complicated because we
want to initialize the counter for each call while we are walking the Abstract Syntax Tree and
there are a few non-calls we also want to count.
## Refining Source References ##
Each statement comes with a source reference. Unfortunately, the following is
counted as one statement:
```r
if (x)
y()
```
To work around this, detailed parse data (obtained from a refined version of
`getParseData`) is analyzed to impute source references at sub-statement level for `if`, `for`, `while` and `switch` constructs.
## Replacing Source In Place ##
After we have our modified function definition, how do we re-define the function
to use the updated definition, and ensure that all other functions which call
the old function also use the new definition? You might try redefining the function directly.
```{r}
f1 <- function() 1
f1 <- function() 2
f1() == 2
```
While this does work for the simple case of calling the new function in the
same environment, it fails if another function calls a function in a different environment.
```{r}
env <- new.env()
f1 <- function() 1
env$f2 <- function() f1() + 1
env$f1 <- function() 2
env$f2() == 3
```
As modifying external environments and correctly restoring them can be tricky
to get correct, we use the C function
[`covr_reassign_function`](https://github.com/r-lib/covr/blob/main/src/reassign.c).
This function takes the target and replacement functions and copies the
replacement's formals, body, attributes and environment into the target.
This allows an in-place replacement of a given function and ensures that all
existing references to it use the new definition.
Copying closure fields directly bypasses R's generational garbage-collection
write barrier. Before copying the fields, covr temporarily attaches an attribute
referencing the replacement through R's barrier-aware attribute API. The attribute
copy is shallow so it retains the exact replacement. This lets R remember the
target for scanning or age the incoming reference graph appropriately.
The replacement's original attributes are then copied onto the target, removing
the temporary reference without undoing the GC bookkeeping. No permanent
`R_PreserveObject()` roots or extra attributes remain.
# Object Orientation #
## S3 Classes ##
R's S3 object oriented classes simply define functions directly in the packages
namespace, so they can be treated the same as any other function.
## S4 Classes ##
S4 methods have a more complicated implementation than S3 classes.
The function definitions are placed in an enclosing environment based on the generic method they implement.
This makes getting the function definition more complicated.
`replacements_S4` first gets all the generic functions for the package environment.
Then for each generic function if finds the mangled meta package name
and gets the corresponding environment from the base environment.
All of the functions within this environment are then traced.
## Reference Classes ##
Similarly to S4 classes reference classes (RC) define their methods in a special environment.
A similar method is used to add the tracing calls to the
class definition.
These calls are then copied to the object methods when the
generator function is run.
# Compiled code #
## Gcov ##
Test coverage of compiled code uses a completely different mechanism than that
of R code. Fortunately we can take advantage of
[Gcov](https://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Gcov.html#Gcov), the
built-in coverage tool for [gcc](https://gcc.gnu.org/) and compatible reports
from [clang](https://clang.llvm.org/) versions 3.5 and greater.
Both of these compilers track execution coverage when given the `--coverage`
flag. In addition it is necessary to turn off compiler optimization `-O0`,
otherwise the coverage output is difficult or impossible to interpret as
multiple lines can be optimized into one, functions can be inlined, etc.
## Makevars ##
R passes flags defined in `PKG_CFLAGS` to the compiler, however it also has
default flags including `-O2` (defined in `$R_HOME/etc/Makeconf`), which need to
be overridden. Unfortunately it is not possible to override the default flags
with environment variables (as the new flags are added to the left of the
defaults rather than the right). However Make variables defined in a user
Makevars file _are_ used in place of the defaults.
Therefore, **covr** writes a temporary Makevars file with the flags in the
`covr.flags` option, which include `-O0 --coverage`, and tells R to use it
(with the `R_MAKEVARS_USER` environment variable) while it installs the package.
Your own `~/.R/Makevars` is never modified.
## Subprocess ##
The last hurdle to getting compiled code coverage working properly is that the
coverage output is only produced when the running process ends.
Therefore you cannot run the tests and get the results in the same R process.
**covr** runs a separate R process when running tests.
However we need to modify the package code first before running the tests.
**covr** installs the package to be tested in a
temporary directory.
By default the package is then instrumented once, right after it is installed:
a new R process loads the installed namespace, traces and byte-compiles its
functions in place and writes the instrumented code next to the original.
The counters, with their source references, are saved with it.
Each test process then loads the instrumented code directly, which is much
faster than tracing the package every time it is loaded.
If the package can't be instrumented ahead of time, e.g. because it stores
functions in `R/sysdata.rda`, **covr** falls back to instrumenting the package as it is loaded: calls are
made to the lazy loading code which installs a user hook to modify the code
when it is loaded.
Set `options(covr.install_instrument = FALSE)` (or the environment variable
`COVR_INSTALL_INSTRUMENT=false`) to always use this load-time instrumentation.
Either way each process registers a finalizer which saves its coverage counts
when the namespace is unloaded or the R process exits.
When the package is instrumented once after it is installed, each process only
saves the counts it recorded, and these are added to the saved counters.
These output files are then aggregated together to determine the coverage.
This procedure works regardless of the number of child R processes used, so
therefore also works with parallel code.
However a process which is killed (e.g. a Shiny app or worker process stopped with `SIGKILL`) never runs its finalizer, so its coverage is lost.
Each process therefore also leaves a marker file when it loads the package, which it removes once its coverage is saved; **covr** warns about any markers left over.
## Packages loaded with `load_all()` ##
`package_coverage()` isn't the only way to measure compiled code.
For a package loaded with `pkgload::load_all()`, which is what
`devtools::test_coverage()` uses, the compiled code is measured in the current
session by `environment_coverage()`, `with_coverage()` and `file_coverage()`.
The DLL that `load_all()` built isn't instrumented, so **covr** builds an
instrumented copy of the package in the session's temporary directory (only
recompiling the files that changed since the last build) and has the namespace
use its DLL while the code runs.
Unloading that DLL afterwards makes `libgcov` write its counts, which `gcov`
then reads and **covr** maps back to the paths of the package.
## Rust ##
R packages build Rust code with cargo into a static library, which is linked
into the package's shared object along with its C code.
gcov can't read the coverage of Rust code, so **covr** uses
[LLVM's source-based coverage](https://doc.rust-lang.org/rustc/instrument-coverage.html)
for it instead:
1. While the package is installed, cargo runs rustc for the crates of the
package's workspace through a wrapper script (set with the
`RUSTC_WORKSPACE_WRAPPER` environment variable) that adds
`-C instrument-coverage`.
This works however the package's Makevars or configure script sets
`RUSTFLAGS`, and its dependencies are built as usual.
1. Static libraries don't include the profiler runtime that writes the counts,
so **covr** links the runtime of the Rust toolchain into the shared object.
1. The `LLVM_PROFILE_FILE` environment variable has each R process that runs
the tests write its counts to a separate `.profraw` file when it exits.
1. `llvm-profdata` merges the counts, and `llvm-cov export` reads the
coverage of each line from them and the installed shared object.
`llvm-profdata` and `llvm-cov` must use the same version of LLVM as rustc,
so **covr** uses those of the Rust toolchain, which are installed with
`rustup component add llvm-tools`.
The Rust of Linux distributions uses the system's LLVM instead, so for it
**covr** looks for the tools of the LLVM version rustc reports, e.g.
`llvm-cov-18`.
# Output Formats #
The output format returned by **covr** is an R object of class "coverage" containing the information gathered when executing the test suite.
It consists of a named list, where the names are colon-delimited information from the source references (the file, line and columns the traced call is from).
The value is the number of times that given expression was called and the source ref of the original call.
```{r}
# an object to analyze
f1 <- function(x) { x + 1 }
# get results with no unit tests
c1 <- function_coverage(fun = f1, code = NULL)
c1
# get results with unit tests
c2 <- function_coverage(fun = f1, code = f1(x = 1) == 2)
c2
```
An `as.data.frame` method is available to make subsetting by various features easy to do.
While **covr** tracks coverage by expression, typically users expect coverage to
be reported by line, so there are functions to convert to line oriented
coverage.
# Reports and Export Formats #
`report()` creates a local HTML report, and `report_html()` the same report to
include in an R Markdown or Quarto document.
Coverage can also be exported to other tools: `cobertura()`, `lcov()` and
`sonarqube()` write files in the formats of the same names, `badge()` writes a
coverage badge as an SVG file, and `coverage_to_markdown()` summarizes the
results as Markdown for pull request comments.
# Codecov.io and Coveralls.io #
[Codecov](https://about.codecov.io/) and [Coveralls](https://coveralls.io/) are a web services to help you track your code coverage
over time, and ensure that all new code is appropriately covered.
They both have JSON-based APIs to submit and report on coverage. The functions `codecov()` and `coveralls()` create outputs that can be consumed by these services.
# Prior Art #
## Overview ##
Prior to writing **covr**, there were a handful of coverage tools for R code.
[**R-coverage**](https://web.archive.org/web/20160611114452/http://r2d2.quartzbio.com/posts/r-coverage-docker.html) by Karl Forner and
[**testCoverage**](https://github.com/MangoTheCat/testCoverage) by Tom Taverner, Chris Campbell & Suchen Jin.
## R-coverage ##
**R-coverage** provides a very robust solution by modifying
the R source code to instrument the code for each call.
Unfortunately this requires you to patch the source of the R application itself.
Getting the changes incorporated into the core R distribution would likely be challenging.
## Test Coverage ##
**testCoverage** uses `getParseData`, R's alternate parser (from 3.0) to analyse the R source code.
The package replaces symbols in the code to be tested with a unique identifier.
This is then injected into a tracing function that will report each time the symbol is called.
The first symbol at each level of the expression tree is traced, allowing the coverage of code branches to be checked.
This is a complicated implementation I do not fully
understand, which is one of the reasons I decided to write **covr**.
## Covr ##
**covr** takes an approach in-between the two previous tools.
Function definitions are modified by parsing the abstract syntax tree and inserting trace statements.
These modified definitions are then transparently replaced in-place using C.
This allows us to correctly instrument every call and function in a package without having to resort to alternate parsing or changes to the R source.
# Conclusion #
**covr** provides an accessible framework which will ease the communication of R unit test suites.
**covr** can be integrated with continuous integration services where R developers are working on larger projects, or as part of multi-disciplinary teams.
**covr** aims to be simple to use to make writing high quality code part of every R user's routine.