Skip to content

Commit

Permalink
Add GHA workflow to compute code coverage via examples (#1785)
Browse files Browse the repository at this point in the history
* Create test-coverage-examples.yaml

closes #1675

* Update test-coverage-examples.yaml

* Update test-coverage-examples.yaml

* Update test-coverage-examples.yaml

* Update test-coverage-examples.yaml

* add a list of exclusions

* include observed coverage in examples as well

* show integer value

* more exceptions; one more example

* Update test-coverage-examples.yaml

* Update test-coverage-examples.yaml

* Update test-coverage-examples.yaml

* Update test-coverage-examples.yaml

* exclude exclude

* Update test-coverage-examples.yaml

* run on a monthly basis
  • Loading branch information
IndrajeetPatil authored May 21, 2024
1 parent e97be9b commit ea1685b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
71 changes: 71 additions & 0 deletions .github/workflows/test-coverage-examples.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
on:
schedule:
# * is a special character in YAML so you have to quote this string
# Trigger once a month at 10:00 on the first day of every month
- cron: "00 10 1 * *"

name: test-coverage-examples

jobs:
test-coverage-examples:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: |
any::covr
local::.
- name: Test example coverage
run: |
options(crayon.enabled = TRUE)
library(covr)
files_to_exclude <- c(
# examples present but not run
"R/lint.R",
"R/use_lintr.R",
# mostly internal utilities
"R/actions.R",
"R/cache.R",
"R/deprecated.R",
"R/exclude.R",
"R/extract.R",
"R/ids_with_token.R",
"R/lintr-deprecated.R",
"R/make_linter_from_regex.R",
"R/make_linter_from_xpath.R",
"R/namespace.R",
"R/methods.R",
"R/settings.R",
"R/shared_constants.R",
"R/with.R",
"R/with_id.R",
"R/zzz.R"
)
coverage <- covr::package_coverage(
type = "examples",
quiet = TRUE,
commentDonttest = FALSE,
commentDontrun = FALSE,
line_exclusions = files_to_exclude
)
print(coverage)
percent_coverage <- as.integer(covr::percent_coverage(coverage))
threshold <- 90
cli::cli_rule()
if (percent_coverage < threshold) {
cli::cli_abort("Code coverage using examples ({percent_coverage}%) is below the required threshold ({threshold}%).")
} else {
cli::cli_alert_success("Code coverage using examples ({percent_coverage}%) is above the required threshold ({threshold}%).")
}
cli::cli_rule()
shell: Rscript {0}
5 changes: 5 additions & 0 deletions R/backport_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
#' linters = backport_linter("4.0.0")
#' )
#'
#' lint(
#' text = "str2lang(x)",
#' linters = backport_linter("3.2.0", except = "str2lang")
#' )
#'
#' @evalRd rd_tags("backport_linter")
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
Expand Down
3 changes: 3 additions & 0 deletions R/exclude.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#' " a character vector of files to exclude or a vector of lines to exclude.",
#' NULL
#' )
#'
#' @keywords internal
exclude <- function(lints, exclusions = settings$exclusions, linter_names = NULL, ...) {
if (length(lints) <= 0L) {
return(lints)
Expand Down Expand Up @@ -100,6 +102,7 @@ line_info <- function(line_numbers, type = c("start", "end")) {
#' @param linter_names Names of active linters.
#'
#' @return A possibly named list of excluded lines, possibly for specific linters.
#' @keywords internal
parse_exclusions <- function(file,
exclude = settings$exclude,
exclude_next = settings$exclude_next,
Expand Down
5 changes: 5 additions & 0 deletions man/backport_linter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/exclude.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/parse_exclusions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ea1685b

Please sign in to comment.