Debian Codegraph @

tests/testthat/test-formats.R

raw
1 test_that("formats successfully produce a document", {
2
3 testFormat <- function(output_format, df_print = NULL) {
4 output_file <- I(tempfile())
5 on.exit(unlink(output_file), add = TRUE)
6 render(test_path("test-formats.Rmd"),
7 output_format = output_format,
8 output_file = output_file,
9 # output_options = ifelse(is.null(df_print), NULL, list(df_print = df_print)),
10 quiet = TRUE)
11 expect_true(file.exists(output_file))
12 }
13
14 testFormat(html_document(), df_print = "kable")
15 testFormat(html_notebook())
16 testFormat(html_fragment(), df_print = "tibble")
17 suppressWarnings(testFormat(html_vignette(), df_print = "tibble"))
18 testFormat(slidy_presentation(), df_print = "kable")
19 testFormat(md_document(), df_print = "kable")
20 testFormat(beamer_presentation(), df_print = "kable")
21 testFormat(word_document(), df_print = "kable")
22 suppressWarnings(testFormat(html_vignette()))
23 })
24
25 test_that("documents with spaces in names can be rendered", {
26
27 # get path to notebook
28 rmd_path <- test_path("resources/empty.Rmd")
29
30 # attempt to write to directory with spaces
31 output_file <- test_path("directory with spaces/r output.nb.html")
32 dir.create(dirname(output_file))
33 on.exit(unlink(test_path("directory with spaces"), recursive = TRUE), add = TRUE)
34
35 # generate copy with space in name
36 with_spaces <- test_path("directory with spaces/no content.Rmd")
37 file.copy(rmd_path, with_spaces)
38
39 output <- rmarkdown::render(with_spaces,
40 output_format = "html_notebook",
41 output_file = output_file,
42 output_options = list(pandoc_args = c('--metadata', 'pagetitle=Test')),
43 quiet = TRUE)
44
45 expect_true(file.exists(output))
46
47 })
48
49 test_that(
50 "setting theme, highlight or fig_retina yields an error on html_vignette",
51 {
52
53 testFormat <- function(output_format) {
54 output_file <- tempfile()
55 expect_error(
56 render(test_path("test-formats.Rmd"),
57 output_format = output_format,
58 output_file = output_file,
59 quiet = TRUE)
60 )
61 }
62
63 testFormat(html_vignette(theme = "z"))
64 testFormat(html_vignette(fig_retina = 2))
65
66 })
67
68 test_that("pdf_document can correctly keep tex file if required", {
69 rmd_file <- "test-formats.Rmd"
70 # input in another dir
71 dir.create(tmpdir <- tempfile())
72 file.copy(test_path(rmd_file), tmpdir)
73 texfile <- xfun::with_ext(rmd_file, "tex")
74 unlink(tmpdir, recursive = TRUE)
75 })
76
77 test_that("url in css arg works HTML based format", {
78 skip_if_offline()
79 rmd <- local_rmd_file("---", "title: test", "---", "", "# test")
80 css <- "https://raw.githubusercontent.com/rstudio/rmarkdown/master/tests/testthat/resources/styles.css"
81 expect_error(render(rmd, html_document_base(css = css), quiet = TRUE), NA)
82 skip_if_not_installed("bslib")
83 expect_error(render(rmd, html_document_base(css = css, theme = list(version = 4)), quiet = TRUE), NA)
84 })
85
86 test_that("css arg works HTML based format", {
87 rmd <- local_rmd_file("---", "title: test", "---", "", "# test")
88 css <- withr::local_tempfile(fileext = ".css")
89 xfun::write_utf8("h1{color:red;}", css)
90 expect_error(render(rmd, html_document_base(css = css), quiet = TRUE), NA)
91 skip_if_not_installed("bslib")
92 expect_error(render(rmd, html_document_base(css = css, theme = list(version = 4)), quiet = TRUE), NA)
93 })
94