Learn R Programming

covr (version 4.0.0)

badge: Create a coverage badge

Description

Write an SVG badge showing the percent coverage, in the style of the badges made by Codecov and shields.io, so a README can show the coverage of a package without uploading it to a coverage service. The badge is made locally and doesn't need a network connection.

Usage

badge(x, file = "coverage.svg", label = "coverage")

Value

The lines of the SVG file, invisibly, after writing them to file.

Arguments

x

the coverage object returned from package_coverage()

file

the name of the SVG file

label

the text on the left side of the badge

Details

The coverage is that of percent_coverage(), rounded down to a whole percent so that only fully covered code shows 100%. The badge is green for coverage of 90% or more, yellow for 75% or more and red below that, the same thresholds used by report(). Coverage without any lines of code, such as that of a package with no R code, shows as unknown on a gray badge.

The badge is a plain file, so on a continuous integration service it needs to be stored somewhere that the README can link to, e.g. committed to a separate branch of the repository. system.file("workflows", "test-coverage-badge.yaml", package = "covr") is a GitHub Actions workflow that does this.

Examples

Run this code
cov <- code_coverage(
  "f <- function(x) {\n  y <- x + 1\n  if (y > 0) {\n    'yes'\n  } else {\n    'no'\n  }\n}",
  "f(1)"
)
tmp <- tempfile(fileext = ".svg")
badge(cov, tmp)
cat(readLines(tmp), sep = "\n")

Run the code above in your browser using DataLab