Skip to content

Commit

Permalink
Add an automated test
Browse files Browse the repository at this point in the history
And resolve issue with using tempdir
  • Loading branch information
hadley committed Apr 30, 2024
1 parent 5703b34 commit 5cf47c1
Show file tree
Hide file tree
Showing 17 changed files with 35 additions and 57 deletions.
5 changes: 3 additions & 2 deletions R/tweak-page.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ tweak_rmarkdown_html <- function(html, input_dir, output_dir, pkg = list(bs_vers
}

# Fix up paths that are relative to input_dir instead of output_dir
input_abs_path <- path_tidy(path(input_dir, src))
output_dir <- path_real(output_dir)
input_abs_path <- purrr::map_chr(src, ~ path_abs(., input_dir))
up_path <- !abs_src & path_has_parent(input_abs_path, output_dir)
if (any(up_path)) {
purrr::walk2(
img[up_path],
path_rel(path(input_dir, src[up_path]), output_dir),
path_rel(path_abs(src[up_path], input_dir), output_dir),
xml2::xml_set_attr,
attr = "src"
)
Expand Down
17 changes: 0 additions & 17 deletions tests/testthat/_snaps/build-articles.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
# links to man/figures are automatically relocated

Code
copy_figures(pkg)
Message
Copying man/figures/kitten.jpg to reference/figures/kitten.jpg

---

Code
build_articles(pkg, lazy = FALSE)
Message
-- Building articles -----------------------------------------------------------
Writing `articles/index.html`
Reading vignettes/kitten.Rmd
Writing `articles/kitten.html`

# warns about missing images

Code
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/assets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

![](foo.png)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,24 @@ knitr::opts_chunk$set(
knitr::include_graphics("../man/figures/kitten.jpg")
```

``` {r}
knitr::include_graphics("another-kitten.jpg")
```

## rmarkdown

![](../man/figures/kitten.jpg)

## Another kitten

![](another-kitten.jpg)

## External package

```{r magick}
magick::image_read("another-kitten.jpg")
```

## Plot

```{r plot}
plot(1:3)
```
17 changes: 12 additions & 5 deletions tests/testthat/test-build-articles.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@ test_that("can recognise intro variants", {
expect_true(article_is_intro("articles/pack-age", "pack.age"))
})

test_that("links to man/figures are automatically relocated", {
test_that("image links relative to output", {
# weird path differences that I don't have the energy to dig into
skip_on_cran()
pkg <- local_pkgdown_site(test_path("assets/man-figures"))
pkg <- local_pkgdown_site(test_path("assets/articles-images"))

expect_snapshot(copy_figures(pkg))
expect_snapshot(build_articles(pkg, lazy = FALSE))
suppressMessages(copy_figures(pkg))
suppressMessages(build_article("kitten", pkg))

html <- xml2::read_html(path(pkg$dst_path, "articles", "kitten.html"))
src <- xpath_attr(html, "//img", "src")

expect_equal(src, c(
# knitr::include_graphics()
"../reference/figures/kitten.jpg",
"another-kitten.jpg",
# rmarkdown image
"../reference/figures/kitten.jpg",
"another-kitten.jpg"
"another-kitten.jpg",
# magick::image_read()
"kitten_files/figure-html/magick-1.png",
# figure
"kitten_files/figure-html/plot-1.jpg"
))

# And files aren't copied
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test_that("package_vignettes() sorts articles alphabetically by file name", {
})

test_that("override works correctly for as_pkgdown", {
pkgdown <- as_pkgdown("assets/man-figures")
pkgdown <- as_pkgdown(test_path("assets/articles-images"))
expected_list <- list(figures = list(dev = "jpeg", fig.ext = "jpg", fig.width = 3, fig.asp = 1))
expect_equal(pkgdown$meta, expected_list)
modified_pkgdown <- as_pkgdown(pkgdown, override = list(figures = list(dev = "png")))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-preview.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("checks its inputs", {
pkg <- local_pkgdown_site(test_path("assets/man-figures"))
pkg <- local_pkgdown_site(test_path("assets/articles-images"))

expect_snapshot(error = TRUE, {
preview_site(pkg, path = 1)
Expand Down
29 changes: 0 additions & 29 deletions vignettes/img-path.Rmd

This file was deleted.

0 comments on commit 5cf47c1

Please sign in to comment.