Skip to content

Commit

Permalink
Support readme's in inst/readme again
Browse files Browse the repository at this point in the history
This used to work, but I think was accidentally broken along the way.

Fixes #2333
  • Loading branch information
jimhester committed Mar 29, 2021
1 parent d5901f8 commit dc06f38
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 66 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# devtools (development version)

* `build_readme()` now supports readme files located in `inst/README.Rmd`, as intended (#2333)

* DT has been moved from Imports to Suggests. DT is only needed when running
`test_coverage()` so now you'll be prompted to install it when needed.

Expand Down
24 changes: 14 additions & 10 deletions R/build-readme.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ build_rmd <- function(files, path = ".", output_options = list(), ..., quiet = T
# Ensure rendering github_document() doesn't generate HTML file
output_options$html_preview <- FALSE

paths <- path(pkg$path, files)
paths <- files

abs_files <- is_absolute_path(files)

paths[!abs_files] <- path(pkg$path, files[!abs_files])

for (path in paths) {
cli::cli_alert_info("Building {.path {path}}")
callr::r_safe(
Expand All @@ -45,16 +50,15 @@ build_rmd <- function(files, path = ".", output_options = list(), ..., quiet = T
#' @export
build_readme <- function(path = ".", quiet = TRUE, ...) {
pkg <- as.package(path)
inst <- path(pkg$path, "inst")
dirs <- c(pkg$path, if (dir_exists(inst)) inst)

readme_path <- grep(
ignore.case = TRUE, value = TRUE,
"readme[.]rmd",
path_file(dir_ls(dirs))
)

readme_path <- path_abs(dir_ls(ignore.case = TRUE, regexp = "(inst/)?readme[.]rmd", recurse = 1, type = "file"))

if (length(readme_path) == 0) {
return(invisible())
rlang::abort("Can't find a 'README.Rmd' or 'inst/README.Rmd' file.")
}

if (length(readme_path) > 1) {
rlang::abort("Can't have both a 'README.Rmd' and 'inst/README.Rmd' file.")
}

build_rmd(readme_path, path = path, quiet = quiet, ...)
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This is a trimmed down version of create_local_thing from usethis
# https://github.com/jimhester/usethis/blob/de8aa116820a8e54f2f952b341039985d78d0352/tests/testthat/helper.R#L28-L68
create_local_package <- function() {
old_project <- asNamespace("usethis")$proj_get_() # this could be `NULL`, i.e. no active project
old_wd <- getwd()
dir <- file_temp()

withr::defer(envir = parent.frame(), {
proj_set(old_project, force = TRUE)
dir_delete(dir)
setwd(old_wd)
})

usethis::ui_silence({
create_package(dir, rstudio = FALSE, open = FALSE, check_name = FALSE)
proj_set(dir)
})

proj_dir <- proj_get()
setwd(proj_dir)

invisible(proj_dir)
}
48 changes: 40 additions & 8 deletions tests/testthat/test-build-readme.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
test_that("Package readme can be built ", {
test_that("Package readme in root directory can be built ", {
skip_on_cran()

on.exit({
file_delete("testReadme/README.md")
dir_delete("testReadme/man/figures")
})
pkg_path <- create_local_package()

suppressMessages(build_readme("testReadme"))
# errors if no readme found
expect_error(
build_readme(pkg_path),
"Can't find a 'README.Rmd'"
)

expect_true(file_exists(path("testReadme", "README.md")))
expect_false(file_exists(path("testReadme", "README.html")))
suppressMessages(use_readme_rmd())

suppressMessages(build_readme(pkg_path))

expect_true(file_exists(path(pkg_path, "README.md")))
expect_false(file_exists(path(pkg_path, "README.html")))
})

test_that("Package readme in inst/ can be built ", {
skip_on_cran()

pkg_path <- create_local_package()
suppressMessages(use_readme_rmd())
dir_create(pkg_path, "inst")
file_copy(
path(pkg_path, "README.Rmd"),
path(pkg_path, "inst", "README.Rmd")
)

# errors if both a root readme and inst readme found
expect_error(
build_readme(pkg_path),
"Can't have both"
)

file_delete(path(pkg_path, "README.Rmd"))

suppressMessages(build_readme(pkg_path))

expect_true(file_exists(path(pkg_path, "inst", "README.md")))
expect_false(file_exists(path(pkg_path, "README.Rmd")))
expect_false(file_exists(path(pkg_path, "README.md")))
expect_false(file_exists(path(pkg_path, "inst", "README.html")))
})
6 changes: 5 additions & 1 deletion tests/testthat/test-install.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
library(mockery)

pkg <- path_real(test_path("testReadme"))
local({

pkg <- create_local_package()

path2char <- function(x) {
if (inherits(x, "fs_path")) {
Expand Down Expand Up @@ -107,3 +109,5 @@ test_that("install_dev_deps passes ellipsis args to remotes::install_deps", {
c(pkg, dev_dep_defaults, extra)
)
})

})
8 changes: 0 additions & 8 deletions tests/testthat/testReadme/DESCRIPTION

This file was deleted.

2 changes: 0 additions & 2 deletions tests/testthat/testReadme/NAMESPACE

This file was deleted.

37 changes: 0 additions & 37 deletions tests/testthat/testReadme/README.Rmd

This file was deleted.

0 comments on commit dc06f38

Please sign in to comment.