Skip to content

Commit

Permalink
tolerate empty lines at start of news file
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey committed Apr 11, 2024
1 parent 9e64daf commit 89e09a6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
usethis no longer uses the `ui_*()` functions internally, in favor of new
cli-based helpers that are not exported.

* `usethis::use_version()` now tolerates empty / blank lines preceding the
first section title in the package NEWS file. (#1976)

# usethis 2.2.3

* Patch release with changes to `.Rd` files requested by CRAN.
Expand Down
18 changes: 14 additions & 4 deletions R/news.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,25 @@ use_news_heading <- function(version) {
}

news <- read_utf8(news_path)
title <- glue("# {project_name()} {version}")
if (length(news) == 0) {
return(news)
}

if (title == news[[1]]) {
# find first non-blank line in news
for (idx in seq_along(news)) {
if (grepl("[^[:space:]]", news[[idx]])) {
break
}
}

title <- glue("# {project_name()} {version}")
if (title == news[[idx]]) {
return(invisible())
}

development_title <- glue("# {project_name()} (development version)")
if (development_title == news[[1]]) {
news[[1]] <- title
if (development_title == news[[idx]]) {
news[[idx]] <- title

ui_bullets(c("v" = "Replacing development heading in {.path NEWS.md}."))
return(write_utf8(news_path, news))
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-news.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,16 @@ test_that("use_news_md() sets version number when 'production version'", {
expect_snapshot(writeLines(read_utf8(proj_path("NEWS.md"))),
transform = scrub_testpkg)
})

test_that("use_news_heading() tolerates blank lines at start", {
create_local_package()

header <- sprintf("# %s (development version)", project_name())
writeLines(c("", header, "", "* Fixed the bugs."), con = "NEWS.md")

use_news_heading(version = "1.0.0")
contents <- read_utf8("NEWS.md")

expected <- sprintf("# %s 1.0.0", project_name())
expect_equal(contents[[2L]], expected)
})

0 comments on commit 89e09a6

Please sign in to comment.