Skip to content

Commit c52c7df

Browse files
authored
✨ Don't lose last snapshot if last nl is missing ✨ (#2164)
Because positron (and other editors) have an edit of trimming additional trailing newlines when saving. It probably would have been better to not write this in the first place, but I think we don't want to change that now as it will cause diffs to all snapshots, and only protects against a problem that happens rarely. Fixes #2092
1 parent 7b71880 commit c52c7df

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

.claude/settings.local.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
2+
"$schema": "https://json.schemastore.org/claude-code-settings.json",
23
"permissions": {
34
"allow": [
45
"Bash(find:*)",
5-
"Bash(R:*)"
6+
"Bash(R:*)",
7+
"Bash(rm:*)",
8+
"Bash(air format:*)"
69
],
710
"deny": []
8-
},
9-
"$schema": "https://json.schemastore.org/claude-code-settings.json"
11+
}
1012
}

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# testthat (development version)
22

3+
* The last snapshot is no longer lost if the snapshot file is missing the final newline (#2092). It's easy to accidentally remove this because there are two trailing new lines in snapshot files and many editors will automatically remove if you touch the file.
34
* New `expect_r6_class()` (#2030).
45
* `expect_*()` functions consistently and rigorously check their inputs (#1754).
56
* `JunitReporter()` no longer fails with `"no applicable method for xml_add_child"` for warnings outside of tests (#1913). Additionally, warnings now save their backtraces.

R/snapshot-serialize.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ snap_from_md <- function(lines) {
1919
sep <- grepl("^-{3,}", lines)
2020
case_group <- cumsum(sep)
2121

22-
# Remove first line and last line, separator, line above and line below
22+
# Remove first line, separator, line above and line below
23+
# Only remove last line if it's empty (to handle missing final newlines)
2324
sep_loc <- which(sep)
24-
drop <- c(1, sep_loc, sep_loc + 1, sep_loc - 1, length(lines))
25+
drop <- c(1, sep_loc, sep_loc + 1, sep_loc - 1)
26+
if (length(lines) > 0 && lines[length(lines)] == "") {
27+
drop <- c(drop, length(lines))
28+
}
2529

2630
cases <- unname(split(lines[-drop], case_group[-drop]))
2731
code_unblock <- function(x) paste0(indent_del(x), collapse = "\n")

tests/testthat/test-snapshot-serialize.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,22 @@ test_that("snapshots always use \n", {
2525
has_cr <- grepl("\r", snap, fixed = TRUE)
2626
expect_equal(has_cr, FALSE)
2727
})
28+
29+
test_that("snap_from_md handles missing final newlines", {
30+
one_newline <- withr::local_tempfile(
31+
fileext = ".md",
32+
lines = c(
33+
"# test_case",
34+
"",
35+
"result1",
36+
"",
37+
"---",
38+
"",
39+
"result2"
40+
)
41+
)
42+
expect_equal(
43+
read_snaps(one_newline),
44+
list(test_case = c("result1", "result2"))
45+
)
46+
})

0 commit comments

Comments
 (0)