Debian Codegraph @

tests/testthat/test-embed.R

raw
1
2 test_that("renv::embed() works with .R files", {
3
4 project <- renv_tests_scope("breakfast")
5 install("breakfast")
6
7 writeLines("library(breakfast)", con = "test-embed.R")
8 embed("test-embed.R")
9 expect_snapshot(writeLines(readLines("test-embed.R")))
10
11 })
12
13 test_that("renv::embed() works with .Rmd files", {
14
15 skip_on_cran()
16 skip_if_not_installed("rmarkdown")
17
18 project <- renv_tests_scope("breakfast", libpaths = TRUE)
19 install("breakfast")
20
21 contents <- heredoc('
22 ---
23 title: Embedding Test
24 ---
25
26 ```{r}
27 library(breakfast)
28 ```
29 ')
30
31 writeLines(contents, con = "test-embed.Rmd")
32 withCallingHandlers(
33 embed("test-embed.Rmd"),
34 warning = function(cnd) invokeRestart("muffleWarning")
35 )
36
37 contents <- readLines("test-embed.Rmd")
38 expect_true(any(grepl("[email protected]", contents)))
39 expect_true(any(grepl("[email protected]", contents)))
40 expect_true(any(grepl("[email protected]", contents)))
41
42 })
43
44 test_that("missing packages are reported", {
45
46 project <- renv_tests_scope("breakfast")
47
48 writeLines("library(breakfast)", con = "test-embed.R")
49 expect_error(embed("test-embed.R"))
50
51 })
52
53 test_that("embed(lockfile = FALSE) ignores the lockfile", {
54
55 project <- renv_tests_scope("bread")
56 init()
57
58 install("breakfast")
59 writeLines("library(breakfast)", con = "test-embed.R")
60 embed("test-embed.R", lockfile = FALSE)
61 })
62
63 test_that("embed warns when lockfile is missing dependencies", {
64
65 project <- renv_tests_scope("bread")
66 init()
67
68 # the lockfile only has 'bread', but the script needs 'breakfast'
69 install("breakfast")
70 writeLines("library(breakfast)", con = "test-embed.R")
71
72 expect_condition(
73 embed("test-embed.R"),
74 class = "renv.embed.missing_packages"
75 )
76
77 })
78
79 test_that("embed(lockfile = NA) uses repository package versions", {
80
81 project <- renv_tests_scope("breakfast")
82 install("breakfast")
83
84 writeLines("library(breakfast)", con = "test-embed.R")
85 embed("test-embed.R", lockfile = NA)
86
87 contents <- readLines("test-embed.R")
88 expect_true(any(grepl("[email protected]", contents)))
89 expect_true(any(grepl("[email protected]", contents)))
90 expect_true(any(grepl("[email protected]", contents)))
91 expect_true(any(grepl("[email protected]", contents)))
92
93 })
94
95 test_that("embed(lockfile = NA) works even when packages are not installed", {
96
97 project <- renv_tests_scope()
98
99 # script references 'bread' which is NOT installed,
100 # but is available in the test repository
101 writeLines("library(bread)", con = "test-embed.R")
102 embed("test-embed.R", lockfile = NA)
103
104 contents <- readLines("test-embed.R")
105 expect_true(any(grepl("[email protected]", contents)))
106
107 })