Skip to content

Enforce that snapshot files should be unique - #1592

Merged
hadley merged 20 commits into
r-lib:mainfrom
schloerke:unique_snapshots
Aug 11, 2025
Merged

hadley merged 20 commits into
r-lib:mainfrom
schloerke:unique_snapshots

Conversation

@schloerke

@schloerke schloerke commented Mar 21, 2022 •

Copy link
Copy Markdown
Contributor

It seems odd that two different tests can save to the same snapshot file without error. This PR enforces that once a snapshot file has had an expectation, it can not have another expectation.

I tried doing this in {shinytest2}, but it felt like a giant hack and should be pushed upstream to {testthat}.


Implementation details:

We can not use SnapshotReporter$snap_file_seen as it is encouraged to use this field via announce_snapshot_file(file) to avoid files being deleted.

However, if we record what has been saved, then no saved file should be duplicated. Once a new test file starts, $snap_file_saved should be reset


  • News entry

Can not use `SnapshotReporter$snap_file_seen` as it is encouraged to use this field via `announce_snapshot_file(file)` to avoid files being deleted.

However, if we record what has been saved, then no saved file should be duplicated. Once a new test file starts, `$snap_file_saved` should be reset
@hadley

hadley commented Jul 25, 2025

Copy link
Copy Markdown
Member

Are you still interesting in this? If so, if you fix the merge conflicts, I'm happy to consider for the next release.

@schloerke

Copy link
Copy Markdown
Contributor Author

Yes. I'll resolve the conflicts here shortly 😃

schloerke and others added 4 commits July 25, 2025 14:54
* upstream/HEAD: (250 commits)
  Implement `expect_shape()` (r-lib#1469)
  Re-enable catch tests on windows (r-lib#2104)
  Move digest to suggests (r-lib#2105)
  Support emscripten in `skip_on_os()` (r-lib#2103)
  Add `skip_unless_r()` (r-lib#2094)
  Require R 4.1 (r-lib#2101)
  Allow unquoting first arg for `expect_s4_class()` (r-lib#2065)
  typo fix (r-lib#2051)
  Explicitly pass `parent.frame()` in `it()` (r-lib#2086)
  Update vignette advice for migrating to {testthat} 3e (r-lib#2080)
  New praise messages (r-lib#1974)
  move '!' outside aggregation (r-lib#2067)
  Handle deprecation of std::uncaught_exception() (r-lib#2097)
  perf: use `anyNA()` (r-lib#2058)
  Upkeep (r-lib#2099)
  Implement `mock_output_sequence()` (r-lib#2061)
  Increment version number to 3.2.3.9000
  Increment version number to 3.2.3
  Update CRAN comments
  Finish off r-lib#2046
  ...
Comment thread R/snapshot-reporter.R Resolved
Comment thread tests/testthat/test-snapshot-file.R Resolved
Comment thread tests/testthat/test-snapshot-file.R Resolved
Comment thread tests/testthat/test-snapshot-file.R Resolved
Comment thread tests/testthat/test-snapshot-file.R Resolved
Comment thread tests/testthat/test-snapshot.R Resolved
Comment thread tests/testthat/test-snapshot.R Resolved
Comment thread tests/testthat/test-snapshot.R Resolved
Comment thread NEWS.md Resolved
Comment thread NEWS.md Resolved
Comment thread tests/testthat/test-snapshot-file.R Resolved
@schloerke

Copy link
Copy Markdown
Contributor Author

@hadley PR is much cleaner now.

  • No air changes.
  • Smaller file
  • Using cli::cli_abort() to signal to pkg author that their snap name is duplicated.

Comment thread R/snapshot-reporter.R Resolved
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.

2 participants