Skip to content

Commit

Permalink
Use own defer implementation (#563)
Browse files Browse the repository at this point in the history
Fixes #561
  • Loading branch information
hadley authored Nov 7, 2023
1 parent fcda53b commit 52fa339
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 12 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ Imports:
progress,
Rcpp,
rlang (>= 0.4.9),
tibble,
withr
tibble
Suggests:
blob,
covr,
Expand All @@ -46,7 +45,8 @@ Suggests:
readr,
sodium,
testthat (>= 3.1.5),
wk (>= 0.3.2)
wk (>= 0.3.2),
withr
LinkingTo:
progress,
rapidjsonr,
Expand Down
8 changes: 4 additions & 4 deletions R/bq-download.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ bq_table_download <-
start_index <- params$start_index

schema_path <- bq_download_schema(x, tempfile())
withr::defer(file.remove(schema_path))
defer(file.remove(schema_path))

if (n_max == 0) {
table_data <- bq_parse_files(
Expand Down Expand Up @@ -134,7 +134,7 @@ bq_table_download <-
)
curl::multi_run(pool = pool)
path_first_chunk <- chunk_plan$dat$path[1]
withr::defer(file.remove(path_first_chunk))
defer(file.remove(path_first_chunk))

chunk_data <- bq_parse_file(schema_path, path_first_chunk)
n_got <- nrow(chunk_data)
Expand Down Expand Up @@ -198,7 +198,7 @@ bq_table_download <-
)
}
curl::multi_run(pool = pool)
withr::defer(file.remove(chunk_plan$dat$path))
defer(file.remove(chunk_plan$dat$path))

table_data <- bq_parse_files(
schema_path,
Expand Down Expand Up @@ -329,7 +329,7 @@ bq_download_callback <- function(path, progress = NULL) {
)

con <- file(path, open = "wb")
withr::defer(close(con))
defer(close(con))
writeBin(result$content, con)
}
}
Expand Down
2 changes: 1 addition & 1 deletion R/bq-perform.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export_json <- function(values) {
})

con <- rawConnection(raw(0), "r+")
on.exit(close(con))
defer(close(con))
jsonlite::stream_out(values, con, verbose = FALSE, na = "null")

rawToChar(rawConnectionValue(con))
Expand Down
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ print.bq_bytes <- function(x, ...) {
}
# nocov end

defer <- function (expr, env = caller_env(), after = FALSE) {
thunk <- as.call(list(function() expr))
do.call(on.exit, list(thunk, TRUE, after), envir = env)
}
2 changes: 1 addition & 1 deletion tests/testthat/test-bq-dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_that("can create and delete datasets", {

test_that("can update dataset metadata", {
ds <- bq_dataset(bq_test_project(), random_name())
on.exit(bq_dataset_delete(ds))
defer(bq_dataset_delete(ds))

bq_dataset_create(ds, description = "a", friendly_name = "b")
bq_dataset_update(ds, description = "b")
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-bq-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ test_that("can roundtrip via save + load", {
gs <- gs_test_object()

bq_table_save(tb1, gs)
on.exit(gs_object_delete(gs))
defer(gs_object_delete(gs))
bq_table_load(tb2, gs)

df <- bq_table_download(tb2)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ test_that("can collect and compute (no dataset)", {
# compute: persistent
name <- paste0("basedata.", random_name())
temp <- dplyr::compute(bq_mtcars, temporary = FALSE, name = name)
on.exit(DBI::dbRemoveTable(con, name))
defer(DBI::dbRemoveTable(con, name))

expect_true(DBI::dbExistsTable(con, name))
})
Expand All @@ -66,7 +66,7 @@ test_that("can collect and compute (with dataset)", {
# compute: persistent
name <- random_name()
temp <- dplyr::compute(bq_mtcars, temporary = FALSE, name = name)
on.exit(DBI::dbRemoveTable(con, name))
defer(DBI::dbRemoveTable(con, name))

expect_true(DBI::dbExistsTable(con, name))
})
Expand Down

0 comments on commit 52fa339

Please sign in to comment.