Skip to content

Commit

Permalink
tolerate empty lines at start of news file (#1977)
Browse files Browse the repository at this point in the history
* tolerate empty lines at start of news file

* Tweak search for first non-whitespace line

---------

Co-authored-by: Jenny Bryan <[email protected]>
  • Loading branch information
kevinushey and jennybc authored Jun 26, 2024
1 parent 90dc393 commit 1398d7b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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
13 changes: 9 additions & 4 deletions R/news.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,20 @@ use_news_heading <- function(version) {
}

news <- read_utf8(news_path)
title <- glue("# {project_name()} {version}")
idx <- match(TRUE, grepl("[^[:space:]]", news))

if (is.na(idx)) {
return(news)
}

if (title == news[[1]]) {
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 1398d7b

Please sign in to comment.