Skip to content

Commit

Permalink
Skip test if offline
Browse files Browse the repository at this point in the history
  • Loading branch information
andrie committed Mar 16, 2017
1 parent 8753820 commit d9b269d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 18 deletions.
4 changes: 3 additions & 1 deletion R/mranUrl.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ stopIfInvalidDate <- function(snapshotDate, verbose = TRUE){
if(as.Date(snapshotDate) > Sys.Date())
stop("snapshotDate can not be in the future!", call. = FALSE)


validSnapshots <- tryCatch(as.Date(getValidSnapshots()), error=function(e)e)
if(inherits(validSnapshots, "error")){
mssg(verbose, "Unable to connect to MRAN. Skipping some date validations.")
Expand Down Expand Up @@ -76,6 +75,9 @@ setCheckpointUrl <- function(url){


tryUrl <- function(url){
timeout <- getOption("timeout")
on.exit(options(timeout = timeout))
options(timeout = 5)
con <- suppressWarnings(tryCatch(url(url), error = function(e)e))
msg <- paste0(
"Invalid value for mranRootUrl.\n",
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
is_online <- function(){
u <- tryCatch(url("https://mran.microsoft.com"),
error = function(e)e)
if(inherits(u, "error")){
u <- url("http://mran.microsoft.com")
}
on.exit(close(u))
z <- tryCatch(suppressWarnings(readLines(u, n = 1, warn = FALSE)),
error = function(e)e)
!inherits(z, "error")
}

skip_if_offline <- function(){
if(!is_online()) testthat::skip("Offline. Skipping test.")
}
1 change: 1 addition & 0 deletions tests/testthat/test-0-is-404.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ if(interactive()) library(testthat)
context("is.404")

test_that("is.404", {
skip_if_offline()
describe("is.404 works with http", {
it("works on http", {
expect_true(is.404("http://mran.microsoft.com/snapshot/1972-01-01", warn = FALSE))
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-0-snapshots.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
if(interactive()) library(testthat)

context("MRAN snapshots")
context("snapshots")

describe("Validate snapshotDate argument",{
it("stops if missing snapshotDate", {
Expand Down Expand Up @@ -34,6 +34,7 @@ describe("Validate snapshotDate argument",{

test_that("set http/https correctly", {
skip_on_cran()
skip_if_offline()
describe("set http/https correctly", {
it("resolves to http/https based on R version number", {
if(getRversion() >= "3.2.0" && httpsSupported()){
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-2-snapshot-dates.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ context("valid snapshot dates")
describe("getValidSnapshots finds valid dates", {

it("returns a list of dates", {
skip_if_offline()
d <- getValidSnapshots()
expect_is(d, "character")
expect_is(as.Date(d), "Date")
Expand All @@ -12,6 +13,7 @@ describe("getValidSnapshots finds valid dates", {

it("suggests a reasonable alternative", {
# On MRAN, 2015-06-04 to 2015-06-08 are missing
skip_if_offline()
expect_error(
stopIfInvalidDate("2015-06-05"),
"Snapshot does not exist on MRAN. Try 2015-06-03 or 2015-06-09."
Expand Down
38 changes: 22 additions & 16 deletions tests/testthat/test-3-checkpoint.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,26 +171,32 @@ test_checkpoint <- function(https = FALSE, snap.dates){

# ------------------------------------------------------------------------


MRAN.dates <- getValidSnapshots()
MRAN.sample <- sample(MRAN.dates, 2, replace = FALSE)

initialUrl <- getOption("checkpoint.mranUrl")

if(getRversion() >= "3.2.0" && httpsSupported()){
context("https")
if(is_online()){
MRAN.dates <- getValidSnapshots()
MRAN.sample <- sample(MRAN.dates, 2, replace = FALSE)

initialUrl <- getOption("checkpoint.mranUrl")

options(checkpoint.mranUrl = "https://mran.microsoft.com/")
test_checkpoint(http = TRUE, snap.dates = MRAN.default)
if(getRversion() >= "3.2.0" && httpsSupported()){
context("https")

options(checkpoint.mranUrl = "https://mran.microsoft.com/")
test_checkpoint(http = TRUE, snap.dates = MRAN.default)

options(checkpoint.mranUrl = NULL)
}

options(checkpoint.mranUrl = NULL)
context("http")
options(checkpoint.mranUrl = "http://mran.microsoft.com/")
test_checkpoint(http = FALSE, snap.dates = MRAN.default)
options(checkpoint.mranUrl = initialUrl)
} else {
context("https")
test_that("No test run in offline", {
skip_if_offline()
})
}

context("http")
options(checkpoint.mranUrl = "http://mran.microsoft.com/")
test_checkpoint(http = FALSE, snap.dates = MRAN.default)
options(checkpoint.mranUrl = initialUrl)




0 comments on commit d9b269d

Please sign in to comment.