Skip to content

Commit

Permalink
Exclude invalid filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed May 28, 2024
1 parent 1f50199 commit 07b63e9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
3 changes: 3 additions & 0 deletions R/build-redirects.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ reference_redirects <- function(pkg) {

names(redirects) <- paste0(names(redirects), ".html")

# Ensure we don't create an invalid file name
redirects <- redirects[grepl("^[a-zA-Z0-9._-]+$", names(redirects))]

# Ensure we don't override an existing file
redirects <- redirects[setdiff(names(redirects), pkg$topics$file_out)]

Expand Down
16 changes: 15 additions & 1 deletion tests/testthat/test-build-redirects.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ test_that("generates redirects only for non-name aliases", {
)
})

test_that("doesn't generates redirect for aliases that can't be file names", {
pkg <- list(
meta = list(url = "http://foo.com"),
topics = list(
name = "bar",
alias = list(c("bar", "baz", "[<-.baz")),
file_out = "bar.html"
)
)
expect_equal(
reference_redirects(pkg),
list(c("reference/baz.html", "reference/bar.html"))
)
})

test_that("never redirects away from existing topic", {
pkg <- list(
meta = list(url = "http://foo.com"),
Expand All @@ -89,4 +104,3 @@ test_that("no redirects if no aliases", {
)
expect_equal(reference_redirects(pkg), list())
})

8 changes: 0 additions & 8 deletions tests/testthat/test-build-search-docs.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,6 @@ test_that("build_search() builds the expected search.json with no URL", {
expect_snapshot_file(json_path, "search-no-url.json")
})

test_that("build_sitemap() handles special xml characters", {
pkg <- local_pkgdown_site()
file_create(path(pkg$dst_path, "[<-.foo.html"))

suppressMessages(build_sitemap(pkg))
expect_no_error(xml2::read_xml(path(pkg$dst_path, "sitemap.xml")))
})

test_that("sitemap excludes redirects", {
pkg <- local_pkgdown_site(meta = list(
url = "https://example.com",
Expand Down

0 comments on commit 07b63e9

Please sign in to comment.