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

Support for temporary files and directories #828

Closed
gaborcsardi opened this issue Dec 12, 2018 · 4 comments
Closed

Support for temporary files and directories #828

gaborcsardi opened this issue Dec 12, 2018 · 4 comments

Comments

@gaborcsardi
Copy link
Member

These should be cleaned up, so it would be nice to support them "natively". E.g. something like:

tmpf <- test_temp_file()
tmpd <- test_temp_dir(cleanup = "block")

cleanup can be block or file or testsuite, to specify when to clean up the files.

withr::local_tempfile() is almost good, and testthat could use it internally, but it is annoying that it fails if you copy and paste a test case to the console:

withr::local_tempfile("foobar")
Error in defer(unlink(mget(new, envir = envir)), envir = envir) :
  attempt to defer event on global environment

The testthat function could just ignore these calls.

Happy to do a PR.

@jimhester
Copy link
Member

That error would be fixed by r-lib/withr#76, which basically we just need to settle on an API for.

@gaborcsardi
Copy link
Member Author

FWIW, here are some simple versions:

test_temp_file <- function(fileext = "", pattern = "test-file-",
                           envir = parent.frame()) {
  tmp <- tempfile(pattern = pattern, fileext = fileext)
  withr::defer(
    try(unlink(tmp, recursive = TRUE, force = TRUE), silent = TRUE),
    envir = envir)
  tmp
}

test_temp_dir <- function(pattern = "test-dir-", envir = parent.frame()) {
  tmp <- test_temp_file(pattern, envir = envir)
  dir.create(tmp, recursive = TRUE, showWarnings = FALSE)
  tmp
}

@hadley
Copy link
Member

hadley commented Mar 28, 2019

Can this now be handled by withr?

@hadley
Copy link
Member

hadley commented Apr 1, 2019

Yes, it seems clear this is now the responsibility of withr.

@hadley hadley closed this as completed Apr 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants