Debian Codegraph @

tests/testthat/test-draft.R

raw
1 test_that("available_templates() correctly returns template", {
2 expect_identical(
3 expect_invisible(available_templates("donotexist")),
4 character()
5 )
6 expect_identical(available_templates(), dir(pkg_file("rmarkdown/templates")))
7 expect_identical(available_templates(full_path = TRUE),
8 dir(pkg_file("rmarkdown/templates"), full.names = TRUE))
9 })
10
11 test_that("draft() correctly creates template from a package", {
12 rmd <- withr::local_tempfile(fileext = ".Rmd")
13 path <- expect_invisible(draft(rmd, "github_document", "rmarkdown", edit = FALSE))
14 expect_identical(rmd, path)
15 expect_true(file.exists(rmd))
16 expect_match(xfun::read_utf8(rmd), "github_document", all = FALSE)
17 })
18
19 test_that("draft() correctly creates template from a path", {
20 # No template
21 template_dir <- withr::local_tempdir("template")
22 rmd <- withr::local_tempfile(fileext = ".Rmd")
23 expect_error(draft(rmd, template_dir, edit = FALSE), "No template\\.yaml")
24 yaml <- file.path(template_dir, "template.yaml")
25
26 # Incorrect yaml
27 xfun::write_utf8(c("dummy: value"), yaml)
28 expect_error(draft(rmd, template_dir, edit = FALSE), "template\\.yaml must contain")
29
30 # Correct template folder with skeleton
31 xfun::write_utf8(c("name: dummy", "description: for test"), yaml)
32 dir.create(file.path(template_dir, "skeleton"))
33 rmd_template <- file.path(template_dir, "skeleton", "skeleton.Rmd")
34 xfun::write_utf8(c("# dummy"), rmd_template)
35 draft(rmd, template_dir, edit = FALSE)
36 expect_true(file.exists(rmd))
37 expect_match(xfun::read_utf8(rmd), "# dummy")
38 })
39
40 test_that("draft() correctly creates a folder from a template", {
41 rmd <- withr::local_tempfile(fileext = ".Rmd")
42 path <- draft(rmd, "github_document", "rmarkdown", create_dir = TRUE, edit = FALSE)
43 expect_identical(basename(rmd), basename(path))
44 expect_identical(xfun::sans_ext(basename(rmd)), basename(dirname(path)))
45 expect_true(file.exists(path))
46 expect_match(xfun::read_utf8(path), "github_document", all = FALSE)
47 })