Skip to content

Commit

Permalink
Check for and fix fs functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed May 13, 2024
1 parent b5a1991 commit 92f25f8
Show file tree
Hide file tree
Showing 19 changed files with 279 additions and 32 deletions.
10 changes: 10 additions & 0 deletions .lintr.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ linters <- list(lintr::undesirable_function_linter(
"cli_alert_warning" = "use cli::cli_inform()",
# fs
"file.path" = "use path()",
"dir" = "use dir_ls()",
"dir.create" = "use dir_create()",
"file.copy" = "use file_copy()",
"file.create" = "use file_create()",
"file.exists" = "use file_exists()",
"file.info" = "use file_info()",
"normalizePath" = "use path_real()",
"unlink" = "use file_delete()",
"basename" = "use path_file()",
"dirname" = "use path_dir()",
# i/o
"readLines" = "use read_lines()",
"writeLines" = "use write_lines()"
Expand Down
5 changes: 2 additions & 3 deletions R/build-favicons.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ build_favicons <- function(pkg = ".", overwrite = FALSE) {
cli::cli_abort("API request failed.", .internal = TRUE)
}

tmp <- tempfile()
on.exit(unlink(tmp))
tmp <- withr::local_tempdir()
result <- httr::RETRY(
"GET",
result$favicon$package_url,
Expand Down Expand Up @@ -120,5 +119,5 @@ copy_favicons <- function(pkg = ".") {
has_favicons <- function(pkg = ".") {
pkg <- as_pkgdown(pkg)

file.exists(path(pkg$src_path, "pkgdown", "favicon"))
file_exists(path(pkg$src_path, "pkgdown", "favicon"))
}
2 changes: 1 addition & 1 deletion R/build-home-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ data_home_sidebar <- function(pkg = ".", call = caller_env()) {
html_path <- path(pkg$src_path, pkg$meta$home$sidebar$html)

if (length(html_path)) {
if (!file.exists(html_path)) {
if (!file_exists(html_path)) {
rel_html_path <- path_rel(html_path, pkg$src_path)
config_abort(
pkg,
Expand Down
2 changes: 1 addition & 1 deletion R/build-home-md.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ render_md <- function(pkg, filename) {
cli::cli_inform("Reading {src_path(path_rel(filename, pkg$src_path))}")

body <- markdown_body(filename, strip_header = TRUE)
path <- path_ext_set(basename(filename), "html")
path <- path_ext_set(path_file(filename), "html")

render_page(pkg, "title-body",
data = list(
Expand Down
2 changes: 1 addition & 1 deletion R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ package_vignettes <- function(path = ".") {
description = desc,
depth = dir_depth(file_out)
)
out[order(basename(out$file_out)), ]
out[order(path_file(out$file_out)), ]
}

find_template_config <- function(package,
Expand Down
3 changes: 0 additions & 3 deletions R/pkgdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ local_pkgdown_site <- function(path = NULL, meta = NULL, env = parent.frame()) {
}
pkg <- as_pkgdown(path, meta)
pkg$dst_path <- withr::local_tempdir(.local_envir = env)

withr::defer(unlink(pkg$dst_path, recursive = TRUE), envir = env)

pkg
}

Expand Down
8 changes: 4 additions & 4 deletions R/rmarkdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ render_rmarkdown <- function(pkg, input, output, ..., seed = NULL, copy_images =

# temporarily copy the rendered html into the input path directory and scan
# again for additional external resources that may be been included by R code
tempfile_in_input_dir <- file_temp(ext = "html", tmp_dir = path_dir(input_path))
file_copy(path, tempfile_in_input_dir)
withr::defer(unlink(tempfile_in_input_dir))
ext_post <- rmarkdown::find_external_resources(tempfile_in_input_dir)
tempfile <- path(path_dir(input_path), "--find-assets.html")
withr::defer(try(file_delete(tempfile)))
file_copy(path, tempfile)
ext_post <- rmarkdown::find_external_resources(tempfile)

ext <- rbind(ext_src, ext_post)
ext <- ext[!duplicated(ext$path), ]
Expand Down
2 changes: 1 addition & 1 deletion R/theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ build_bslib <- function(pkg = ".") {
}

data_deps <- function(pkg, depth) {
if (!file.exists(data_deps_path(pkg))) {
if (!file_exists(data_deps_path(pkg))) {
cli::cli_abort(
"Run {.fn pkgdown::init_site} first.",
call = caller_env()
Expand Down
2 changes: 1 addition & 1 deletion R/utils-fs.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ out_of_date <- function(source, target) {
)
}

file.info(source)$mtime > file.info(target)$mtime
file_mtime(source) > file_mtime(target)
}

# Path helpers ------------------------------------------------------------
Expand Down
241 changes: 241 additions & 0 deletions tests/testthat/assets/-find-assets.html

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions tests/testthat/test-build-articles.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test_that("articles don't include header-attrs.js script", {
html <- xml2::read_html(path)
js <- xpath_attr(html, ".//body//script", "src")
# included for pandoc 2.7.3 - 2.9.2.1 improve accessibility
js <- js[basename(js) != "empty-anchor.js"]
js <- js[path_file(js) != "empty-anchor.js"]
expect_equal(js, character())
})

Expand Down Expand Up @@ -90,8 +90,8 @@ test_that("html widgets get needed css/js", {
css <- xpath_attr(html, ".//body//link", "href")
js <- xpath_attr(html, ".//body//script", "src")

expect_true("diffviewer.css" %in% basename(css))
expect_true("diffviewer.js" %in% basename(js))
expect_true("diffviewer.css" %in% path_file(css))
expect_true("diffviewer.js" %in% path_file(js))
})

test_that("can override options with _output.yml", {
Expand Down Expand Up @@ -163,8 +163,8 @@ test_that("articles in vignettes/articles/ are unnested into articles/", {
suppressMessages(path <- build_article("articles/nested", pkg))

expect_equal(
normalizePath(path),
normalizePath(path(pkg$dst_path, "articles", "nested.html"))
path_real(path),
path_real(path(pkg$dst_path, "articles", "nested.html"))
)

# Check automatic redirect from articles/articles/foo.html -> articles/foo.html
Expand Down Expand Up @@ -250,7 +250,7 @@ test_that("check doesn't include getting started vignette", {
pkg <- local_pkgdown_site(test_path("assets/articles-resources"))
getting_started <- path(pkg$src_path, "vignettes", paste0(pkg$package, ".Rmd"))
file_create(getting_started)
withr::defer(unlink(getting_started))
withr::defer(file_delete(getting_started))

pkg <- local_pkgdown_site(test_path("assets/articles-resources"), meta = "
articles:
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-build-home.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test_that("intermediate files cleaned up automatically", {
pkg <- local_pkgdown_site(test_path("assets/home-index-rmd"))
suppressMessages(build_home(pkg))

expect_setequal(dir(pkg$src_path), c("DESCRIPTION", "index.Rmd"))
expect_setequal(path_file(dir_ls(pkg$src_path)), c("DESCRIPTION", "index.Rmd"))
})

test_that("intermediate files cleaned up automatically", {
Expand All @@ -16,7 +16,7 @@ test_that("intermediate files cleaned up automatically", {
suppressMessages(build_home(pkg))

expect_setequal(
dir(pkg$src_path),
path_file(dir_ls(pkg$src_path)),
c("NAMESPACE", "DESCRIPTION", "README.md", "README.Rmd")
)
})
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-build-reference.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test_that("examples_env sets width", {
code:
width: 50
")
dir.create(path(pkg$dst_path, "reference"), recursive = TRUE)
dir_create(path(pkg$dst_path, "reference"))

examples_env(pkg)
expect_equal(getOption("width"), 50)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-check-built.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_that("readme can use images from vignettes", {
test_path("assets/articles-images/man/figures/kitten.jpg"),
path(pkg$src_path, "vignettes/kitten.jpg")
)
withr::defer(unlink(path(pkg$src_path, "vignettes/kitten.jpg")))
withr::defer(file_delete(path(pkg$src_path, "vignettes/kitten.jpg")))

suppressMessages(build_home(pkg))
suppressMessages(build_articles(pkg))
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-highlight.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("highlight_examples captures dependencies", {
withr::defer(unlink(test_path("Rplot001.png")))
withr::defer(file_delete(test_path("Rplot001.png")))

dummy_dep <- htmltools::htmlDependency("dummy", "1.0.0", "dummy.js")
widget <- htmlwidgets::createWidget("test", list(), dependencies = dummy_dep)
Expand All @@ -10,7 +10,7 @@ test_that("highlight_examples captures dependencies", {
})

test_that("highlight_text & highlight_examples include sourceCode div", {
withr::defer(unlink(test_path("Rplot001.png")))
withr::defer(file_delete(test_path("Rplot001.png")))

html <- xml2::read_html(highlight_examples("a + a", "x"))
expect_equal(xpath_attr(html, "./body/div", "class"), "sourceCode")
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test_that("handles empty inputs", {
expect_equal(markdown_text_block(""), NULL)

path <- withr::local_tempfile()
file.create(path)
file_create(path)
expect_equal(markdown_body(path), NULL)
})

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-navbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ test_that("data_navbar() can re-order default elements", {
left: [github, search]
right: [news]
")
file.create(path(pkg$src_path, "NEWS.md"))
file_create(path(pkg$src_path, "NEWS.md"))

expect_snapshot(data_navbar(pkg)[c("left", "right")])
})
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ test_that("package_vignettes() detects conflicts in final article paths", {
test_that("package_vignettes() sorts articles alphabetically by file name", {
pkg <- local_pkgdown_site(test_path("assets/articles"))
expect_equal(
order(basename(pkg$vignettes$file_out)),
order(path_file(pkg$vignettes$file_out)),
seq_len(nrow(pkg$vignettes))
)
})
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-templates.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ test_that("find templates in local pkgdown first", {
# Expected contents -------------------------------------------------------

test_that("BS5 templates have main + aside", {
names <- dir(path_pkgdown("bs5", "templates"), pattern = "content-")
names <- path_ext_remove(names)
names <- dir_ls(path_pkgdown("bs5", "templates"), regexp = "content-")
names <- path_ext_remove(path_file(names))
names <- gsub("content-", "", names)

templates <- lapply(names, read_template_html,
Expand Down

0 comments on commit 92f25f8

Please sign in to comment.