Skip to content

Commit e20f7e7

Browse files
committed
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.
1 parent bfb4209 commit e20f7e7

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

R/show-news.R

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,32 @@
77
#' @export
88
show_news <- function(pkg = ".", latest = TRUE, ...) {
99
pkg <- as.package(pkg)
10-
news_path <- path(pkg$path, "NEWS")
1110

12-
if (!file_exists(news_path)) {
11+
news_path <- path_abs(c(
12+
file.path(pkg$path, "inst", "NEWS.Rd"),
13+
file.path(pkg$path, "NEWS.md"),
14+
file.path(pkg$path, "NEWS"),
15+
file.path(pkg$path, "inst", "NEWS")
16+
))
17+
news_path <- news_path[file_exists(news_path)]
18+
19+
if (length(news_path) == 0) {
1320
cli::cli_abort("No NEWS found")
1421
}
1522

23+
news_db <- switch (path_ext(news_path),
24+
Rd = ("tools" %:::% ".build_news_db_from_package_NEWS_Rd")(news_path),
25+
md = ("tools" %:::% ".build_news_db_from_package_NEWS_md")(news_path),
26+
("tools" %:::% ".news_reader_default")(news_path)
27+
)
28+
29+
if (is.null(news_db)) {
30+
cli::cli_abort("Unable to parse NEWS")
31+
}
32+
1633
check_dots_used(action = getOption("devtools.ellipsis_action", rlang::warn))
1734

18-
out <- utils::news(..., db = ("tools" %:::% ".news_reader_default")(news_path))
35+
out <- utils::news(..., db = news_db)
1936
if (latest) {
2037
ver <- numeric_version(out$Version)
2138
recent <- ver == max(ver)

tests/testthat/test-show-news.R

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
test_that("can read NEWS.md in root directory", {
2+
skip_on_cran()
3+
4+
pkg <- local_package_create()
5+
write(
6+
"# test 0.0.1\n\n* Initial CRAN submission.\n",
7+
path(pkg, "NEWS.md")
8+
)
9+
10+
expect_no_error(show_news(pkg))
11+
})
12+
13+
test_that("can read NEWS.Rd in inst directory", {
14+
skip_on_cran()
15+
16+
pkg <- local_package_create()
17+
18+
dir_create(pkg, "inst")
19+
write(
20+
"\\name{NEWS}
21+
\\title{News for Package 'test'}
22+
23+
\\section{CHANGES IN test VERSION 0.0.1}{
24+
\\itemize{
25+
\\item First version
26+
}
27+
}",
28+
path(pkg, "inst", "NEWS.Rd")
29+
)
30+
31+
expect_no_error(show_news(pkg))
32+
})
33+
34+
test_that("can read NEWS in inst directory", {
35+
skip_on_cran()
36+
37+
pkg <- local_package_create()
38+
39+
dir_create(pkg, "inst")
40+
write("v0.1-1 (2024-01-09)\n\n o first release", path(pkg, "inst", "NEWS"))
41+
42+
expect_no_error(show_news(pkg))
43+
})
44+
45+
test_that("fails when NEWS is missing", {
46+
skip_on_cran()
47+
48+
pkg <- local_package_create()
49+
50+
expect_error(show_news(pkg))
51+
})
52+
53+
test_that("fails when NEWS is improperly formatted", {
54+
skip_on_cran()
55+
56+
pkg <- local_package_create()
57+
suppressMessages(usethis::with_project(pkg, use_news_md()))
58+
59+
dir_create(pkg, "inst")
60+
file_create(pkg, "inst", "NEWS.Rd")
61+
62+
expect_error(show_news(pkg))
63+
})

0 commit comments

Comments
 (0)