File: coverage.R

package info (click to toggle)
r-cran-usethis 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,228 kB
  • sloc: sh: 26; makefile: 17; cpp: 6; ansic: 3
file content (57 lines) | stat: -rw-r--r-- 1,653 bytes parent folder | download
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
#' Test coverage
#'
#' Adds test coverage reporting to a package, using either Codecov
#' (`https://codecov.io`) or Coveralls (`https://coveralls.io`).
#'
#' @param type Which web service to use.
#' @eval param_repo_spec()
#' @export
use_coverage <- function(type = c("codecov", "coveralls"), repo_spec = NULL) {
  repo_spec <- repo_spec %||% target_repo_spec()

  type <- match.arg(type)
  if (type == "codecov") {
    new <- use_template("codecov.yml", ignore = TRUE)
    if (!new) {
      return(invisible(FALSE))
    }
  } else if (type == "coveralls") {
    ui_bullets(c(
      "_" = "Turn on coveralls for this repo at {.url https://coveralls.io/repos/new}."
    ))
  }

  switch(
    type,
    codecov = use_codecov_badge(repo_spec),
    coveralls = use_coveralls_badge(repo_spec)
  )

  ui_bullets(c(
    "_" = "Call {.code use_github_action(\"test-coverage\")} to continuously
           monitor test coverage."
  ))

  invisible(TRUE)
}

#' @export
#' @rdname use_coverage
#' @param files Character vector of file globs.
use_covr_ignore <- function(files) {
  use_build_ignore(".covrignore")
  write_union(proj_path(".covrignore"), files)
}

use_codecov_badge <- function(repo_spec) {
  url <- glue("https://app.codecov.io/gh/{repo_spec}")
  img <- glue("https://codecov.io/gh/{repo_spec}/graph/badge.svg")
  use_badge("Codecov test coverage", url, img)
}

use_coveralls_badge <- function(repo_spec) {
  default_branch <- git_default_branch()
  url <- glue("https://coveralls.io/r/{repo_spec}?branch={default_branch}")
  img <- glue("https://coveralls.io/repos/github/{repo_spec}/badge.svg")
  use_badge("Coveralls test coverage", url, img)
}