From faf89e3b511d84ef533a8e19429d353bdfd7fcbf Mon Sep 17 00:00:00 2001 From: Michael Mahoney Date: Mon, 12 Feb 2024 12:28:34 -0500 Subject: [PATCH] Skip tests, rendering if offline (#39) * Skip tests, rendering if offline * Fix check --- tests/testthat/_snaps/spectral_indices.md | 6 +----- tests/testthat/test-calculate_indices.R | 8 ++++---- tests/testthat/test-filters.R | 9 +++++---- tests/testthat/test-get_stac_data.R | 10 ++++++++++ tests/testthat/test-spectral_indices.R | 23 ++++++++++++++++++++++- vignettes/rsi.Rmd | 2 +- 6 files changed, 43 insertions(+), 15 deletions(-) diff --git a/tests/testthat/_snaps/spectral_indices.md b/tests/testthat/_snaps/spectral_indices.md index 85f2a92..e876392 100644 --- a/tests/testthat/_snaps/spectral_indices.md +++ b/tests/testthat/_snaps/spectral_indices.md @@ -1,11 +1,7 @@ # no cache, update false Code - spectral_indices(update_cache = FALSE) - Condition - Warning: - No cache file present and `download_indices` set to `FALSE`. - i Returning (likely outdated) package data instead. + suppressWarnings(spectral_indices(update_cache = FALSE)) Output # A tibble: 231 x 9 application_domain bands contributor date_of_addition formula long_name diff --git a/tests/testthat/test-calculate_indices.R b/tests/testthat/test-calculate_indices.R index dd72880..4465cf5 100644 --- a/tests/testthat/test-calculate_indices.R +++ b/tests/testthat/test-calculate_indices.R @@ -7,7 +7,7 @@ test_that("Index calculation is stable", { expect_no_error( out <- calculate_indices( system.file("rasters/example_sentinel1.tif", package = "rsi"), - filter_platforms(spectral_indices(download_indices = FALSE, update_cache = FALSE), platforms = "Sentinel-1 (Dual Polarisation VV-VH)"), + suppressWarnings(filter_platforms(spectral_indices(download_indices = FALSE, update_cache = FALSE), platforms = "Sentinel-1 (Dual Polarisation VV-VH)")), index_out, names_suffix = "sentinel1" ) @@ -27,7 +27,7 @@ test_that("Index calculations fail when missing a column", { expect_error( calculate_indices( system.file("rasters/example_sentinel1.tif", package = "rsi"), - filter_platforms(spectral_indices(download_indices = FALSE, update_cache = FALSE), platforms = "Sentinel-1 (Dual Polarisation VV-VH)")["formula"], + suppressWarnings(filter_platforms(spectral_indices(download_indices = FALSE, update_cache = FALSE), platforms = "Sentinel-1 (Dual Polarisation VV-VH)"))["formula"], index_out ), class = "rsi_missing_column" @@ -40,7 +40,7 @@ test_that("Index calculations fail when missing bands", { expect_error( calculate_indices( system.file("rasters/example_sentinel1.tif", package = "rsi"), - filter_platforms(spectral_indices(download_indices = FALSE, update_cache = FALSE), platforms = "Landsat-OLI"), + suppressWarnings(filter_platforms(spectral_indices(download_indices = FALSE, update_cache = FALSE), platforms = "Landsat-OLI")), index_out ), class = "rsi_missing_indices" @@ -49,7 +49,7 @@ test_that("Index calculations fail when missing bands", { test_that("Index calculations stop obvious security issues", { skip_on_cran() - example_indices <- filter_platforms(spectral_indices(download_indices = FALSE, update_cache = FALSE), platforms = "Sentinel-1 (Dual Polarisation VV-VH)")[1, ] + example_indices <- suppressWarnings(filter_platforms(spectral_indices(download_indices = FALSE, update_cache = FALSE), platforms = "Sentinel-1 (Dual Polarisation VV-VH)"))[1, ] example_indices$formula <- 'system("echo something bad")' expect_error(calculate_indices( system.file("rasters/example_sentinel1.tif", package = "rsi"), diff --git a/tests/testthat/test-filters.R b/tests/testthat/test-filters.R index 605ade7..3c34c56 100644 --- a/tests/testthat/test-filters.R +++ b/tests/testthat/test-filters.R @@ -8,7 +8,8 @@ test_that("filter_platforms() works", { test_plats <- c("Landsat-OLI", "Sentinel-2") expect_no_error( - both_plats <- filter_platforms(idxs, test_plats) + # no internet and no cache file == warning + both_plats <- suppressWarnings(filter_platforms(idxs, test_plats), classes = "rsi_failed_download") ) expect_true( @@ -18,7 +19,7 @@ test_that("filter_platforms() works", { ) expect_no_error( - either_plat <- filter_platforms(idxs, test_plats, operand = "any") + either_plat <- suppressWarnings(filter_platforms(idxs, test_plats, operand = "any"), classes = "rsi_failed_download") ) expect_true( @@ -38,7 +39,7 @@ test_that("filter_bands() works", { test_bands <- c("R", "N") expect_no_error( - both_bands <- filter_bands(idxs, test_bands) + both_bands <- suppressWarnings(filter_bands(idxs, test_bands), classes = "rsi_failed_download") ) expect_true( @@ -48,7 +49,7 @@ test_that("filter_bands() works", { ) expect_no_error( - either_band <- filter_bands(idxs, test_bands, operand = "any") + either_band <- suppressWarnings(filter_bands(idxs, test_bands, operand = "any"), classes = "rsi_failed_download") ) expect_true( diff --git a/tests/testthat/test-get_stac_data.R b/tests/testthat/test-get_stac_data.R index 3e847cf..46a9f9d 100644 --- a/tests/testthat/test-get_stac_data.R +++ b/tests/testthat/test-get_stac_data.R @@ -1,5 +1,6 @@ test_that("get_landsat_imagery() is stable", { skip_on_cran() + skip_if_offline() aoi <- sf::st_point(c(-74.912131, 44.080410)) aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326) aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100) @@ -23,6 +24,7 @@ test_that("get_landsat_imagery() is stable", { test_that("get_sentinel1_imagery() is stable", { skip_on_cran() + skip_if_offline() aoi <- sf::st_point(c(-74.912131, 44.080410)) aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326) aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100) @@ -46,6 +48,7 @@ test_that("get_sentinel1_imagery() is stable", { test_that("get_sentinel2_imagery() is stable", { skip_on_cran() + skip_if_offline() aoi <- sf::st_point(c(-74.912131, 44.080410)) aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326) aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100) @@ -70,6 +73,7 @@ test_that("get_sentinel2_imagery() is stable", { test_that("get_dem() is stable", { skip_on_cran() + skip_if_offline() aoi <- sf::st_point(c(-74.912131, 44.080410)) aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326) aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100) @@ -91,6 +95,7 @@ test_that("get_dem() is stable", { test_that("non-default mappings work", { skip_on_cran() + skip_if_offline() aoi <- sf::st_point(c(-74.912131, 44.080410)) aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326) aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100) @@ -116,6 +121,7 @@ test_that("non-default mappings work", { test_that("can download RTC products", { skip_if(Sys.getenv("rsi_pc_key") == "", "Environment variable `rsi_pc_key` not set") skip_on_cran() + skip_if_offline() aoi <- sf::st_point(c(-74.912131, 44.080410)) aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326) aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100) @@ -140,6 +146,7 @@ test_that("can download RTC products", { test_that("hidden arguments work", { skip_on_cran() + skip_if_offline() aoi <- sf::st_point(c(-74.912131, 44.080410)) aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326) aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100) @@ -165,6 +172,7 @@ test_that("hidden arguments work", { test_that("simple merge method works", { skip_on_cran() + skip_if_offline() aoi <- sf::st_point(c(-74.912131, 44.080410)) aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326) aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100) @@ -187,6 +195,7 @@ test_that("simple merge method works", { test_that("warning (but not error) fires if `mask_band` is not NULL with NULL `mask_function`", { skip_on_cran() + skip_if_offline() aoi <- sf::st_point(c(-74.912131, 44.080410)) aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326) aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100) @@ -205,6 +214,7 @@ test_that("warning (but not error) fires if `mask_band` is not NULL with NULL `m test_that("get_*_data works with mapply() (#17)", { skip_on_cran() + skip_if_offline() san_antonio = sf::st_point(c(-98.491142, 29.424349)) san_antonio = sf::st_sfc(san_antonio, crs = "EPSG:4326") san_antonio = sf::st_buffer(sf::st_transform(san_antonio, "EPSG:3081"), 100) diff --git a/tests/testthat/test-spectral_indices.R b/tests/testthat/test-spectral_indices.R index 187f176..c613c5f 100644 --- a/tests/testthat/test-spectral_indices.R +++ b/tests/testthat/test-spectral_indices.R @@ -15,6 +15,7 @@ test_that("spectral_indices() works", { # Second, if we're online, we should be able to download the current indices: skip_if_offline() + skip_on_cran() expect_no_warning(spectral_indices(update_cache = FALSE, download_indices = TRUE)) # Third, if we're online and can write to the cache folder, @@ -48,15 +49,18 @@ test_that("spectral_indices_url() respects environment variables", { test_that("no cache", { skip_if_offline() + skip_on_cran() indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda") if (file.exists(indices_path)) file.remove(indices_path) expect_no_warning(spectral_indices()) }) test_that("no cache, update false", { + skip_if_offline() + skip_on_cran() indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda") if (file.exists(indices_path)) file.remove(indices_path) - expect_snapshot(spectral_indices(update_cache = FALSE)) + expect_snapshot(suppressWarnings(spectral_indices(update_cache = FALSE))) }) test_that("no cache, download false", { @@ -73,6 +77,7 @@ test_that("no cache, download and update false", { test_that("no cache, update true", { skip_if_offline() + skip_on_cran() indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda") if (file.exists(indices_path)) file.remove(indices_path) expect_no_warning(spectral_indices(update_cache = TRUE)) @@ -80,6 +85,7 @@ test_that("no cache, update true", { test_that("no cache, download true", { skip_if_offline() + skip_on_cran() indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda") if (file.exists(indices_path)) file.remove(indices_path) expect_no_warning(spectral_indices(download_indices = TRUE)) @@ -87,6 +93,7 @@ test_that("no cache, download true", { test_that("no cache, download and update true", { skip_if_offline() + skip_on_cran() indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda") if (file.exists(indices_path)) file.remove(indices_path) expect_no_warning(spectral_indices(download_indices = TRUE, update_cache = TRUE)) @@ -94,48 +101,56 @@ test_that("no cache, download and update true", { test_that("new cache", { skip_if_offline() + skip_on_cran() invisible(spectral_indices(update_cache = TRUE)) expect_no_warning(spectral_indices()) }) test_that("new cache, update false", { skip_if_offline() + skip_on_cran() invisible(spectral_indices(update_cache = TRUE)) expect_no_warning(spectral_indices(update_cache = FALSE)) }) test_that("new cache, download false", { skip_if_offline() + skip_on_cran() invisible(spectral_indices(update_cache = TRUE)) expect_no_warning(spectral_indices(download_indices = FALSE)) }) test_that("new cache, download and update false", { skip_if_offline() + skip_on_cran() invisible(spectral_indices(update_cache = TRUE)) expect_no_warning(spectral_indices(download_indices = FALSE, update_cache = FALSE)) }) test_that("new cache, update true", { skip_if_offline() + skip_on_cran() invisible(spectral_indices(update_cache = TRUE)) expect_no_warning(spectral_indices(update_cache = TRUE)) }) test_that("new cache, download true", { skip_if_offline() + skip_on_cran() invisible(spectral_indices(update_cache = TRUE)) expect_no_warning(spectral_indices(download_indices = TRUE)) }) test_that("new cache, download and update true", { skip_if_offline() + skip_on_cran() invisible(spectral_indices(update_cache = TRUE)) expect_no_warning(spectral_indices(download_indices = TRUE, update_cache = TRUE)) }) test_that("old cache", { skip_if_offline() + skip_on_cran() indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda") invisible(spectral_indices(update_cache = TRUE)) Sys.setFileTime(indices_path, "1970-01-01") @@ -144,6 +159,7 @@ test_that("old cache", { test_that("old cache, update false", { skip_if_offline() + skip_on_cran() indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda") invisible(spectral_indices(update_cache = TRUE)) Sys.setFileTime(indices_path, "1970-01-01") @@ -152,6 +168,7 @@ test_that("old cache, update false", { test_that("old cache, download false", { skip_if_offline() + skip_on_cran() indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda") invisible(spectral_indices(update_cache = TRUE)) Sys.setFileTime(indices_path, "1970-01-01") @@ -160,6 +177,7 @@ test_that("old cache, download false", { test_that("old cache, download and update false", { skip_if_offline() + skip_on_cran() indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda") invisible(spectral_indices(update_cache = TRUE)) Sys.setFileTime(indices_path, "1970-01-01") @@ -168,6 +186,7 @@ test_that("old cache, download and update false", { test_that("old cache, update true", { skip_if_offline() + skip_on_cran() indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda") invisible(spectral_indices(update_cache = TRUE)) Sys.setFileTime(indices_path, "1970-01-01") @@ -176,6 +195,7 @@ test_that("old cache, update true", { test_that("old cache, download true", { skip_if_offline() + skip_on_cran() indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda") invisible(spectral_indices(update_cache = TRUE)) Sys.setFileTime(indices_path, "1970-01-01") @@ -184,6 +204,7 @@ test_that("old cache, download true", { test_that("old cache, download and update true", { skip_if_offline() + skip_on_cran() indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda") invisible(spectral_indices(update_cache = TRUE)) Sys.setFileTime(indices_path, "1970-01-01") diff --git a/vignettes/rsi.Rmd b/vignettes/rsi.Rmd index b41be66..e6e0427 100644 --- a/vignettes/rsi.Rmd +++ b/vignettes/rsi.Rmd @@ -11,7 +11,7 @@ vignette: > knitr::opts_chunk$set( collapse = TRUE, comment = "#>", - eval = rlang::is_installed("curl") && !is.null(curl::nslookup("r-project.org", error = FALSE)) + eval = rlang::is_installed("curl") && !is.null(curl::nslookup("r-project.org", error = FALSE)) && !(!interactive() && !isTRUE(as.logical(Sys.getenv("NOT_CRAN", "false")))) ) ```