diff --git a/NEWS.md b/NEWS.md index 550de611e..ac2aff12f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # testthat (development version) +* `skip_if_not_installed()` produces a clearer message (@MichaelChirico, #1959). * `with_mock()` and `local_mock()` have been unconditionally deprecated as they will no longer work in future versions of R (#1999). * `expect_condition()` and friends now include the `class` of the expected condition in the failure mesage, if used (#1987). * `LANGUAGE` is now set to `"C"` in reprocucible environments (i.e. diff --git a/R/skip.R b/R/skip.R index 4d6e693b7..6dba4885b 100644 --- a/R/skip.R +++ b/R/skip.R @@ -100,8 +100,14 @@ skip_if <- function(condition, message = NULL) { #' @param minimum_version Minimum required version for the package #' @rdname skip skip_if_not_installed <- function(pkg, minimum_version = NULL) { + # most common case: it's not installed + tryCatch( + find.package(pkg), + error = function(e) skip(paste0("{", pkg, "} is not installed")) + ) + # rarer: it's installed, but fails to load if (!requireNamespace(pkg, quietly = TRUE)) { - skip(paste0(pkg, " cannot be loaded")) + skip(paste0("{", pkg, "} cannot be loaded")) } if (!is.null(minimum_version)) { diff --git a/tests/testthat/_snaps/skip.md b/tests/testthat/_snaps/skip.md index 85b627712..4756a3cbb 100644 --- a/tests/testthat/_snaps/skip.md +++ b/tests/testthat/_snaps/skip.md @@ -26,7 +26,7 @@ # skip_if_not_installed() works as expected - Reason: doesntexist cannot be loaded + Reason: {doesntexist} is not installed ---