Skip to content

Commit

Permalink
Use knitr and test sewed output
Browse files Browse the repository at this point in the history
- Use knitr instead of rmarkdown
- Test error thrown and capture in cell output
  • Loading branch information
cderv committed Dec 20, 2024
1 parent 6b31084 commit bc3e3d7
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 30 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Suggests:
methods,
pkgload,
rlang,
rmarkdown,
knitr,
testthat (>= 3.0.0),
withr
Config/Needs/website: tidyverse/tidytemplate
Expand Down
28 changes: 20 additions & 8 deletions tests/testthat/_snaps/conditions/abort-error.txt
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions tests/testthat/_snaps/conditions/rmd-abort-error.md
Original file line number Diff line number Diff line change
@@ -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("!")
```
12 changes: 12 additions & 0 deletions tests/testthat/_snaps/conditions/rmd-abort-error.txt
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions tests/testthat/_snaps/conditions/rmd-stop-error-auto-entrace.txt
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions tests/testthat/_snaps/conditions/rmd-stop-error-entrace-sewed.md
Original file line number Diff line number Diff line change
@@ -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()
```
12 changes: 0 additions & 12 deletions tests/testthat/_snaps/conditions/stop-error-auto-entrace.txt

This file was deleted.

1 change: 1 addition & 0 deletions tests/testthat/ressources/with-stop-error-auto-entrace.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ title: document with error

```{r}
rlang::global_entrace()
options(rlang_backtrace_on_error_report = "full")
```

```{r}
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/ressources/with-stop-error-sewed.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: document with error
---

```{r}
rlang::global_entrace()
```


```{r}
f <- function() g()
g <- function() h()
h <- function() stop("!")
f()
```
37 changes: 28 additions & 9 deletions tests/testthat/test-conditions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
)
})
})

0 comments on commit bc3e3d7

Please sign in to comment.