Skip to content

Commit

Permalink
Don’t overwrite existing file if appending an empty data frame (#451)
Browse files Browse the repository at this point in the history
* add test for appending empty file

* check `append` before returning empty file

* check gha

* change test to `altrep = FALSE`

* change how `append` test is written

* restyle test

* should still return early with empty file + `append`

* modify comments in early return

Co-authored-by: Jennifer (Jenny) Bryan <jenny.f.bryan@gmail.com>

* remove `skip_empty_rows` from test

* restyle

* Match style of other expectations in this file

* Add NEWS bullet

Co-authored-by: Jennifer (Jenny) Bryan <jenny.f.bryan@gmail.com>
  • Loading branch information
sbearrows and jennybc committed Aug 17, 2022
1 parent 0f6cb14 commit a8abdc1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
@@ -1,5 +1,7 @@
# vroom (development version)

* `vroom_write(append = TRUE)` does not modify an existing file when appending an empty data frame. In particular, it does not overwrite (delete) the existing contents of that file (https://github.com/tidyverse/readr/issues/1408, #451).

* `vroom::problems()` now defaults to `.Last.value` for its primary input, similar to how `readr::problems()` works (#443).

* The warning that indicates the existence of parsing problems has been improved, which should make it easier for the user to follow-up (https://github.com/tidyverse/readr/issues/1322).
Expand Down
4 changes: 2 additions & 2 deletions R/vroom_write.R
Expand Up @@ -58,9 +58,9 @@ vroom_write <- function(x, file, delim = '\t', eol = "\n", na = "NA", col_names
# Standardise path returns a list, but we will only ever have 1 output file.
file <- standardise_one_path(file, write = TRUE)

# If there are no columns in the data frame, just create an empty file and return
if (NCOL(x) == 0) {
if (!inherits(file, "connection")) {
if (!append && !inherits(file, "connection")) {
# if file already exists, it is overwritten with an empty file!
file.create(file)
}
return(invisible(input))
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-vroom_write.R
Expand Up @@ -306,3 +306,17 @@ test_that("na argument modifies how missing values are written", {
df <- data.frame(x = c(NA, "x"), y = c(1, 2))
expect_equal(vroom_format(df, ",", na = "None"), "x,y\nNone,1\nx,2\n")
})

test_that("vroom_write() does not overwrite file when appending empty data frame", {
tf <- withr::local_tempfile()
data <- tibble::tibble(
a = "1",
b = "2",
c = "3"
)

vroom_write(data, file = tf, delim = ",")
vroom_write(data.frame(), file = tf, append = TRUE, delim = ",")

expect_equal(vroom_lines(tf, altrep = FALSE), c("a,b,c", "1,2,3"))
})

0 comments on commit a8abdc1

Please sign in to comment.