From 87eab9af2fb6af78ff2ed08e435eece90e3fab59 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Tue, 10 Sep 2019 10:52:32 -0500 Subject: [PATCH] Also look for templates in man/roxygen/templates Fixes #888 --- NEWS.md | 2 + R/rd-template.R | 8 +++- tests/testthat/man-roxygen/reg.ex.R | 0 .../{ => templates}/man-roxygen/UCase.R | 0 .../{ => templates}/man-roxygen/lcase.r | 0 .../man/roxygen/templates/new-path.R | 2 + tests/testthat/test-rd-template.R | 45 +++++++++---------- 7 files changed, 30 insertions(+), 27 deletions(-) delete mode 100644 tests/testthat/man-roxygen/reg.ex.R rename tests/testthat/{ => templates}/man-roxygen/UCase.R (100%) rename tests/testthat/{ => templates}/man-roxygen/lcase.r (100%) create mode 100644 tests/testthat/templates/man/roxygen/templates/new-path.R diff --git a/NEWS.md b/NEWS.md index 1b4d3fc74..e25b76b7a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # roxygen2 (development version) +* roxygen now looks for templates in `man/roxygen/templates` (#888). + * Files generated on Windows systems now retain their existing line endings, or use unix-style line endings for new files (@jonthegeek, @jimhester, #840). diff --git a/R/rd-template.R b/R/rd-template.R index 187030cf8..5902c5bac 100644 --- a/R/rd-template.R +++ b/R/rd-template.R @@ -1,12 +1,16 @@ template_find <- function(base_path, template_name) { - path <- file.path(base_path, "man-roxygen", paste0(template_name, ".", c("R", "r"))) + file_name <- paste0(template_name, ".", c("R", "r")) + path <- c( + file.path(base_path, "man-roxygen", file_name), + file.path(base_path, "man", "roxygen", "templates", file_name) + ) path_exists <- file.exists(path) if (!any(path_exists)) { stop("Can't find template '", template_name, "'", call. = FALSE) } - path[path_exists][1] + path[path_exists][[1]] } template_eval <- function(template_path, vars) { diff --git a/tests/testthat/man-roxygen/reg.ex.R b/tests/testthat/man-roxygen/reg.ex.R deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/testthat/man-roxygen/UCase.R b/tests/testthat/templates/man-roxygen/UCase.R similarity index 100% rename from tests/testthat/man-roxygen/UCase.R rename to tests/testthat/templates/man-roxygen/UCase.R diff --git a/tests/testthat/man-roxygen/lcase.r b/tests/testthat/templates/man-roxygen/lcase.r similarity index 100% rename from tests/testthat/man-roxygen/lcase.r rename to tests/testthat/templates/man-roxygen/lcase.r diff --git a/tests/testthat/templates/man/roxygen/templates/new-path.R b/tests/testthat/templates/man/roxygen/templates/new-path.R new file mode 100644 index 000000000..c76ed50b6 --- /dev/null +++ b/tests/testthat/templates/man/roxygen/templates/new-path.R @@ -0,0 +1,2 @@ +#' <%= x %> +#' @param <%= y %> <%= z %> diff --git a/tests/testthat/test-rd-template.R b/tests/testthat/test-rd-template.R index 679e08086..fe0093203 100644 --- a/tests/testthat/test-rd-template.R +++ b/tests/testthat/test-rd-template.R @@ -1,43 +1,38 @@ context("Rd: template") -test_that("template_find finds files with .r and .R extension, and fails to find missing files", { - my.tempdir <- "." - my.mandir <- file.path(my.tempdir, "man-roxygen") - my.ucase <- file.path(my.mandir, "UCase.R") - my.regex <- file.path(my.mandir, "reg.ex.R") - my.lcase <- file.path(my.mandir, "lcase.r") +test_that("can find template from name", { + base <- test_path("templates/") - expect_equal(template_find(my.tempdir, "UCase"), my.ucase) - expect_error(template_find(my.tempdir, "Case")) - expect_error(template_find(my.tempdir, "UCas")) - expect_equal(template_find(my.tempdir, "reg.ex"), my.regex) - expect_error(template_find(my.tempdir, "reggex")) - expect_error(template_find(my.tempdir, "nada")) + expect_equal( + template_find(base, "UCase"), + file.path(base, "man-roxygen", "UCase.R") + ) # On case-insentive file systems, will find upper case version first - expect_equal(tolower(template_find(my.tempdir, "lcase")), tolower(my.lcase)) -}) + expect_equal( + tolower(template_find(base, "lcase")), + tolower(file.path(base, "man-roxygen", "lcase.r")) + ) -test_that("templates replace variables with their values", { - out <- roc_proc_text(rd_roclet(), " - #' @template values - #' @templateVar x a - #' @templateVar y b - #' @templateVar z c - x <- 10")[[1]] + expect_equal( + template_find(base, "new-path"), + file.path(base, "man" , "roxygen", "templates", "new-path.R") + ) - expect_equal(get_tag(out, "title")$values, "a") - expect_equal(get_tag(out, "param")$values, c(b = "c")) + expect_error( + template_find(base, "missing"), + "Can't find template" + ) }) -test_that("allow empty line after @template", { +test_that("templates replace variables with their values", { out <- roc_proc_text(rd_roclet(), " #' @template values - #' #' @templateVar x a #' @templateVar y b #' @templateVar z c x <- 10")[[1]] expect_equal(get_tag(out, "title")$values, "a") + expect_equal(get_tag(out, "param")$values, c(b = "c")) })