Skip to content

Commit

Permalink
feat: Keep empty lines when merging (#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr authored Nov 24, 2024
1 parent 1714265 commit 8955580
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 18 deletions.
21 changes: 15 additions & 6 deletions R/update-news.R
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,21 @@ regroup_news <- function(list) {
}

merge_news_group <- function(name, groups) {
this_group <- do.call(
c,
purrr::map(groups[names(groups) == name], unlist, recursive = FALSE)
)
this_group <- unique(this_group[this_group != ""])
unname(this_group)
out <- purrr::reduce(groups[names(groups) == name], ~ c(.x, "", .y))

# Remove duplicates and their associated empty lines
dupes <- duplicated(out) & (out != "")
after_dupes <- c(FALSE, dupes[-length(dupes)])
after_dupes_empty <- after_dupes & (out == "")
keep <- !(dupes | after_dupes_empty)
out <- out[keep]

# Edge case: Duplicate removal leads to empty last line
if (out[[length(out)]] == "") {
out <- out[-length(out)]
}

out
}

# Grouping -------
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/_snaps/auto/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# tea 2.0.0

- Add cool blop.

- Add cool bla.

- Added a `NEWS.md` file to track changes to the package.

10 changes: 10 additions & 0 deletions tests/testthat/_snaps/demo/demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ cat(news, sep = "\n")
## # tea 0.0.0.9001
##
## - Add tests for cup of tea.
##
## - New cup_of_tea() function makes it easy to drink a cup of tee.
##
## - Added a `NEWS.md` file to track changes to the package.
```

Expand All @@ -334,7 +336,9 @@ cat(news, sep = "\n")
## # tea 0.0.0.9001
##
## - Add tests for cup of tea.
##
## - New cup_of_tea() function makes it easy to drink a cup of tea.
##
## - Added a `NEWS.md` file to track changes to the package.
writeLines(news, "NEWS.md")
```
Expand Down Expand Up @@ -386,7 +390,9 @@ cat(news, sep = "\n")
## # tea 0.0.0.9001
##
## - Add tests for cup of tea.
##
## - New cup_of_tea() function makes it easy to drink a cup of tea.
##
## - Added a `NEWS.md` file to track changes to the package.
```

Expand Down Expand Up @@ -488,7 +494,9 @@ writeLines(news)
## # tea 0.0.0.9001
##
## - Add tests for cup of tea.
##
## - New cup_of_tea() function makes it easy to drink a cup of tea.
##
## - Added a `NEWS.md` file to track changes to the package.
fledge::finalize_version(push = TRUE)
## > Resetting to previous commit.
Expand Down Expand Up @@ -562,7 +570,9 @@ cat(news, sep = "\n")
## # tea 0.0.0.9001
##
## - Add tests for cup of tea.
##
## - New cup_of_tea() function makes it easy to drink a cup of tea.
##
## - Added a `NEWS.md` file to track changes to the package.
```

Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/_snaps/update-news.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,21 @@
}
]

# regroup_news() works

Code
regroup_news(combined)
Output
$`Custom type`
[1] "cool right"
$Features
[1] "- feat1" "" "- feat2"
$Documentation
[1] "- stuff" "" "- other" "" "- again"
$Uncategorized
[1] "- blop" "" "- etc" "" "- pof" "" "- ok"

1 change: 1 addition & 0 deletions tests/testthat/_snaps/update-news/samedev-updated.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
## Features

- Neat helper.

- New stuff.


Expand Down
20 changes: 8 additions & 12 deletions tests/testthat/test-update-news.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,22 @@ test_that("normalize_news() works", {

test_that("regroup_news() works", {
news_list1 <- list(
Uncategorized = c("- blop", "- etc"),
Documentation = c("- stuff", "- other")
Uncategorized = c("- blop", "", "- etc"),
Documentation = c("- stuff", "", "- other")
)

news_list2 <- list(
Features = c("- feat1", "- feat2"),
Features = c("- feat1", "", "- feat2"),
`Custom type` = "cool right",
Uncategorized = c("- pof", "- ok"),
Uncategorized = c("- pof", "", "- ok"),
Documentation = "- again"
)

combined <- c(news_list1, news_list2)
regrouped <- regroup_news(combined)

expect_equal(
names(regrouped),
c("Custom type", "Features", "Documentation", "Uncategorized")
)
expect_length(regrouped[["Documentation"]], 3)
expect_length(regrouped[["Uncategorized"]], 4)
regrouped <-
expect_snapshot({
regroup_news(combined)
})
})

test_that("Can update dev version news item", {
Expand Down

0 comments on commit 8955580

Please sign in to comment.