Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 4 additions & 2 deletions .github/workflows/test-actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ jobs:

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages:
"local::."
needs: |
shinytest2-testing
extra-packages: |
local::.

# ## Suggested usage
# - name: Test app
Expand Down
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Imports:
rlang (>= 1.0.0),
rmarkdown,
shiny,
pkgload,
withr,
lifecycle
Suggests:
Expand All @@ -52,6 +53,8 @@ Suggests:
usethis,
vdiffr (>= 1.0.0),
spelling
Config/Needs/shinytest2-testing:
decor
Config/Needs/check:
rstudio/shiny,
bslib
Expand Down
166 changes: 113 additions & 53 deletions R/app-driver-start.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Do not delete
# Paired with tests/testthat/apps/test-env/ 's tests on `.internal_value_used_by_shinytest2_test`
.internal_value_used_by_shinytest2_test <- TRUE # nolint

app_start_shiny <- function(
self, private,
self,
private,
...,
seed = NULL,
load_timeout = 10000,
shiny_args = list(),
Expand All @@ -14,6 +19,18 @@ app_start_shiny <- function(
# the RNG kind should inherit from the parent process
rng_kind <- RNGkind()

package_path <- tryCatch(pkgload::pkg_path(), error = function(e) NULL)
package_name <- tryCatch(pkgload::pkg_name(), error = function(e) NULL)

# `testthat::is_checking()` is TRUE inside `testthat::test_check()`, typically
# called in `R CMD check`.
# If we're doing R CMD check, we only want to use installed packages.
# Therefore, disable the local package sourcing
if (testthat::is_checking()) {
package_path <- NULL
package_name <- NULL
}

p <- local({
# https://github.com/r-lib/testthat/issues/603
withr::local_envvar(c("R_TESTS" = NA))
Expand All @@ -25,58 +42,93 @@ app_start_shiny <- function(
stderr = sprintf(tempfile_format, "shiny-stderr"),
supervise = TRUE,
args = list(
app_dir = self$get_dir(),
shiny_args = shiny_args,
has_rmd = app_dir_has_rmd(self, private),
seed = seed,
rng_kind = rng_kind,
render_args = render_args,
options = options
.app_dir = self$get_dir(),
.shiny_args = shiny_args,
.has_rmd = app_dir_has_rmd(self, private),
.seed = seed,
.rng_kind = rng_kind,
.render_args = render_args,
.options = options,
.package_name = package_name,
.package_path = package_path
),
function(app_dir, shiny_args, has_rmd, seed, rng_kind, render_args, options) {

if (!is.null(seed)) {
function(
.app_dir,
.shiny_args,
.has_rmd,
.seed,
.rng_kind,
.render_args,
.options,
.package_name,
.package_path
) {
if (!is.null(.seed)) {
# Prior to R 3.6, RNGkind has 2 args, otherwise it has 3
do.call(RNGkind, as.list(rng_kind))
set.seed(seed)
getNamespace("shiny")$withPrivateSeed(set.seed(seed + 11))
do.call(RNGkind, as.list(.rng_kind))
set.seed(.seed)
getNamespace("shiny")$withPrivateSeed(set.seed(.seed + 11))
}

options <- as.list(options)
options[["shiny.testmode"]] <- TRUE
.options <- as.list(.options)
.options[["shiny.testmode"]] <- TRUE
# TODO-future; Adjust shiny to add htmldeps to the list of the rendered page
# options[["shiny-testmode-html-dep"]] <- getTracerDep()
do.call(base::options, options)

# Return value is important for `AppDriver$stop()`
# Do not add code after this if else block
if (has_rmd) {
# Shiny document
rmarkdown::run(
file = NULL, # Do not open anything in browser
dir = app_dir,
default_file = NULL, # Let rmarkdown find the default file
# DO NOT ENABLE! Makes things like `app$wait_for_idle()` not work as expected.
auto_reload = FALSE, # Do not constantly poll for file changes. Drastically reduces `app$get_logs()`
shiny_args = shiny_args,
render_args = render_args
do.call(base::options, .options)

# Taken inspiration from `testthat:::test_files_setup`.
# Motivation:
# * Shiny will only have access to the installed package namepace after
# a library call, so during testing we should try to mimic this
# behavior to avoid surprises
# * Whereas testthat wants to have already `library()`ed {testthat} and
# the package by the time the test file is sourced
if (!is.null(.package_path)) {
pkgload::load_all(
.package_path,
# Be sure to add the local package to the search path,
# but do not "library()" the local package.
# (Typically) Shiny will not have access to the local package
# until the package is loaded. We should mimic this behavior!
attach = FALSE,
)
} else {
# Normal shiny app
do.call(shiny::runApp, c(app_dir, shiny_args))
}

ret <-
if (.has_rmd) {
# Shiny document
rmarkdown::run(
file = NULL, # Do not open anything in browser
dir = .app_dir,
default_file = NULL, # Let rmarkdown find the default file
# DO NOT ENABLE! Makes things like `app$wait_for_idle()` not work as expected.
auto_reload = FALSE, # Do not constantly poll for file changes. Drastically reduces `app$get_logs()`
shiny_args = .shiny_args,
render_args = .render_args
)
} else {
# Normal shiny app
do.call(shiny::runApp, c(.app_dir, .shiny_args))
}

# Return value is important for `AppDriver$stop()`
return(ret)
}
)
})

private$shiny_process <- p # nolint

"!DEBUG waiting for shiny to start"
if (! p$is_alive()) {
app_abort(self, private, paste0(
"Failed to start shiny. Error:\n",
strwrap(readLines(p$get_error_file()))
))
if (!p$is_alive()) {
app_abort(
self,
private,
paste0(
"Failed to start shiny. Error:\n",
strwrap(readLines(p$get_error_file()))
)
)
}

"!DEBUG finding shiny port"
Expand All @@ -87,27 +139,35 @@ app_start_shiny <- function(
err_lines <- readLines(p$get_error_file())

if (!p$is_alive()) {
app_abort(self, private, paste0(
"Error starting shiny application:\n",
paste(err_lines, collapse = "\n")
))
app_abort(
self,
private,
paste0(
"Error starting shiny application:\n",
paste(err_lines, collapse = "\n")
)
)
}
if (any(grepl("Listening on http", err_lines, fixed = TRUE))) break

Sys.sleep(0.2)

if (i == max_i) {
app_abort(self, private, sprintf(
paste(
"The Shiny app failed to start up within %s second%s.",
"To increase the loading timeout, consult the documentation in `?AppDriver`",
"for the `load_timeout` argument of `AppDriver$new()`.",
"The app printed the following lines to stderr during start up:\n%s"
),
load_timeout / 1000,
if (load_timeout == 1000) "" else "s",
paste(err_lines, collapse = "\n")
))
app_abort(
self,
private,
sprintf(
paste(
"The Shiny app failed to start up within %s second%s.",
"To increase the loading timeout, consult the documentation in `?AppDriver`",
"for the `load_timeout` argument of `AppDriver$new()`.",
"The app printed the following lines to stderr during start up:\n%s"
),
load_timeout / 1000,
if (load_timeout == 1000) "" else "s",
paste(err_lines, collapse = "\n")
)
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Package: R6
Title: Encapsulated Classes with Reference Semantics
Version: 2.5.1
Authors@R: person("Winston", "Chang", role = c("aut", "cre"), email = "[email protected]")
Description: Creates classes with reference semantics, similar to R's built-in
reference classes. Compared to reference classes, R6 classes are simpler
and lighter-weight, and they are not built on S4 classes so they do not
require the methods package. These classes allow public and private
members, and they support inheritance, even when the classes are defined in
different packages.
Depends: R (>= 3.0)
Suggests: testthat, pryr
License: MIT + file LICENSE
URL: https://r6.r-lib.org, https://github.com/r-lib/R6/
BugReports: https://github.com/r-lib/R6/issues
RoxygenNote: 7.1.1
NeedsCompilation: no
Packaged: 2021-08-06 20:18:46 UTC; winston
Author: Winston Chang [aut, cre]
Maintainer: Winston Chang <[email protected]>
Repository: CRAN
Date/Publication: 2021-08-19 14:00:05 UTC
Built: R 4.4.1; ; 2025-02-01 04:45:16 UTC; unix
RemoteType: standard
RemotePkgRef: R6
RemoteRef: R6
RemoteRepos: https://cloud.r-project.org/
RemotePkgPlatform: aarch64-apple-darwin20
RemoteSha: 2.5.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
R6Class Create an R6 reference object generator
as.list.R6 Create a list from an R6 object
is.R6 Is an object an R6 Class Generator or Object?
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
YEAR: 2015
COPYRIGHT HOLDER: RStudio
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
11 changes: 11 additions & 0 deletions rhino/renv/library/macos/R-4.4/aarch64-apple-darwin20/R6/NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Generated by roxygen2: do not edit by hand

S3method(as.list,R6)
S3method(format,R6)
S3method(format,R6ClassGenerator)
S3method(plot,R6)
S3method(print,R6)
S3method(print,R6ClassGenerator)
export(R6Class)
export(is.R6)
export(is.R6Class)
Loading
Loading