Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .lintr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
linters: all_linters(
backport_linter("3.6.0", except = c("R_user_dir", "deparse1", "...names")),
backport_linter("auto", except = c("R_user_dir", "deparse1", "...names")),
line_length_linter(120L),
object_overwrite_linter(allow_names = c("line", "lines", "pipe", "symbols")),
todo_comment_linter(
Expand Down
19 changes: 15 additions & 4 deletions R/backport_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
#'
#' Check for usage of unavailable functions. Not reliable for testing r-devel dependencies.
#'
#' @param r_version Minimum R version to test for compatibility. Defaults to
#' the R version currently in use. The version can be specified as a version
#' number, or as a version alias (such as `"devel"`, `"oldrel"`, `"oldrel-1"`).
#' @param r_version Minimum R version to test for compatibility. The version
#' can be specified as a version number, or as a version alias (such as `"devel"`,
#' `"oldrel"`, `"oldrel-1"`). It can also be `"auto"` (the default) to use the minimum R version

Check warning on line 7 in R/backport_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=R/backport_linter.R,line=7,col=98,[trailing_whitespace_linter] Remove trailing whitespace.
#' declared in the `DESCRIPTION` file of R packages, and the R version in the current
#' R session outside of packages.
#' @param except Character vector of functions to be excluded from linting.
#' Use this to list explicitly defined backports, e.g. those imported from the `{backports}` package or manually
#' defined in your package.
Expand Down Expand Up @@ -46,7 +48,7 @@
#' @evalRd rd_tags("backport_linter")
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
backport_linter <- function(r_version = getRversion(), except = character()) {
backport_linter <- function(r_version = "auto", except = character()) {
r_version <- normalize_r_version(r_version)

if (all(r_version >= R_system_version(names(backports)))) {
Expand Down Expand Up @@ -120,6 +122,15 @@
))

r_version <- R_system_version(available_patches[selected_patch])
} else if (identical(r_version, "auto")) {
r_version <- min_r_version()
if (is.na(r_version)) {
cli_warn(c(
x = "No DESCRIPTION file or no specified minimum R version could be found.",
i = "Setting {.arg r_version} to the currently running R version (R {getRversion()})."
))
r_version <- getRversion()
}
} else if (is.character(r_version)) {
r_version <- R_system_version(r_version, strict = TRUE)
} else if (!inherits(r_version, "R_system_version")) {
Expand Down
20 changes: 20 additions & 0 deletions R/settings_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,23 @@ pkg_name <- function(path = find_package()) {
nm <- read.dcf(file.path(path, "DESCRIPTION"), fields = "Package")[1L]
if (!is.na(nm)) nm
}

min_r_version <- function(path = find_package(".")) {
if (is.null(path)) {
return(NA_character_)
}
depends_field <- read.dcf(file.path(path, "DESCRIPTION"), fields = "Depends")

depends <- trimws(strsplit(depends_field, ",", fixed = TRUE)[[1L]])

min_r_ver <- gsub("^R \\(>=?\\s(.+)\\)", "\\1", grep("R ", depends, value = TRUE, fixed = TRUE))

if (length(min_r_ver) == 0L) {
return(NA_character_)
}
# According to 'Writing R Extensions', the trailing 0 for patch version can
# be dropped.
# But we want to identically match an existing version number so we add it if
# it's missing.
gsub("^(\\d+\\.\\d+)$", "\\1.0", min_r_ver)
}
10 changes: 6 additions & 4 deletions man/backport_linter.Rd

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

3 changes: 3 additions & 0 deletions tests/testthat/dummy_packages/auto_backport_fail/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Package: mypkg
Version: 0.1.0
Depends: R (>= 4.0.0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
my_numtobits <- function() {
numToBits(2)
}
3 changes: 3 additions & 0 deletions tests/testthat/dummy_packages/auto_backport_pass/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Package: mypkg
Version: 0.1.0
Depends: R (>= 4.2.0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
my_numtobits <- function() {
numToBits(2)
}
46 changes: 45 additions & 1 deletion tests/testthat/test-backport_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
expect_no_lint(".getNamespaceInfo(dir.exists(lapply(x, toTitleCase)))", backport_linter())
expect_no_lint(".getNamespaceInfo(dir.exists(lapply(x, toTitleCase)))", backport_linter("release"))
expect_no_lint(".getNamespaceInfo(dir.exists(lapply(x, toTitleCase)))", backport_linter("devel"))

expect_no_lint(".getNamespaceInfo(dir.exists(lapply(x, toTitleCase)))", backport_linter("auto"))

Check warning on line 14 in tests/testthat/test-backport_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-backport_linter.R,line=14,col=1,[trailing_whitespace_linter] Remove trailing whitespace.
expect_lint(
"numToBits(2)",
rex::rex("numToBits (R 4.1.0) is not always available for requested dependency (R >= 4.0.0)."),
Expand Down Expand Up @@ -82,3 +83,46 @@
)
expect_identical(l, lint(tmp, backport_linter("3.0.0")))
})

test_that("backport_linter works with package R version", {
pkg_lints_from_outside <- lint_package(
"dummy_packages/auto_backport_fail",
linters = backport_linter(),
parse_settings = FALSE
)
pkg_lints_from_root <- withr::with_dir(
"dummy_packages/auto_backport_fail",
lint_package(linters = backport_linter(), parse_settings = FALSE)
)

expect_length(pkg_lints_from_outside, 1L)
expect_identical(
pkg_lints_from_outside[[1]]$message,

Check warning on line 100 in tests/testthat/test-backport_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-backport_linter.R,line=100,col=30,[implicit_integer_linter] Use 1L or 1.0 to avoid implicit integers.
"numToBits (R 4.1.0) is not always available for requested dependency (R >= 4.0.0)."
)
expect_length(pkg_lints_from_root, 1L)
expect_identical(
pkg_lints_from_root[[1]]$message,

Check warning on line 105 in tests/testthat/test-backport_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-backport_linter.R,line=105,col=27,[implicit_integer_linter] Use 1L or 1.0 to avoid implicit integers.
"numToBits (R 4.1.0) is not always available for requested dependency (R >= 4.0.0)."
)

# Can be overwritten
pkg_lints_from_outside_noauto <- lint_package(
"dummy_packages/auto_backport_fail",

Check warning on line 111 in tests/testthat/test-backport_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-backport_linter.R,line=111,col=41,[trailing_whitespace_linter] Remove trailing whitespace.
backport_linter("3.0.0"),
parse_settings = FALSE
)

expect_length(pkg_lints_from_outside_noauto, 1L)
expect_identical(
pkg_lints_from_outside_noauto[[1]]$message,

Check warning on line 118 in tests/testthat/test-backport_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-backport_linter.R,line=118,col=37,[implicit_integer_linter] Use 1L or 1.0 to avoid implicit integers.
"numToBits (R 4.1.0) is not always available for requested dependency (R >= 3.0.0)."
)

pkg_lints_auto_pass <- lint_package(
"dummy_packages/auto_backport_pass",
linters = backport_linter(),
parse_settings = FALSE
)
expect_length(pkg_lints_auto_pass, 0L)
})
Loading