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

Don’t overwrite existing file if appending an empty data frame #451

Merged
merged 13 commits into from Aug 17, 2022

Conversation

sbearrows
Copy link
Contributor

Fixes tidyverse/readr#1408

Previously, if vroom_write() was supplied an empty data frame, vroom created an empty file without checking the value of append. But this meant data from the file could be deleted such as in this case.

library(vroom)
tf <- withr::local_tempfile()
data <- tibble::tibble(a = "1", b = "2", c = "3")

vroom_write(data, file = tf)
vroom(tf, show_col_types = FALSE)
#> # A tibble: 1 × 3
#>       a     b     c
#>   <dbl> <dbl> <dbl>
#> 1     1     2     3

vroom_write(data.frame(), file = tf, append = TRUE)
vroom(tf, show_col_types = FALSE)
#> # A tibble: 0 × 0

Created on 2022-07-15 by the reprex package (v2.0.1.9000)

@sbearrows sbearrows requested a review from jennybc July 16, 2022 03:33
@jennybc
Copy link
Member

jennybc commented Aug 12, 2022

I recommend turning off altrep / lazy reading in this test.

R/vroom_write.R Outdated Show resolved Hide resolved
@sbearrows sbearrows changed the title Check append before returning empty file Don’t overwrite existing file if appending an empty data frame Aug 12, 2022
@sbearrows
Copy link
Contributor Author

@jennybc I believe this is ready for another look!

R/vroom_write.R Outdated Show resolved Hide resolved
R/vroom_write.R Show resolved Hide resolved
expect_equal(
vroom_lines(tf,
altrep = FALSE,
skip_empty_rows = FALSE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't see why we need to specify skip_empty_rows.

If we don't, let's remove it. In which case, the expectation can be come more compact.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need it. I just wanted to make sure it wasn't writing an empty line when vroom_write(data.frame(), file = file, append = TRUE)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if vroom_write(data.frame(), file = file, append = TRUE) wrote an empty line, that would be a bug and we would want to know about it. So I think the test is better without it (which I see has happened now).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick:

let's remove it. In which case, the expectation can be come more compact.

There's still this.

Copy link
Member

@jennybc jennybc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the expectation in the new test more similar to other in this file / package.

I added a NEWS bullet.

@jennybc jennybc merged commit a8abdc1 into main Aug 17, 2022
@sbearrows sbearrows deleted the append_empty_data branch August 17, 2022 17:07
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

Successfully merging this pull request may close these issues.

write_delim clearing (non-empty) files with append = TRUE
2 participants