diff --git a/NEWS.md b/NEWS.md index b06bd788c..311e2707d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,13 @@ # usethis (development version) +## Bug fixes and minor improvements + +* `use_package()` now decreases a package minimum version required when + `min_version` is lower than what is currently specified in the DESCRIPTION + file (@jplecavalier, #1957). + * `use_data()` now uses serialization version 3 by default. (@laurabrianna, #2044) + * Reverse dependency checks are only suggested if they exist (#1817, @seankross). diff --git a/R/helpers.R b/R/helpers.R index edbdbdd87..d09d28278 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -62,19 +62,21 @@ use_dependency <- function(package, type, min_version = NULL) { "!" = "Package {.pkg {package}} is already listed in {.field {existing_type}} in DESCRIPTION; no change made." )) - } else if (delta == 0 && !is.null(min_version)) { - # change version - upgrade <- existing_version == "*" || - numeric_version(min_version) > version_spec(existing_version) - if (upgrade) { - ui_bullets(c( - "v" = "Increasing {.pkg {package}} version to {.val {version}} in - DESCRIPTION." - )) - desc$set_dep(package, type, version = version) - desc$write() - changed <- TRUE + } else if (delta == 0 && version_spec(version) != version_spec(existing_version)) { + if (version_spec(version) > version_spec(existing_version)) { + direction <- "Increasing" + } else { + direction <- "Decreasing" } + + ui_bullets(c( + "v" = "{direction} {.pkg {package}} version to {.val {version}} in + DESCRIPTION." + )) + desc$set_dep(package, type, version = version) + desc$write() + changed <- TRUE + } else if (delta > 0) { # moving from, e.g., Suggests to Imports ui_bullets(c( @@ -96,6 +98,7 @@ r_version <- function() { } version_spec <- function(x) { + if (x == "*") x <- "0" x <- gsub("(<=|<|>=|>|==)\\s*", "", x) numeric_version(x) } diff --git a/tests/testthat/_snaps/helpers.md b/tests/testthat/_snaps/helpers.md index 807c258c3..d5fc01a0b 100644 --- a/tests/testthat/_snaps/helpers.md +++ b/tests/testthat/_snaps/helpers.md @@ -26,6 +26,13 @@ Message v Increasing crayon version to ">= 2.0.0" in DESCRIPTION. +--- + + Code + use_dependency("crayon", "Imports", min_version = "1.0.0") + Message + v Decreasing crayon version to ">= 1.0.0" in DESCRIPTION. + # use_dependency() upgrades a dependency Code diff --git a/tests/testthat/test-helpers.R b/tests/testthat/test-helpers.R index 3b063325d..d408b77fd 100644 --- a/tests/testthat/test-helpers.R +++ b/tests/testthat/test-helpers.R @@ -52,7 +52,9 @@ test_that("we message for version change and are silent for same version", { expect_snapshot( use_dependency("crayon", "Imports", min_version = "2.0.0") ) - expect_silent(use_dependency("crayon", "Imports", min_version = "1.0.0")) + expect_snapshot( + use_dependency("crayon", "Imports", min_version = "1.0.0") + ) }) ## https://github.com/r-lib/usethis/issues/99