diff --git a/NAMESPACE b/NAMESPACE index d60d83e..b94f76d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -30,6 +30,7 @@ export(using_nix_shell) export(using_option) export(using_os) export(using_podman_container) +export(using_posit_connect) export(using_r_version) export(using_rstudio) export(using_rstudio_dark_theme) diff --git a/R/rstudio.R b/R/rstudio.R index c36acb8..a51751d 100644 --- a/R/rstudio.R +++ b/R/rstudio.R @@ -2,7 +2,7 @@ #' @title RStudio environments #' @description #' These functions enable you to determine whether code is being run in the -#' presence of various features of the RStudio IDE. +#' presence of various features of the RStudio IDE and other Posit products. #' #' `using_rstudio()` determines whether code is being run in #' RStudio @@ -41,3 +41,23 @@ using_rstudio_dark_theme <- function() { FALSE } } + +#' @noRd +#' @description `using_rstudio_product()` checks whether or not the +#' `RSTUDIO_PRODUCT` environment variable is set +#' @param name Optional name of a particular product +using_rstudio_product <- function(name = NULL) { + using_envvar("RSTUDIO_PRODUCT", value = name) +} + +#' @rdname rstudio +#' @description `using_posit_connect()` checks whether [Posit Connect]() is +#' being used +#' @seealso https://docs.posit.co/connect/user/content-settings/#content-vars +#' +#' @export +#' @examples +#' using_posit_connect() +using_posit_connect <- function() { + using_rstudio_product(name = "CONNECT") +} diff --git a/_pkgdown.yml b/_pkgdown.yml index 057e5a5..c8e5256 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -38,6 +38,7 @@ reference: contents: - using_r_version - using_rstudio + - using_posit_connect - using_rstudio_jobs - using_rstudio_dark_theme - using_vscode diff --git a/man/rstudio.Rd b/man/rstudio.Rd index 1fdcfac..2f3178d 100644 --- a/man/rstudio.Rd +++ b/man/rstudio.Rd @@ -4,6 +4,7 @@ \alias{using_rstudio} \alias{using_rstudio_jobs} \alias{using_rstudio_dark_theme} +\alias{using_posit_connect} \title{RStudio environments} \usage{ using_rstudio() @@ -11,13 +12,15 @@ using_rstudio() using_rstudio_jobs() using_rstudio_dark_theme() + +using_posit_connect() } \value{ A logical value } \description{ These functions enable you to determine whether code is being run in the -presence of various features of the RStudio IDE. +presence of various features of the RStudio IDE and other Posit products. \code{using_rstudio()} determines whether code is being run in RStudio @@ -27,9 +30,16 @@ an \href{https://docs.posit.co/ide/user/ide/guide/tools/jobs.html}{RStudio Job} \code{using_rstudio_dark_theme()} determines whether a dark theme is being used + +\code{using_posit_connect()} checks whether \url{Posit Connect} is +being used } \examples{ using_rstudio() using_rstudio_jobs() using_rstudio_dark_theme() +using_posit_connect() +} +\seealso{ +https://docs.posit.co/connect/user/content-settings/#content-vars } diff --git a/tests/testthat/test-rstudio.R b/tests/testthat/test-rstudio.R index 5b6dd68..f011f2b 100644 --- a/tests/testthat/test-rstudio.R +++ b/tests/testthat/test-rstudio.R @@ -17,3 +17,25 @@ test_that("using_rstudio_jobs() works as expected", { test_that("using_rstudio_dark_theme() returns a boolean", { expect_true(is.logical(using_rstudio_dark_theme())) }) + +test_that("using_rstudio_product validates input properly", { + expect_error(using_rstudio_product(name = NA_character_)) + expect_error(using_rstudio_product(name = 1)) + expect_error(using_rstudio_product(name = FALSE)) + expect_error(using_rstudio_product(name = c("CONNECT", "WORKBENCH"))) +}) + +test_that("using_rstudio_product works as expected when `RSTUDIO_PRODUCT` not set", { # nolint: line_length_linter + withr::local_envvar(list("USING_RSTUDIO_PRODUCT" = NA)) + expect_false(using_rstudio_product()) + expect_false(using_rstudio_product(name = "CONNECT")) + expect_false(using_posit_connect()) +}) + +test_that("using_rstudio_product works as expected when `RSTUDIO_PRODUCT` is set", { # nolint: line_length_linter + withr::local_envvar(list("RSTUDIO_PRODUCT" = "CONNECT")) + expect_true(using_rstudio_product()) + expect_true(using_rstudio_product(name = "CONNECT")) + expect_false(using_rstudio_product(name = "WORKBENCH")) + expect_true(using_posit_connect()) +})