Skip to content
Snippets Groups Projects
Commit 8c4dc2ca authored by David Dorchies's avatar David Dorchies
Browse files

fix(list_reports): issues with non report folders

Refs #4
parent 5b2a4122
No related branches found
No related tags found
No related merge requests found
Pipeline #176634 failed
......@@ -16,11 +16,20 @@
list_reports <- function(reports_dir = file.path(pkgload::pkg_path(), "reports"),
reports = list.dirs(reports_dir, full.names = FALSE, recursive = FALSE),
index = "index.Rmd") {
if (length(reports) == 0) {
warning("No report found in ", reports_dir)
return(NULL)
}
paths <- setNames(file.path(reports_dir, reports), reports)
l <- lapply(paths, get_report_header)
if (all(sapply(l, is.null))) {
warning("No report found in ", reports_dir)
return(NULL)
}
df <- do.call(dplyr::bind_rows, l)
df <- cbind(report = reports, path = paths, df)
df <- cbind(report = reports[!sapply(l, is.null)],
path = paths[!sapply(l, is.null)],
df)
return(df)
}
......@@ -34,8 +43,14 @@ list_reports <- function(reports_dir = file.path(pkgload::pkg_path(), "reports")
#' @noRd
#'
get_report_header <- function(path, index = "index.Rmd") {
l <- read_rmd(file.path(path, index))
return(l$header)
f <- file.path(path, index)
if (file.exists(f)) {
l <- read_rmd(file.path(path, index))
return(l$header)
} else {
warning("File ", f, " not found, the folder ", basename(path), " is ignored")
return(NULL)
}
}
......
path <- tempfile(pattern = paste(sample(LETTERS, 8, TRUE), collapse = ""))
dir.create(path, recursive = TRUE)
create_reports(path)
test_that("list_reports return NULL and a warning when reports folder is empty", {
expect_warning(list_reports(reports_dir = file.path(path, "reports")))
expect_null(suppressWarnings(list_reports(reports_dir = file.path(path, "reports"))))
dir.create(file.path(path, "reports/not_a_report"))
expect_null(suppressWarnings(list_reports(reports_dir = file.path(path, "reports"))))
unlink(file.path(path, "reports/not_a_report"), recursive = TRUE)
})
add_report("test1", path = path)
add_report("test2", path = path)
test_that("list_reports return a correct data.frame", {
df <- list_reports(reports_dir = file.path(path, "reports"))
expect_equal(nrow(df), 2)
expect_true(all(sapply(
c("report", "path", "title", "author", "date", "site", "documentclass",
"bibliography", "biblio-style", "link-citations", "description",
"always_allow_html"),
function(x) {x %in% names(df)}
)))
})
test_that("list_reports send warning if a folder is not a report", {
dir.create(file.path(path, "reports/not_a_report"))
expect_warning(list_reports(reports_dir = file.path(path, "reports")))
df <- suppressWarnings(list_reports(reports_dir = file.path(path, "reports")))
expect_equal(nrow(df), 2)
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment