Skip to content

Commit c843a3b

Browse files
Reinforce that skip is due to a package (#1959)
1 parent 0d1d1a3 commit c843a3b

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# testthat (development version)
22

3+
* `skip_if_not_installed()` produces a clearer message (@MichaelChirico, #1959).
34
* `with_mock()` and `local_mock()` have been unconditionally deprecated as they will no longer work in future versions of R (#1999).
45
* `expect_condition()` and friends now include the `class` of the expected condition in the failure mesage, if used (#1987).
56
* `LANGUAGE` is now set to `"C"` in reprocucible environments (i.e.

R/skip.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,14 @@ skip_if <- function(condition, message = NULL) {
100100
#' @param minimum_version Minimum required version for the package
101101
#' @rdname skip
102102
skip_if_not_installed <- function(pkg, minimum_version = NULL) {
103+
# most common case: it's not installed
104+
tryCatch(
105+
find.package(pkg),
106+
error = function(e) skip(paste0("{", pkg, "} is not installed"))
107+
)
108+
# rarer: it's installed, but fails to load
103109
if (!requireNamespace(pkg, quietly = TRUE)) {
104-
skip(paste0(pkg, " cannot be loaded"))
110+
skip(paste0("{", pkg, "} cannot be loaded"))
105111
}
106112

107113
if (!is.null(minimum_version)) {

tests/testthat/_snaps/skip.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
# skip_if_not_installed() works as expected
2828

29-
Reason: doesntexist cannot be loaded
29+
Reason: {doesntexist} is not installed
3030

3131
---
3232

0 commit comments

Comments
 (0)