From be8e6b6ae87642c157c5ed6510076ba37e1bc0ed Mon Sep 17 00:00:00 2001 From: Jim Hester Date: Thu, 29 Mar 2018 12:00:46 -0400 Subject: [PATCH] Add skip_if_offline 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. --- DESCRIPTION | 1 + NAMESPACE | 1 + NEWS.md | 5 +++++ R/skip.R | 14 ++++++++++++++ man/skip.Rd | 8 ++++++++ 5 files changed, 29 insertions(+) diff --git a/DESCRIPTION b/DESCRIPTION index d69c37d05..2acd04001 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -27,6 +27,7 @@ Imports: withr (>= 2.0.0) Suggests: covr, + curl (>= 0.9.5), devtools, knitr, rmarkdown, diff --git a/NAMESPACE b/NAMESPACE index d549c6daa..b01386f5a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/NEWS.md b/NEWS.md index 3099fd28d..616909221 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/skip.R b/R/skip.R index 0392dde4a..d378222ce 100644 --- a/R/skip.R +++ b/R/skip.R @@ -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. #' @@ -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") @@ -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() { diff --git a/man/skip.Rd b/man/skip.Rd index b5b86a4b3..7997fca7a 100644 --- a/man/skip.Rd +++ b/man/skip.Rd @@ -5,6 +5,7 @@ \alias{skip_if_not} \alias{skip_if} \alias{skip_if_not_installed} +\alias{skip_if_offline} \alias{skip_on_cran} \alias{skip_on_os} \alias{skip_on_travis} @@ -21,6 +22,8 @@ skip_if(condition, message = deparse(substitute(condition))) skip_if_not_installed(pkg, minimum_version = NULL) +skip_if_offline(host = "r-project.org") + skip_on_cran() skip_on_os(os) @@ -43,6 +46,8 @@ skip_if_translated() \item{minimum_version}{Minimum required version for the package} +\item{host}{A string with a hostname to lookup} + \item{os}{Character vector of system names. Supported values are \code{"windows"}, \code{"mac"}, \code{"linux"} and \code{"solaris"}.} } @@ -63,6 +68,9 @@ statement, not how many expectations were skipped. \code{skip_if_not()} works like \code{\link[=stopifnot]{stopifnot()}}, generating a message automatically based on the first argument. +\code{skip_if_offline()} skips tests if an internet connection is not available +using \code{\link[curl:nslookup]{curl::nslookup()}}. + \code{skip_on_cran()} skips tests on CRAN, using the \code{NOT_CRAN} environment variable set by devtools.