Skip to content

Commit

Permalink
Add allow_decrease = FALSE for certain operations of usethis
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy committed Oct 25, 2024
1 parent c63c6e8 commit 272e932
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
4 changes: 2 additions & 2 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ use_data <- function(...,
objs <- get_objs_from_dots(dots(...))

if (version < 3) {
use_dependency("R", "depends", "2.10")
use_dependency("R", "depends", "2.10", allow_decrease = FALSE)
} else {
use_dependency("R", "depends", "3.5")
use_dependency("R", "depends", "3.5", allow_decrease = FALSE)
}
if (internal) {
use_directory("R")
Expand Down
23 changes: 15 additions & 8 deletions R/helpers.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use_dependency <- function(package, type, min_version = NULL) {
use_dependency <- function(package, type, min_version = NULL, allow_decrease = TRUE) {
check_name(package)
check_name(type)
check_bool(allow_decrease)

if (package != "R") {
check_installed(package)
Expand Down Expand Up @@ -65,17 +66,23 @@ use_dependency <- function(package, type, min_version = NULL) {
} else if (delta == 0 && version_spec(version) != version_spec(existing_version)) {
if (version_spec(version) > version_spec(existing_version)) {
direction <- "Increasing"
} else {
} else if (allow_decrease) {
direction <- "Decreasing"
} else {
direction <- NULL
}

ui_bullets(c(
"v" = "{direction} {.pkg {package}} version to {.val {version}} in
if (!is.null(direction)) {
ui_bullets(c(
"v" = "{direction} {.pkg {package}} version to {.val {version}} in
DESCRIPTION."
))
desc$set_dep(package, type, version = version)
desc$write()
changed <- TRUE
))
desc$set_dep(package, type, version = version)
desc$write()
changed <- TRUE
} else {
changed <- FALSE
}

} else if (delta > 0) {
# moving from, e.g., Suggests to Imports
Expand Down
2 changes: 1 addition & 1 deletion R/test.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use_testthat_impl <- function(edition = NULL, parallel = FALSE) {
if (is_package()) {
edition <- check_edition(edition)

use_dependency("testthat", "Suggests", paste0(edition, ".0.0"))
use_dependency("testthat", "Suggests", paste0(edition, ".0.0"), allow_decrease = FALSE)
proj_desc_field_update("Config/testthat/edition", as.character(edition), overwrite = TRUE)

if (parallel) {
Expand Down
2 changes: 1 addition & 1 deletion R/use_standalone.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ use_standalone <- function(repo_spec, file = NULL, ref = NULL, host = NULL) {
ver <- import$ver
}
ui_silence(
use_package(import$pkg, min_version = ver)
use_dependency(import$pkg, "Imports", min_version = ver, allow_decrease = FALSE)
)
}

Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ test_that("use_package(type = 'Suggests') guidance w/o and w/ rlang", {
expect_snapshot(use_package("purrr", "Suggests"))
})

test_that("use_data() will not decrease R version if called internally.", {
create_local_package()
# Use a minimum R version
use_package("R", "Depends", "4.1")
use_dependency("R", "Depends", "3.6", allow_decrease = FALSE)
expect_equal(subset(proj_deps(), package == "R")$version, ">= 4.1")
})

# use_dev_package() -----------------------------------------------------------

test_that("use_dev_package() writes a remote", {
Expand Down

0 comments on commit 272e932

Please sign in to comment.