Skip to content

Commit

Permalink
use withr::local_tempfile() in place of tempfile()
Browse files Browse the repository at this point in the history
  • Loading branch information
ijlyttle committed Jan 2, 2024
1 parent 03169f2 commit b578cf1
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 20 deletions.
5 changes: 1 addition & 4 deletions R/boxr_read.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ box_read <- function(file_id, type = NULL, version_id = NULL,
...) {
checkAuth()

temp_file <- tempfile()
temp_file <- withr::local_tempfile()

Check warning on line 57 in R/boxr_read.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_read.R#L57

Added line #L57 was not covered by tests

# Make the request
req <- boxGet(file_id, local_file = temp_file, version_id = version_id,
Expand Down Expand Up @@ -106,9 +106,6 @@ box_read <- function(file_id, type = NULL, version_id = NULL,
cont <- unclass(cont)
}

# Delete the tempfile
unlink(temp_file, force = TRUE)

message(
"Remote file '", new_name, "' read into memory as an object of class ",
paste(class(cont), collapse = ", "),
Expand Down
9 changes: 1 addition & 8 deletions R/boxr_save_load.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@
box_save <- function(..., dir_id = box_getwd(), file_name = ".RData",
description = NULL) {

# TODO: fs
#temp_file <- withr::local_tempfile(pattern = file_name)
temp_file <- normalizePath(file.path(tempdir(), file_name), mustWork = FALSE)

# clean up after ourselves
# TODO: withr 2.3.0 may have a cleaner way to do this: local_tempfile()
# - see https://github.com/r-lib/usethis/issues/1217
on.exit(fs::file_delete(temp_file))
temp_file <- withr::local_tempfile(pattern = file_name)

Check warning on line 34 in R/boxr_save_load.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_save_load.R#L34

Added line #L34 was not covered by tests

save(..., file = temp_file)

Expand Down
5 changes: 1 addition & 4 deletions R/boxr_upload_download.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ box_dl <- function(file_id, local_dir = getwd(), overwrite = FALSE,
stop("File already exists locally, and overwrite = FALSE")

# Get a temp file
temp_file <- tempfile()
temp_file <- withr::local_tempfile()

Check warning on line 86 in R/boxr_upload_download.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_upload_download.R#L86

Added line #L86 was not covered by tests

# Download to a tempfile with boxGet
req <- boxGet(file_id = file_id, version_id = version_id,
Expand Down Expand Up @@ -127,9 +127,6 @@ box_dl <- function(file_id, local_dir = getwd(), overwrite = FALSE,
stop("Problem writing file to ", new_file,
".\n Check that directory is writable.")

# Remove the tempfile to free up space
file.remove(temp_file)

return(new_file)
}

Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test_05_read_write.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test_that("You can source a remote R script", {
skip_if_no_token()

# Write a little R script
tf <- paste(tempfile(), ".R")
tf <- withr::local_tempfile(fileext = ".R")
writeLines("test_vector <- 1:10\n", tf)

# Upload it, so that you can 'source it' back down
Expand All @@ -26,7 +26,7 @@ test_that("You can write/read a remote .csv file", {
skip_if_no_token()

# Write a little .csv file
tf <- paste0(tempfile(), ".csv")
tf <- withr::local_tempfile(fileext = ".csv")
# Note: It looks like although httr says it uses read.csv for the .csv files,
# it doesn't obey the usual R behaviour of treating strings as factors by
# default. So to get two objects that match, you'll need to make sure they're
Expand Down Expand Up @@ -58,7 +58,7 @@ test_that("You can write/read a remote .tsv file", {
skip_if_no_token()

# Write a little .tsv file
tf <- paste0(tempfile(), ".tsv")
tf <- withr::local_tempfile(fileext = ".tsv")

df <- data.frame(a = letters[1:5], b = 1:5, c = rnorm(5),
stringsAsFactors = FALSE)
Expand Down Expand Up @@ -88,7 +88,7 @@ test_that("You can write/read a remote .json file", {
skip_if_no_token()

# Write a little .json file
tf <- paste0(tempfile(), ".json")
tf <- withr::local_tempfile(fileext = ".json")
df <- data.frame(a = letters[1:5], b = 1:5, c = rnorm(5),
stringsAsFactors = FALSE)
l <- list(a = 1:10, b = matrix(1, 3, 3), c = df)
Expand Down

0 comments on commit b578cf1

Please sign in to comment.