Skip to content

Commit

Permalink
Let use_package() decrement a package dependency. (#2043)
Browse files Browse the repository at this point in the history
* Let `use_package()` decrement a package dependency. Fixes #1957.

* Using if () ... else () instead of ifelse() on non-vectorized operation

* Style

* Simplify glue string

* Credit @jplecavalier for the PR

---------

Co-authored-by: Jenny Bryan <[email protected]>
  • Loading branch information
jplecavalier and jennybc authored Aug 15, 2024
1 parent 4ce704b commit a225a21
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).

Expand Down
27 changes: 15 additions & 12 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -96,6 +98,7 @@ r_version <- function() {
}

version_spec <- function(x) {
if (x == "*") x <- "0"
x <- gsub("(<=|<|>=|>|==)\\s*", "", x)
numeric_version(x)
}
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/_snaps/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a225a21

Please sign in to comment.