Skip to content

Commit

Permalink
Fixes for local testing issues (#132)
Browse files Browse the repository at this point in the history
* Using the default plot device was causing `devtools::test()` to create a bunch of plot files in the test directory. I don't think there's any reason to use the default plot device (since that may lead to windows popping up for the user), so we just switch to using `pdf(NULL)` everywhere
* There was one test that used `dev.new()` to start a new device; there I made sure that the new device was always a quiet `pdf()` device.
* I also fixed a partial argument match warning that was causing a local test failure for me since I have `options(warnPartialMatchArgs = TRUE)`
* Add a test for plotmath too (to ensure that (e.g.) we can compute string widths)
  • Loading branch information
hadley authored Jun 18, 2024
1 parent b2a2689 commit c750df4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
7 changes: 3 additions & 4 deletions R/eval.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ evaluate <- function(input,
}

if (new_device) {
# Start new graphics device and clean up afterwards
if (identical(grDevices::pdf, getOption("device"))) {
dev.new(file = NULL)
} else dev.new()
# Ensure we have a graphics device available for recording, but choose
# one that's available on all platforms and doesn't write to disk
pdf(file = NULL)
dev.control(displaylist = "enable")
dev <- dev.cur()
on.exit(dev.off(dev))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/plot-persp.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
x <- seq(-10, 10, length = 30)
x <- seq(-10, 10, length.out = 30)
y <- x
ff <- function(x,y) { r <- sqrt(x^2 + y^2); 10 * sin(r) / r }
z <- outer(x, y, ff)
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-graphics.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ test_that("by default, evaluate() always records plots regardless of the device"
})

test_that("Rplots.pdf files are not created", {
op <- options(device = pdf)
on.exit(options(op))
evaluate(file("plot.R"))
ev <- evaluate("plot(1)")
expect_false(file.exists("Rplots.pdf"))
})

# https://github.com/yihui/knitr/issues/2297
test_that("existing plots will not leak into evaluate()", {
withr::local_options(device = function() pdf(NULL))

pdf(NULL)
dev.control('enable')
d <- dev.cur()
Expand Down

0 comments on commit c750df4

Please sign in to comment.