From 36eb4bc8a51d71cd205790333f1e4afdd4ba7279 Mon Sep 17 00:00:00 2001 From: Salim B Date: Thu, 19 Oct 2023 00:05:12 +0200 Subject: [PATCH 1/6] Fix setting `seed` in `build_reference()` --- R/build-reference.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/build-reference.R b/R/build-reference.R index add462c13..48c65f3cc 100644 --- a/R/build-reference.R +++ b/R/build-reference.R @@ -222,7 +222,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) } From b2d3cce8efd7161716aaf26e11a2b648a7109833 Mon Sep 17 00:00:00 2001 From: Salim B Date: Fri, 1 Dec 2023 17:07:10 +0100 Subject: [PATCH 2/6] Test_that examples are reproducible by default i.e. RNG 'seed' is respected --- tests/testthat/_snaps/build-reference.md | 17 +++++++++++++++++ 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 | 11 +++++++++++ 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/assets/reference/man/f.Rd diff --git a/tests/testthat/_snaps/build-reference.md b/tests/testthat/_snaps/build-reference.md index 31f9b6a92..9cd0dcdc6 100644 --- a/tests/testthat/_snaps/build-reference.md +++ b/tests/testthat/_snaps/build-reference.md @@ -44,3 +44,20 @@ Reading man/a.Rd Writing reference/a.html +# examples are reproducible by default, i.e. 'seed' is respected + + Code + build_reference(pkg, topics = "f") + Message + Writing reference/index.html + Reading man/f.Rd + Writing reference/f.html + +--- + + Code + rvest::html_text(rvest::html_node(xml2::read_html(file.path(pkg$dst_path, + "reference", "f.html")), "div#ref-examples div.sourceCode")) + Output + [1] "testpackage:::f()\n#> [1] 0.080750138 0.834333037 0.600760886 0.157208442 0.007399441 0.466393497\n#> [7] 0.497777389 0.289767245 0.732881987 0.772521511 0.874600661 0.174940627\n#> [13] 0.034241333 0.320385731 0.402328238 0.195669835 0.403538117 0.063661457\n#> [19] 0.388701313 0.975547835\n" + diff --git a/tests/testthat/assets/reference/DESCRIPTION b/tests/testthat/assets/reference/DESCRIPTION index 8e969ae06..564f9eab7 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 a537ae407..c41de4571 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(20L)} diff --git a/tests/testthat/assets/reference/man/f.Rd b/tests/testthat/assets/reference/man/f.Rd new file mode 100644 index 000000000..f093faac2 --- /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 43356f136..07828b402 100644 --- a/tests/testthat/test-build-reference.R +++ b/tests/testthat/test-build-reference.R @@ -94,6 +94,17 @@ 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", { + local_edition(3) + pkg <- local_pkgdown_site(test_path("assets/reference")) + expect_snapshot(build_reference(pkg, topics = "f")) + expect_snapshot( + xml2::read_html(file.path(pkg$dst_path, "reference", "f.html")) |> + rvest::html_node("div#ref-examples div.sourceCode") |> + rvest::html_text() + ) +}) + 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") From ec70da871a3b9188e694868798f72b43df7e11f9 Mon Sep 17 00:00:00 2001 From: Salim B Date: Fri, 1 Dec 2023 17:24:03 +0100 Subject: [PATCH 3/6] Suggest pkg rvest --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index ae63aff58..0d1d1e36b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -55,6 +55,7 @@ Suggests: rsconnect, rstudioapi, rticles, + rvest, sass, testthat (>= 3.1.3), tools From 1d94ed0521d98fc74314b625989e035b2adf01c5 Mon Sep 17 00:00:00 2001 From: Salim B Date: Fri, 1 Dec 2023 18:10:15 +0100 Subject: [PATCH 4/6] Make test output platform independent by replacing line feeds with regular whitespace --- tests/testthat/_snaps/build-reference.md | 7 ++++--- tests/testthat/test-build-reference.R | 5 ++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/testthat/_snaps/build-reference.md b/tests/testthat/_snaps/build-reference.md index 9cd0dcdc6..4a86d2da4 100644 --- a/tests/testthat/_snaps/build-reference.md +++ b/tests/testthat/_snaps/build-reference.md @@ -56,8 +56,9 @@ --- Code - rvest::html_text(rvest::html_node(xml2::read_html(file.path(pkg$dst_path, - "reference", "f.html")), "div#ref-examples div.sourceCode")) + gsub("\\s+", " ", x = rvest::html_text(rvest::html_node(xml2::read_html( + file.path(pkg$dst_path, "reference", "f.html")), + "div#ref-examples div.sourceCode"))) Output - [1] "testpackage:::f()\n#> [1] 0.080750138 0.834333037 0.600760886 0.157208442 0.007399441 0.466393497\n#> [7] 0.497777389 0.289767245 0.732881987 0.772521511 0.874600661 0.174940627\n#> [13] 0.034241333 0.320385731 0.402328238 0.195669835 0.403538117 0.063661457\n#> [19] 0.388701313 0.975547835\n" + [1] "testpackage:::f() #> [1] 0.080750138 0.834333037 0.600760886 0.157208442 0.007399441 0.466393497 #> [7] 0.497777389 0.289767245 0.732881987 0.772521511 0.874600661 0.174940627 #> [13] 0.034241333 0.320385731 0.402328238 0.195669835 0.403538117 0.063661457 #> [19] 0.388701313 0.975547835 " diff --git a/tests/testthat/test-build-reference.R b/tests/testthat/test-build-reference.R index 07828b402..e0d4245ec 100644 --- a/tests/testthat/test-build-reference.R +++ b/tests/testthat/test-build-reference.R @@ -95,13 +95,16 @@ test_that("pkgdown html dependencies are suppressed from examples in references" }) test_that("examples are reproducible by default, i.e. 'seed' is respected", { + skip_if(getRversion() < numeric_version("4.2")) local_edition(3) pkg <- local_pkgdown_site(test_path("assets/reference")) expect_snapshot(build_reference(pkg, topics = "f")) expect_snapshot( xml2::read_html(file.path(pkg$dst_path, "reference", "f.html")) |> rvest::html_node("div#ref-examples div.sourceCode") |> - rvest::html_text() + rvest::html_text() |> + # replace line feeds with whitespace to make output platform independent + gsub("\\s+", " ", x = _) ) }) From 78e76793119f3017b4e1caaba68b2d744f7b0630 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Mon, 11 Mar 2024 10:15:55 -0500 Subject: [PATCH 5/6] Simplify test --- tests/testthat/_snaps/build-reference.md | 16 +++------------- tests/testthat/assets/reference/R/funs.R | 2 +- tests/testthat/test-build-reference.R | 19 +++++++++---------- 3 files changed, 13 insertions(+), 24 deletions(-) diff --git a/tests/testthat/_snaps/build-reference.md b/tests/testthat/_snaps/build-reference.md index 4a86d2da4..b7f436bd5 100644 --- a/tests/testthat/_snaps/build-reference.md +++ b/tests/testthat/_snaps/build-reference.md @@ -47,18 +47,8 @@ # examples are reproducible by default, i.e. 'seed' is respected Code - build_reference(pkg, topics = "f") - Message - Writing reference/index.html - Reading man/f.Rd - Writing reference/f.html - ---- - - Code - gsub("\\s+", " ", x = rvest::html_text(rvest::html_node(xml2::read_html( - file.path(pkg$dst_path, "reference", "f.html")), - "div#ref-examples div.sourceCode"))) + cat(examples) Output - [1] "testpackage:::f() #> [1] 0.080750138 0.834333037 0.600760886 0.157208442 0.007399441 0.466393497 #> [7] 0.497777389 0.289767245 0.732881987 0.772521511 0.874600661 0.174940627 #> [13] 0.034241333 0.320385731 0.402328238 0.195669835 0.403538117 0.063661457 #> [19] 0.388701313 0.975547835 " + testpackage:::f() + #> [1] 0.080750138 0.834333037 0.600760886 0.157208442 0.007399441 diff --git a/tests/testthat/assets/reference/R/funs.R b/tests/testthat/assets/reference/R/funs.R index c41de4571..b5ead798d 100644 --- a/tests/testthat/assets/reference/R/funs.R +++ b/tests/testthat/assets/reference/R/funs.R @@ -26,4 +26,4 @@ NULL #' @keywords internal #' @examples #' testpackage:::f() -f <- function() {runif(20L)} +f <- function() {runif(5L)} diff --git a/tests/testthat/test-build-reference.R b/tests/testthat/test-build-reference.R index e0d4245ec..1366c8ae1 100644 --- a/tests/testthat/test-build-reference.R +++ b/tests/testthat/test-build-reference.R @@ -95,17 +95,16 @@ test_that("pkgdown html dependencies are suppressed from examples in references" }) test_that("examples are reproducible by default, i.e. 'seed' is respected", { - skip_if(getRversion() < numeric_version("4.2")) - local_edition(3) pkg <- local_pkgdown_site(test_path("assets/reference")) - expect_snapshot(build_reference(pkg, topics = "f")) - expect_snapshot( - 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("\\s+", " ", x = _) - ) + 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", { From 3d5002c6b5f0c72b4018f52e5e418784aab47f28 Mon Sep 17 00:00:00 2001 From: Salim B Date: Mon, 11 Mar 2024 16:27:19 +0100 Subject: [PATCH 6/6] Update NEWS.md --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 169fd5c3e..cc4b1594b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,6 @@ # pkgdown (development version) * Correct parse usage for S3 methods with non-syntactic class names (#2384). - * Deprecated `build_favicon()` was removed (`build_favicons()` remains). * Use [cli](https://github.com/r-lib/cli) to provide interactive feedback. * Preserve Markdown code blocks with class rmd from roxygen2 docs (@salim-b, #2298). @@ -11,6 +10,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) # pkgdown 2.0.7