From ccfd542bcc1ac0b6ccdb0b2d024bf94edc264e62 Mon Sep 17 00:00:00 2001 From: chainsawriot Date: Fri, 24 Feb 2023 10:03:04 +0100 Subject: [PATCH] Fix #75 --- R/installation.R | 34 +++++++++++++++++++------------ tests/testthat/test_dockerize.R | 7 +++++++ tests/testthat/test_expost_rang.R | 13 +++++++++--- 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/R/installation.R b/R/installation.R index 5512fc6..de8289c 100644 --- a/R/installation.R +++ b/R/installation.R @@ -131,9 +131,9 @@ } else { lib_as_character <- paste0("\"", lib, "\"") } - if(!is.null(rang$bioc_version)){ + if(!is.null(rang$bioc_version)) { bioc_txt <- paste0(", bioc_mirror = \"", bioc_mirror,rang$bioc_version,"/","\"") - } else{ + } else { bioc_txt <- NULL } writeLines(paste0("## rang::export_rang(rang = rang, path = \"", path, "\", verbose = ", @@ -226,7 +226,7 @@ source = source, uid = uid, cache_dir = cache_dir, verbose = verbose) } - if(source == "bioc"){ + if(source == "bioc") { .cache_pkg_bioc(x = x, version = version, cache_dir = cache_dir, bioc_mirror = bioc_mirror,bioc_version = rang$bioc_version, verbose = verbose) } @@ -268,16 +268,16 @@ return(dockerfile_content) } -.generate_docker_readme <- function(output_dir,image){ - file.create(file.path(output_dir,"README")) - con <- file(file.path(output_dir,"README"), open="w") - readme <- readLines(system.file("readme_template.txt", package = "rang")) - readme <- gsub("__DATE__",Sys.Date(),readme) - readme <- gsub("__OUTPUT__",output_dir,readme) - readme <- gsub("__IMAGE__",image,readme) - writeLines(readme,file.path(output_dir,"README")) - close(con) - invisible(readme) +.generate_docker_readme <- function(output_dir,image) { + file.create(file.path(output_dir,"README")) + con <- file(file.path(output_dir,"README"), open="w") + readme <- readLines(system.file("readme_template.txt", package = "rang")) + readme <- gsub("__DATE__",Sys.Date(),readme) + readme <- gsub("__OUTPUT__",output_dir,readme) + readme <- gsub("__IMAGE__",image,readme) + writeLines(readme,file.path(output_dir,"README")) + close(con) + invisible(readme) } #' Export The Resolved Result As Installation Script @@ -314,6 +314,10 @@ export_rang <- function(rang, path, rang_as_comment = TRUE, verbose = TRUE, lib if (utils::compareVersion(rang$r_version, "2.1") == -1) { stop("`export_rang` doesn't support this R version (yet).") } + if (length(rang$ranglets) == 0) { + warning("Nothing to export.") + return(invisible(NULL)) + } cran_mirror <- .normalize_url(cran_mirror) if (isTRUE(check_cran_mirror)) { ## probably need to stop this also if #17 is implemented if (isFALSE(.query_mirror_validity(cran_mirror))) { @@ -381,6 +385,10 @@ dockerize <- function(rang, output_dir, materials_dir = NULL, image = c("r-ver", rang_as_comment = TRUE, cache = FALSE, verbose = TRUE, lib = NA, cran_mirror = "https://cran.r-project.org/", check_cran_mirror = TRUE, bioc_mirror = "https://bioconductor.org/packages/") { + if (length(rang$ranglets) == 0) { + warning("Nothing to dockerize.") + return(invisible(NULL)) + } if (missing(output_dir)) { stop("You must provide `output_dir`.", call. = FALSE) } diff --git a/tests/testthat/test_dockerize.R b/tests/testthat/test_dockerize.R index f2e047e..e9361cc 100644 --- a/tests/testthat/test_dockerize.R +++ b/tests/testthat/test_dockerize.R @@ -7,6 +7,13 @@ test_that("defensive programming", { expect_error(dockerize(graph, output_dir = tempdir())) }) +test_that("empty rang dockerize #75", { + graph <- readRDS("../testdata/rang_ok.RDS") + graph$ranglets <- list() + expect_warning(x <- dockerize(graph, output_dir = .generate_temp_dir())) + expect_equal(x, NULL) +}) + test_that("integration of #13 in dockerize()", { rang_ok <- readRDS("../testdata/rang_ok.RDS") temp_dir <- .generate_temp_dir() diff --git a/tests/testthat/test_expost_rang.R b/tests/testthat/test_expost_rang.R index 0160e5c..0f446b6 100644 --- a/tests/testthat/test_expost_rang.R +++ b/tests/testthat/test_expost_rang.R @@ -115,9 +115,16 @@ test_that("issue #38", { }) test_that("Bioconductor <2.0",{ - expect_error(.bioc_package_history(bioc_version = "1.9")) + expect_error(.bioc_package_history(bioc_version = "1.9")) }) test_that("Bioconductor new release",{ - expect_equal(.query_biocver("2023-01-01")$version,"3.16") -}) \ No newline at end of file + expect_equal(.query_biocver("2023-01-01")$version,"3.16") +}) + +test_that("empty rang export, #75", { + graph <- readRDS("../testdata/rang_ok.RDS") + graph$ranglets <- list() + expect_warning(x <- export_rang(graph, path = tempfile())) + expect_equal(x, NULL) +})