Skip to content

Commit

Permalink
Add skip_if_offline
Browse files Browse the repository at this point in the history
This is useful for packages which test against online APIs. We use
`curl::nslookup()` rather than `utils::nsl()` because the latter is not
available on Windows.
  • Loading branch information
jimhester committed Mar 29, 2018
1 parent 25d3325 commit be8e6b6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Imports:
withr (>= 2.0.0)
Suggests:
covr,
curl (>= 0.9.5),
devtools,
knitr,
rmarkdown,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export(skip)
export(skip_if)
export(skip_if_not)
export(skip_if_not_installed)
export(skip_if_offline)
export(skip_if_translated)
export(skip_on_appveyor)
export(skip_on_bioc)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# testthat 2.0.0.9000

## New and improved skips

* `skip_if_offline()` skips tests if an internet connection is not available
(#685).

## Minor improvements and bug fixes

* `expect_equal_to_reference` `update` parameter default value restored to
Expand Down
14 changes: 14 additions & 0 deletions R/skip.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#' `skip_if_not()` works like [stopifnot()], generating
#' a message automatically based on the first argument.
#'
#' `skip_if_offline()` skips tests if an internet connection is not available
#' using [curl::nslookup()].
#'
#' `skip_on_cran()` skips tests on CRAN, using the `NOT_CRAN`
#' environment variable set by devtools.
#'
Expand All @@ -31,6 +34,7 @@
#' a side effect, because the package is likely to be used anyway.
#'
#' @param message A message describing why the test was skipped.
#' @param host A string with a hostname to lookup
#' @export
#' @examples
#' if (FALSE) skip("No internet connection")
Expand Down Expand Up @@ -98,6 +102,16 @@ skip_if_not_installed <- function(pkg, minimum_version = NULL) {
return(invisible(TRUE))
}

#' @export
#' @rdname skip
skip_if_offline <- function(host = "r-project.org") {
skip_if_not_installed("curl")
has_internet <- !is.null(curl::nslookup(host, error = FALSE))
if (!has_internet) {
skip("offline")
}
}

#' @export
#' @rdname skip
skip_on_cran <- function() {
Expand Down
8 changes: 8 additions & 0 deletions man/skip.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit be8e6b6

Please sign in to comment.