Skip to content

expect_snapshot does not work during auto_test_package() when single test file reruns are triggered #2049

@sebkopf

Description

@sebkopf

Apologies upfront, I don't know how to reprex this particular issue but here's how it can be reproduced in a few steps.

In a bare R package, add a file with a simple snapshot test, such as test-example.R below, and run devtools::test() once to create the snapshot.

test_that("produce auto_test issue with snapshots", {
  expect_snapshot(1)
})

Now run auto_test_package(). At first it behaves as I would expect. It runs the tests (or single test in this example) and reruns them when I make code changes in any files in R or src folders. However if I make a change in the test file (test-example.R) and it thus triggers a rerun of only the tests in that file, the paths to the snapshot end up wrong somehow, it tries to re-create the snapshot and the tests fail:

> auto_test_package()
ℹ Loading examplepkg
✔ | F W  S  OK | Context
✔ |          1 | example                                                                                                                                                                                                                                                                

══ Results ═════════════════
[ FAIL 0 | WARN 0 | SKIP 0 | PASS 1 ]
Changed code:  example.R 
Rerunning all tests
✔ | F W  S  OK | Context
✔ |          1 | example                                                                                                                                                                                                                                                                

══ Results ═════════════════
[ FAIL 0 | WARN 0 | SKIP 0 | PASS 1 ]
Rerunning tests:  test-example.R 
✔ | F W  S  OK | Context
✔ |   1      1 | /Users/sk/R/examplepkg/tests/testthat/test-example                                                                     
──────────────────
Warning (/Users/sk/R/examplepkg/tests/testthat/test-example.R:2:3): produce auto_test issue with snapshots
Adding new snapshot:
Code
  1
Output
  [1] 1
──────────────────
Error in brio::write_file(out, path) : 
  Could not open file: /Users/sk/R/examplepkg/tests/testthat/_snaps//Users/sk/R/examplepkg/tests/testthat/test-example.md

I suspect this has something todo with the tests paths of the watcher here but not sure what's going wrong exactly:

test_paths = tests,

Session info:

> sessionInfo()
R version 4.3.3 (2024-02-29)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.6.1

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/Berlin
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1]  testthat_3.2.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions