Skip to content

Commit

Permalink
Run in several locales, print nicer failure
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico authored Dec 18, 2023
1 parent ff3220f commit 331f31a
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions .dev/roxygen_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,50 @@ library(roxygen2)
old_dir <- file.path(tempdir(), "man")
if (dir.exists(old_dir)) unlink(old_dir, recursive = TRUE)
file.copy("man", tempdir(), recursive = TRUE)
old_files <- list.files(old_dir, pattern = "\\.Rd$")
new_dir <- "man"
.Last <- function() unlink(old_dir, recursive = TRUE)

roxygenize()

old_files <- list.files(old_dir, pattern = "\\.Rd$")
new_files <- list.files(new_dir, pattern = "\\.Rd$")

old_not_new <- setdiff(old_files, new_files)
if (length(old_not_new) > 0L) {
stop("Found saved .Rd files gone from a fresh run of roxygenize(): ", toString(old_not_new))
}

new_not_old <- setdiff(new_files, old_files)
if (length(new_not_old) > 0L) {
stop("Found new .Rd files from a fresh run of roxygenize(): ", toString(new_not_old))
}

# Rd2txt() prints to its out= argument, so we'd have to compare file contents;
# plain parse_Rd() keeps srcref info that encodes the file path, which as.character() strips.
normalize_rd <- function(rd_file) as.character(parse_Rd(rd_file))

rd_equal <- function(f1, f2) isTRUE(all.equal(normalize_rd(f1), normalize_rd(f2)))

for (file in new_files) {
if (rd_equal(file.path(old_dir, file), file.path(new_dir, file))) {
next
check_roxygenize_idempotent <- function() {
roxygenize()

new_files <- list.files(new_dir, pattern = "\\.Rd$")

old_not_new <- setdiff(old_files, new_files)
if (length(old_not_new) > 0L) {
stop("Found saved .Rd files gone from a fresh run of roxygenize(): ", toString(old_not_new))
}

new_not_old <- setdiff(new_files, old_files)
if (length(new_not_old) > 0L) {
stop("Found new .Rd files from a fresh run of roxygenize(): ", toString(new_not_old))
}
stop("roxygenize() output differs from saved output for ", file, ".")

for (file in new_files) {
old_file <- file.path(old_dir, file)
new_file <- file.path(new_dir, file)
if (rd_equal(old_file, new_file)) {
next
}
cat(sprintf("roxygenize() output differs from saved output for %s.\n", file))
cat("Here's the Rd2txt() output for what's saved:\n")
Rd2txt(old_file)
cat("Here's the Rd2txt() output for roxygenize():\n")
Rd2txt(new_file)
stop(call. = FALSE)
}
}

# Run the check in a few locales to ensure there's no idempotency issues w.r.t. sorting, too
for (LOCALE in c("C", "en_US", "hu_HU", "ja_JP")) {
Sys.setlocale("LC_COLLATE", LOCALE)
check_roxygenize_idempotent()
}

unlink(old_dir, recursive = TRUE)

0 comments on commit 331f31a

Please sign in to comment.