Skip to content

Commit

Permalink
Support NEWS in Rd and md formats and under inst/
Browse files Browse the repository at this point in the history
Mirrors the search path documented in utils::news() for NEWS files. Uses
the internal machinery of tools to build news databases.
  • Loading branch information
arcresu committed Jan 9, 2024
1 parent bfb4209 commit e20f7e7
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
23 changes: 20 additions & 3 deletions R/show-news.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
63 changes: 63 additions & 0 deletions tests/testthat/test-show-news.R
Original file line number Diff line number Diff line change
@@ -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))
})

0 comments on commit e20f7e7

Please sign in to comment.