From 89e09a6269699ac954c8ec44c787cbb5d62dfa35 Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Wed, 10 Apr 2024 22:33:59 -0700 Subject: [PATCH 1/2] tolerate empty lines at start of news file --- NEWS.md | 3 +++ R/news.R | 18 ++++++++++++++---- tests/testthat/test-news.R | 13 +++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) 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..a82d1006d 100644 --- a/R/news.R +++ b/R/news.R @@ -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)) 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) +}) From 9426de123336d14d49e601c77ff42272ff030709 Mon Sep 17 00:00:00 2001 From: Jenny Bryan Date: Wed, 26 Jun 2024 16:31:47 -0700 Subject: [PATCH 2/2] Tweak search for first non-whitespace line --- R/news.R | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/R/news.R b/R/news.R index a82d1006d..706c63e9c 100644 --- a/R/news.R +++ b/R/news.R @@ -40,15 +40,10 @@ use_news_heading <- function(version) { } news <- read_utf8(news_path) - if (length(news) == 0) { - return(news) - } + idx <- match(TRUE, grepl("[^[:space:]]", news)) - # find first non-blank line in news - for (idx in seq_along(news)) { - if (grepl("[^[:space:]]", news[[idx]])) { - break - } + if (is.na(idx)) { + return(news) } title <- glue("# {project_name()} {version}")