r-lib / testthat

An R 📦 to make testing 😀
https://testthat.r-lib.org
Other
890 stars 316 forks source link

File snapshot tests fail when switching between git branches #1595

Closed torres-pedro closed 2 years ago

torres-pedro commented 2 years ago

Hello, first and foremost I apologise if I am missing something obvious that leads to this error, I'm new at R and there's many thing yet to learn!

Let's say for simplicity that I have a function csv_to_tsv() that reads an input csv file data.csv and then writes a new tab separated file with that data, data.tsv. I attempted to implement expect_snapshot_file() in order to keep a snapshot file as a standard in case I would want to change something in the function, as in going from write.table to readr::write_table, for instance. This is how I implemented it in testconvert.R:

save_tsv <- function(file) {
  path <- tempfile(fileext = ".tsv")
  invisible(csv_to_tsvfile,path))
  return(path)
}

test_that("Converting CSV to TSV gives expected results", {
  local_edition(3)
  expect_snapshot_file(save_tsv("data.csv"),"datasnap.tsv")
})

The first time I run devtools::test() the snapshots are created and everytime I test again everything goes smoothly and tests are passed, as expected.

However, if I change git branches to work on something else and come back to this branch later, running devtools::test() fails:

Failure (testconvert.R:38:3): Converting CSV to TSV gives expected results
Snapshot of `save_tsv("data.csv")` to 'testconvert/datasnap.tsv' has changed
Run `testthat::snapshot_review('testconvert/')` to review changes

Checking the differences shows there aren't any... and most curiously, if I delete the previously created snapshots and run test() again, the new snapshots created are not even registered as modifications in the git branch, since they are exactly as the ones that were there before.

What could be the reason for this error? Is there some file metadata checking going on when comparing the files, or something of a similar nature?

hadley commented 2 years ago

No idea, sorry. I'd need a reprex to be able to figure it out.