Skip to content

Commit c3c0f5b

Browse files
committed
Account for repeated requests for a LinkingTo dependency
Closes #1648, closes #1329
1 parent 9511265 commit c3c0f5b

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

R/helpers.R

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ use_dependency <- function(package, type, min_version = NULL) {
3434

3535
changed <- FALSE
3636

37-
# No existing dependency, so can simply add
37+
# One of:
38+
# * No existing dependency
39+
# * Adding existing non-LinkingTo dependency to LinkingTo
40+
# * New use of a LinkingTo package as a non-LinkingTo dependency
41+
# In all cases, we can can simply make the change.
3842
if (!any(existing_dep) || any(is_linking_to)) {
3943
ui_done("Adding {ui_value(package)} to {ui_field(type)} field in DESCRIPTION")
4044
desc$set_dep(package, type, version = version)
@@ -43,6 +47,16 @@ use_dependency <- function(package, type, min_version = NULL) {
4347
return(invisible(changed))
4448
}
4549

50+
# Request to add a dependency that is already in LinkingTo and only in
51+
# LinkingTo as a LinkingTo dependency --> no need to do anything.
52+
if (identical(existing_type, "LinkingTo") && type == "LinkingTo") {
53+
ui_done(
54+
"Package {ui_value(package)} is already listed in \\
55+
{ui_value('LinkingTo')} in DESCRIPTION, no change made."
56+
)
57+
return(invisible(changed))
58+
}
59+
4660
existing_type <- setdiff(existing_type, "LinkingTo")
4761
delta <- sign(match(existing_type, types) - match(type, types))
4862
if (delta < 0) {

tests/testthat/_snaps/package.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,10 @@
6060
* In your package code, use `rlang::is_installed("purrr")` or `rlang::check_installed("purrr")` to test if purrr is installed
6161
* Then directly refer to functions with `purrr::fun()`
6262

63+
# use_package() does not fall over on 2nd LinkingTo request
64+
65+
Code
66+
use_dependency("rlang", "LinkingTo")
67+
Message
68+
v Package 'rlang' is already listed in 'LinkingTo' in DESCRIPTION, no change made.
69+

tests/testthat/test-package.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ test_that("use_package(type = 'Suggests') guidance w/o and w/ rlang", {
4040
expect_snapshot(use_package("purrr", "Suggests"))
4141
})
4242

43+
test_that("use_package() does not fall over on 2nd LinkingTo request", {
44+
local_edition(3)
45+
create_local_package()
46+
local_interactive(FALSE)
47+
48+
use_dependency("rlang", "LinkingTo")
49+
50+
withr::local_options(list(usethis.quiet = FALSE))
51+
52+
expect_snapshot(use_dependency("rlang", "LinkingTo"))
53+
})
54+
4355
# use_dev_package() -----------------------------------------------------------
4456

4557
test_that("use_dev_package() writes a remote", {

0 commit comments

Comments
 (0)