Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .claude/settings.local.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"$schema": "https://json.schemastore.org/claude-code-settings.json",
"permissions": {
"allow": [
"Bash(find:*)",
"Bash(R:*)"
"Bash(R:*)",
"Bash(rm:*)",
"Bash(air format:*)"
],
"deny": []
},
"$schema": "https://json.schemastore.org/claude-code-settings.json"
}
}
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# testthat (development version)

* 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.
* New `expect_r6_class()` (#2030).
* `expect_*()` functions consistently and rigorously check their inputs (#1754).
* `JunitReporter()` no longer fails with `"no applicable method for xml_add_child"` for warnings outside of tests (#1913). Additionally, warnings now save their backtraces.
Expand Down
8 changes: 6 additions & 2 deletions R/snapshot-serialize.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ snap_from_md <- function(lines) {
sep <- grepl("^-{3,}", lines)
case_group <- cumsum(sep)

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

cases <- unname(split(lines[-drop], case_group[-drop]))
code_unblock <- function(x) paste0(indent_del(x), collapse = "\n")
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test-snapshot-serialize.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,22 @@ test_that("snapshots always use \n", {
has_cr <- grepl("\r", snap, fixed = TRUE)
expect_equal(has_cr, FALSE)
})

test_that("snap_from_md handles missing final newlines", {
one_newline <- withr::local_tempfile(
fileext = ".md",
lines = c(
"# test_case",
"",
"result1",
"",
"---",
"",
"result2"
)
)
expect_equal(
read_snaps(one_newline),
list(test_case = c("result1", "result2"))
)
})
Loading