diff --git a/R/build-redirects.R b/R/build-redirects.R index d61b7cf72..9b43ab232 100644 --- a/R/build-redirects.R +++ b/R/build-redirects.R @@ -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)] diff --git a/tests/testthat/test-build-redirects.R b/tests/testthat/test-build-redirects.R index 5b254cd70..ddc8bc87d 100644 --- a/tests/testthat/test-build-redirects.R +++ b/tests/testthat/test-build-redirects.R @@ -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"), @@ -89,4 +104,3 @@ test_that("no redirects if no aliases", { ) expect_equal(reference_redirects(pkg), list()) }) - diff --git a/tests/testthat/test-build-search-docs.R b/tests/testthat/test-build-search-docs.R index fa2edeee9..d9432c482 100644 --- a/tests/testthat/test-build-search-docs.R +++ b/tests/testthat/test-build-search-docs.R @@ -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",