diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index a9ad68e8..86bdbae4 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -22,7 +22,7 @@ jobs: - uses: r-lib/actions/setup-r-dependencies@v2 with: - extra-packages: local::., any::lintr, any::devtools, any::testthat + extra-packages: local::., any::lintr, any::devtools, any::testthat, any::cyclocomp needs: lint - name: Lint diff --git a/NEWS.md b/NEWS.md index bad488bd..f6ad81f9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # connectapi (development version) +## New features + +- New functions `get_product()`, `is_local()`, `on_connect()` (updated), and `on_workbench()` help detect the Posit product environment (Posit Connect, Posit Workbench, or local) via environment variables. `get_product()` checks both `POSIT_PRODUCT` and `RSTUDIO_PRODUCT` environment variables. (#371) + # connectapi 0.6.0 ## New features diff --git a/R/utils.R b/R/utils.R index 2670a82d..f12cdaa3 100644 --- a/R/utils.R +++ b/R/utils.R @@ -194,8 +194,28 @@ endpoint_does_not_exist <- function(res) { !("code" %in% names(httr::content(res, as = "parsed"))) } +get_product <- function() { + posit_product <- Sys.getenv("POSIT_PRODUCT") + if (posit_product != "") { + return(posit_product) + } + Sys.getenv("RSTUDIO_PRODUCT") +} + +# Returns `TRUE` if we're running locally (no product env var set), +# else `FALSE`. +is_local <- function() { + get_product() == "" +} + # Returns `TRUE` if we're running on Connect as determined by the -# `RSTUDIO_PRODUCT` env var, else `FALSE`. +# `POSIT_PRODUCT` or `RSTUDIO_PRODUCT` env var, else `FALSE`. on_connect <- function() { - Sys.getenv("RSTUDIO_PRODUCT") == "CONNECT" + get_product() == "CONNECT" +} + +# Returns `TRUE` if we're running on Workbench as determined by the +# `POSIT_PRODUCT` or `RSTUDIO_PRODUCT` env var, else `FALSE`. +on_workbench <- function() { + get_product() == "WORKBENCH" } diff --git a/tests/testthat/test-connect.R b/tests/testthat/test-connect.R index faa7df72..c59e0da5 100644 --- a/tests/testthat/test-connect.R +++ b/tests/testthat/test-connect.R @@ -121,7 +121,7 @@ test_that("Visitor client can successfully be created running on Connect", { withr::local_envvar( CONNECT_SERVER = "https://connect.example", CONNECT_API_KEY = "fake", - RSTUDIO_PRODUCT = "CONNECT" + POSIT_PRODUCT = "CONNECT" ) client <- connect(token = "my-token") @@ -181,7 +181,7 @@ test_that("Visitor client code path errs with older Connect version", { withr::local_envvar( CONNECT_SERVER = "https://connect.example", CONNECT_API_KEY = "fake", - RSTUDIO_PRODUCT = "CONNECT" + POSIT_PRODUCT = "CONNECT" ) expect_error( @@ -190,3 +190,42 @@ test_that("Visitor client code path errs with older Connect version", { ) }) }) + +test_that("environment detection functions work", { + withr::with_envvar(c(POSIT_PRODUCT = "", RSTUDIO_PRODUCT = ""), { + expect_true(is_local()) + expect_false(on_connect()) + expect_false(on_workbench()) + }) + + withr::with_envvar(c(POSIT_PRODUCT = "CONNECT", RSTUDIO_PRODUCT = ""), { + expect_false(is_local()) + expect_true(on_connect()) + expect_false(on_workbench()) + }) + + withr::with_envvar(c(POSIT_PRODUCT = "WORKBENCH", RSTUDIO_PRODUCT = ""), { + expect_false(is_local()) + expect_false(on_connect()) + expect_true(on_workbench()) + }) + + withr::with_envvar(c(POSIT_PRODUCT = "", RSTUDIO_PRODUCT = "CONNECT"), { + expect_false(is_local()) + expect_true(on_connect()) + expect_false(on_workbench()) + }) + + withr::with_envvar(c(POSIT_PRODUCT = "", RSTUDIO_PRODUCT = "WORKBENCH"), { + expect_false(is_local()) + expect_false(on_connect()) + expect_true(on_workbench()) + }) + + # POSIT_PRODUCT takes precedence over RSTUDIO_PRODUCT + withr::with_envvar(c(POSIT_PRODUCT = "CONNECT", RSTUDIO_PRODUCT = "WORKBENCH"), { + expect_false(is_local()) + expect_true(on_connect()) + expect_false(on_workbench()) + }) +})