Skip to content

Commit

Permalink
Use testthat's new mocking functions and modernize these snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Apr 4, 2023
1 parent 788356e commit 1c06c41
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 162 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ Suggests:
covr,
httpuv,
knitr,
mockr,
rmarkdown,
sodium,
spelling,
testthat (>= 3.1.5)
testthat (>= 3.1.7)
VignetteBuilder:
knitr
Config/Needs/website: tidyverse/tidytemplate
Expand Down
1 change: 0 additions & 1 deletion inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ json
keyless
macOS
misconfigured
mockr
mortem
novo
oauth
Expand Down
13 changes: 7 additions & 6 deletions tests/testthat/_snaps/request_retry.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# request_retry() logic works as advertised

Code
writeLines(fix_strategy(fix_wait_time(msg_fail_once)))
Output
fail_then_succeed <- request_retry(max_total_wait_time_in_seconds = 5)
Message
x Request failed [429]
oops
i Retry 1 happens in {WAIT_TIME} seconds ...
Expand All @@ -11,8 +11,8 @@
---

Code
writeLines(fix_strategy(fix_wait_time(msg_retry_after)))
Output
fail_then_succeed <- request_retry()
Message
x Request failed [429]
oops
i Retry 1 happens in {WAIT_TIME} seconds ...
Expand All @@ -21,8 +21,9 @@
---

Code
writeLines(fix_strategy(fix_wait_time(msg_max_tries)))
Output
fail_max_tries <- request_retry(max_tries_total = 3,
max_total_wait_time_in_seconds = 6)
Message
x Request failed [429]
oops
i Retry 1 happens in {WAIT_TIME} seconds ...
Expand Down
4 changes: 0 additions & 4 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ expect_gargle2.0_token <- function(object, expected) {
expect_equal( object$params, expected$params)
}

with_mock <- function(..., .parent = parent.frame()) {
mockr::with_mock(..., .parent = .parent, .env = "gargle")
}

skip_if_no_auth <- function() {
testthat::skip_if_not(
secret_can_decrypt("gargle"),
Expand Down
48 changes: 16 additions & 32 deletions tests/testthat/test-credentials_app_default.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ test_that("credentials_app_default_path(), default, non-Windows", {
GOOGLE_APPLICATION_CREDENTIALS = NA, CLOUDSDK_CONFIG = NA,
APPDATA = NA, SystemDrive = NA
))
with_mock(
is_windows = function() FALSE,
{
expect_equal(
credentials_app_default_path(),
path_home(".config", "gcloud", "application_default_credentials.json")
)
}
local_mocked_bindings(is_windows = function() FALSE)
expect_equal(
credentials_app_default_path(),
path_home(".config", "gcloud", "application_default_credentials.json")
)
})

Expand All @@ -19,14 +15,10 @@ test_that("credentials_app_default_path(), default, Windows", {
GOOGLE_APPLICATION_CREDENTIALS = NA, CLOUDSDK_CONFIG = NA,
APPDATA = NA, SystemDrive = NA
))
with_mock(
is_windows = function() TRUE,
{
expect_equal(
credentials_app_default_path(),
path("C:", "gcloud", "application_default_credentials.json")
)
}
local_mocked_bindings(is_windows = function() TRUE)
expect_equal(
credentials_app_default_path(),
path("C:", "gcloud", "application_default_credentials.json")
)
})

Expand All @@ -35,14 +27,10 @@ test_that("credentials_app_default_path(), system drive, Windows", {
GOOGLE_APPLICATION_CREDENTIALS = NA, CLOUDSDK_CONFIG = NA,
APPDATA = NA, SystemDrive = "D:"
))
with_mock(
is_windows = function() TRUE,
{
expect_equal(
credentials_app_default_path(),
path("D:", "gcloud", "application_default_credentials.json")
)
}
local_mocked_bindings(is_windows = function() TRUE)
expect_equal(
credentials_app_default_path(),
path("D:", "gcloud", "application_default_credentials.json")
)
})

Expand All @@ -51,14 +39,10 @@ test_that("credentials_app_default_path(), APPDATA env var, Windows", {
GOOGLE_APPLICATION_CREDENTIALS = NA, CLOUDSDK_CONFIG = NA,
APPDATA = path("D:", "AppData"), SystemDrive = "D:"
))
with_mock(
is_windows = function() TRUE,
{
expect_equal(
credentials_app_default_path(),
path("D:", "AppData", "gcloud", "application_default_credentials.json")
)
}
local_mocked_bindings(is_windows = function() TRUE)
expect_equal(
credentials_app_default_path(),
path("D:", "AppData", "gcloud", "application_default_credentials.json")
)
})

Expand Down
39 changes: 16 additions & 23 deletions tests/testthat/test-oauth-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,25 @@ test_that("cache_establish() insists on sensible input", {
})

test_that("`cache = TRUE` uses default cache path", {
with_mock(
local_mocked_bindings(
## we don't want to actually initialize a cache
cache_create = function(path) NULL,
{
expect_equal(cache_establish(TRUE), gargle_default_oauth_cache_path())
}
cache_create = function(path) NULL
)
expect_equal(cache_establish(TRUE), gargle_default_oauth_cache_path())
})

test_that("`cache = FALSE` does nothing", {
expect_null(cache_establish(FALSE))
})

test_that("`cache = NA` is like `cache = FALSE` if cache not available", {
with_mock(
local_mocked_bindings(
# we want no existing cache to be found, be it current or legacy
gargle_default_oauth_cache_path = function() file_temp(),
gargle_legacy_default_oauth_cache_path = function() file_temp(),
cache_allowed = function(path) FALSE,
{
expect_equal(cache_establish(NA), cache_establish(FALSE))
}
cache_allowed = function(path) FALSE
)
expect_equal(cache_establish(NA), cache_establish(FALSE))
})

test_that("`cache = <filepath>` creates cache folder, recursively", {
Expand Down Expand Up @@ -58,20 +54,17 @@ test_that("`cache = <filepath>` adds new cache folder to relevant 'ignores'", {
})

test_that("default is to consult and set the oauth cache option", {
withr::with_options(
list(gargle_oauth_cache = NA),
with_mock(
# we want no existing cache to be found, be it current or legacy
gargle_default_oauth_cache_path = function() file_temp(),
gargle_legacy_default_oauth_cache_path = function() file_temp(),
cache_allowed = function(path) FALSE,
{
expect_equal(getOption("gargle_oauth_cache"), NA)
cache_establish()
expect_false(getOption("gargle_oauth_cache"))
}
)
withr::local_options(list(gargle_oauth_cache = NA))
local_mocked_bindings(
# we want no existing cache to be found, be it current or legacy
gargle_default_oauth_cache_path = function() file_temp(),
gargle_legacy_default_oauth_cache_path = function() file_temp(),
cache_allowed = function(path) FALSE
)

expect_equal(getOption("gargle_oauth_cache"), NA)
cache_establish()
expect_false(getOption("gargle_oauth_cache"))
})

# cache_allowed() --------------------------------------------------------------
Expand Down
Loading

0 comments on commit 1c06c41

Please sign in to comment.