Skip to content

Commit 47c988c

Browse files
arcresuhadley
andauthored
show_news(): Support NEWS in Rd and md formats and under inst (#2551)
Mirrors the search path documented in utils::news() for NEWS files. Uses the internal machinery of tools to build news databases. --------- Co-authored-by: Hadley Wickham <h.wickham@gmail.com>
1 parent 8eecd80 commit 47c988c

4 files changed

Lines changed: 79 additions & 8 deletions

File tree

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* New `check_mac_devel()` function to check a package using the macOS builder at https://mac.r-project.org/macbuilder/submit.html (@nfrerebeau, #2507)
55
* `is_loading()` is now re-exported from pkgload (#2556).
66
* `load_all()` now errors if called recursively, i.e. if you accidentally include a `load_all()` call in one of your R source files (#2617).
7+
* `show_news()` now looks for NEWS files in the same locations as `utils::news()`: `inst/NEWS.Rd`, `NEWS.md`, `NEWS`, and `inst/NEWS` (@arcresu, #2499).
78

89
# devtools 2.4.6
910

R/show-news.R

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,25 @@
77
#' @export
88
show_news <- function(pkg = ".", latest = TRUE, ...) {
99
pkg <- as.package(pkg)
10-
news_path <- path(pkg$path, "NEWS")
10+
news_path <- find_news(pkg$path)
1111

12-
if (!file_exists(news_path)) {
13-
cli::cli_abort("No NEWS found")
14-
}
12+
withCallingHandlers(
13+
{
14+
news_db <- switch(
15+
path_ext(news_path),
16+
Rd = ("tools" %:::% ".build_news_db_from_package_NEWS_Rd")(news_path),
17+
md = ("tools" %:::% ".build_news_db_from_package_NEWS_md")(news_path),
18+
("tools" %:::% ".news_reader_default")(news_path)
19+
)
20+
},
21+
error = function(e) {
22+
cli::cli_abort("Failed to parse NEWS file.", parent = e)
23+
}
24+
)
1525

1626
check_dots_used(action = getOption("devtools.ellipsis_action", rlang::warn))
1727

18-
out <- utils::news(
19-
...,
20-
db = ("tools" %:::% ".news_reader_default")(news_path)
21-
)
28+
out <- utils::news(..., db = news_db)
2229
if (latest) {
2330
ver <- numeric_version(out$Version)
2431
recent <- ver == max(ver)
@@ -27,3 +34,19 @@ show_news <- function(pkg = ".", latest = TRUE, ...) {
2734
out
2835
}
2936
}
37+
38+
find_news <- function(path) {
39+
news_paths <- c(
40+
file.path(path, "inst", "NEWS.Rd"),
41+
file.path(path, "NEWS.md"),
42+
file.path(path, "NEWS"),
43+
file.path(path, "inst", "NEWS")
44+
)
45+
news_path <- news_paths[file_exists(news_paths)]
46+
47+
if (length(news_path) == 0) {
48+
cli::cli_abort("Failed to find NEWS file.")
49+
}
50+
51+
path_abs(news_path[[1]])
52+
}

tests/testthat/_snaps/show-news.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# fails when NEWS is missing or improperly formatted
2+
3+
Code
4+
show_news(pkg)
5+
Condition
6+
Error in `find_news()`:
7+
! Failed to find NEWS file.
8+
9+
---
10+
11+
Code
12+
show_news(pkg)
13+
Condition
14+
Error in `show_news()`:
15+
! Failed to parse NEWS file.
16+
Caused by error:
17+
! NEWS.Rd: Sections \title, and \name must exist and be unique in Rd files
18+

tests/testthat/test-show-news.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
test_that("find_news() finds NEWS in all expected locations", {
2+
pkg <- local_package_create()
3+
4+
dir_create(pkg, "inst")
5+
file_create(pkg, "inst", "NEWS")
6+
expect_equal(path_rel(find_news(pkg), pkg), path("inst/NEWS"))
7+
8+
file_create(pkg, "NEWS")
9+
expect_equal(path_rel(find_news(pkg), pkg), path("NEWS"))
10+
11+
file_create(pkg, "NEWS.md")
12+
expect_equal(path_rel(find_news(pkg), pkg), path("NEWS.md"))
13+
14+
file_create(pkg, "inst", "NEWS.Rd")
15+
expect_equal(path_rel(find_news(pkg), pkg), path("inst/NEWS.Rd"))
16+
})
17+
18+
test_that("fails when NEWS is missing or improperly formatted", {
19+
skip_on_cran()
20+
skip_unless_r(">= 4.2.0") # different error message
21+
22+
pkg <- local_package_create()
23+
expect_snapshot(show_news(pkg), error = TRUE)
24+
25+
dir_create(pkg, "inst")
26+
file_create(pkg, "inst", "NEWS.Rd")
27+
28+
expect_snapshot(show_news(pkg), error = TRUE)
29+
})

0 commit comments

Comments
 (0)