Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
})