ITADN

`expect_snapshot` and friends suggest incorrect review and accept code when `variant` is not NULL

#2325Openmrcaseb 创建于 2026-02-20
M
mrcasebcommented
Let's start with what I am observing during package development: When passing a `variant` to `expect_snapshot()` or `expect_snapshot_value()` the printed `snapshot_hint` prints incorrect `files` as running the suggested code results in `#> No snapshots to update`. The printed `files` are the name of the test file and it misses the `variant` directory. Since I don't really know the internals of testthat, I had a hard time creating a reprex. I went off of #1545 and was asked by Hadley to file a new issue in https://github.com/r-lib/testthat/issues/1545#issuecomment-3931169096 ``` r library(testthat) # I need test_dir as path for snapshot_review test_dir <- withr::local_tempdir() # and I need snap_dir as path where snapshots should be saved to snap_dir <- file.path(test_dir, "_snaps") snapper <- testthat:::local_snapshotter(snap_dir) snapper$start_file("test", "test") test_that("test", { local_edition(3) expect_snapshot_value("a", variant = "barret") }) #> ── Warning: test ─────────────────────────────────────────────────────────────── #> Adding new snapshot for variant 'barret': #> "a" #> Test passed with 1 success 🎉. snapper$end_file() list.files(test_dir, recursive = TRUE) #> [1] "_snaps/barret/test.md" snapper$start_file("test", "test") test_that("test", { local_edition(3) expect_snapshot_value("b", variant = "barret") }) #> ── Failure: test ─────────────────────────────────────────────────────────────── #> Snapshot of "b" has changed (variant 'barret'): #> old | new #> [1] a - b [1] #> * Run `testthat::snapshot_accept("test")` to accept the change. #> * Run `testthat::snapshot_review("test")` to review the change. #> Error: #> ! Test failed with 1 failure and 0 successes. snapper$end_file() list.files(test_dir, recursive = TRUE) #> [1] "_snaps/barret/test.md" "_snaps/barret/test.new.md" # The suggested code: doesn't work snapshot_review("test", path = test_dir) #> No snapshots to update. # The following two work as expected # what snapshot_meta expects us to pass snapshot_review("barret/", path = test_dir) # also works because it checks all recursive dirs? # I wonder if this would let us review more than we wanted if # there were other changed snapshots in other sub directories. snapshot_review(path = test_dir) ``` <sup>Created on 2026-02-19 with [reprex v2.1.1](https://reprex.tidyverse.org)</sup> <details style="margin-bottom:10px;"> <summary> Session info </summary> ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.5.2 Patched (2025-10-31 r88977) #> os macOS Tahoe 26.2 #> system aarch64, darwin20 #> ui X11 #> language en #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Berlin #> date 2026-02-19 #> pandoc 3.6.3 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/aarch64/ (via rmarkdown) #> quarto 1.7.33 @ /usr/local/bin/quarto #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> brio 1.1.5 2024-04-24 [1] CRAN (R 4.5.0) #> cli 3.6.5 2025-04-23 [1] CRAN (R 4.5.0) #> crayon 1.5.3 2024-06-20 [1] CRAN (R 4.5.0) #> desc 1.4.3 2023-12-10 [1] CRAN (R 4.5.0) #> diffobj 0.3.6 2025-04-21 [1] CRAN (R 4.5.0) #> diffviewer 0.1.2 2024-06-12 [1] CRAN (R 4.5.0) #> digest 0.6.39 2025-11-19 [1] CRAN (R 4.5.2) #> evaluate 1.0.5 2025-08-27 [1] CRAN (R 4.5.0) #> fastmap 1.2.0 2024-05-15 [1] CRAN (R 4.5.0) #> fs 1.6.6 2025-04-12 [1] CRAN (R 4.5.0) #> glue 1.8.0 2024-09-30 [1] CRAN (R 4.5.0) #> htmltools 0.5.9 2025-12-04 [1] CRAN (R 4.5.2) #> httpuv 1.6.16 2025-04-16 [1] CRAN (R 4.5.0) #> jsonlite 2.0.0 2025-03-27 [1] CRAN (R 4.5.0) #> knitr 1.51 2025-12-20 [1] CRAN (R 4.5.2) #> later 1.4.6 2026-02-13 [1] CRAN (R 4.5.2) #> lifecycle 1.0.5 2026-01-08 [1] CRAN (R 4.5.2) #> magrittr 2.0.4 2025-09-12 [1] CRAN (R 4.5.0) #> mime 0.13 2025-03-17 [1] CRAN (R 4.5.0) #> otel 0.2.0 2025-08-29 [1] CRAN (R 4.5.0) #> pillar 1.11.1 2025-09-17 [1] CRAN (R 4.5.0) #> pkgload 1.5.0 2026-02-03 [1] CRAN (R 4.5.2) #> promises 1.5.0 2025-11-01 [1] CRAN (R 4.5.0) #> R6 2.6.1 2025-02-15 [1] CRAN (R 4.5.0) #> Rcpp 1.1.1 2026-01-10 [1] CRAN (R 4.5.2) #> reprex 2.1.1 2024-07-06 [1] CRAN (R 4.5.0) #> rlang 1.1.7 2026-01-09 [1] CRAN (R 4.5.2) #> rmarkdown 2.30 2025-09-28 [1] CRAN (R 4.5.0) #> rprojroot 2.1.1 2025-08-26 [1] CRAN (R 4.5.0) #> rstudioapi 0.18.0 2026-01-16 [1] CRAN (R 4.5.2) #> sessioninfo 1.2.3 2025-02-05 [1] CRAN (R 4.5.0) #> shiny 1.12.1 2025-12-09 [1] CRAN (R 4.5.2) #> testthat * 3.3.2 2026-01-11 [1] CRAN (R 4.5.2) #> vctrs 0.7.1 2026-01-23 [1] CRAN (R 4.5.2) #> waldo 0.6.2 2025-07-11 [1] CRAN (R 4.5.0) #> withr 3.0.2 2024-10-28 [1] CRAN (R 4.5.0) #> xfun 0.56 2026-01-18 [1] CRAN (R 4.5.2) #> xtable 1.8-4 2019-04-21 [1] CRAN (R 4.5.0) #> yaml 2.3.12 2025-12-10 [1] CRAN (R 4.5.2) #> #> [2] /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library #> * ── Packages attached to the search path. #> #> ────────────────────────────────────────────────────────────────────────────── ``` </details>
1 条评论