Skip to content

WIP feat: Allow for packages to be dynamically loaded in app #402

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

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2dae122
Commit code before gutting
schloerke Sep 24, 2024
7c70eb6
commit code before computer breaks
schloerke Dec 11, 2024
918664e
Merge branch 'main' into pkg_test_env
schloerke Feb 10, 2025
5167a2d
Merge branch 'main' into pkg_test_env
schloerke Feb 10, 2025
bf57461
Add pkgload as new dep
schloerke Feb 10, 2025
1fcf20a
Update test files
schloerke Feb 10, 2025
e89fdd2
Commit changes before chopping code
schloerke Feb 10, 2025
bbca165
Merge branch 'pkg_test_env' of https://github.com/rstudio/shinytest2 …
schloerke Feb 10, 2025
0d6bf0a
Do not enable the local package's namespace when testing
schloerke Feb 10, 2025
767f7ef
Remove old setup code
schloerke Feb 10, 2025
eb95f71
Be explicit on return value
schloerke Feb 10, 2025
67d8fd8
Update comment for `load_package`
schloerke Feb 10, 2025
b50d8d0
Clean up loading code to only load exported source fns
schloerke Feb 10, 2025
13e5a58
Move variant folders for more robust testing
schloerke Feb 10, 2025
6c7fa51
Update to latest snaps
schloerke Feb 10, 2025
1a5fee0
Update local variable name used for testing
schloerke Feb 11, 2025
db958d3
Do not attach local package. Only add it to the search path for local…
schloerke Feb 11, 2025
a1a8045
Update snaps
schloerke Feb 11, 2025
58c7b59
lint
schloerke Feb 11, 2025
4feb531
Fix sanity check for testing (now that we're trying to compile the pa…
schloerke Feb 11, 2025
eea9330
Add vanilla golem app to test
schloerke Feb 11, 2025
1c8879d
temp. please vet files
schloerke Mar 25, 2025
6d60117
Merge branch 'main' into pkg_test_env
schloerke Apr 9, 2025
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
38 changes: 36 additions & 2 deletions R/app-driver-start.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
internal_shinytest2_value <- TRUE

app_start_shiny <- function(
self, private,
...,
seed = NULL,
load_timeout = 10000,
shiny_args = list(),
Expand All @@ -20,6 +22,14 @@ app_start_shiny <- function(
# Load the app's .Rprofile / .Renviron if possible
withr::local_dir(self$get_dir())

# Find the package name if we're running in a package
package_name <- testthat::testing_package()
if (!nzchar(package_name)) package_name <- NULL
# How to load the package is determined by the testthat context
# * If we're `test_check()`ing, we should load the installed package
# * Otherwise, we should source the R files before loading the app
load_package <- if (testthat::is_checking()) "installed" else "source"

callr::r_bg(
stdout = sprintf(tempfile_format, "shiny-stdout"),
stderr = sprintf(tempfile_format, "shiny-stderr"),
Expand All @@ -31,9 +41,21 @@ app_start_shiny <- function(
seed = seed,
rng_kind = rng_kind,
render_args = render_args,
options = options
options = options,
package_name = package_name,
load_package = load_package
),
function(app_dir, shiny_args, has_rmd, seed, rng_kind, render_args, options) {
function(
app_dir,
shiny_args,
has_rmd,
seed,
rng_kind,
render_args,
options,
package_name,
load_package
) {

if (!is.null(seed)) {
# Prior to R 3.6, RNGkind has 2 args, otherwise it has 3
Expand All @@ -48,6 +70,18 @@ app_start_shiny <- function(
# options[["shiny-testmode-html-dep"]] <- getTracerDep()
do.call(base::options, options)

# Add testthat to app's environment
library(testthat)
testthat___test_files_setup_env <- getFromNamespace("test_files_setup_env", "testthat")

test_env = testthat___test_files_setup_env(
test_package = package_name,
test_dir = app_dir,
load_package = load_package,
env = NULL
)
withr::local_environment(test_env)

# Return value is important for `AppDriver$stop()`
# Do not add code after this if else block
if (has_rmd) {
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/apps/test-env/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
library(shiny)

# Test for internal value
# This is defined within shinytest2's ./R/app-driver-start.R
# and will only allow the app to run if the app has access to shinytest2's internal functions
# or the "local package" values.
value <- internal_shinytest2_value

ui <- fluidPage(
tags$h1("Internal test value:"),
verbatimTextOutput("value", placeholder = TRUE),
)
server <- function(input, output, session) {
output$value <- renderText({
value
})
}

shinyApp(ui, server)
1 change: 1 addition & 0 deletions tests/testthat/apps/test-env/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shinytest2::test_app()
1 change: 1 addition & 0 deletions tests/testthat/apps/test-env/tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shinytest2::load_app_env()
6 changes: 6 additions & 0 deletions tests/testthat/apps/test-env/tests/testthat/test-app-env.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

test_that("local pkg env is loaded", {
expect_equal(internal_shinytest2_value, TRUE)

AppDriver$new(variant = NULL)
})
Loading