Skip to content

Conversation

@hadley
Copy link
Member

@hadley hadley commented Aug 12, 2025

I haven't yet figured out how to test snapshots with snapshots, so you can run this to test interactively

test_that("expect_snapshot_file() show differences for text files", {
  path1 <- withr::local_tempfile(lines = sample(letters[1:10]))
  expect_snapshot_file(path1, "text-diff.txt")
})

Fixes #1593

@schloerke can you please try this out and see how it feels?

I haven't yet figured out how to test snapshots with snapshots, so you can run this to test interactively

```R
test_that("expect_snapshot_file() show differences for text files", {
  path1 <- withr::local_tempfile(lines = sample(letters[1:10]))
  expect_snapshot_file(path1, "text-diff.txt")
})
```

Fixes #1593
@schloerke
Copy link
Contributor

Test

test_that("expect_snapshot_file() show differences for text files", {
  path1 <- withr::local_tempfile(
    lines = jsonlite::toJSON(sample(letters[1:10]))
  )
  expect_snapshot_file(path1, "text-diff.json")
})

testthat v3.2.3

✔ | F W  S  OK | Context
✖ | 1        1 | validate_remotes_order
─────────────────────────────────────────────
Failure (test-validate_remotes_order.R:13:3): expect_snapshot_file() show differences for text files
Snapshot of `path1` to 'validate_remotes_order/text-diff.json' has changed
Run `testthat::snapshot_review('validate_remotes_order/')` to review changes
─────────────────────────────────────────────

══ Results ════════════════════════════════════════════════════════
── Failed tests ─────────────────────────────────────
Failure (test-validate_remotes_order.R:13:3): expect_snapshot_file() show differences for text files
Snapshot of `path1` to 'validate_remotes_order/text-diff.json' has changed
Run `testthat::snapshot_review('validate_remotes_order/')` to review changes

[ FAIL 1 | WARN 0 | SKIP 0 | PASS 1 ]
Error: Test failures

testthat#2205

✔ | F W  S  OK | Context
✖ | 1        1 | validate_remotes_order                                                                                                                       
─────────────────────────────────────────────
Failure (test-validate_remotes_order.R:13:3): expect_snapshot_file() show differences for text files
Snapshot of `path1` has changed.
Differences:
    old                                       | new                                          
[1] ["h","i","d","a","e","g","c","j","b","f"] - ["f","i","a","j","g","b","d","e","c","h"] [1]
* Run `testthat::snapshot_accept('validate_remotes_order/')` to accept the change.
* Run `testthat::snapshot_review('validate_remotes_order/')` to review changes

─────────────────────────────────────────────

══ Results ════════════════════════════════════════════════════════
── Failed tests ─────────────────────────────────────
Failure (test-validate_remotes_order.R:13:3): expect_snapshot_file() show differences for text files
Snapshot of `path1` has changed.
Differences:
    old                                       | new                                          
[1] ["h","i","d","a","e","g","c","j","b","f"] - ["f","i","a","j","g","b","d","e","c","h"] [1]
* Run `testthat::snapshot_accept('validate_remotes_order/')` to accept the change.
* Run `testthat::snapshot_review('validate_remotes_order/')` to review changes


[ FAIL 1 | WARN 0 | SKIP 0 | PASS 1 ]
Error:
! Test failures.

Looks great! Thank you!!

@hadley hadley merged commit 71a33cd into main Aug 27, 2025
13 checks passed
@hadley hadley deleted the show-diff-snapshot-text-file branch August 27, 2025 20:35
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.

Display Snapshot file diff for plain text comparisons

3 participants