Skip to content
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

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

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
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"
schloerke marked this conversation as resolved.
Show resolved Hide resolved

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