Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the cyclocomp namespace check into the factory #2785

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
18 changes: 9 additions & 9 deletions R/cyclocomp_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
cyclocomp_linter <- function(complexity_limit = 15L) {
# nocov start
if (!requireNamespace("cyclocomp", quietly = TRUE)) {
cli::cli_warn(c(
"Cyclocomp complexity is computed using {.fn cyclocomp::cyclocomp}.",
i = "Please install the needed {.pkg cyclocomp} package."
))
return(Linter(function(.) cli_abort("cyclocomp_linter is disabled due to lack of {.pkg cyclocomp} package")))
}
# nocov end
Linter(linter_level = "expression", function(source_expression) {
# nocov start
if (!requireNamespace("cyclocomp", quietly = TRUE)) {
cli::cli_abort(c(
"Cyclocomp complexity is computed using {.fn cyclocomp::cyclocomp}.",
i = "Please install the needed {.pkg cyclocomp} package."
))
}
# nocov end

complexity <- try_silently(
cyclocomp::cyclocomp(parse(text = source_expression$content))
)
Expand Down
2 changes: 1 addition & 1 deletion R/settings.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ read_config_file <- function(config_file, call = parent.frame()) {
error = malformed
),
warning = function(w) {
cli::cli_warn(
cli_warn(
"Warning encountered while loading config:",
parent = w
)
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-cyclocomp_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,15 @@ test_that("returns the correct linting", {
cyclocomp_linter(5L)
)
})

test_that("a null linter is returned, with warning, if cyclocomp is unavailable", {
# simple requireNamspace->FALSE won't work since expect_no_lint checks for testthat
local_mocked_bindings(
requireNamespace = function(pkg, ...) pkg != "cyclocomp" && base::requireNamespace(pkg, ...)
)
expect_warning(regexp = "Please install", fixed = TRUE, {
linter <- cyclocomp_linter(1L)
})

expect_error(lint(text = "if (TRUE) 1 else 2", linters = linter), "disabled", fixed = TRUE)
})
1 change: 1 addition & 0 deletions tests/testthat/test-get_source_expressions.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ test_that("Multi-byte character truncated by parser is ignored", {
})

test_that("Can read non UTF-8 file", {
withr::local_options(lintr.linter_file = tempfile())
file <- test_path("dummy_projects", "project", "cp1252.R")
lintr:::read_settings(file)
expect_null(get_source_expressions(file)$error)
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-linter_tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ test_that("default_linters and default tag match up", {
})

test_that("warnings occur only for deprecated linters", {
skip_if_not_installed("cyclocomp") # actually we expect a warning there

expect_silent(linters_with_tags(tags = NULL))
num_deprecated_linters <- nrow(available_linters(tags = "deprecated", exclude_tags = NULL))
deprecation_warns_seen <- 0L
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-settings.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ test_that("linters_with_defaults doesn't break on very long input", {
})

test_that("it has a smart default for encodings", {
withr::local_options(list(lintr.linter_file = tempfile()))

lintr:::read_settings(NULL)
expect_identical(settings$encoding, "UTF-8")

Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-use_lintr.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ test_that("use_lintr with type = full also works", {
file.path(normalize_path(tmp), ".lintr")
)

skip_if_not_installed("cyclocomp") # avoid warning
lints <- lint_dir(tmp)
expect_length(lints, 0L)
})
6 changes: 6 additions & 0 deletions tests/testthat/test-with.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ test_that("all default linters are tagged default", {
})

test_that("can instantiate all linters without arguments", {
skip_if_not_installed("cyclocomp") # avoid warning

all_linters <- linters_with_tags(tags = NULL)

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

test_that("all_linters contains all available linters", {
skip_if_not_installed("cyclocomp") # avoid warning

all_linters <- all_linters(packages = "lintr")

expect_identical(linters_with_tags(NULL, packages = "lintr"), all_linters)
expect_length(all_linters, nrow(available_linters()))
})

test_that("all_linters respects ellipsis argument", {
skip_if_not_installed("cyclocomp") # avoid warning

expect_identical(
linters_with_tags(tags = NULL, implicit_integer_linter = NULL),
all_linters(packages = "lintr", implicit_integer_linter = NULL)
Expand Down
Loading