Skip to contents

instrument_packages() instruments packages installed in a library, so that every R process that loads one of them counts the code it runs and saves the counts in a trace directory when it exits. trace_coverage() reads the counts saved there into a single coverage object, and uninstrument_packages() restores the packages. local_instrumented_packages() instruments them until the calling function returns.

This is the workflow package_coverage() uses, split up so that the code can be run by anything: the tests of several packages, a test runner or build system, or a script that starts many R processes. The traces of several packages, and of processes that load more than one of them, are kept apart, so one directory can collect the coverage of a set of packages, e.g. those of a monorepo.

Usage

instrument_packages(
  packages,
  lib = .libPaths()[[1]],
  trace_dir = file.path(lib, "covr_traces"),
  cores = getOption("Ncpus", 1L)
)

uninstrument_packages(packages, lib = .libPaths()[[1]])

local_instrumented_packages(
  packages,
  lib = .libPaths()[[1]],
  trace_dir = file.path(lib, "covr_traces"),
  cores = getOption("Ncpus", 1L),
  .local_envir = parent.frame()
)

trace_coverage(
  trace_dir = NULL,
  lib = NULL,
  packages = NULL,
  paths = NULL,
  relative_path = TRUE,
  line_exclusions = NULL,
  function_exclusions = NULL
)

Arguments

packages

The names of the packages to instrument, installed in lib. For trace_coverage(), the packages to include, or NULL for all those with traces in trace_dir or instrumented in lib.

lib

The library the packages are installed in. For trace_coverage(), a library of instrumented packages, whose packages that no process loaded are included with no coverage, or NULL.

trace_dir

The directory the counts are saved in, if the environment variable COVR_TRACE_DIR is not set when an R process loads an instrumented package. It is created if it doesn't exist. NULL to only measure the packages when COVR_TRACE_DIR is set; otherwise they load their original code without covr. For trace_coverage(), the directories the counts were saved in, e.g. one for each shard of a CI run; by default COVR_TRACE_DIR, or the default trace_dir of lib (or of the first library in .libPaths()).

cores

The number of packages to instrument at once, each in its own R process.

.local_envir

The environment whose exit restores the packages.

paths

The source directories of the packages, named by package, e.g. c(pkgA = "src/pkgA"). By default they are the directories the packages were installed from, as recorded in their source references. Give them if the packages were installed from somewhere else, e.g. a copy of the source in a sandbox, and the file names of their code are changed to be in these. Packages without coverage are ignored.

Or a function of the file names (as recorded when the packages were installed) and the name of their package, which returns the full paths of the files in the source, e.g. for a build that copied files from other directories into R/.

relative_path

Whether file names are relative to the source directory of the package, or for several packages, of the directory that contains all of them, or absolute paths. If a string, file names are relative to that directory.

line_exclusions

a named list of files with the lines to exclude from each file.

function_exclusions

a vector of regular expressions matching function names to exclude. Example print\\\. to match print methods.

Value

instrument_packages() and local_instrumented_packages() return a logical vector, invisibly, named by package: TRUE for a package that was instrumented when it was installed, and FALSE for one that is instrumented each time it is loaded, e.g. because it stores functions in R/sysdata.rda. uninstrument_packages() returns packages, invisibly.

trace_coverage() returns a coverage object with the coverage of all the instrumented packages whose counts are in trace_dir. Files of the packages that the .covrignore file (see exclusions) in their source directory lists are left out. If there are none, it warns (with class covr_no_coverage) and returns a coverage object without any.

Requirements

  • The packages must be installed with their source references, e.g. with R CMD INSTALL --with-keep.source. File names in the coverage are those of the source the packages were installed from.

  • covr must be installed in a library that the R processes use, as the instrumented packages call it when they are loaded, unless they were instrumented with trace_dir = NULL and COVR_TRACE_DIR is not set: they then load as installed, without covr.

  • Processes must exit normally to save their counts; processes that are killed lose them, and trace_coverage() warns (with class covr_unsaved_trace) about the processes that loaded an instrumented package but didn't save their counts.

The R processes save their counts in the directory in the environment variable COVR_TRACE_DIR, or if it is not set, in trace_dir. Packages instrumented with trace_dir = NULL are only measured when COVR_TRACE_DIR is set, and otherwise load the code they were installed with, without covr, so they can be instrumented once, e.g. when they are built, and their coverage measured whenever it is set. Compiled code isn't instrumented.

The coverage includes the instrumented packages that no process loaded, with none of their code covered, if they were instrumented with a trace_dir, or if trace_coverage() is given their lib. This needs them to be instrumented when they are installed, which works for most packages (see the value of instrument_packages()).

The options that change what is measured, such as covr.branch_coverage and covr.record_tests, and covr.fix_parallel_mcexit (see package_coverage()), are read when the packages are instrumented. Unlike package_coverage(), instrument_packages() doesn't set the environment variable R_COVR, so in_covr() is FALSE in the processes unless they set it.

Example workflow

lib <- "lib"
# Install the packages with their source references, e.g.
#   R CMD INSTALL --with-keep.source -l lib pkgA pkgB
instrument_packages(c("pkgA", "pkgB"), lib, trace_dir = "traces")

# Then run anything that loads them from `lib`, e.g.
#   R_LIBS=lib Rscript -e 'testthat::test_dir("pkgA/tests/testthat",
#     package = "pkgA", load_package = "installed")'

cov <- trace_coverage("traces")
lcov(cov, "lcov.info")

uninstrument_packages(c("pkgA", "pkgB"), lib)

See also

package_coverage(), which runs the tests of one or more packages and measures their coverage in one call.