diff --git a/NEWS.md b/NEWS.md index d7f3a70a0..9c652ddca 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. diff --git a/R/news.R b/R/news.R index 548377c4a..706c63e9c 100644 --- a/R/news.R +++ b/R/news.R @@ -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)) diff --git a/tests/testthat/test-news.R b/tests/testthat/test-news.R index 73f1f73f0..469d70ff2 100644 --- a/tests/testthat/test-news.R +++ b/tests/testthat/test-news.R @@ -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) +})