diff --git a/DESCRIPTION b/DESCRIPTION index cf3d66a..58a8e73 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -18,4 +18,4 @@ Config/testthat/edition: 3 Encoding: UTF-8 LazyData: true Roxygen: list(markdown = TRUE) -RoxygenNote: 7.1.2 +RoxygenNote: 7.2.0 diff --git a/R/golem_hook.R b/R/golem_hook.R index 9aceadd..6abe4ea 100644 --- a/R/golem_hook.R +++ b/R/golem_hook.R @@ -1,4 +1,4 @@ -# Generated by fusen: do not edit by hand +# WARNING - Generated by {fusen} from /dev/flat_golem_hook.Rmd: do not edit by hand #' Golem Hook function #' @@ -10,14 +10,11 @@ #' if (requireNamespace("golem") & interactive()) { #' golem::create_golem("myapp", project_hook = golem_hook) #' } -golem_hook <- function( - path, - package_name, - ... -) { +golem_hook <- function(path, package_name, ...) { unlink("R/run_app.R", TRUE, TRUE) unlink("R/app_ui.R", TRUE, TRUE) unlink("R/app_server.R", TRUE, TRUE) + file.copy( system.file( "golem/run_app.R", @@ -25,7 +22,7 @@ golem_hook <- function( ), "R/run_app.R" ) - + # Include `package_name` as `app_title` in in run_app() run_app <- readLines( "R/run_app.R" ) @@ -35,7 +32,7 @@ golem_hook <- function( run_app ) write(run_app, "R/run_app.R") - + file.copy( system.file( "golem/mod_home.R", @@ -43,7 +40,7 @@ golem_hook <- function( ), "R/mod_home.R" ) - + dev_R <- readLines( "dev/02_dev.R" ) @@ -55,7 +52,7 @@ golem_hook <- function( ) ) ] <- 'golem::add_module(name = "name_of_module1", with_test = TRUE, module_template = brochure::new_page) # Name of the module' - + dev_R[ which( grepl( diff --git a/R/new_page.R b/R/new_page.R index 3cca478..b5dfad2 100644 --- a/R/new_page.R +++ b/R/new_page.R @@ -1,4 +1,4 @@ -# Generated by fusen: do not edit by hand +# WARNING - Generated by {fusen} from /dev/flat_add_page.Rmd: do not edit by hand #' Add page #' diff --git a/brochure.Rproj b/brochure.Rproj new file mode 100644 index 0000000..21a4da0 --- /dev/null +++ b/brochure.Rproj @@ -0,0 +1,17 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source diff --git a/dev/flat_add_page.Rmd b/dev/flat_add_page.Rmd index 68e09cf..8165eca 100644 --- a/dev/flat_add_page.Rmd +++ b/dev/flat_add_page.Rmd @@ -57,7 +57,7 @@ if (requireNamespace("golem") & interactive()) { test_that("golem_hook works", { x <- tempfile(fileext = ".R") new_page("pouet", x) - + tmplt <- readLines( x ) @@ -71,17 +71,27 @@ test_that("golem_hook works", { expect_true( grepl("page", tmplt) ) - + skip_if_not_installed("golem") - old <- setwd(tempdir()) - on.exit(setwd(old)) - unlink("testgolembrochure", TRUE, TRUE) - golem::create_golem("testgolembrochure", project_hook = brochure::golem_hook) - setwd("testgolembrochure") + + path_dummy_golem <- tempfile(pattern = "dummygolem") + golem::create_golem( + path = path_dummy_golem, + open = FALSE, + project_hook = brochure::golem_hook + ) + + old_wd <- setwd(path_dummy_golem) + on.exit({ + unlink(path_dummy_golem, TRUE, TRUE) + setwd(old_wd) + }) + golem::add_module(name = "pouet", module_template = brochure::new_page) expect_true( file.exists("R/mod_pouet.R") ) + }) ``` @@ -89,6 +99,11 @@ test_that("golem_hook works", { ```{r development-inflate, eval=FALSE} # Run but keep eval=FALSE to avoid infinite loop # Execute in the console directly -fusen::inflate(flat_file = "dev/flat_add_page.Rmd", vignette_name = NA, document = FALSE) +fusen::inflate( + flat_file = "dev/flat_add_page.Rmd", + vignette_name = NA, + check = FALSE, + document = FALSE +) ``` diff --git a/dev/flat_golem_hook.Rmd b/dev/flat_golem_hook.Rmd index 8a771c4..cd8b45c 100644 --- a/dev/flat_golem_hook.Rmd +++ b/dev/flat_golem_hook.Rmd @@ -28,6 +28,7 @@ golem_hook <- function(path, package_name, ...) { unlink("R/run_app.R", TRUE, TRUE) unlink("R/app_ui.R", TRUE, TRUE) unlink("R/app_server.R", TRUE, TRUE) + file.copy( system.file( "golem/run_app.R", @@ -35,6 +36,17 @@ golem_hook <- function(path, package_name, ...) { ), "R/run_app.R" ) + # Include `package_name` as `app_title` in in run_app() + run_app <- readLines( + "R/run_app.R" + ) + run_app <- gsub( + "REPLACEME", + package_name, + run_app + ) + write(run_app, "R/run_app.R") + file.copy( system.file( "golem/mod_home.R", @@ -42,7 +54,7 @@ golem_hook <- function(path, package_name, ...) { ), "R/mod_home.R" ) - + dev_R <- readLines( "dev/02_dev.R" ) @@ -54,7 +66,7 @@ golem_hook <- function(path, package_name, ...) { ) ) ] <- 'golem::add_module(name = "name_of_module1", with_test = TRUE, module_template = brochure::new_page) # Name of the module' - + dev_R[ which( grepl( @@ -76,12 +88,23 @@ if (requireNamespace("golem") & interactive()) { ```{r tests-my_fun} test_that("golem_hook works", { + skip_if_not_installed("golem") - old <- setwd(tempdir()) - on.exit(setwd(old)) - unlink("testgolembrochure", TRUE, TRUE) - golem::create_golem("testgolembrochure", project_hook = brochure::golem_hook) - setwd("testgolembrochure") + + path_dummy_golem <- tempfile(pattern = "dummygolem") + + golem::create_golem( + path = path_dummy_golem, + open = FALSE, + project_hook = brochure::golem_hook + ) + + old_wd <- setwd(path_dummy_golem) + on.exit({ + unlink(path_dummy_golem, TRUE, TRUE) + setwd(old_wd) + }) + expect_true( file.exists("R/mod_home.R") ) @@ -99,24 +122,44 @@ test_that("golem_hook works", { "R/mod_home.R" ) ) - expect_equal( - readLines( - system.file( - "golem/run_app.R", - package = "brochure" - ) - ), - readLines( - "R/run_app.R" + + remove_app_title_line <- function(run_app_lines) { + grep( + pattern = "app_title = ", + x = run_app_lines, + value = TRUE, + invert = TRUE + ) + } + run_app_brochure_template <-readLines( + system.file( + "golem/run_app.R", + package = "brochure" ) ) - + run_app_dummy_golem <- readLines("R/run_app.R") + ## run_app is properly copied and... + expect_equal( + remove_app_title_line(run_app_brochure_template), + remove_app_title_line(run_app_dummy_golem) + ) + # ...app_title was modified + expect_error( + expect_equal( + run_app_brochure_template, + run_app_dummy_golem + ), + regexp = "app_title = " + ) + + # Module template was added in calls to golem::add_module() in 02_dev.R expect_true( grepl( - "brochure", + "module_template = brochure::new_page", paste(readLines("dev/02_dev.R"), collapse = " ") ) ) + }) ``` @@ -124,6 +167,11 @@ test_that("golem_hook works", { ```{r development-inflate, eval=FALSE} # Run but keep eval=FALSE to avoid infinite loop # Execute in the console directly -fusen::inflate(flat_file = "dev/flat_golem_hook.Rmd", vignette_name = NA, document = FALSE) +fusen::inflate( + flat_file = "dev/flat_golem_hook.Rmd", + vignette_name = NA, + check = FALSE, + document = FALSE +) ``` diff --git a/man/page.Rd b/man/page.Rd index 366808d..563285b 100644 --- a/man/page.Rd +++ b/man/page.Rd @@ -7,7 +7,8 @@ page( href, ui = tagList(), - server = function(input, output, session) { }, + server = function(input, output, session) { + }, req_handlers = list(), res_handlers = list() ) diff --git a/tests/testthat/test-golem_hook.R b/tests/testthat/test-golem_hook.R index f169643..73ac924 100644 --- a/tests/testthat/test-golem_hook.R +++ b/tests/testthat/test-golem_hook.R @@ -1,12 +1,20 @@ -# Generated by fusen: do not edit by hand +# WARNING - Generated by {fusen} from /dev/flat_golem_hook.Rmd: do not edit by hand test_that("golem_hook works", { + skip_if_not_installed("golem") - old <- setwd(tempdir()) - on.exit(setwd(old)) - unlink("testgolembrochure", TRUE, TRUE) - golem::create_golem("testgolembrochure", project_hook = brochure::golem_hook) - setwd("testgolembrochure") + + path_dummy_golem <- tempfile(pattern = "dummygolem") + + golem::create_golem( + path = path_dummy_golem, + open = FALSE, + project_hook = brochure::golem_hook + ) + + old_wd <- setwd(path_dummy_golem) + on.exit(setwd(old_wd)) + expect_true( file.exists("R/mod_home.R") ) @@ -24,22 +32,42 @@ test_that("golem_hook works", { "R/mod_home.R" ) ) - expect_equal( - readLines( - system.file( - "golem/run_app.R", - package = "brochure" - ) - ), - readLines( - "R/run_app.R" + + remove_app_title_line <- function(run_app_lines) { + grep( + pattern = "app_title = ", + x = run_app_lines, + value = TRUE, + invert = TRUE + ) + } + run_app_brochure_template <-readLines( + system.file( + "golem/run_app.R", + package = "brochure" ) ) - + run_app_dummy_golem <- readLines("R/run_app.R") + ## run_app is properly copied and... + expect_equal( + remove_app_title_line(run_app_brochure_template), + remove_app_title_line(run_app_dummy_golem) + ) + # ...app_title was modified + expect_error( + expect_equal( + run_app_brochure_template, + run_app_dummy_golem + ), + regexp = "app_title = " + ) + + # Module template was added in calls to golem::add_module() in 02_dev.R expect_true( grepl( - "brochure", + "module_template = brochure::new_page", paste(readLines("dev/02_dev.R"), collapse = " ") ) ) + }) diff --git a/tests/testthat/test-new_page.R b/tests/testthat/test-new_page.R index 11d3ff0..0ed1641 100644 --- a/tests/testthat/test-new_page.R +++ b/tests/testthat/test-new_page.R @@ -1,9 +1,9 @@ -# Generated by fusen: do not edit by hand +# WARNING - Generated by {fusen} from /dev/flat_add_page.Rmd: do not edit by hand test_that("golem_hook works", { x <- tempfile(fileext = ".R") new_page("pouet", x) - + tmplt <- readLines( x ) @@ -17,15 +17,25 @@ test_that("golem_hook works", { expect_true( grepl("page", tmplt) ) - + skip_if_not_installed("golem") - old <- setwd(tempdir()) - on.exit(setwd(old)) - unlink("testgolembrochure", TRUE, TRUE) - golem::create_golem("testgolembrochure", project_hook = brochure::golem_hook) - setwd("testgolembrochure") + + path_dummy_golem <- tempfile(pattern = "dummygolem") + golem::create_golem( + path = path_dummy_golem, + open = FALSE, + project_hook = brochure::golem_hook + ) + + old_wd <- setwd(path_dummy_golem) + on.exit({ + unlink(path_dummy_golem, TRUE, TRUE) + setwd(old_wd) + }) + golem::add_module(name = "pouet", module_template = brochure::new_page) expect_true( file.exists("R/mod_pouet.R") ) + })