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

Support compressed snapshot files #1949

Closed
MLopez-Ibanez opened this issue Apr 11, 2024 · 1 comment
Closed

Support compressed snapshot files #1949

MLopez-Ibanez opened this issue Apr 11, 2024 · 1 comment

Comments

@MLopez-Ibanez
Copy link

I would like to store my snapshot files compressed with .xz, so it would be great to have something like compare_file_text_compressed.

This is what I have implemented:

compare_file_text_compressed <- function(old, new)
{
  if (compare_file_binary(old, new))
    return(TRUE)
  old <- base::readLines(withr::local_connection(gzfile(old, open = "r")), warn = FALSE)
  new <- base::readLines(withr::local_connection(gzfile(new, open = "r")), warn = FALSE)
  identical(old, new)
}

save_csv_xz <- function(code, pattern)
{
  path <- tempfile(fileext = ".csv.xz")
  res <- code
  write.table(file = withr::local_connection(xzfile(path, "w")),
    res, row.names = FALSE, col.names=FALSE, sep=",")
  path
}

expect_snapshot_csv_xz <- function(name, code)
{
  # skip_on_ci() # Skip for now until we implement this: https://github.com/tidyverse/ggplot2/blob/main/tests/testthat/helper-vdiffr.R
  name <- paste0(name, ".csv.xz")
  # Announce the file before touching `code`. This way, if `code`
  # unexpectedly fails or skips, testthat will not auto-delete the
  # corresponding snapshot file.
  testthat::announce_snapshot_file(name = name)
  path <- save_csv_xz(code)
  testthat::expect_snapshot_file(path, name = name, compare = compare_file_text_compressed)
}
@hadley
Copy link
Member

hadley commented Oct 22, 2024

I think this is a great helper for you to use locally, but it's not something I'm particularly keen to add to testthat because I believe it eliminates one of the big benefits of snapshot testing: you can no longer inspect the snapshots when reviewing code.

@hadley hadley closed this as completed Oct 22, 2024
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

No branches or pull requests

2 participants