From e20f7e77d27051e285982dbd077ea87930a887ee Mon Sep 17 00:00:00 2001 From: Carl Suster Date: Tue, 9 Jan 2024 17:03:16 +1100 Subject: [PATCH] Support NEWS in Rd and md formats and under inst/ Mirrors the search path documented in utils::news() for NEWS files. Uses the internal machinery of tools to build news databases. --- R/show-news.R | 23 ++++++++++-- tests/testthat/test-show-news.R | 63 +++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 tests/testthat/test-show-news.R diff --git a/R/show-news.R b/R/show-news.R index 2349bec0b..03630ed68 100644 --- a/R/show-news.R +++ b/R/show-news.R @@ -7,15 +7,32 @@ #' @export show_news <- function(pkg = ".", latest = TRUE, ...) { pkg <- as.package(pkg) - news_path <- path(pkg$path, "NEWS") - if (!file_exists(news_path)) { + news_path <- path_abs(c( + file.path(pkg$path, "inst", "NEWS.Rd"), + file.path(pkg$path, "NEWS.md"), + file.path(pkg$path, "NEWS"), + file.path(pkg$path, "inst", "NEWS") + )) + news_path <- news_path[file_exists(news_path)] + + if (length(news_path) == 0) { cli::cli_abort("No NEWS found") } + news_db <- switch (path_ext(news_path), + Rd = ("tools" %:::% ".build_news_db_from_package_NEWS_Rd")(news_path), + md = ("tools" %:::% ".build_news_db_from_package_NEWS_md")(news_path), + ("tools" %:::% ".news_reader_default")(news_path) + ) + + if (is.null(news_db)) { + cli::cli_abort("Unable to parse NEWS") + } + check_dots_used(action = getOption("devtools.ellipsis_action", rlang::warn)) - out <- utils::news(..., db = ("tools" %:::% ".news_reader_default")(news_path)) + out <- utils::news(..., db = news_db) if (latest) { ver <- numeric_version(out$Version) recent <- ver == max(ver) diff --git a/tests/testthat/test-show-news.R b/tests/testthat/test-show-news.R new file mode 100644 index 000000000..344502f1d --- /dev/null +++ b/tests/testthat/test-show-news.R @@ -0,0 +1,63 @@ +test_that("can read NEWS.md in root directory", { + skip_on_cran() + + pkg <- local_package_create() + write( + "# test 0.0.1\n\n* Initial CRAN submission.\n", + path(pkg, "NEWS.md") + ) + + expect_no_error(show_news(pkg)) +}) + +test_that("can read NEWS.Rd in inst directory", { + skip_on_cran() + + pkg <- local_package_create() + + dir_create(pkg, "inst") + write( + "\\name{NEWS} +\\title{News for Package 'test'} + +\\section{CHANGES IN test VERSION 0.0.1}{ + \\itemize{ + \\item First version + } +}", + path(pkg, "inst", "NEWS.Rd") + ) + + expect_no_error(show_news(pkg)) +}) + +test_that("can read NEWS in inst directory", { + skip_on_cran() + + pkg <- local_package_create() + + dir_create(pkg, "inst") + write("v0.1-1 (2024-01-09)\n\n o first release", path(pkg, "inst", "NEWS")) + + expect_no_error(show_news(pkg)) +}) + +test_that("fails when NEWS is missing", { + skip_on_cran() + + pkg <- local_package_create() + + expect_error(show_news(pkg)) +}) + +test_that("fails when NEWS is improperly formatted", { + skip_on_cran() + + pkg <- local_package_create() + suppressMessages(usethis::with_project(pkg, use_news_md())) + + dir_create(pkg, "inst") + file_create(pkg, "inst", "NEWS.Rd") + + expect_error(show_news(pkg)) +})