Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make is_interactive() react to "rlang_interactive" option #734

Merged
merged 3 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
Comment on lines +217 to +219
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't use rlang::is_interactive() because httr does not depend on rlang at this point. And it doesn't seem worth bringing a whole standalone file in for this.


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)

Comment on lines +114 to +117
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test would previously fail when run with test_active_file(), so I fixed that while I was here.

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)
Comment on lines +124 to +126
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took the chance to get rid of the with_mock() call, even though httr doesn't use testthat 3e. httr also doesn't use withr.


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