From 272e932db7d50b7a1b93b7edafe4df6731197a48 Mon Sep 17 00:00:00 2001 From: Olivier Roy Date: Fri, 25 Oct 2024 15:22:32 -0400 Subject: [PATCH] Add `allow_decrease = FALSE` for certain operations of usethis --- R/data.R | 4 ++-- R/helpers.R | 23 +++++++++++++++-------- R/test.R | 2 +- R/use_standalone.R | 2 +- tests/testthat/test-package.R | 8 ++++++++ 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/R/data.R b/R/data.R index 5003c0c77..41ba8d7e0 100644 --- a/R/data.R +++ b/R/data.R @@ -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") diff --git a/R/helpers.R b/R/helpers.R index d09d28278..403e061ea 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -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) @@ -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 diff --git a/R/test.R b/R/test.R index 057217b2b..2feb5c5d9 100644 --- a/R/test.R +++ b/R/test.R @@ -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) { diff --git a/R/use_standalone.R b/R/use_standalone.R index 11ca47e12..ec7bd877c 100644 --- a/R/use_standalone.R +++ b/R/use_standalone.R @@ -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) ) } diff --git a/tests/testthat/test-package.R b/tests/testthat/test-package.R index db4b44e52..405a2e3e5 100644 --- a/tests/testthat/test-package.R +++ b/tests/testthat/test-package.R @@ -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", {