diff --git a/DESCRIPTION b/DESCRIPTION index a398b03..114dff1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -32,7 +32,7 @@ Suggests: methods, pkgload, rlang, - rmarkdown, + knitr, testthat (>= 3.0.0), withr Config/Needs/website: tidyverse/tidytemplate diff --git a/tests/testthat/_snaps/conditions/abort-error.txt b/tests/testthat/_snaps/conditions/abort-error.txt index fd12b07..3cfb05d 100644 --- a/tests/testthat/_snaps/conditions/abort-error.txt +++ b/tests/testthat/_snaps/conditions/abort-error.txt @@ -1,12 +1,24 @@ - - -processing file: with-abort-error.Rmd Error in `h()`: ! ! Backtrace: - 1. global f() - 2. global g() - 3. global h() - -Quitting from lines 6-10 [unnamed-chunk-1] (with-abort-error.Rmd) + ▆ + 1. ├─evaluate::evaluate(...) + 2. │ ├─base::withRestarts(...) + 3. │ │ └─base (local) withRestartList(expr, restarts) + 4. │ │ ├─base (local) withOneRestart(withRestartList(expr, restarts[-nr]), restarts[[nr]]) + 5. │ │ │ └─base (local) doWithOneRestart(return(expr), restart) + 6. │ │ └─base (local) withRestartList(expr, restarts[-nr]) + 7. │ │ └─base (local) withOneRestart(expr, restarts[[1L]]) + 8. │ │ └─base (local) doWithOneRestart(return(expr), restart) + 9. │ ├─evaluate:::with_handlers(...) + 10. │ │ ├─base::eval(call) + 11. │ │ │ └─base::eval(call) + 12. │ │ └─base::withCallingHandlers(...) + 13. │ ├─base::withVisible(eval(expr, envir)) + 14. │ └─base::eval(expr, envir) + 15. │ └─base::eval(expr, envir) + 16. └─global f() + 17. └─global g() + 18. └─global h() + 19. └─rlang::abort("!") Execution halted diff --git a/tests/testthat/_snaps/conditions/rmd-abort-error.md b/tests/testthat/_snaps/conditions/rmd-abort-error.md new file mode 100644 index 0000000..0fa32f4 --- /dev/null +++ b/tests/testthat/_snaps/conditions/rmd-abort-error.md @@ -0,0 +1,22 @@ +--- +title: document with error +--- + + +``` r +f <- function() g() +g <- function() h() +h <- function() rlang::abort("!") +f() +``` + +``` +## Error in `h()`: +## ! ! +## Backtrace: +## x +## 1. \-evaluate (local) f() +## 2. \-evaluate (local) g() +## 3. \-evaluate (local) h() +## 4. \-rlang::abort("!") +``` diff --git a/tests/testthat/_snaps/conditions/rmd-abort-error.txt b/tests/testthat/_snaps/conditions/rmd-abort-error.txt new file mode 100644 index 0000000..114acb6 --- /dev/null +++ b/tests/testthat/_snaps/conditions/rmd-abort-error.txt @@ -0,0 +1,12 @@ + + +processing file: ressources/with-abort-error.Rmd +Error in `h()`: +! ! +Backtrace: + 1. global f() + 2. global g() + 3. global h() + +Quitting from lines 6-10 [unnamed-chunk-1] (ressources/with-abort-error.Rmd) +Execution halted diff --git a/tests/testthat/_snaps/conditions/rmd-stop-error-auto-entrace.txt b/tests/testthat/_snaps/conditions/rmd-stop-error-auto-entrace.txt new file mode 100644 index 0000000..bd5d96c --- /dev/null +++ b/tests/testthat/_snaps/conditions/rmd-stop-error-auto-entrace.txt @@ -0,0 +1,12 @@ + + +processing file: ressources/with-stop-error-auto-entrace.Rmd +Error in `h()`: +! ! +Backtrace: + 1. global f() + 2. global g() + 3. global h() + +Quitting from lines 11-15 [unnamed-chunk-2] (ressources/with-stop-error-auto-entrace.Rmd) +Execution halted diff --git a/tests/testthat/_snaps/conditions/rmd-stop-error-entrace-sewed.md b/tests/testthat/_snaps/conditions/rmd-stop-error-entrace-sewed.md new file mode 100644 index 0000000..a7f1e5e --- /dev/null +++ b/tests/testthat/_snaps/conditions/rmd-stop-error-entrace-sewed.md @@ -0,0 +1,27 @@ +--- +title: document with error +--- + + +``` r +rlang::global_entrace() +options(rlang_backtrace_on_error_report = "full") +``` + + +``` r +f <- function() g() +g <- function() h() +h <- function() stop("!") +f() +``` + +``` +## Error in `h()`: +## ! ! +## Backtrace: +## x +## 1. \-evaluate (local) f() +## 2. \-evaluate (local) g() +## 3. \-evaluate (local) h() +``` diff --git a/tests/testthat/_snaps/conditions/stop-error-auto-entrace.txt b/tests/testthat/_snaps/conditions/stop-error-auto-entrace.txt deleted file mode 100644 index 4572c1c..0000000 --- a/tests/testthat/_snaps/conditions/stop-error-auto-entrace.txt +++ /dev/null @@ -1,12 +0,0 @@ - - -processing file: with-stop-error-auto-entrace.Rmd -Error in `h()`: -! ! -Backtrace: - 1. global f() - 2. global g() - 3. global h() - -Quitting from lines 10-14 [unnamed-chunk-2] (with-stop-error-auto-entrace.Rmd) -Execution halted diff --git a/tests/testthat/ressources/with-stop-error-auto-entrace.Rmd b/tests/testthat/ressources/with-stop-error-auto-entrace.Rmd index 1a5572e..2d9b679 100644 --- a/tests/testthat/ressources/with-stop-error-auto-entrace.Rmd +++ b/tests/testthat/ressources/with-stop-error-auto-entrace.Rmd @@ -4,6 +4,7 @@ title: document with error ```{r} rlang::global_entrace() +options(rlang_backtrace_on_error_report = "full") ``` ```{r} diff --git a/tests/testthat/ressources/with-stop-error-sewed.Rmd b/tests/testthat/ressources/with-stop-error-sewed.Rmd new file mode 100644 index 0000000..c33ba64 --- /dev/null +++ b/tests/testthat/ressources/with-stop-error-sewed.Rmd @@ -0,0 +1,15 @@ +--- +title: document with error +--- + +```{r} +rlang::global_entrace() +``` + + +```{r} +f <- function() g() +g <- function() h() +h <- function() stop("!") +f() +``` diff --git a/tests/testthat/test-conditions.R b/tests/testthat/test-conditions.R index ca04fc4..11c0902 100644 --- a/tests/testthat/test-conditions.R +++ b/tests/testthat/test-conditions.R @@ -137,16 +137,16 @@ test_that("errors during printing are captured", { expect_output_types(ev, c("source", "error")) }) -test_that("Error can be entraced and are shown correctly when stop_on_error = 2L.", { +test_that("Error can be entraced and correctly handled in outputs", { skip_if_not_installed("rlang") - skip_if_not_installed("rmarkdown") + skip_if_not_installed("knitr") skip_if_not_installed("callr") skip_on_cran() # install dev version of package in temp directory withr::local_temp_libpaths() quick_install(pkgload::pkg_path("."), lib = .libPaths()[1]) - out <- withr::local_tempfile(fileext = "txt") + out <- withr::local_tempfile(fileext = ".txt") # Checking different way to entrace with evaluate callr::rscript(test_path("ressources/with-stop-error-no-trace.R"), fail_on_status = FALSE, show = FALSE, stderr = out) @@ -164,13 +164,32 @@ test_that("Error can be entraced and are shown correctly when stop_on_error = 2L callr::rscript(test_path("ressources/with-abort-error.R"), fail_on_status = FALSE, show = FALSE, stderr = out) expect_snapshot_file(out, name = 'abort-error.txt') - # Checking error in rmarkdown and knitr context - rscript <- withr::local_tempfile(fileext = "R") - writeLines(sprintf("rmarkdown::render(%s)", dQuote(test_path("ressources/with-stop-error-auto-entrace.Rmd"), FALSE)), con = rscript) + # Checking error thrown when in rmarkdown and knitr context + rscript <- withr::local_tempfile(fileext = ".R") + out2 <- normalizePath(withr::local_tempfile(fileext = ".md"), winslash = "/", mustWork = FALSE) + writeLines(c( + "options(knitr.chunk.error = FALSE)", + sprintf('knitr::knit("%s", output = "%s")', test_path("ressources/with-stop-error-auto-entrace.Rmd"), out2) + ), con = rscript) callr::rscript(rscript, fail_on_status = FALSE, show = FALSE, stderr = out) - expect_snapshot_file(out, name = 'stop-error-auto-entrace.txt') + expect_snapshot_file(out, name = 'rmd-stop-error-auto-entrace.txt') - writeLines(sprintf("rmarkdown::render(%s)", dQuote(test_path("ressources/with-abort-error.Rmd"), FALSE)), con = rscript) + writeLines(c( + "options(knitr.chunk.error = FALSE)", + sprintf('res <- knitr::knit("%s", output = "%s")', test_path("ressources/with-abort-error.Rmd"), out2) + ), con = rscript) callr::rscript(rscript, fail_on_status = FALSE, show = FALSE, stderr = out) - expect_snapshot_file(out, name = 'abort-error.txt') + expect_snapshot_file(out, name = 'rmd-abort-error.txt') + + # Checking error captured in cell output in rmarkdown and knitr context + withr::with_options(list(options(knitr.chunk.error = TRUE)), { + expect_snapshot_file( + knitr::knit(test_path("ressources/with-stop-error-auto-entrace.Rmd"), output = out, quiet = TRUE), + name = "rmd-stop-error-entrace-sewed.md" + ) + expect_snapshot_file( + knitr::knit(test_path("ressources/with-abort-error.Rmd"), output = out, quiet = TRUE), + name = "rmd-abort-error.md" + ) + }) })