Skip to content

Commit

Permalink
Make is_interactive() react to "rlang_interactive" option (#734)
Browse files Browse the repository at this point in the history
* Make `is_interactive()` react to "rlang_interactive" option

Helpful for gargle's pseudo-OOB flow  in Google Colab

* Actually, I removed with mocks

* Add NEWS bullet
  • Loading branch information
jennybc authored Feb 22, 2023
1 parent 93a7e96 commit 849a3e8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# httr (development version)

* An internal helper that checks for an interactive session in the OOB flow now
honors the `"rlang_interactive"` global option, in case it's necessary to
declare the session to be interactive (enough) for OOB (@jennybc, #734).

# httr 1.4.4

* Fix intermittent failing test.
Expand Down
8 changes: 6 additions & 2 deletions R/oauth-init.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,12 @@ check_scope <- function(x) {
paste(x, collapse = " ")
}

# Wrap base::interactive in a non-primitive function so that the call can be mocked for testing
is_interactive <- function() interactive()
# gargle needs to access (pseudo-)OOB flow from Google Colab, so we need to
# be able to use the "rlang_interactive" option to signal that we are in
# an environment that is interactive (enough) to complete this flow.
is_interactive <- function() {
getOption("rlang_interactive") %||% interactive()
}

check_oob <- function(use_oob, oob_value = NULL) {
if (!is.logical(use_oob) || length(use_oob) != 1) {
Expand Down
14 changes: 10 additions & 4 deletions tests/testthat/test-oauth.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,19 @@ test_that("oob must be a flag", {
})

test_that("can not use oob in non-interactive session", {
old <- options()
on.exit(options(old))
options(rlang_interactive = FALSE)

expect_error(check_oob(TRUE), "interactive")
})

test_that("can not use custom oob value without enabling oob", {
testthat::skip_if_not_installed("httpuv")
with_mock(
`httr:::is_interactive` = function() TRUE,
expect_error(check_oob(FALSE, "custom_value"), "custom oob value")
)

old <- options()
on.exit(options(old))
options(rlang_interactive = TRUE)

expect_error(check_oob(FALSE, "custom_value"), "custom oob value")
})

0 comments on commit 849a3e8

Please sign in to comment.