diff --git a/R/launch_editor.R b/R/launch_editor.R index 5da66e708..ae1e20caf 100644 --- a/R/launch_editor.R +++ b/R/launch_editor.R @@ -71,9 +71,15 @@ launch_editor <- function(app_loc, # "editing-app". This is used to know what to do on close server_mode <- "initializing" + # Validate that we're pointing to a directory. If the user has supplied a + # direct file. E.g. a app.R or app.py file we should back up the app loc to + # the parent location + app_loc <- validateAppLoc(app_loc) + # Type of app we're in. Can be "SINGLE-FILE", "MULTI-FILE", or "MISSING" app_type <- get_app_file_type(app_loc) + # ---------------------------------------------------------------------------- # Initialize classes for controling app preview and polling for updates # ---------------------------------------------------------------------------- diff --git a/R/validateAppLoc.R b/R/validateAppLoc.R new file mode 100644 index 000000000..fd15d0cd4 --- /dev/null +++ b/R/validateAppLoc.R @@ -0,0 +1,13 @@ +# Validate that we're pointing to a directory. If the user has supplied a +# direct file. E.g. a app.R or app.py file we should back up the app loc to +# the parent location +validateAppLoc <- function(loc) { + + # If the file ends in app.R or ui.R or server.R we should back up the + # location to the parent directory + if (fs::path_ext(loc) %in% "R") { + loc <- fs::path_dir(loc) + } + + loc +} \ No newline at end of file diff --git a/scratch/start_editor_non_interactive.R b/scratch/start_editor_non_interactive.R index 796a72025..f1e223676 100644 --- a/scratch/start_editor_non_interactive.R +++ b/scratch/start_editor_non_interactive.R @@ -1,8 +1,8 @@ devtools::load_all(".") launch_editor( - app_loc = here::here("scratch/a-brand-new-app3/"), + app_loc = here::here("scratch/a-brand-new-app3"), port = 8888, - launch_browser = FALSE, - stop_on_browser_close = FALSE + # launch_browser = FALSE, + # stop_on_browser_close = FALSE ) diff --git a/tests/testthat/test-app-location-validation.R b/tests/testthat/test-app-location-validation.R new file mode 100644 index 000000000..32430416b --- /dev/null +++ b/tests/testthat/test-app-location-validation.R @@ -0,0 +1,13 @@ + +test_that("Handles being passed path directly to app script, or to folder container app", { + + expect_equal( + validateAppLoc("my/app/loc/app.R"), + validateAppLoc("my/app/loc") + ) + + expect_equal( + validateAppLoc("my/app/loc/ui.R"), + validateAppLoc("my/app/loc") + ) +}) diff --git a/tests/testthat/test-serialize_ast.R b/tests/testthat/test-serialize_ast.R deleted file mode 100644 index ef3703276..000000000 --- a/tests/testthat/test-serialize_ast.R +++ /dev/null @@ -1,39 +0,0 @@ - -test_that("Can fill in unnamed named arguments", { - ui_expr <- rlang::expr({ - library(gridlayout) - grid_card_text("myArea", content = "B") - }) - - serialized <- serialize_ast(ui_expr) - - grid_card_name_arg <- serialized[[3]]$val[[2]] - expect_equal( - grid_card_name_arg$name, - "area" - ) - expect_equal( - grid_card_name_arg$val, - "myArea" - ) -}) - - -test_that("Can handle functions declared in scope. E.g. reactives.", { - expr_with_fn_declaration <- rlang::expr({ - my_fn <- function(name) paste("hello", name) - my_fn("shiny") - }) - - serialized <- serialize_ast(expr_with_fn_declaration) - - my_fn_call <- serialized[[3]] - - expect_equal( - my_fn_call$val, - list( - list(val = "my_fn", type = "s"), - list(val = "shiny", type = "c") - ) - ) -})