Skip to content

Commit 91df560

Browse files
authored
Clean up temporary file in verify_exec() (#1876)
And reduce tempfile creation in tests
1 parent 89d1546 commit 91df560

File tree

11 files changed

+27
-16
lines changed

11 files changed

+27
-16
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
* `expect_setequal()` correctly displays results when only one of actual and
3232
expected is missing values (#1835).
3333

34+
* `expect_snapshot()` and friends no longer create a temporary file for every
35+
invocation.
36+
3437
* `expect_snapshot_file()` now generates clickable links to review changes
3538
(#1821).
3639

R/reporter-junit.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ JunitReporter <- R6::R6Class("JunitReporter",
136136
if (is.character(self$out)) {
137137
xml2::write_xml(self$doc, self$out, format = TRUE)
138138
} else if (inherits(self$out, "connection")) {
139-
file <- tempfile()
139+
file <- withr::local_tempfile()
140140
xml2::write_xml(self$doc, file, format = TRUE)
141141
cat(brio::read_file(file), file = self$out)
142142
} else {

R/snapshot-file.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,14 @@ split_path <- function(path) {
239239
)
240240
}
241241

242-
write_tmp_lines <- function(lines, ext = ".txt", eol = "\n") {
243-
path <- tempfile(fileext = ext)
242+
write_tmp_lines <- function(lines, ext = ".txt", eol = "\n", envir = caller_env()) {
243+
path <- withr::local_tempfile(fileext = ext, .local_envir = envir)
244244
brio::write_lines(lines, path, eol = eol)
245245
path
246246
}
247247

248248
local_snap_dir <- function(paths, .env = parent.frame()) {
249-
dir <- tempfile()
249+
dir <- withr::local_tempfile(.local_envir = .env)
250250
withr::defer(unlink(paths), envir = .env)
251251

252252
dirs <- file.path(dir, unique(dirname(paths)))

R/test-example.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ test_rd <- function(rd, title = attr(rd, "Rdfile")) {
5353
#' @export
5454
#' @rdname test_examples
5555
test_example <- function(path, title = path) {
56-
ex_path <- tempfile(fileext = ".R")
56+
ex_path <- withr::local_tempfile(pattern = "test_example-", fileext = ".R")
5757
tools::Rd2ex(path, ex_path)
5858
if (!file.exists(ex_path)) {
5959
return(invisible(FALSE))

R/verify-output.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ verify_exec <- function(expr, env = caller_env(), replay = output_replay) {
7171
exprs <- list(expr)
7272
}
7373

74-
withr::local_pdf(tempfile())
74+
device_path <- withr::local_tempfile(pattern = "verify_exec_")
75+
withr::local_pdf(device_path)
7576
grDevices::dev.control(displaylist = "enable")
7677

7778
exprs <- lapply(exprs, function(x) if (is.character(x)) paste0("# ", x) else expr_deparse(x))

tests/testthat/test-expect-known.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test_that("uses specified width", {
1111
})
1212

1313
test_that("creates file on first run", {
14-
file <- tempfile()
14+
file <- withr::local_tempfile()
1515
expect_success(
1616
expect_warning(
1717
expect_known_output(cat("ok!\n"), file),
@@ -23,7 +23,7 @@ test_that("creates file on first run", {
2323
})
2424

2525
test_that("ignores incomplete last line", {
26-
file <- tempfile()
26+
file <- withr::local_tempfile()
2727
writeLines("Hi!", file)
2828
expect_success(expect_known_output(cat("Hi!"), file))
2929
expect_success(expect_known_output(cat("Hi!\n"), file))
@@ -32,7 +32,7 @@ test_that("ignores incomplete last line", {
3232
})
3333

3434
test_that("updates by default", {
35-
file <- tempfile()
35+
file <- withr::local_tempfile()
3636
writeLines("Hi!", file)
3737
expect_failure(expect_known_output(cat("oops"), file, update = FALSE))
3838

tests/testthat/test-reporter.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
test_that("can control output with file arg/option", {
22
# powered through Reporter base class so we only test one reporter
3-
path <- tempfile()
4-
withr::defer(unlink(path))
3+
path <- withr::local_tempfile()
54

65
with_reporter(
76
MinimalReporter$new(file = path),

tests/testthat/test-snapshot-file.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ test_that("expect_snapshot_file works", {
66
compare = compare_file_text
77
)
88

9-
path <- tempfile()
9+
path <- withr::local_tempfile()
1010
png(path, width = 300, height = 300, type = "cairo")
1111
plot(1:10, xlab = "", ylab = "", pch = 20, cex = 5, axes = FALSE)
1212
dev.off()
1313
expect_snapshot_file(path, "foo.png")
1414

15-
path <- tempfile()
15+
path <- withr::local_tempfile()
1616
mtcars2 <- mtcars
1717
# mtcars2$wt[10] <- NA
1818
write.csv(mtcars2, path)

tests/testthat/test-test-files.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test_that("runs all tests and records output", {
2020

2121
test_that("complains if no files", {
2222
withr::local_envvar(TESTTHAT_PARALLEL = "FALSE")
23-
path <- tempfile()
23+
path <- withr::local_tempfile()
2424
dir.create(path)
2525

2626
expect_error(test_dir(path), "test files")

tests/testthat/test-verify-output.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,11 @@ test_that("verify_output() handles carriage return", {
8383
cat("\r\n")
8484
})
8585
})
86+
87+
test_that("verify_exec() doesn't leave tempfiles around", {
88+
before <- dir(tempdir())
89+
verify_exec(quote(1 + 1))
90+
after <- dir(tempdir())
91+
92+
expect_equal(before, after)
93+
})

0 commit comments

Comments
 (0)