Skip to content

Commit

Permalink
enforce fewer layout restrictions
Browse files Browse the repository at this point in the history
fixes #998
  • Loading branch information
aronatkins committed Sep 26, 2023
1 parent 41907af commit 3740ee3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 52 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

* Added `space` parameter to deploy directly to a space in Posit Cloud.

* Improve reporting of errors returned by shinyapps.io. (#997)

* Remove most directory layout validation checks. (#998)

# rsconnect 1.1.0

* Fixed analysis of directories that were smaller than the
Expand Down
27 changes: 2 additions & 25 deletions R/appMetadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ appMetadata <- function(appDir,

checkAppLayout <- function(appDir, appPrimaryDoc = NULL) {
appFilesBase <- tolower(list.files(appDir))
wwwFiles <- tolower(list.files(file.path(appDir, "www/")))

primaryIsRScript <- identical(tolower(tools::file_ext(appPrimaryDoc)), "r")

# check for single-file app collision
Expand All @@ -93,29 +91,8 @@ checkAppLayout <- function(appDir, appPrimaryDoc = NULL) {
)
}

# Do some checks for a valid application structure
satisfiedLayouts <- c(
shinyAndUi = all(c("server.r", "ui.r") %in% appFilesBase),
shinyAndIndex = "server.r" %in% appFilesBase && "index.html" %in% wwwFiles,
app = primaryIsRScript || any("app.r" %in% appFilesBase),
Rmd = any(grepl(glob2rx("*.rmd"), appFilesBase)),
Qmd = any(grepl(glob2rx("*.qmd"), appFilesBase)),
static = any(grepl("(?:html?|pdf)$", appFilesBase)),
plumber = any(c("entrypoint.r", "plumber.r") %in% appFilesBase)
)

if (any(satisfiedLayouts)) {
return()
}

cli::cli_abort(c(
"Cancelling deployment: invalid project layout.",
i = "Expecting one of the following publication types:",
" " = "1. A Shiny app with `app.R` or `server.R` + `ui.R`",
" " = "2. R Markdown (`.Rmd`) or Quarto (`.qmd`) documents.",
" " = "3. A website containing `.html` and/or `.pdf` files.",
" " = "4. A plumber API with `plumber.R` or `entrypoint.R`."
))
# all other layouts are allowed; the server determines (with the required packages) if the content
# can be run/served.
}

# infer the mode of the application from files in the root dir
Expand Down
15 changes: 1 addition & 14 deletions tests/testthat/_snaps/appMetadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,11 @@
# checkLayout() errors if primary doc & app.R

Code
checkAppLayout(dir, "myscript.R")
checkAppLayout(dir, appPrimaryDoc = "myscript.R")
Condition
Error in `checkAppLayout()`:
! Project must not contain both 'app.R' and a single-file Shiny app.

# checkLayout fails if no known structure

Code
checkAppLayout(dir)
Condition
Error in `checkAppLayout()`:
! Cancelling deployment: invalid project layout.
i Expecting one of the following publication types:
1. A Shiny app with `app.R` or `server.R` + `ui.R`
2. R Markdown (`.Rmd`) or Quarto (`.qmd`) documents.
3. A website containing `.html` and/or `.pdf` files.
4. A plumber API with `plumber.R` or `entrypoint.R`.

# errors if no files with needed extension

Code
Expand Down
39 changes: 26 additions & 13 deletions tests/testthat/test-appMetadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,43 @@ test_that("checkLayout() errors if primary doc & app.R", {
"myscript.R" = ""
))

expect_snapshot(checkAppLayout(dir, "myscript.R"), error = TRUE)
})

test_that("checkLayout fails if no known structure", {
dir <- local_temp_app(list(
"data.txt" = "",
"cats.csv" = ""
))

expect_snapshot(checkAppLayout(dir), error = TRUE)
expect_snapshot(checkAppLayout(dir, appPrimaryDoc = "myscript.R"), error = TRUE)
})

test_that("checkLayout succeeds with some common app structures", {
rmd <- local_temp_app(list("foo.Rmd" = ""))
expect_no_error(checkAppLayout(rmd))

qmd <- local_temp_app(list("foo.qmd" = ""))
expect_no_error(checkAppLayout(qmd))

# perhaps script.R is a pre-render script that generates *.qmd
project <- local_temp_app(list("_quarto.yml" = "", "script.R" = ""))
expect_no_error(checkAppLayout(project))

md <- local_temp_app(list("_quarto.yml" = "", "index.md" = ""))
expect_no_error(checkAppLayout(md))

shiny1 <- local_temp_app(list("app.R" = ""))
expect_no_error(checkAppLayout(rmd))
expect_no_error(checkAppLayout(shiny1))

shiny2 <- local_temp_app(list("server.R" = "", "ui.R" = ""))
expect_no_error(checkAppLayout(rmd))
expect_no_error(checkAppLayout(shiny2))

api <- local_temp_app(list("plumber.R" = ""))
expect_no_error(checkAppLayout(api))

static <- local_temp_app(list("foo.html" = ""))
expect_no_error(checkAppLayout(rmd))
expect_no_error(checkAppLayout(static))

staticxml <- local_temp_app(list("data.xml" = ""))
expect_no_error(checkAppLayout(staticxml))

staticdata <- local_temp_app(list(
"data.txt" = "",
"cats.csv" = ""
))
expect_no_error(checkAppLayout(staticdata))
})

# inferAppMode ------------------------------------------------------------
Expand Down

0 comments on commit 3740ee3

Please sign in to comment.