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

Make local_temp_app() usage more consistent #844

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/applications.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# syncAppMetadata deletes deployment records if needed

Code
syncAppMetadata(app)
syncAppMetadata(dir)
Message
Deleting deployment record for deleted app 123.

8 changes: 4 additions & 4 deletions tests/testthat/_snaps/bundlePackage.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
# can snapshot deps with renv

Code
pkgs <- bundlePackages(app_dir)
pkgs <- bundlePackages(dir)
Message
i Capturing R dependencies with renv
v Found 1 dependency

# can snapshot deps with packrat

Code
pkgs <- bundlePackages(app_dir)
pkgs <- bundlePackages(dir)
Message
i Capturing R dependencies with packrat
v Found 1 dependency

# can capture deps from renv lockfile

Code
pkgs <- bundlePackages(app_dir)
pkgs <- bundlePackages(dir)
Message
i Capturing R dependencies from renv.lock
v Found 1 dependency

# error if can't find source

Code
. <- bundlePackages(app_dir)
. <- bundlePackages(dir)
Message
i Capturing R dependencies with renv
v Found 1 dependency
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/bundlePackagePackrat.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# uninstalled packages error

Code
snapshotPackratDependencies(app)
snapshotPackratDependencies(dir)
Condition
Error in `addPackratSnapshot()`:
! Failed to snapshot dependencies
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/deployApp.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
# applicationDeleted() errors or prompts as needed

Code
applicationDeleted(client, target, app)
applicationDeleted(client, target, dir)
Condition
Error in `applicationDeleted()`:
! Failed to find existing application on server; it's probably been deleted.
Expand All @@ -71,7 +71,7 @@
---

Code
. <- applicationDeleted(client, target, app)
. <- applicationDeleted(client, target, dir)
Message
Failed to find existing application on server; it's probably been deleted.
What do you want to do?
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/_snaps/deploymentTarget.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
# errors if no previous deployments and multiple accounts

Code
deploymentTarget(app_dir)
deploymentTarget(dir)
Condition
Error in `deploymentTarget()`:
! Found multiple accounts.
Please disambiguate by setting `server` and/or `account`.
i Available servers: "foo1" and "foo2".
i Available account names: "ron".
Code
deploymentTarget(app_dir, appName = "test")
deploymentTarget(dir, appName = "test")
Condition
Error in `deploymentTarget()`:
! Found multiple accounts.
Expand All @@ -44,7 +44,7 @@
# handles accounts if only server specified

Code
deploymentTarget(app_dir, server = "foo")
deploymentTarget(dir, server = "foo")
Condition
Error in `deploymentTarget()`:
! Found multiple accounts for `server` = "foo".
Expand All @@ -54,7 +54,7 @@
# errors/prompts if multiple deployments

Code
deploymentTarget(app_dir, appName = "test")
deploymentTarget(dir, appName = "test")
Condition
Error:
! This directory has been previously deployed in multiple places.
Expand All @@ -63,7 +63,7 @@
* test (server: server1.com / username: ron): <https://server1.com/ron/123>
* test (server: server2.com / username: ron): <https://server2.com/ron/123>
Code
deploymentTarget(app_dir)
deploymentTarget(dir)
Condition
Error:
! This directory has been previously deployed in multiple places.
Expand All @@ -75,7 +75,7 @@
---

Code
out <- deploymentTarget(app_dir)
out <- deploymentTarget(dir)
Message
This directory has been previously deployed in multiple places.
Which deployment do you want to use?
Expand All @@ -86,7 +86,7 @@
# errors if single deployment and appId doesn't match

Code
deploymentTarget(app_dir, appName = "test", appId = "2")
deploymentTarget(dir, appName = "test", appId = "2")
Condition
Error:
! Supplied `appId` (2) does not match deployment record (1).
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/deployments-find.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# errors if no deployments

Code
findDeployment(app)
findDeployment(dir)
Condition
Error:
! Couldn't find any deployments matching supplied criteria.

# disambiguates multiple deployments

Code
findDeployment(app)
findDeployment(dir)
Condition
Error:
! This directory has been previously deployed in multiple places.
Expand All @@ -21,7 +21,7 @@
---

Code
dep <- findDeployment(app)
dep <- findDeployment(dir)
Message
This directory has been previously deployed in multiple places.
Which deployment do you want to use?
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ local_temp_config <- function(env = caller_env()) {
withr::local_envvar(R_USER_CONFIG_DIR = path, .local_envir = env)
}

local_temp_app <- function(files = list(), env = caller_env()) {
dir <- withr::local_tempdir(.local_envir = env)
local_temp_app <- function(..., .env = caller_env()) {
dir <- withr::local_tempdir(.local_envir = .env)

files <- list(...)
for (name in names(files)) {
content <- files[[name]]
writeLines(content, file.path(dir, name))
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-appDependencies.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
test_that("appDepenencies includes implicit deps", {
path <- local_temp_app(list("test.Rmd" = ""))
deps <- appDependencies(path)
dir <- local_temp_app(test.Rmd = "")
deps <- appDependencies(dir)

expect_true("rmarkdown" %in% deps$Package)
})

test_that("static project doesn't have deps", {
path <- local_temp_app(list("index.html" = ""))
deps <- appDependencies(path)
dir <- local_temp_app(index.html = "")
deps <- appDependencies(dir)

expect_equal(deps, data.frame(
Package = character(),
Expand Down
74 changes: 37 additions & 37 deletions tests/testthat/test-appMetadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test_that("clear error if no files", {
})

test_that("quarto affects mode inference", {
dir <- local_temp_app(list("foo.Rmd" = ""))
dir <- local_temp_app(foo.Rmd = "")

metadata <- appMetadata(dir)
expect_equal(metadata$appMode, "rmd-static")
Expand All @@ -16,53 +16,53 @@ test_that("quarto affects mode inference", {
})

test_that("quarto path is deprecated", {
dir <- local_temp_app(list("foo.Rmd" = ""))
dir <- local_temp_app(foo.Rmd = "")
expect_snapshot(. <- appMetadata(dir, quarto = "abc"))
})

test_that("validates quarto argument", {
dir <- local_temp_app(list("foo.Rmd" = ""))
dir <- local_temp_app(foo.Rmd = "")
expect_snapshot(appMetadata(dir, quarto = 1), error = TRUE)
})


test_that("handles special case of appPrimaryDoc as R file", {
dir <- local_temp_app(list("foo.R" = ""))
dir <- local_temp_app(foo.R = "")
metadata <- appMetadata(dir, appPrimaryDoc = "foo.R")
expect_equal(metadata$appMode, "shiny")
})

# checkLayout -------------------------------------------------------------

test_that("checkLayout() errors if primary doc & app.R", {
dir <- local_temp_app(list(
"app.R" = "",
"myscript.R" = ""
))
dir <- local_temp_app(
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" = ""
))
dir <- local_temp_app(
data.txt = "",
cats.csv = ""
)

expect_snapshot(checkAppLayout(dir), error = TRUE)
})

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

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

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

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

Expand All @@ -79,7 +79,7 @@ test_that("can infer mode for shiny apps", {
})

test_that("can infer mode for static quarto and rmd docs", {
dir <- local_temp_app(list("foo.Rmd" = ""))
dir <- local_temp_app(foo.Rmd = "")
paths <- list.files(dir, full.names = TRUE)

expect_equal(inferAppMode(paths), "rmd-static")
Expand All @@ -93,25 +93,25 @@ test_that("can infer mode for shiny rmd docs", {
c("---", paste0("runtime: ", runtime), "---")
}

dir <- local_temp_app(list("index.Rmd" = yaml_runtime("shiny")))
dir <- local_temp_app(index.Rmd = yaml_runtime("shiny"))
paths <- list.files(dir, full.names = TRUE)
expect_equal(inferAppMode(paths), "rmd-shiny")

dir <- local_temp_app(list("index.Rmd" = yaml_runtime("shinyrmd")))
dir <- local_temp_app(index.Rmd = yaml_runtime("shinyrmd"))
paths <- list.files(dir, full.names = TRUE)
expect_equal(inferAppMode(paths), "rmd-shiny")

dir <- local_temp_app(list("index.Rmd" = yaml_runtime("shiny_prerendered")))
dir <- local_temp_app(index.Rmd = yaml_runtime("shiny_prerendered"))
paths <- list.files(dir, full.names = TRUE)
expect_equal(inferAppMode(paths), "rmd-shiny")

# can pair server.R with shiny runtime
dir <- local_temp_app(list("index.Rmd" = yaml_runtime("shiny"), "server.R" = ""))
dir <- local_temp_app(index.Rmd = yaml_runtime("shiny"), server.R = "")
paths <- list.files(dir, full.names = TRUE)
expect_equal(inferAppMode(paths), "rmd-shiny")

# Beats static rmarkdowns
dir <- local_temp_app(list("index.Rmd" = yaml_runtime("shiny"), "foo.Rmd" = ""))
dir <- local_temp_app(index.Rmd = yaml_runtime("shiny"), foo.Rmd = "")
paths <- list.files(dir, full.names = TRUE)
expect_equal(inferAppMode(paths), "rmd-shiny")
})
Expand All @@ -121,20 +121,20 @@ test_that("can infer mode for shiny qmd docs", {
c("---", paste0("runtime: ", runtime), "---")
}

dir <- local_temp_app(list("index.Qmd" = yaml_runtime("shiny")))
dir <- local_temp_app(index.Qmd = yaml_runtime("shiny"))
paths <- list.files(dir, full.names = TRUE)
expect_equal(inferAppMode(paths), "quarto-shiny")

# Can force Rmd to use quarto
dir <- local_temp_app(list("index.Rmd" = yaml_runtime("shiny")))
dir <- local_temp_app(index.Rmd = yaml_runtime("shiny"))
paths <- list.files(dir, full.names = TRUE)
expect_equal(inferAppMode(paths, usesQuarto = TRUE), "quarto-shiny")

# Prefers quarto if both present
dir <- local_temp_app(list(
"index.Qmd" = yaml_runtime("shiny"),
"index.Rmd" = yaml_runtime("shiny")
))
dir <- local_temp_app(
index.Qmd = yaml_runtime("shiny"),
index.Rmd = yaml_runtime("shiny")
)
paths <- list.files(dir, full.names = TRUE)
expect_equal(inferAppMode(paths), "quarto-shiny")
})
Expand Down Expand Up @@ -187,29 +187,29 @@ test_that("errors if no files with needed extension", {
# appHasParameters --------------------------------------------------------

test_that("non-documents don't have parameters", {
dir <- local_temp_app(list("foo.R" = ""))
dir <- local_temp_app(foo.R = "")

expect_false(appHasParameters(dir, "foo.R", "static"))
expect_false(appHasParameters(dir, "foo.R", "shiny"))
})

test_that("documents don't have parameters if part of a site", {
dir <- local_temp_app(list("index.Rmd" = c("---", "params: [1, 2]", "---")))
dir <- local_temp_app(index.Rmd = c("---", "params: [1, 2]", "---"))

expect_false(appHasParameters(dir, "index.Rmd", "rmd-static", "site"))
expect_false(appHasParameters(dir, "index.Rmd", "qmd-shiny", "site"))
})

test_that("non-Rmd files don't have parameters", {
dir <- local_temp_app(list("app.r" = c("")))
dir <- local_temp_app(app.r = c(""))
expect_false(appHasParameters(dir, "app.R", "rmd-shiny"))
})

test_that("otherwise look at yaml metadata", {
dir <- local_temp_app(list("index.Rmd" = c("---", "params: [1, 2]", "---")))
dir <- local_temp_app(index.Rmd = c("---", "params: [1, 2]", "---"))
expect_true(appHasParameters(dir, "index.Rmd", "rmd-shiny"))

dir <- local_temp_app(list("index.Rmd" = c("---", "params: ~", "---")))
dir <- local_temp_app(index.Rmd = c("---", "params: ~", "---"))
expect_false(appHasParameters(dir, "index.Rmd", "rmd-shiny"))
})

Expand All @@ -219,17 +219,17 @@ test_that("dir without Rmds doesn't have have python", {
dir <- local_temp_app()
expect_false(detectPythonInDocuments(dir))

dir <- local_temp_app(list("foo.R" = ""))
dir <- local_temp_app(foo.R = "")
expect_false(detectPythonInDocuments(dir))
})

test_that("Rmd or qmd with python chunk has python", {
dir <- local_temp_app(list("foo.qmd" = c("```{r}", "1+1", "````")))
dir <- local_temp_app(foo.qmd = c("```{r}", "1+1", "````"))
expect_false(detectPythonInDocuments(dir))

dir <- local_temp_app(list("foo.Rmd" = c("```{python}", "1+1", "````")))
dir <- local_temp_app(foo.Rmd = c("```{python}", "1+1", "````"))
expect_true(detectPythonInDocuments(dir))

dir <- local_temp_app(list("foo.qmd" = c("```{python}", "1+1", "````")))
dir <- local_temp_app(foo.qmd = c("```{python}", "1+1", "````"))
expect_true(detectPythonInDocuments(dir))
})
Loading