Skip to content

Commit

Permalink
Reinforce that skip is due to a package (#1959)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico authored Oct 29, 2024
1 parent 0d1d1a3 commit c843a3b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
8 changes: 7 additions & 1 deletion R/skip.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/skip.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

# skip_if_not_installed() works as expected

Reason: doesntexist cannot be loaded
Reason: {doesntexist} is not installed

---

Expand Down

0 comments on commit c843a3b

Please sign in to comment.