From ae1d16c607827d23edf6d8629266abf22b66eaaa Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 3 Jul 2024 14:58:41 -0500 Subject: [PATCH] Forward compatibility with evaluate 1.0.0 (#2690) --- R/highlight.R | 18 ++++++++++++++++-- tests/testthat/test-highlight.R | 5 ++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/R/highlight.R b/R/highlight.R index 19ce4f061..07fb15288 100644 --- a/R/highlight.R +++ b/R/highlight.R @@ -28,8 +28,22 @@ highlight_examples <- function(code, topic, env = globalenv()) { do.call(fig_save, c(list(plot, name), fig_settings())) } - hide_dontshow <- function(src, call) { - if (is_call(call, c("DONTSHOW", "TESTONLY"))) NULL else src + hide_dontshow <- function(src, expr) { + if (is.expression(expr)) { + # evaluate 1.0.0 + if (length(expr) > 0) { + hide <- is_call(expr[[1]], c("DONTSHOW", "TESTONLY")) + } else { + hide <- FALSE + } + } else if (is.call(expr)) { + # evaluate 0.24.0 + hide <- is_call(expr, c("DONTSHOW", "TESTONLY")) + } else { + hide <- FALSE + } + + if (hide) NULL else src } handler <- evaluate::new_output_handler( value = pkgdown_print, diff --git a/tests/testthat/test-highlight.R b/tests/testthat/test-highlight.R index 90abbf56f..2611c494c 100644 --- a/tests/testthat/test-highlight.R +++ b/tests/testthat/test-highlight.R @@ -1,5 +1,5 @@ test_that("highlight_examples captures dependencies", { - withr::defer(file_delete(test_path("Rplot001.png"))) + withr::defer(try(file_delete(test_path("Rplot001.png")), TRUE)) dummy_dep <- htmltools::htmlDependency("dummy", "1.0.0", "dummy.js") widget <- htmlwidgets::createWidget("test", list(), dependencies = dummy_dep) @@ -9,14 +9,13 @@ test_that("highlight_examples captures dependencies", { expect_equal(attr(out, "dependencies")[-1], list(dummy_dep)) }) - test_that("highlight_examples runs and hides DONTSHOW calls()", { out <- highlight_examples("DONTSHOW(x <- 1)\nx") expect_snapshot(cat(strip_html_tags(out))) }) test_that("highlight_text & highlight_examples include sourceCode div", { - withr::defer(file_delete(test_path("Rplot001.png"))) + withr::defer(try(file_delete(test_path("Rplot001.png")), TRUE)) html <- xml2::read_html(highlight_examples("a + a", "x")) expect_equal(xpath_attr(html, "./body/div", "class"), "sourceCode")