Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up temporary file in verify_exec() #1876

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
* `expect_setequal()` correctly displays results when only one of actual and
expected is missing values (#1835).

* `expect_snapshot()` and friends no longer create a temporary file for every
invocation.

* `expect_snapshot_file()` now generates clickable links to review changes
(#1821).

Expand Down
2 changes: 1 addition & 1 deletion R/reporter-junit.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ JunitReporter <- R6::R6Class("JunitReporter",
if (is.character(self$out)) {
xml2::write_xml(self$doc, self$out, format = TRUE)
} else if (inherits(self$out, "connection")) {
file <- tempfile()
file <- withr::local_tempfile()
xml2::write_xml(self$doc, file, format = TRUE)
cat(brio::read_file(file), file = self$out)
} else {
Expand Down
6 changes: 3 additions & 3 deletions R/snapshot-file.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,14 @@ split_path <- function(path) {
)
}

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

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

dirs <- file.path(dir, unique(dirname(paths)))
Expand Down
2 changes: 1 addition & 1 deletion R/test-example.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ test_rd <- function(rd, title = attr(rd, "Rdfile")) {
#' @export
#' @rdname test_examples
test_example <- function(path, title = path) {
ex_path <- tempfile(fileext = ".R")
ex_path <- withr::local_tempfile(pattern = "test_example-", fileext = ".R")
tools::Rd2ex(path, ex_path)
if (!file.exists(ex_path)) {
return(invisible(FALSE))
Expand Down
3 changes: 2 additions & 1 deletion R/verify-output.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ verify_exec <- function(expr, env = caller_env(), replay = output_replay) {
exprs <- list(expr)
}

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

exprs <- lapply(exprs, function(x) if (is.character(x)) paste0("# ", x) else expr_deparse(x))
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-expect-known.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_that("uses specified width", {
})

test_that("creates file on first run", {
file <- tempfile()
file <- withr::local_tempfile()
expect_success(
expect_warning(
expect_known_output(cat("ok!\n"), file),
Expand All @@ -23,7 +23,7 @@ test_that("creates file on first run", {
})

test_that("ignores incomplete last line", {
file <- tempfile()
file <- withr::local_tempfile()
writeLines("Hi!", file)
expect_success(expect_known_output(cat("Hi!"), file))
expect_success(expect_known_output(cat("Hi!\n"), file))
Expand All @@ -32,7 +32,7 @@ test_that("ignores incomplete last line", {
})

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

Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/test-reporter.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
test_that("can control output with file arg/option", {
# powered through Reporter base class so we only test one reporter
path <- tempfile()
withr::defer(unlink(path))
path <- withr::local_tempfile()

with_reporter(
MinimalReporter$new(file = path),
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-snapshot-file.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ test_that("expect_snapshot_file works", {
compare = compare_file_text
)

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

path <- tempfile()
path <- withr::local_tempfile()
mtcars2 <- mtcars
# mtcars2$wt[10] <- NA
write.csv(mtcars2, path)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-test-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test_that("runs all tests and records output", {

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

expect_error(test_dir(path), "test files")
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-verify-output.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,11 @@ test_that("verify_output() handles carriage return", {
cat("\r\n")
})
})

test_that("verify_exec() doesn't leave tempfiles around", {
before <- dir(tempdir())
verify_exec(quote(1 + 1))
after <- dir(tempdir())

expect_equal(before, after)
})
4 changes: 2 additions & 2 deletions tests/testthat/test-watcher.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("compare state works correctly", {
loc <- tempfile("watcher")
loc <- withr::local_tempfile(pattern = "watcher")
dir.create(loc)

empty <- dir_state(loc)
Expand Down Expand Up @@ -45,7 +45,7 @@ test_that("watcher works correctly", {
skip("touch (or which) not available")
}

loc <- tempfile("watcher")
loc <- withr::local_tempfile(pattern = "watcher")
dir.create(loc)

code_path <- file.path(loc, "R")
Expand Down
Loading