Debian Codegraph @

tests/testthat/test-site.R

raw
1 # copy part of our demo site to a tempdir
2 local_create_site <- function(files, env = parent.frame()) {
3 site_dir <- tempfile()
4 dir.create(site_dir, recursive = TRUE)
5 withr::defer(unlink(site_dir, recursive = TRUE), envir = env)
6 # by default copy all files
7 if (missing(files)) files <- c("_site.yml", "index.Rmd", "PageA.Rmd",
8 "PageB.rmd", "PageC.md", "PageD.R", "styles.css",
9 "script.R", "docs.txt")
10 file.copy(test_path("site", files), site_dir, recursive = TRUE)
11 site_dir
12 }
13
14 test_that("render_site works", {
15
16 # copy our demo site to a tempdir
17 site_dir <- local_create_site()
18
19 # render it
20 capture.output(render_site(site_dir))
21
22 # did the html files get rendered and the css get copied?
23 html_files <- c("index.html", "PageA.html", "PageB.html", "PageC.html",
24 "PageD.html")
25 html_files <- file.path(site_dir, "_site", html_files)
26 expect_true(all(file.exists(html_files)))
27
28 # moved directories
29 moved <- c("site_libs", "PageA_files")
30 expect_true(all(!file.exists(file.path(site_dir, moved))))
31 expect_true(all(file.exists(file.path(site_dir, "_site", moved))))
32
33 # respected includes
34 included <- "script.R"
35 expect_true(all(file.exists(file.path(site_dir, "_site", included))))
36
37 # respected excluded
38 excluded <- "docs.txt"
39 expect_true(all(!file.exists(file.path(site_dir, "_site", excluded))))
40 })
41
42 test_that("site_generator() correctly try to use custom site generator", {
43 # copy our demo site to a tempdir
44 site_dir <- local_create_site()
45 withr::local_dir(site_dir)
46 expect_equal(site_generator(), default_site("."))
47 xfun::gsub_file("index.Rmd", "(output: html_document)", "\\1\nsite: rmarkdown::dummy_site")
48 # It errors if not found
49 expect_error(site_generator(), regexp = "'dummy_site' is not an exported object from 'namespace:rmarkdown'", fixed = TRUE)
50 file.rename("index.Rmd", "index.rmd")
51 expect_error(site_generator(), regexp = "'dummy_site' is not an exported object from 'namespace:rmarkdown'", fixed = TRUE)
52 })
53
54 test_that("render_site respects 'new_session' in the config", {
55
56 skip_if_not_installed("xfun", "0.13")
57
58 # copy parts of our demo site to a tempdir
59 files <- c("_site.yml", "index.Rmd", "PageA.Rmd", "PageB.rmd", "PageD.R")
60 site_dir <- local_create_site(files)
61
62 # default behaviour --> new_session: false
63 render_site(site_dir, quiet = TRUE)
64 a <- readLines(file.path(site_dir, "_site", "PageA.html"))
65 b <- readLines(file.path(site_dir, "_site", "PageB.html"))
66
67 # pkg loaded in PageA (tinytex) should show up in search path of PageB
68 expect_match(a, "library(tinytex)", fixed = TRUE, all = FALSE)
69 expect_true(any(grepl("tinytex", b, fixed = TRUE)))
70
71 # edit config --> new_session: true
72 cat("new_session: true", file = file.path(site_dir, "_site.yml"), append = TRUE)
73
74 render_site(site_dir, quiet = TRUE)
75 a <- readLines(file.path(site_dir, "_site", "PageA.html"))
76 b <- readLines(file.path(site_dir, "_site", "PageB.html"))
77
78 # pkg loaded in PageA (tinytex) should NOT show up in search path of PageB
79 expect_match(a, "library(tinytex)", fixed = TRUE, all = FALSE)
80 expect_false(any(grepl("tinytex", b, fixed = TRUE)))
81 })
82
83 test_that("clean_site gives notices before removing", {
84 site_dir <- local_create_site()
85 render_site(site_dir, quiet = TRUE)
86 withr::local_dir(site_dir)
87 expect_output(clean_site(), "removed.*_site")
88 expect_true(dir.exists("_site"))
89 expect_silent(clean_site(preview = FALSE, quiet = TRUE))
90 expect_output(clean_site(preview = FALSE, quiet = FALSE), "Nothing")
91 })