Skip to content

Commit 1ae30a1

Browse files
authored
Minor improvement to installed package checks (#1348)
Fixes #1332
1 parent d979e2f commit 1ae30a1

File tree

11 files changed

+22
-23
lines changed

11 files changed

+22
-23
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Imports:
3737
lifecycle,
3838
purrr,
3939
rappdirs,
40-
rlang (>= 0.4.3),
40+
rlang (>= 0.4.10),
4141
rprojroot (>= 1.2),
4242
rstudioapi,
4343
stats,

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# usethis (development version)
22

3+
* All functions that require a package now ask you if you'd like to install it.
4+
35
`use_readme_rmd()`, `use_readme_md()`, `use_tidy_contributing()`, and `use_tidy_support()` use updated logic for determining the `OWNER/REPO` spec of the target repo (#1312).
46

57
# usethis 2.0.0

R/helpers.R

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@ use_dependency <- function(package, type, min_version = NULL) {
2727
stopifnot(is_string(package))
2828
stopifnot(is_string(type))
2929

30-
if (package != "R" && !is_installed(package)) {
31-
ui_stop(c(
32-
"{ui_value(package)} must be installed before you can ",
33-
"take a dependency on it."
34-
))
30+
if (package != "R") {
31+
check_installed(package)
3532
}
3633

3734
if (isTRUE(min_version)) {

R/roxygen.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ use_roxygen_md <- function() {
2121
if (!uses_git()) {
2222
ui_todo("Use git to ensure that you don't lose any data")
2323
}
24+
25+
check_installed("roxygen2md")
2426
ui_todo(
25-
"Run {ui_code('roxygen2md::roxygen2md()')} to convert existing Rd markdown to RMarkdown"
27+
"Run {ui_code('roxygen2md::roxygen2md()')} to convert existing Rd commands to RMarkdown"
2628
)
2729
ui_todo("Run {ui_code('devtools::document()')} when you're done.")
2830
}

R/utils.R

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,12 @@ compact <- function(x) {
4444
x[!is_empty]
4545
}
4646

47-
check_installed <- function(pkg) {
48-
if (!is_installed(pkg)) {
49-
ui_stop("Package {ui_value(pkg)} required. Please install before re-trying.")
50-
}
51-
}
52-
47+
# Needed for mocking
5348
is_installed <- function(pkg) {
54-
requireNamespace(pkg, quietly = TRUE)
49+
rlang::is_installed(pkg)
50+
}
51+
check_installed <- function(pkg) {
52+
rlang::check_installed(pkg)
5553
}
5654

5755
interactive <- function() {

tests/testthat/test-cpp11.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test_that("use_cpp11() creates files/dirs, edits DESCRIPTION and .gitignore", {
1010
local_interactive(FALSE)
1111
with_mock(
1212
# Required to pass the check re: whether cpp11 is installed
13-
is_installed = function(pkg) TRUE,
13+
check_installed = function(pkg) TRUE,
1414
check_cpp_register_deps = function() invisible(),
1515
use_cpp11()
1616
)

tests/testthat/test-data-table.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ test_that("use_data_table() requires a package", {
66
test_that("use_data_table() Imports data.table", {
77
create_local_package()
88
with_mock(
9-
is_installed = function(pkg) TRUE,
9+
check_installed = function(pkg) TRUE,
1010
use_data_table()
1111
)
1212
expect_match(desc::desc_get("Imports"), "data.table")

tests/testthat/test-lifecycle.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
test_that("use_lifecycle() imports badges", {
22
create_local_package()
33
with_mock(
4-
is_installed = function(pkg) TRUE,
4+
check_installed = function(pkg) TRUE,
55
use_lifecycle()
66
)
77
expect_proj_file("man", "figures", "lifecycle-stable.svg")

tests/testthat/test-pkgdown.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ test_that("use_pkgdown() creates and ignores the promised file/dir", {
77
create_local_package()
88
local_interactive(FALSE)
99
with_mock(
10-
is_installed = function(pkg) TRUE,
10+
check_installed = function(pkg) TRUE,
1111
use_pkgdown()
1212
)
1313
expect_true(uses_pkgdown())
@@ -28,7 +28,7 @@ test_that("pkgdown_config_meta() returns a list", {
2828
create_local_package()
2929
local_interactive(FALSE)
3030
with_mock(
31-
is_installed = function(pkg) TRUE,
31+
check_installed = function(pkg) TRUE,
3232
use_pkgdown()
3333
)
3434
expect_equal(pkgdown_config_meta(), list())
@@ -43,7 +43,7 @@ test_that("pkgdown_url() returns correct data, warns if pedantic", {
4343
create_local_package()
4444
local_interactive(FALSE)
4545
with_mock(
46-
is_installed = function(pkg) TRUE,
46+
check_installed = function(pkg) TRUE,
4747
use_pkgdown()
4848
)
4949

tests/testthat/test-rcpp.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test_that("use_rcpp_armadillo() creates Makevars files and edits DESCRIPTION", {
2323
local_interactive(FALSE)
2424
with_mock(
2525
# Required to pass the check re: whether RcppArmadillo is installed
26-
is_installed = function(pkg) TRUE,
26+
check_installed = function(pkg) TRUE,
2727
use_rcpp_armadillo()
2828
)
2929
expect_match(desc::desc_get("LinkingTo"), "RcppArmadillo")
@@ -37,7 +37,7 @@ test_that("use_rcpp_eigen() edits DESCRIPTION", {
3737

3838
with_mock(
3939
# Required to pass the check re: whether RcppEigen is installed
40-
is_installed = function(pkg) TRUE,
40+
check_installed = function(pkg) TRUE,
4141
use_rcpp_eigen()
4242
)
4343
expect_match(desc::desc_get("LinkingTo"), "RcppEigen")

0 commit comments

Comments
 (0)