tests/testthat/helper-env-vars.R

# Helper to preserve or fake environment variables during tests
# Set stubbing = FALSE to turn of stubbing
with_stubbed_credentials <- function(code_block, stubbing = TRUE) {
  key <- Sys.getenv("OPENAI_KEY")
  model <- Sys.getenv("OPENAI_MODEL")

  on.exit({
    Sys.setenv("OPENAI_KEY" = key)
    Sys.setenv("OPENAI_MODEL" = model)
  })

  if (stubbing) {
    Sys.setenv("OPENAI_KEY" = "fake-key")
    Sys.setenv("OPENAI_MODEL" = "fake-model")
  }

  code_block
}

Try the air package in your browser

Any scripts or data that you put into this service are public.

air documentation built on Aug. 8, 2025, 6:46 p.m.