From 8c4dc2ca21a72413a5010b79d5d1c89121ed5fd6 Mon Sep 17 00:00:00 2001 From: David Dorchies <david.dorchies@inrae.fr> Date: Mon, 4 Mar 2024 17:21:34 +0100 Subject: [PATCH] fix(list_reports): issues with non report folders Refs #4 --- R/list_reports.R | 23 +++++++++++++++++---- tests/testthat/test-list_reports.R | 32 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 tests/testthat/test-list_reports.R diff --git a/R/list_reports.R b/R/list_reports.R index 53f9393..ad8ded2 100644 --- a/R/list_reports.R +++ b/R/list_reports.R @@ -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) + } } diff --git a/tests/testthat/test-list_reports.R b/tests/testthat/test-list_reports.R new file mode 100644 index 0000000..4f7a324 --- /dev/null +++ b/tests/testthat/test-list_reports.R @@ -0,0 +1,32 @@ +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) +}) -- GitLab