rstudio / shinytest2

https://rstudio.github.io/shinytest2/
Other
104 stars 16 forks source link

Warnings from `{golem}` apps #264

Open schloerke opened 2 years ago

schloerke commented 2 years ago

From https://github.com/rstudio/shinytest2/issues/245#issue-1333355278

In addition: Warning messages: 1: In shiny::loadSupport(app_dir, renv = renv, globalrenv = globalrenv) : Loading R/ subdirectory for Shiny application, but this directory appears to contain an R package. Sourcing files in R/ may cause unexpected behavior.

Looking at reprex Shiny golem package: https://github.com/whipson/shinytest2reprex

tree:

.
├── DESCRIPTION
├── NAMESPACE
├── R
│   ├── _disable_autoload.R
│   ├── app_config.R
│   ├── app_server.R
│   ├── app_ui.R
│   └── run_app.R
├── app.R
├── dev
│   ├── 01_start.R
│   ├── 02_dev.R
│   ├── 03_deploy.R
│   └── run_dev.R
├── inst
│   ├── app
│   │   └── www
│   │       └── favicon.ico
│   └── golem-config.yml
├── man
│   └── run_app.Rd
├── shinytest2reprex.Rproj
└── tests
    ├── testthat
    │   ├── setup.R
    │   └── test-shinytest2.R
    └── testthat.R

8 directories, 19 files

DESCRIPTION file:

Package: shinytest2reprex
Title: An Amazing Shiny App
Version: 0.0.0.9000
Authors@R:
    person(given = "firstname",
           family = "lastname",
           role = c("aut", "cre"),
           email = "your@email.com")
Description: What the package does (one paragraph).
License: What license is it under?
Imports:
    config (>= 0.3.1),
    golem (>= 0.3.2),
    pkgload,
    shiny (>= 1.7.1)
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.0
Suggests:
    shinytest2

app.R

# Launch the ShinyApp (Do not remove this comment)
# To deploy, run: rsconnect::deployApp()
# Or use the blue button on top of this file

pkgload::load_all(export_all = FALSE,helpers = FALSE,attach_testthat = FALSE)
options( "golem.app.prod" = TRUE)
shinytest2reprex::run_app() # add parameters here (if any)

shinytest2reprex::run_app() returns a shiny object, it does NOT call a shiny::runApp().

schloerke commented 2 years ago

Should we not run the load_env() logic if:

Current logic in shiny::loadSupport() that triggers the warning:

if (
  file.exists(file.path.ci(appDir, "NAMESPACE")) || 
  (
    file.exists(descFile) &&
    identical(as.character(read.dcf(descFile, fields = "Type")), "Package")
  )
) {
  # display warning
}
schloerke commented 2 years ago

Maybe it is a documentation thing. If NAMESPACE is found, then maybe we should not auto check for the tests/testthat/setup-shinytest2.R file in shinytest2::test_app(check_setup=TRUE) in tests/testthat.R's

Maybe the initial value could be !fs::file_exists(fs::path(app_dir, "NAMESPACE"))? Reasoning is that NAMESPACE files will never really exist without a DESCRIPTION file.

schloerke commented 2 years ago

Could also look for inst/golem-config.yml. If that file exists, then do not require a setup file. But this seems too specific to {golem}.

schloerke commented 2 years ago

Moving forward with trying to find a NAMESPACE file and altering behavior accordingly.

schloerke commented 1 year ago

So more can of worms... which will cause this to be moved to next release 😞

Should we try using testthat for everything? Ex: test_dir(load_package = "source")


Situations:


Current behavior:


Using testthat only?:

Feels bad not using {shiny} load support method. But it could be argued that shiny::loadSupport() should not have existed in the first place.


What about using test_env(PKG.NAME)? This might resolve https://github.com/rstudio/shinytest2/issues/215 moving forward.

Test env may need to be loaded in the R background process if used in the foreground.

schloerke commented 1 year ago

In conversation with @Shelmith-Kariuki, she was able to get around the warnings by creating the App object and supplying it directly to AppDirver. Ex:

# Create app
shiny_app <- shinyreprexpkg::run_app()

# Test app
app <- AppDriver$new(shiny_app)
# test using `app`

In addition, the R folder does not need to be sourced as shinyreprexpkg should exist as an installed package. To get around the setup script check, tests/testthat/setup-shinytest.R file only needs to contain the text load_app_env. For example, this can be inside a comment:

# File: tests/testthat/setup-shinytest2.R

# Do not load code via `load_app_env`

... which will be found with https://github.com/rstudio/shinytest2/blob/6850de4a421fd592873e638d6deda8a4fd176660/R/test-app.R#L293