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

Prevent snapshot cleanup #1481

Open
rickhelmus opened this issue Nov 4, 2021 · 3 comments
Open

Prevent snapshot cleanup #1481

rickhelmus opened this issue Nov 4, 2021 · 3 comments
Labels
feature a feature request or enhancement snapshot 📷

Comments

@rickhelmus
Copy link

Hello,

I have quite a lot of tests relying on vdiffr, which recently started using snapshots. Which tests are executed is pre-determined by a shell environment variable. However, this conditional system doesn't play nicely anymore with the automatic cleaning of 'unused' snapshot files, as all the reference snapshots from tests not being executed are removed. I wondered if there is a way to prevent activation of the automatic cleaning system?

Thanks,
Rick

@hadley hadley added feature a feature request or enhancement snapshot 📷 labels Dec 20, 2021
@hadley hadley added this to the v3.1.2 milestone Dec 20, 2021
@hadley
Copy link
Member

hadley commented Jan 4, 2022

Do you have an example you could point me to? I think you might be able to use variants to help with this.

@hadley
Copy link
Member

hadley commented Jan 5, 2022

Ok, the root problem is coming back to me — when you run expect_snapshot_file(..., name = "foo"), testthat registers foo as a known file, and something that shouldn't be deleted. If expect_snapshot_file() isn't run (regardless of how), that name never gets registered, and the file is slated for automatic deletion. One way to work around this problem is to use annouce_file_snapshot() which registers the name without running the test. This looks something like:

if (run_snaps()) {
  expect_snapshot_file(path, name = "foo")
} else {
  announce_file_snapshot(name = "foo")
  skip("Snaps off")
}

This is obviously a bit clunky, but you could wrap up into a helper:

my_expect_snapshot_file <- function(path, name = basename(path), ...) {
    if (run_snaps()) {
    expect_snapshot_file(path, name = name, ...)
  } else {
    announce_file_snapshot(name = name)
    skip("Snaps off")
  }
}

It's possible that testthat could do more here, but I'm not sure what, and hopefully this code helps a little.

@katossky
Copy link

I have a similar issue with skipped test. When I do test offline, I do not want the online-only snapshots to disappear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement snapshot 📷
Projects
None yet
Development

No branches or pull requests

3 participants