Skip to content

Commit fdb001c

Browse files
Move the cyclocomp namespace check into the factory (#2785)
1 parent 52aeb34 commit fdb001c

File tree

8 files changed

+34
-10
lines changed

8 files changed

+34
-10
lines changed

R/cyclocomp_linter.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222
#' @seealso [linters] for a complete list of linters available in lintr.
2323
#' @export
2424
cyclocomp_linter <- function(complexity_limit = 15L) {
25+
# nocov start
26+
if (!requireNamespace("cyclocomp", quietly = TRUE)) {
27+
cli::cli_warn(c(
28+
"Cyclocomp complexity is computed using {.fn cyclocomp::cyclocomp}.",
29+
i = "Please install the needed {.pkg cyclocomp} package."
30+
))
31+
return(Linter(function(.) cli_abort("cyclocomp_linter is disabled due to lack of the {.pkg cyclocomp} package")))
32+
}
33+
# nocov end
2534
Linter(linter_level = "expression", function(source_expression) {
26-
# nocov start
27-
if (!requireNamespace("cyclocomp", quietly = TRUE)) {
28-
cli::cli_abort(c(
29-
"Cyclocomp complexity is computed using {.fn cyclocomp::cyclocomp}.",
30-
i = "Please install the needed {.pkg cyclocomp} package."
31-
))
32-
}
33-
# nocov end
34-
3535
complexity <- try_silently(
3636
cyclocomp::cyclocomp(parse(text = source_expression$content))
3737
)

R/settings.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ read_config_file <- function(config_file, call = parent.frame()) {
157157
error = malformed
158158
),
159159
warning = function(w) {
160-
cli::cli_warn(
160+
cli_warn(
161161
"Warning encountered while loading config:",
162162
parent = w
163163
)

tests/testthat/test-cyclocomp_linter.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,15 @@ test_that("returns the correct linting", {
5959
cyclocomp_linter(5L)
6060
)
6161
})
62+
63+
test_that("a null linter is returned, with warning, if cyclocomp is unavailable", {
64+
# simple requireNamspace->FALSE won't work since expect_no_lint checks for testthat
65+
local_mocked_bindings(
66+
requireNamespace = function(pkg, ...) pkg != "cyclocomp" && base::requireNamespace(pkg, ...)
67+
)
68+
expect_warning(regexp = "Please install", fixed = TRUE, {
69+
linter <- cyclocomp_linter(1L)
70+
})
71+
72+
expect_error(lint(text = "if (TRUE) 1 else 2", linters = linter), "disabled", fixed = TRUE)
73+
})

tests/testthat/test-get_source_expressions.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ test_that("Multi-byte character truncated by parser is ignored", {
104104
})
105105

106106
test_that("Can read non UTF-8 file", {
107+
withr::local_options(lintr.linter_file = tempfile())
107108
file <- test_path("dummy_projects", "project", "cp1252.R")
108109
lintr:::read_settings(file)
109110
expect_null(get_source_expressions(file)$error)

tests/testthat/test-linter_tags.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ test_that("default_linters and default tag match up", {
5353
})
5454

5555
test_that("warnings occur only for deprecated linters", {
56+
skip_if_not_installed("cyclocomp") # actually we expect a warning there
57+
5658
expect_silent(linters_with_tags(tags = NULL))
5759
num_deprecated_linters <- nrow(available_linters(tags = "deprecated", exclude_tags = NULL))
5860
deprecation_warns_seen <- 0L

tests/testthat/test-settings.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ test_that("linters_with_defaults doesn't break on very long input", {
106106
})
107107

108108
test_that("it has a smart default for encodings", {
109+
withr::local_options(list(lintr.linter_file = tempfile()))
110+
109111
lintr:::read_settings(NULL)
110112
expect_identical(settings$encoding, "UTF-8")
111113

tests/testthat/test-use_lintr.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ test_that("use_lintr with type = full also works", {
3232
file.path(normalize_path(tmp), ".lintr")
3333
)
3434

35+
skip_if_not_installed("cyclocomp") # avoid warning
3536
lints <- lint_dir(tmp)
3637
expect_length(lints, 0L)
3738
})

tests/testthat/test-with.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ test_that("all default linters are tagged default", {
6464
})
6565

6666
test_that("can instantiate all linters without arguments", {
67+
skip_if_not_installed("cyclocomp") # avoid warning
68+
6769
all_linters <- linters_with_tags(tags = NULL)
6870

6971
expect_type(all_linters, "list")
@@ -100,13 +102,17 @@ test_that("linters_with_defaults(default = .) is supported with a deprecation wa
100102
})
101103

102104
test_that("all_linters contains all available linters", {
105+
skip_if_not_installed("cyclocomp") # avoid warning
106+
103107
all_linters <- all_linters(packages = "lintr")
104108

105109
expect_identical(linters_with_tags(NULL, packages = "lintr"), all_linters)
106110
expect_length(all_linters, nrow(available_linters()))
107111
})
108112

109113
test_that("all_linters respects ellipsis argument", {
114+
skip_if_not_installed("cyclocomp") # avoid warning
115+
110116
expect_identical(
111117
linters_with_tags(tags = NULL, implicit_integer_linter = NULL),
112118
all_linters(packages = "lintr", implicit_integer_linter = NULL)

0 commit comments

Comments
 (0)