I've a method generating an image in R markdown or Quarto (e.g. [](link)
). The method generates image as .pdf or .svg depending on the output format of the rendered document.
I previously used expect_match()
to test that method, which worked well for simple cases. I now need to find a better alternative to perform more tests that produce slightly different outputs. expect_snapshot()
seems to be the best option. My only issue is that using expect_snapshot(my_function())
generates paths from the root directory of my local machine (e.g. /User/my_user_name/.../file.svg
), which I don't want.
I've messed around with withr::with_tempdir()
and withr::local_tempfile()
but I'm struggling to make the snapshot test work. One of the difficulty is that the function picks the right image format automatically on rendering, meaning that I've to use expect_snapshot()
together with rmarkdown::render()
.
My question is how could I use expect_snapshot()
so that it uses a temporary directory in this case?
Here's one of the things I've tried, without success:
render <- purrr::partial(rmarkdown::render, clean = FALSE, quiet = TRUE)
read_rendered_md <- function() {
readr::read_file(list.files(pattern = "\\.md$"))
}
withr::with_tempdir({
tmp_file <- withr::local_tempfile(lines = dedent("
```{r echo = FALSE, results = 'asis'}
aut <- Plume$new(basic_df())
cat(aut$get_orcids(), sep = '\n\n')
```
"), fileext = ".Rmd", tmpdir = getwd())
render(tmp_file, output_format = "rtf_document")
expect_snapshot(read_rendered_md())
render(tmp_file, output_format = "html_document")
expect_snapshot(read_rendered_md())
})
I've also read about expect_snapshot_file()
but couldn't really figure out if that's what I need and how to make it work in my case.