From 49198d486c66ba3efff95b00b74722db14836610 Mon Sep 17 00:00:00 2001 From: Salim B Date: Mon, 11 Mar 2024 17:05:51 +0100 Subject: [PATCH] Fix setting `seed` in `build_reference()` (#2355) And test that it works --- DESCRIPTION | 1 + NEWS.md | 1 + R/build-reference.R | 2 +- tests/testthat/_snaps/build-reference.md | 8 ++++++++ tests/testthat/assets/reference/DESCRIPTION | 2 +- tests/testthat/assets/reference/R/funs.R | 6 ++++++ tests/testthat/assets/reference/man/f.Rd | 15 +++++++++++++++ tests/testthat/test-build-reference.R | 13 +++++++++++++ 8 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/assets/reference/man/f.Rd diff --git a/DESCRIPTION b/DESCRIPTION index ae63aff587..0d1d1e36b5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -55,6 +55,7 @@ Suggests: rsconnect, rstudioapi, rticles, + rvest, sass, testthat (>= 3.1.3), tools diff --git a/NEWS.md b/NEWS.md index c0b32ce0c6..130e633951 100644 --- a/NEWS.md +++ b/NEWS.md @@ -11,6 +11,7 @@ * Add Catalan translation (@jmaspons, #2333) * Set RNG seed for htmlwidgets IDs. This reduces noise in pkgdown reference HTML output when examples generate htmlwidgets (@salim-b, #2294). * Fix BS5 navbar template to get `navbar.type: dark` to work with bslib 0.6+ / Bootstrap 5.3+ (@tanho63, #2388) +* Fix setting `seed` in `build_reference()`. The seed was reset too early, before. (@salim-b, #2355) * Translates citation sections (@eliocamp, #2410). * Topic names that conflict with selector functions can now be listed as references in `_pkgdown.yml` (@dmurdoch, #2397). diff --git a/R/build-reference.R b/R/build-reference.R index 3ebb37c222..5d915fbc71 100644 --- a/R/build-reference.R +++ b/R/build-reference.R @@ -227,7 +227,7 @@ examples_env <- function(pkg, seed = 1014, devel = TRUE, envir = parent.frame()) withr::local_dir(path(pkg$dst_path, "reference"), .local_envir = envir) width <- purrr::pluck(pkg, "meta", "code", "width", .default = 80) withr::local_options(width = width, .local_envir = envir) - withr::local_seed(seed) + withr::local_seed(seed, .local_envir = envir) if (requireNamespace("htmlwidgets", quietly = TRUE)) { htmlwidgets::setWidgetIdSeed(seed) } diff --git a/tests/testthat/_snaps/build-reference.md b/tests/testthat/_snaps/build-reference.md index 31f9b6a921..b7f436bd5a 100644 --- a/tests/testthat/_snaps/build-reference.md +++ b/tests/testthat/_snaps/build-reference.md @@ -44,3 +44,11 @@ Reading man/a.Rd Writing reference/a.html +# examples are reproducible by default, i.e. 'seed' is respected + + Code + cat(examples) + Output + testpackage:::f() + #> [1] 0.080750138 0.834333037 0.600760886 0.157208442 0.007399441 + diff --git a/tests/testthat/assets/reference/DESCRIPTION b/tests/testthat/assets/reference/DESCRIPTION index 8e969ae067..564f9eab77 100644 --- a/tests/testthat/assets/reference/DESCRIPTION +++ b/tests/testthat/assets/reference/DESCRIPTION @@ -3,4 +3,4 @@ Version: 1.0.0 Title: A test package Description: A test package Authors@R: person("Hadley Wickham") -RoxygenNote: 7.2.0 +RoxygenNote: 7.2.3 diff --git a/tests/testthat/assets/reference/R/funs.R b/tests/testthat/assets/reference/R/funs.R index a537ae407f..b5ead798d2 100644 --- a/tests/testthat/assets/reference/R/funs.R +++ b/tests/testthat/assets/reference/R/funs.R @@ -21,3 +21,9 @@ c <- function() {} #' E #' @name e NULL + +#' F +#' @keywords internal +#' @examples +#' testpackage:::f() +f <- function() {runif(5L)} diff --git a/tests/testthat/assets/reference/man/f.Rd b/tests/testthat/assets/reference/man/f.Rd new file mode 100644 index 0000000000..f093faac2e --- /dev/null +++ b/tests/testthat/assets/reference/man/f.Rd @@ -0,0 +1,15 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/funs.R +\name{f} +\alias{f} +\title{F} +\usage{ +f() +} +\description{ +F +} +\examples{ +testpackage:::f() +} +\keyword{internal} diff --git a/tests/testthat/test-build-reference.R b/tests/testthat/test-build-reference.R index 43356f1368..1366c8ae1f 100644 --- a/tests/testthat/test-build-reference.R +++ b/tests/testthat/test-build-reference.R @@ -94,6 +94,19 @@ test_that("pkgdown html dependencies are suppressed from examples in references" expect_length(bs_css_href, 1) }) +test_that("examples are reproducible by default, i.e. 'seed' is respected", { + pkg <- local_pkgdown_site(test_path("assets/reference")) + suppressMessages(build_reference(pkg, topics = "f")) + + examples <- xml2::read_html(file.path(pkg$dst_path, "reference", "f.html")) %>% + rvest::html_node("div#ref-examples div.sourceCode") %>% + rvest::html_text() %>% + # replace line feeds with whitespace to make output platform independent + gsub("\r", "", .) + + expect_snapshot(cat(examples)) +}) + test_that("get_rdname handles edge cases", { expect_equal(get_rdname(list(file_in = "foo..Rd")), "foo.") expect_equal(get_rdname(list(file_in = "foo.rd")), "foo")