Skip to content
Merged

Fix 838 #1238

Show file tree
Hide file tree
Changes from all 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
77 changes: 64 additions & 13 deletions R/add_r_files.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ add_r_files <- function(
open = TRUE,
dir_create = TRUE,
with_test = FALSE,
template = NULL,
...,
pkg
) {
signal_arg_is_deprecated(
Expand Down Expand Up @@ -140,12 +142,26 @@ add_r_files <- function(
module
)
) {
# Must be a function or utility file being created
append_roxygen_comment(
name = name,
path = where,
ext = ext
)
if (
ext == "fct" &&
!is.null(
template
)
) {
template(
name = name,
path = where,
export = FALSE,
...
)
} else {
# Must be a function or utility file being created
append_roxygen_comment(
name = name,
path = where,
ext = ext
)
}
}

cat_created(
Expand Down Expand Up @@ -184,6 +200,10 @@ add_r_files <- function(
#' @param name The name of the file
#' @param module If not NULL, the file will be module specific
#' in the naming (you don't need to add the leading `mod_`).
#' @param template Function writing the default contents of a function file.
#' Defaults to the built-in function template. Ignored for
#' module-specific files.
#' @param ... Arguments passed to the `template` function.
#' @inheritParams add_module
#'
#' @rdname file_creation
Expand All @@ -197,6 +217,8 @@ add_fct <- function(
open = TRUE,
dir_create = TRUE,
with_test = FALSE,
template = fct_template,
...,
pkg
) {
signal_arg_is_deprecated(
Expand All @@ -207,13 +229,15 @@ add_fct <- function(
"pkg"
)
add_r_files(
name,
module,
name = name,
ext = "fct",
module = module,
golem_wd = golem_wd,
open = open,
dir_create = dir_create,
with_test = with_test
with_test = with_test,
template = template,
...
)
}

Expand All @@ -236,9 +260,9 @@ add_utils <- function(
"pkg"
)
add_r_files(
name,
module,
name = name,
ext = "utils",
module = module,
golem_wd = golem_wd,
open = open,
dir_create = dir_create,
Expand All @@ -265,15 +289,42 @@ add_r6 <- function(
"pkg"
)
add_r_files(
name,
module,
name = name,
ext = "class",
module = module,
golem_wd = golem_wd,
open = open,
dir_create = dir_create,
with_test = with_test
)
}

#' Default template for `add_fct()`
#'
#' This function does not aim at being called as is by
#' users, but to be passed as an argument to [add_fct()].
#'
#' @param path The path to the R script where the function will be written.
#' @param name The name of the generated function.
#' @param export Whether the generated function should be exported.
#'
#' @return Used for side effect
#' @keywords internal
#' @noRd
fct_template <- function(
name,
path,
export = FALSE,
...
) {
append_roxygen_comment(
name = name,
path = path,
ext = "fct",
export = export
)
}

#' Append roxygen comments to `fct_` and `utils_` files
#'
#' This function add boilerplate roxygen comments
Expand Down
8 changes: 8 additions & 0 deletions man/file_creation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions tests/testthat/test-add_r_files.R
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,86 @@ test_that("add_fct sanitizes names correctly", {
)
})
})

test_that("fct_template works", {
fct_template(
"my_fun",
path <- tempfile(),
export = TRUE
)
on.exit({
unlink(
path,
recursive = TRUE,
force = TRUE
)
})
fct_read <- paste(
readLines(
path
),
collapse = " "
)
expect_true(
grepl(
"my_fun <- function",
fct_read
)
)
expect_true(
grepl(
"@export",
fct_read
)
)
})

test_that("add_fct accepts a template", {
run_quietly_in_a_dummy_golem({
add_fct(
"custom",
open = FALSE,
template = function(
name,
path,
export = FALSE,
...
) {
writeLines(
c(
"#' custom function",
if (export) "#' @export" else "#' @noRd",
sprintf(
"%s <- function() \"ok\"",
name
)
),
con = path
)
}
)

expect_exists(
file.path(
"R",
"fct_custom.R"
)
)

file_content <- paste(
readLines(
file.path(
"R",
"fct_custom.R"
)
),
collapse = " "
)
expect_true(
grepl(
'custom <- function\\(\\) "ok"',
file_content
)
)
})
})
Loading