-
Notifications
You must be signed in to change notification settings - Fork 326
/
test-python-knitr-engine.R
123 lines (83 loc) · 3.13 KB
/
test-python-knitr-engine.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
context("knitr")
test_that("An R Markdown document can be rendered using reticulate", {
skip_on_cran()
skip_on_os("windows")
skip_if_not_installed("rmarkdown")
skip_if(py_version() < "3") # plotly _repr_html_ test fails in py2
modules <- c("numpy", "matplotlib", "pandas", "plotly",
"tabulate", "IPython")
for (module in modules) {
if (!py_module_available(module)) {
fmt <- "module '%s' not available; skipping"
skip(sprintf(fmt, module))
}
}
owd <- setwd(test_path("resources"))
status <- rmarkdown::render("eng-reticulate-example.Rmd", quiet = TRUE)
setwd(owd)
expect_true(file.exists(status), "example.Rmd rendered successfully")
})
test_that("Figures saved in correct place with custom root.dir", {
skip_on_cran()
skip_if_not_installed("rmarkdown")
skip_if(py_version() < "3.8") # end_lineno attr added in 3.8
rmd_filepath <- normalizePath(file.path(test_path("resources"),
"test-custom-root-dir.Rmd"))
dir.create(dir <- tempfile("reticulate-knitr-test-dir"))
owd <- setwd(dir)
file.copy(rmd_filepath, ".")
output <- knitr::knit("test-custom-root-dir.Rmd", quiet = TRUE)
expect_true(file.exists(output), "test-custom-root-dir.Rmd rendered successfully")
setwd(owd)
})
test_that("In Rmd chunks, comments and output attach to code correctly", {
skip_on_cran()
skip_if_not_installed("rmarkdown")
skip_if(py_version() < "3.8") # end_lineno attr added in 3.8
local_edition(3) # needed for expect_snapshot_file()
# TODO: update the full testsuite to testthat edition 3
owd <- setwd(test_path("resources"))
rmarkdown::render("test-chunking.Rmd", quiet = TRUE)
expect_snapshot_file("test-chunking.md")
setwd(owd)
})
test_that("knitr 'warning=FALSE' option", {
skip_on_cran()
skip_if_not_installed("rmarkdown")
local_edition(3) # needed for expect_snapshot_file()
owd <- setwd(test_path("resources"))
rmarkdown::render("knitr-warn.Rmd", quiet = TRUE)
setwd(owd)
rendered <- test_path("resources", "knitr-warn.md")
res <- paste0(readLines(rendered), collapse = "\n")
expect_snapshot_file(rendered)
expect_no_match(res, "UserWarning", fixed = TRUE)
})
test_that("Output streams are remaped when kniting", {
skip_on_cran()
skip_if_not_installed("rmarkdown")
local_edition(3)
owd <- setwd(test_path("resources"))
rmarkdown::render("knitr-print.Rmd", quiet = TRUE)
setwd(owd)
rendered <- test_path("resources", "knitr-print.md")
expect_snapshot_file(rendered)
# if remaping is set by default we have no way to check that the options
# is correctly reset
skip_if(!is.na(Sys.getenv("RETICULATE_REMAP_OUTPUT_STREAMS", unset = NA)))
bt <- reticulate::import_builtins()
x <- py_capture_output(out <- capture.output({
bt$print("hello world")
}))
expect_length(out, 0)
# make sure it works from a background session
callr::r(function(path) {
setwd(path)
rmarkdown::render(
"knitr-print.Rmd",
output_file = "knitr-print2.md",
quiet = TRUE
)
}, args = list(path = test_path("resources")))
expect_snapshot_file(test_path("resources", "knitr-print2.md"))
})