Context
To protect sensitive data and speed up development, our test suite skips database tests by default. These are switched on interactively in the console using R session options: options(test.run_snapshot = TRUE).
However, standard package checks (like devtools::check()) execute in an isolated, non-interactive background R process. Because these background checks run with a clean state, they do not read our active workspace options() or our .Rprofile.
Consequently, database-dependent tests will always be skipped during local package checks.
Questions to resolve
- Is there a clean, native way to pass our testing toggles to the background check process?
- Do we want database-dependent snapshot tests to run during devtools::check()?
Initial Research
1. Passing options/environment variables to background checks
- project-level .Renviron - doesn't work:
devtools::check() executes tests inside a temporary folder, ignoring the project-level .Renviron (a user-level .Renviron might work, but probably not a good idea)
- add argument
env_vars - works!: We can pass environment variables directly with devtools::check(env_vars = ...)
- RStudio Build Options: We can prepend environment variables (e.g.:
--env-vars=TEST_RUN_SNAPSHOT=TRUE) to the "Check Package" parameteres in Project Options -> Build Tools
2. Extra problem: the global environment variable test_con
Even if we can run the tests in our background process, our helper function fetch_watina_connection() will stop the tests.
fetch_watina_connection <- function() {
skip_if_not(
exists("test_con", envir = .GlobalEnv),
message = "No active database connection found..."
)
return(get("test_con", envir = .GlobalEnv))
}
This function expects an active test_con object inside .GlobalEnv. During a background check, this environment is empty.
How should we solve this?
- Option A - Keep them skipped: Accept that devtools::check() is strictly offline code testing without a database connection. Database tests are only run interactively via
devtools::test().
- Option B - Automatic connection and teardown in
setup.R: If check-specific environment variables are detected (e.g.: Sys.getenv("WATINA_RUN_SNAPSHOT") == "TRUE"), we can modify our setup file to automatically establish the connection in the background .GlobalEnv and cleanly close it (teardown_env()) when the check is finished.
- Option C - Find a way to detect the
test_con variable?
Next Steps
Context
To protect sensitive data and speed up development, our test suite skips database tests by default. These are switched on interactively in the console using R session options:
options(test.run_snapshot = TRUE).However, standard package checks (like
devtools::check()) execute in an isolated, non-interactive background R process. Because these background checks run with a clean state, they do not read our active workspaceoptions()or our.Rprofile.Consequently, database-dependent tests will always be skipped during local package checks.
Questions to resolve
Initial Research
1. Passing options/environment variables to background checks
devtools::check()executes tests inside a temporary folder, ignoring the project-level .Renviron (a user-level .Renviron might work, but probably not a good idea)env_vars- works!: We can pass environment variables directly withdevtools::check(env_vars = ...)--env-vars=TEST_RUN_SNAPSHOT=TRUE) to the "Check Package" parameteres in Project Options -> Build Tools2. Extra problem: the global environment variable
test_conEven if we can run the tests in our background process, our helper function
fetch_watina_connection()will stop the tests.This function expects an active
test_conobject inside.GlobalEnv. During a background check, this environment is empty.How should we solve this?
devtools::test().setup.R: If check-specific environment variables are detected (e.g.:Sys.getenv("WATINA_RUN_SNAPSHOT") == "TRUE"), we can modify our setup file to automatically establish the connection in the background.GlobalEnvand cleanly close it (teardown_env()) when the check is finished.test_convariable?Next Steps
devtools::check()(currently the option withenv_varsseems the best solution)