Debian Codegraph @

tests/testthat/test-internal.R

raw
1
2 test_that("R files have balanced covr exclusions", {
3
4 renv_scope_wd("../..")
5
6 errors <- stack()
7
8 files <- list.files("R", pattern = "[.][rR]$", full.names = TRUE)
9 lapply(files, function(file) {
10
11 nocov <- FALSE
12 contents <- catch(readLines(file))
13 if (inherits(contents, "error")) {
14 bulletin(values = "[%s]: %s", file, conditionMessage(contents))
15 return()
16 }
17
18 for (i in seq_along(contents)) {
19 line <- contents[[i]]
20
21 if (grepl("#\\s+nocov\\s+start\\s*$", line)) {
22 if (nocov) {
23 errors$push(list(file, i, "# nocov start"))
24 } else {
25 nocov <- TRUE
26 }
27 }
28
29 if (grepl("#\\s+nocov\\s+end\\s*$", line)) {
30 if (nocov) {
31 nocov <- FALSE
32 } else {
33 errors$push(list(file, i, "# nocov end"))
34 }
35 }
36
37 }
38
39 })
40
41 invalid <- errors$data()
42 if (length(invalid)) {
43 lines <- map_chr(invalid, function(loc) {
44 fmt <- "[%s:%i]: unexpected '%s'"
45 sprintf(fmt, loc[[1]], loc[[2]], loc[[3]])
46 })
47 warning(lines)
48 }
49
50 expect_length(invalid, 0L)
51
52 })