From 3ef3a5050f0bca1f0ede25a4fd40d53c6814b56b Mon Sep 17 00:00:00 2001 From: Andrie de Vries Date: Tue, 26 Dec 2017 20:07:47 +0000 Subject: [PATCH] Use mocking to speed up tests #107 --- DESCRIPTION | 5 +- NAMESPACE | 2 + R/addPackages.R | 17 +- R/github-functions.R | 7 +- R/{helpers.R => internal.R} | 0 R/makeRepo.R | 20 +- R/mock-makeRepo.R | 53 +++ R/pkgDep.R | 4 +- R/pkgDepTools.R | 18 +- R/updatePackages.R | 4 +- inst/doc/miniCRAN-dependency-graph.R | 40 ++ inst/doc/miniCRAN-dependency-graph.html | 4 +- inst/doc/miniCRAN-introduction.R | 160 ++++++++ inst/doc/miniCRAN-introduction.html | 4 +- inst/doc/miniCRAN-non-CRAN-repos.R | 78 ++++ inst/doc/miniCRAN-non-CRAN-repos.html | 4 +- man/repoBinPath.Rd | 2 +- man/repoPrefix.Rd | 2 +- man/twodigitRversion.Rd | 2 +- tests/test-1-pkgAvail.R | 4 - tests/test-3&4-makeRepo.R | 4 - tests/test-5-updateRepo.R | 4 - tests/test-6-getCranDescription.R | 4 - tests/test-7-add-GithubPackage.R | 4 - tests/{test-2-pkgDep.R => test-all.R} | 2 +- tests/testthat/helpers.R | 14 +- tests/testthat/test-3-makeRepo.R | 33 +- .../testthat/test-4-makeRepo-from-localCRAN.R | 29 +- tests/testthat/test-5-updateRepo.R | 370 ++++++++++-------- 29 files changed, 615 insertions(+), 279 deletions(-) rename R/{helpers.R => internal.R} (100%) create mode 100644 R/mock-makeRepo.R delete mode 100644 tests/test-1-pkgAvail.R delete mode 100644 tests/test-3&4-makeRepo.R delete mode 100644 tests/test-5-updateRepo.R delete mode 100644 tests/test-6-getCranDescription.R delete mode 100644 tests/test-7-add-GithubPackage.R rename tests/{test-2-pkgDep.R => test-all.R} (62%) diff --git a/DESCRIPTION b/DESCRIPTION index 373c650..41e8e92 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,7 +12,7 @@ Description: Makes it possible to create an internally consistent subset. The user can then install packages from this repository directly, rather than from CRAN. This is useful in production settings, e.g. server behind a firewall, or remote locations with slow (or zero) Internet access. -Date: 2017-12-19 +Date: 2017-12-26 Version: 0.2.11.9000 URL: https://github.com/andrie/miniCRAN BugReports: https://github.com/andrie/miniCRAN/issues @@ -26,7 +26,8 @@ Imports: tools, utils, XML, - igraph + igraph, + mockery Suggests: devtools, knitr, diff --git a/NAMESPACE b/NAMESPACE index 9ec8946..2267080 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -32,9 +32,11 @@ importFrom(methods,is) importFrom(stats,reshape) importFrom(stats,setNames) importFrom(tools,write_PACKAGES) +importFrom(utils,available.packages) importFrom(utils,chooseCRANmirror) importFrom(utils,compareVersion) importFrom(utils,download.file) +importFrom(utils,download.packages) importFrom(utils,flush.console) importFrom(utils,installed.packages) importFrom(utils,select.list) diff --git a/R/addPackages.R b/R/addPackages.R index 7e0b9f1..9965f92 100644 --- a/R/addPackages.R +++ b/R/addPackages.R @@ -186,13 +186,7 @@ addOldPackage <- function(pkgs = NULL, path = NULL, vers = NULL, #' .listFiles <- function(pkgs, path, type) { stopifnot(dir.exists(path)) - pattern <- switch(type, - mac.binary = ".tgz", - mac.binary.leopard = ".tgz", - mac.binary.mavericks = ".tgz", - source = ".tar.gz", - win.binary = ".zip", - stop("Type ", type, "not recognised.")) + pattern <- pkgFileExt(type) # get a list of all files in pkgPaths directory matching pattern f <- list.files(path, pattern = pattern) @@ -223,11 +217,10 @@ addOldPackage <- function(pkgs = NULL, path = NULL, vers = NULL, id.v <- which(fv == max(fv[ids.p])) f[id.v] - }) %>% unique() - - return(fout) + }) + unique(fout) } else { - return(character()) + character() } } @@ -328,5 +321,5 @@ addLocalPackage <- function(pkgs = NULL, pkgPath = NULL, path = NULL, # write package index for each folder: index <- updateRepoIndex(path = path, type = type, Rversion = Rversion) - return(invisible(index)) + invisible(index) } diff --git a/R/github-functions.R b/R/github-functions.R index 29b1c48..3184874 100644 --- a/R/github-functions.R +++ b/R/github-functions.R @@ -11,19 +11,18 @@ pkgFileExt <- function(type) { } - getPkgVersFromFile <- function(file) { file <- grep("\\.(tar\\.gz|zip|tgz)$", basename(as.character(file)), value = TRUE) if (length(file)) { file <- sapply(strsplit(file, "\\.tar\\.gz"), "[[", 1) file <- sapply(strsplit(file, "\\.zip"), "[[", 1) file <- sapply(strsplit(file, "\\.tgz"), "[[", 1) - pkg <- sapply(strsplit(file, "_"), "[[", 1) + pkg <- sapply(strsplit(file, "_"), "[[", 1) vers <- sapply(strsplit(file, "_"), "[[", 2) df <- data.frame(package = pkg, version = vers, stringsAsFactors = FALSE) - return(df[order(df$package),]) + df[order(df$package),] } else { - return(NULL) + NULL } } diff --git a/R/helpers.R b/R/internal.R similarity index 100% rename from R/helpers.R rename to R/internal.R diff --git a/R/makeRepo.R b/R/makeRepo.R index da6db0f..234ee0a 100644 --- a/R/makeRepo.R +++ b/R/makeRepo.R @@ -51,6 +51,8 @@ #' #' @export #' @family update repo functions +#' +#' @importFrom utils download.packages #' #' @example /inst/examples/example_makeRepo.R makeRepo <- function(pkgs, path, repos = getOption("repos"), type = "source", @@ -59,10 +61,10 @@ makeRepo <- function(pkgs, path, repos = getOption("repos"), type = "source", downloaded <- lapply(type, function(t) { pkgPath <- repoBinPath(path = path, type = t, Rversion = Rversion) - if(!file.exists(pkgPath)) { + if (!file.exists(pkgPath)) { result <- dir.create(pkgPath, recursive = TRUE, showWarnings = FALSE) - if(result) { - if(!quiet) message("Created new folder: ", pkgPath) + if (result) { + if (!quiet) message("Created new folder: ", pkgPath) } else { stop("Unable to create repo path: ", pkgPath) } @@ -71,7 +73,7 @@ makeRepo <- function(pkgs, path, repos = getOption("repos"), type = "source", pdb <- pkgAvail(repos = repos, type = t, Rversion = Rversion) if (download) { - utils::download.packages(pkgs, destdir = pkgPath, available = pdb, repos = repos, + download.packages(pkgs, destdir = pkgPath, available = pdb, repos = repos, contriburl = contribUrl(repos, t, Rversion), type = t, quiet = quiet) } @@ -83,7 +85,7 @@ makeRepo <- function(pkgs, path, repos = getOption("repos"), type = "source", ## allow for more than one repo fromLocalRepos <- grepl("^file://", repos) - if(any(fromLocalRepos)){ + if (any(fromLocalRepos)) { # need to copy files to correct folder if (sum(fromLocalRepos) > 1) warning("More than one local repos provided. Only the first listed will be used.") @@ -113,7 +115,7 @@ updateRepoIndex <- function(path, type = "source", Rversion = R.version) { tools::write_PACKAGES(dir = pkgPath, type = t) }) names(n) <- type - return(n) + n } @@ -125,11 +127,5 @@ updateRepoIndex <- function(path, type = "source", Rversion = R.version) { makeLibrary <- function(pkgs, path, type = "source"){ .Deprecated("makeRepo") NULL - # if(!file.exists(path)) stop("Download path does not exist") - # wd <- getwd() - # on.exit(setwd(wd)) - # setwd(normalizePath(path)) - # message(getwd()) - # download.packages(pkgs, destdir = path, type = type) } diff --git a/R/mock-makeRepo.R b/R/mock-makeRepo.R new file mode 100644 index 0000000..07c9440 --- /dev/null +++ b/R/mock-makeRepo.R @@ -0,0 +1,53 @@ +# These functions mock downloading of packages, for fast testing purposes only + + +#' @importFrom utils available.packages +mock.download.packages <- function(pkgs, destdir, available, type, ...){ + if (missing(available) || is.null(available)) available <- available.packages() + downloadFileName <- function(package, version, type){ + paste0(package, "_", version, pkgFileExt(type)) + } + versions <- setNames(available[pkgs, "Version"], pkgs) + downloads <- mapply(names(versions), versions, + USE.NAMES = FALSE, + FUN = function(p, v){ + fn <- file.path(destdir, downloadFileName(p, v, type)) + writeLines("", fn) + matrix(c(p, fn), ncol = 2) + } + ) + t(downloads) +} + +mock.updateRepoIndex <- function(path, type = "source", Rversion = R.version){ + do_one <- function(t) { + pkg_path <- repoBinPath(path = path, type = t, Rversion = Rversion) + pattern <- ".tgz$|.zip$|.tar.gz$" + if (grepl("mac.binary", t)) t <- "mac.binary" + ff <- list.files(pkg_path, recursive = TRUE, full.names = TRUE, pattern = pattern) + ffb <- basename(ff) + pkgs <- ffb[!grepl("^PACKAGES.*", ffb)] + np <- length(pkgs) + pkg_names <- gsub(pattern, "", pkgs) + db <- matrix(unlist(strsplit(pkg_names, "_")), ncol = 2, byrow = TRUE) + colnames(db) <- c("Package", "Version") + db + + if (np > 0L) { + db[!is.na(db) & (db == "")] <- NA_character_ + con <- file(file.path(pkg_path, "PACKAGES"), "wt") + write.dcf(db, con) + close(con) + con <- gzfile(file.path(pkg_path, "PACKAGES.gz"), "wt") + write.dcf(db, con) + close(con) + rownames(db) <- db[, "Package"] + saveRDS(db, file.path(pkg_path, "PACKAGES.rds"), compress = "xz") + } + np + } + n <- sapply(type, do_one, USE.NAMES = TRUE, simplify = FALSE) + names(n) <- type + n +} + diff --git a/R/pkgDep.R b/R/pkgDep.R index c5e10f2..afb21b7 100644 --- a/R/pkgDep.R +++ b/R/pkgDep.R @@ -162,8 +162,8 @@ contribUrl <- function (repos = getOption("repos"), type <- "source" if (type == "binary") type <- .Platform$pkgType - if (is.null(repos)) - return(NULL) + if (is.null(repos)) return(NULL) + if ("@CRAN@" %in% repos && interactive()) { cat(gettext("--- Please select a CRAN mirror for use in this session ---"), "\n", sep = "") diff --git a/R/pkgDepTools.R b/R/pkgDepTools.R index 3b0150b..cd28e13 100644 --- a/R/pkgDepTools.R +++ b/R/pkgDepTools.R @@ -36,9 +36,8 @@ split_op_version <- function (x) { # @rdname pkgDepTools # @keywords internal -split_dependencies <- function (x) { - if (!length(x)) - return(list()) +split_dependencies <- function(x) { + if (!length(x)) return(list()) x <- unlist(strsplit(x, ",")) x <- sub("[[:space:]]+$", "", x) x <- unique(sub("^[[:space:]]*(.*)", "\\1", x)) @@ -54,13 +53,10 @@ split_dependencies <- function (x) { # @rdname pkgDepTools # @keywords internal cleanPkgField <- function(val) { - if (is.na(val)) - return(character(0)) + if (is.na(val)) return(character(0)) val <- names(split_dependencies(val)) - if (is.null(val)) - return(character(0)) - val <- val[! val %in% "R"] - if (length(val)) - return(val) - return(character(0)) + if (is.null(val)) return(character(0)) + val <- val[!val %in% "R"] + if (length(val)) return(val) + character(0) } diff --git a/R/updatePackages.R b/R/updatePackages.R index 95f6f55..fe1157b 100644 --- a/R/updatePackages.R +++ b/R/updatePackages.R @@ -118,10 +118,10 @@ updatePackages <- function(path = NULL, repos = getOption("repos"), method, ask } if (!is.null(subset)) { oldPkgs <- oldPkgs[rownames(oldPkgs) %in% subset, , drop = FALSE] - if (nrow(oldPkgs)==0) return(invisible()) + if (nrow(oldPkgs) == 0) return(invisible()) } update <- if (is.character(ask) && ask == "graphics") { - if (.Platform$OS.type ==" windows" || .Platform$GUI == "AQUA" || + if (.Platform$OS.type == " windows" || .Platform$GUI == "AQUA" || (capabilities("tcltk") && capabilities("X11"))) { k <- select.list(oldPkgs[, 1L], oldPkgs[, 1L], multiple = TRUE, title = "Packages to be updated", graphics = TRUE) diff --git a/inst/doc/miniCRAN-dependency-graph.R b/inst/doc/miniCRAN-dependency-graph.R index 115e61e..2b0361b 100644 --- a/inst/doc/miniCRAN-dependency-graph.R +++ b/inst/doc/miniCRAN-dependency-graph.R @@ -18,3 +18,43 @@ dg <- makeDepGraph(tags, enhances = TRUE, availPkgs = cranJuly2014) set.seed(1) plot(dg, legendPosition = c(-1, -1), vertex.size = 10, cex = 0.7) +## ----init---------------------------------------------------------------- +library("miniCRAN") + +## ----pkgdep-------------------------------------------------------------- +tags <- "chron" +pkgDep(tags, availPkgs = cranJuly2014) + +## ----makeDepGraph, warning=FALSE----------------------------------------- +dg <- makeDepGraph(tags, enhances = TRUE, availPkgs = cranJuly2014) +set.seed(1) +plot(dg, legendPosition = c(-1, 1), vertex.size = 20) + +## ----so-tags, warning=FALSE, fig.width=10, fig.height=10----------------- +tags <- c("ggplot2", "data.table", "plyr", "knitr", "shiny", "xts", "lattice") +pkgDep(tags, suggests = TRUE, enhances = FALSE, availPkgs = cranJuly2014) + +dg <- makeDepGraph(tags, enhances = TRUE, availPkgs = cranJuly2014) +set.seed(1) +plot(dg, legendPosition = c(-1, -1), vertex.size = 10, cex = 0.7) + +## ----init---------------------------------------------------------------- +library("miniCRAN") + +## ----pkgdep-------------------------------------------------------------- +tags <- "chron" +pkgDep(tags, availPkgs = cranJuly2014) + +## ----makeDepGraph, warning=FALSE----------------------------------------- +dg <- makeDepGraph(tags, enhances = TRUE, availPkgs = cranJuly2014) +set.seed(1) +plot(dg, legendPosition = c(-1, 1), vertex.size = 20) + +## ----so-tags, warning=FALSE, fig.width=10, fig.height=10----------------- +tags <- c("ggplot2", "data.table", "plyr", "knitr", "shiny", "xts", "lattice") +pkgDep(tags, suggests = TRUE, enhances = FALSE, availPkgs = cranJuly2014) + +dg <- makeDepGraph(tags, enhances = TRUE, availPkgs = cranJuly2014) +set.seed(1) +plot(dg, legendPosition = c(-1, -1), vertex.size = 10, cex = 0.7) + diff --git a/inst/doc/miniCRAN-dependency-graph.html b/inst/doc/miniCRAN-dependency-graph.html index dff8732..46b794c 100644 --- a/inst/doc/miniCRAN-dependency-graph.html +++ b/inst/doc/miniCRAN-dependency-graph.html @@ -12,7 +12,7 @@ - + Using miniCRAN to identify package dependencies @@ -70,7 +70,7 @@

Using miniCRAN to identify package dependencies

Andrie de Vries

-

December 19, 2017

+

December 26, 2017

diff --git a/inst/doc/miniCRAN-introduction.R b/inst/doc/miniCRAN-introduction.R index 1a7a05d..b8f0da7 100644 --- a/inst/doc/miniCRAN-introduction.R +++ b/inst/doc/miniCRAN-introduction.R @@ -78,3 +78,163 @@ pkgList # # Delete temporary folder # unlink(pth, recursive = TRUE) +## ----make-repo-1--------------------------------------------------------- +library("miniCRAN") + +# use Revolution Analytics CRAN mirror +revolution <- c(CRAN = "http://cran.microsoft.com") + +# Specify list of packages to download +pkgs <- c("foreach") +pkgList <- pkgDep(pkgs, repos = revolution, type = "source", suggests = FALSE, + availPkgs = cranJuly2014) +pkgList + +## ----make-repo-2, eval=FALSE--------------------------------------------- +# # Create temporary folder for miniCRAN +# dir.create(pth <- file.path(tempdir(), "miniCRAN")) +# +# # Make repo for source and win.binary +# makeRepo(pkgList, path=pth, repos=revolution, type=c("source", "win.binary")) + +## ----make-repo-3, eval=FALSE--------------------------------------------- +# # List all files in miniCRAN +# list.files(pth, recursive = TRUE, full.names = FALSE) + +## ----make-repo-4, eval=FALSE--------------------------------------------- +# # Check for available packages +# pkgAvail(repos = pth, type = "win.binary")[, c(1:3, 5)] + +## ----make-repo-5, eval=FALSE--------------------------------------------- +# install.packages(pkgs, +# repos = paste0("file:///", pth), +# type = "source") + +## ----addto-repo-new-1, eval=FALSE---------------------------------------- +# # Add new packages (from CRAN) to the miniCRAN repo +# addPackage("Matrix", path = pth, repos = revolution, type = c("source", "win.binary")) +# pkgAvail(repos = pth, type = "win.binary")[, c(1:3, 5)] + +## ----addto-repo-old-1, eval=FALSE---------------------------------------- +# # create a data frame with the package and version info +# oldVers <- data.frame( +# package = c("foreach", "codetools", "iterators"), +# version = c("1.4.0", "0.2-7", "1.0.5"), +# stringsAsFactors = FALSE +# ) +# +# # download old source package version and create repo index +# addOldPackage(pkgList, path = pth, vers = oldVers$version, repos = revolution, type = "source") + +## ----addto-repo-old-2, eval=FALSE---------------------------------------- +# # List package versions in the miniCRAN repo (produces warning about duplicates) +# pkgVersionsSrc <- checkVersions(pkgList, path = pth, type = "source") +# pkgVersionsBin <- checkVersions(pkgList, path = pth, type = "win.binary") +# +# # After inspecting package versions, remove old versions +# basename(pkgVersionsSrc) # duplicate versions +# basename(pkgVersionsBin) +# +# file.remove(pkgVersionsSrc[c(2,4,6)]) +# +# # rebuild the package index after removing duplicate package versions +# updateRepoIndex(pth, type = c("source", "win.binary")) + +## ----addto-repo-old-3, eval=FALSE---------------------------------------- +# pkgAvail(pth, type = "source")[, c(1:3, 5)] # contains the old versions +# pkgAvail(pth, type = "win.binary")[, c(1:3, 5)] # contains the current versions + +## ----update-repo-1, eval=FALSE------------------------------------------- +# # Check if updated packages are available +# oldPackages(path = pth, repos = revolution, type = "source")[, 1:3] # should need update +# oldPackages(path = pth, repos = revolution, type = "win.binary")[, 1:3] # should be current + +## ----update-repo-2, eval=FALSE------------------------------------------- +# # Update available packages +# updatePackages(path = pth, repos = revolution, type = "source", ask = FALSE) # should need update +# updatePackages(path = pth, repos = revolution, type = "win.binary", ask = FALSE) # should be current + +## ----cleanup, include=FALSE, eval=FALSE---------------------------------- +# # Delete temporary folder +# unlink(pth, recursive = TRUE) + +## ----make-repo-1--------------------------------------------------------- +library("miniCRAN") + +# use Revolution Analytics CRAN mirror +revolution <- c(CRAN = "http://cran.microsoft.com") + +# Specify list of packages to download +pkgs <- c("foreach") +pkgList <- pkgDep(pkgs, repos = revolution, type = "source", suggests = FALSE, + availPkgs = cranJuly2014) +pkgList + +## ----make-repo-2, eval=FALSE--------------------------------------------- +# # Create temporary folder for miniCRAN +# dir.create(pth <- file.path(tempdir(), "miniCRAN")) +# +# # Make repo for source and win.binary +# makeRepo(pkgList, path=pth, repos=revolution, type=c("source", "win.binary")) + +## ----make-repo-3, eval=FALSE--------------------------------------------- +# # List all files in miniCRAN +# list.files(pth, recursive = TRUE, full.names = FALSE) + +## ----make-repo-4, eval=FALSE--------------------------------------------- +# # Check for available packages +# pkgAvail(repos = pth, type = "win.binary")[, c(1:3, 5)] + +## ----make-repo-5, eval=FALSE--------------------------------------------- +# install.packages(pkgs, +# repos = paste0("file:///", pth), +# type = "source") + +## ----addto-repo-new-1, eval=FALSE---------------------------------------- +# # Add new packages (from CRAN) to the miniCRAN repo +# addPackage("Matrix", path = pth, repos = revolution, type = c("source", "win.binary")) +# pkgAvail(repos = pth, type = "win.binary")[, c(1:3, 5)] + +## ----addto-repo-old-1, eval=FALSE---------------------------------------- +# # create a data frame with the package and version info +# oldVers <- data.frame( +# package = c("foreach", "codetools", "iterators"), +# version = c("1.4.0", "0.2-7", "1.0.5"), +# stringsAsFactors = FALSE +# ) +# +# # download old source package version and create repo index +# addOldPackage(pkgList, path = pth, vers = oldVers$version, repos = revolution, type = "source") + +## ----addto-repo-old-2, eval=FALSE---------------------------------------- +# # List package versions in the miniCRAN repo (produces warning about duplicates) +# pkgVersionsSrc <- checkVersions(pkgList, path = pth, type = "source") +# pkgVersionsBin <- checkVersions(pkgList, path = pth, type = "win.binary") +# +# # After inspecting package versions, remove old versions +# basename(pkgVersionsSrc) # duplicate versions +# basename(pkgVersionsBin) +# +# file.remove(pkgVersionsSrc[c(2,4,6)]) +# +# # rebuild the package index after removing duplicate package versions +# updateRepoIndex(pth, type = c("source", "win.binary")) + +## ----addto-repo-old-3, eval=FALSE---------------------------------------- +# pkgAvail(pth, type = "source")[, c(1:3, 5)] # contains the old versions +# pkgAvail(pth, type = "win.binary")[, c(1:3, 5)] # contains the current versions + +## ----update-repo-1, eval=FALSE------------------------------------------- +# # Check if updated packages are available +# oldPackages(path = pth, repos = revolution, type = "source")[, 1:3] # should need update +# oldPackages(path = pth, repos = revolution, type = "win.binary")[, 1:3] # should be current + +## ----update-repo-2, eval=FALSE------------------------------------------- +# # Update available packages +# updatePackages(path = pth, repos = revolution, type = "source", ask = FALSE) # should need update +# updatePackages(path = pth, repos = revolution, type = "win.binary", ask = FALSE) # should be current + +## ----cleanup, include=FALSE, eval=FALSE---------------------------------- +# # Delete temporary folder +# unlink(pth, recursive = TRUE) + diff --git a/inst/doc/miniCRAN-introduction.html b/inst/doc/miniCRAN-introduction.html index 01bcfe2..9f1f9cf 100644 --- a/inst/doc/miniCRAN-introduction.html +++ b/inst/doc/miniCRAN-introduction.html @@ -12,7 +12,7 @@ - + Using miniCRAN to create a local CRAN repository @@ -70,7 +70,7 @@

Using miniCRAN to create a local CRAN repository

Andrie de Vries and Alex Chubaty

-

December 19, 2017

+

December 26, 2017

diff --git a/inst/doc/miniCRAN-non-CRAN-repos.R b/inst/doc/miniCRAN-non-CRAN-repos.R index c02e2a0..d5d4f90 100644 --- a/inst/doc/miniCRAN-non-CRAN-repos.R +++ b/inst/doc/miniCRAN-non-CRAN-repos.R @@ -37,3 +37,81 @@ index <- function(url, type = "source", filters = NULL, head = 5, cols = c("Pack # # index(bioc["BioCsoft"]) +## ----setup--------------------------------------------------------------- +# Wrapper around available.packages --------------------------------------- + +index <- function(url, type = "source", filters = NULL, head = 5, cols = c("Package", "Version")){ + contribUrl <- contrib.url(url, type = type) + p <- available.packages(contribUrl, type = type, filters = filters) + p[1:head, cols] +} + + +## ----CRAN, eval=FALSE---------------------------------------------------- +# CRAN <- "http://cran.r-project.org" +# index(CRAN) + +## ----revo, eval=FALSE---------------------------------------------------- +# revoStable <- "http://packages.revolutionanalytics.com/cran/3.1/stable" +# index(revoStable) +# +# revoMirror <- "http://cran.revolutionanalytics.com" +# index(revoMirror) + +## ----rforge, eval=FALSE-------------------------------------------------- +# rforge <- "http://r-forge.r-project.org" +# index(rforge) + +## ----bioc, eval=FALSE---------------------------------------------------- +# bioc <- local({ +# env <- new.env() +# on.exit(rm(env)) +# evalq(source("http://bioconductor.org/biocLite.R", local = TRUE), env) +# biocinstallRepos() +# }) +# +# bioc +# bioc[grep("BioC", names(bioc))] +# +# +# index(bioc["BioCsoft"]) + +## ----setup--------------------------------------------------------------- +# Wrapper around available.packages --------------------------------------- + +index <- function(url, type = "source", filters = NULL, head = 5, cols = c("Package", "Version")){ + contribUrl <- contrib.url(url, type = type) + p <- available.packages(contribUrl, type = type, filters = filters) + p[1:head, cols] +} + + +## ----CRAN, eval=FALSE---------------------------------------------------- +# CRAN <- "http://cran.r-project.org" +# index(CRAN) + +## ----revo, eval=FALSE---------------------------------------------------- +# revoStable <- "http://packages.revolutionanalytics.com/cran/3.1/stable" +# index(revoStable) +# +# revoMirror <- "http://cran.revolutionanalytics.com" +# index(revoMirror) + +## ----rforge, eval=FALSE-------------------------------------------------- +# rforge <- "http://r-forge.r-project.org" +# index(rforge) + +## ----bioc, eval=FALSE---------------------------------------------------- +# bioc <- local({ +# env <- new.env() +# on.exit(rm(env)) +# evalq(source("http://bioconductor.org/biocLite.R", local = TRUE), env) +# biocinstallRepos() +# }) +# +# bioc +# bioc[grep("BioC", names(bioc))] +# +# +# index(bioc["BioCsoft"]) + diff --git a/inst/doc/miniCRAN-non-CRAN-repos.html b/inst/doc/miniCRAN-non-CRAN-repos.html index 23f7c9e..5d595d6 100644 --- a/inst/doc/miniCRAN-non-CRAN-repos.html +++ b/inst/doc/miniCRAN-non-CRAN-repos.html @@ -12,7 +12,7 @@ - + Using repositories other than CRAN with miniCRAN @@ -70,7 +70,7 @@

Using repositories other than CRAN with miniCRAN

Andrie de Vries

-

December 19, 2017

+

December 26, 2017

diff --git a/man/repoBinPath.Rd b/man/repoBinPath.Rd index b1c3a09..1afc412 100644 --- a/man/repoBinPath.Rd +++ b/man/repoBinPath.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/helpers.R +% Please edit documentation in R/internal.R \name{repoBinPath} \alias{repoBinPath} \title{Construct path to full binary location} diff --git a/man/repoPrefix.Rd b/man/repoPrefix.Rd index ac32f6c..ca97b6f 100644 --- a/man/repoPrefix.Rd +++ b/man/repoPrefix.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/helpers.R +% Please edit documentation in R/internal.R \name{repoPrefix} \alias{repoPrefix} \title{Get the path to the repo directory containing the package files.} diff --git a/man/twodigitRversion.Rd b/man/twodigitRversion.Rd index 607e765..e3a699e 100644 --- a/man/twodigitRversion.Rd +++ b/man/twodigitRversion.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/helpers.R +% Please edit documentation in R/internal.R \name{twodigitRversion} \alias{twodigitRversion} \title{Get a two-digit version of the R version} diff --git a/tests/test-1-pkgAvail.R b/tests/test-1-pkgAvail.R deleted file mode 100644 index 8595e06..0000000 --- a/tests/test-1-pkgAvail.R +++ /dev/null @@ -1,4 +0,0 @@ -if (require(testthat)) { - source(file.path("testthat", "helpers.R")) - test_check("miniCRAN", filter = "pkgAvail") -} diff --git a/tests/test-3&4-makeRepo.R b/tests/test-3&4-makeRepo.R deleted file mode 100644 index 87b219a..0000000 --- a/tests/test-3&4-makeRepo.R +++ /dev/null @@ -1,4 +0,0 @@ -if (require(testthat)) { - source(file.path("testthat", "helpers.R")) - test_check("miniCRAN", filter = "makeRepo") -} diff --git a/tests/test-5-updateRepo.R b/tests/test-5-updateRepo.R deleted file mode 100644 index 282da49..0000000 --- a/tests/test-5-updateRepo.R +++ /dev/null @@ -1,4 +0,0 @@ -if (require(testthat)) { - source(file.path("testthat", "helpers.R")) - test_check("miniCRAN", filter = "updateRepo") -} diff --git a/tests/test-6-getCranDescription.R b/tests/test-6-getCranDescription.R deleted file mode 100644 index 58c027d..0000000 --- a/tests/test-6-getCranDescription.R +++ /dev/null @@ -1,4 +0,0 @@ -if (require(testthat)) { - source(file.path("testthat", "helpers.R")) - test_check("miniCRAN", filter = "getCranDescription") -} diff --git a/tests/test-7-add-GithubPackage.R b/tests/test-7-add-GithubPackage.R deleted file mode 100644 index 1f08477..0000000 --- a/tests/test-7-add-GithubPackage.R +++ /dev/null @@ -1,4 +0,0 @@ -if (require(testthat)) { - source(file.path("testthat", "helpers.R")) - test_check("miniCRAN", filter = "addGithubPackage") -} diff --git a/tests/test-2-pkgDep.R b/tests/test-all.R similarity index 62% rename from tests/test-2-pkgDep.R rename to tests/test-all.R index f6194ef..fd1af6a 100644 --- a/tests/test-2-pkgDep.R +++ b/tests/test-all.R @@ -1,4 +1,4 @@ if (require(testthat)) { source(file.path("testthat", "helpers.R")) - test_check("miniCRAN", filter = "pkgDep") + test_check("miniCRAN") } diff --git a/tests/testthat/helpers.R b/tests/testthat/helpers.R index 4af9b32..473bb2d 100644 --- a/tests/testthat/helpers.R +++ b/tests/testthat/helpers.R @@ -24,7 +24,7 @@ is.online <- function(url = MRAN(), tryHttp = TRUE){ # Interrupt the test if url can not be reached skip_if_offline <- function(url = MRAN()){ - if(!is.online(url)) testthat::skip("offline") + if (!is.online(url)) testthat::skip("offline") } @@ -41,6 +41,12 @@ skip_if_offline <- function(url = MRAN()){ } +mock.makeRepo <- function(...){ + mockery::stub(makeRepo, "download.packages", mock.download.packages) + mockery::stub(makeRepo, "updateRepoIndex", mock.updateRepoIndex) + makeRepo(...) +} + # Create sample repo from MRAN snapshot .createSampleRepo <- function(MRAN, path, pkgs, Rversion = "3.1"){ if (missing(MRAN)) MRAN <- MRAN("2014-10-15") @@ -55,21 +61,21 @@ skip_if_offline <- function(url = MRAN()){ pkgList_source <- pkgDep(pkgs, availPkgs = pdb_source, repos = MRAN, type = "source", suggests = FALSE, Rversion = Rversion) - makeRepo(pkgList_source, path = path, repos = MRAN, + mock.makeRepo(pkgList_source, path = path, repos = MRAN, type = "source", quiet = TRUE, Rversion = Rversion) pkgList_win <- pkgDep(pkgs, availPkgs = pdb_win, repos = MRAN, type = "win.binary", suggests = FALSE, Rversion = Rversion) - makeRepo(pkgList_win, path = path, repos = MRAN, + mock.makeRepo(pkgList_win, path = path, repos = MRAN, type = "win.binary", quiet = TRUE, Rversion = Rversion) pkgList_mac <- pkgDep(pkgs, availPkgs = pdb_mac, repos = MRAN, type = "mac.binary", suggests = FALSE, Rversion = Rversion) - makeRepo(pkgList_mac, path = path, repos = MRAN, + mock.makeRepo(pkgList_mac, path = path, repos = MRAN, type = "mac.binary", quiet = TRUE, Rversion = Rversion) } diff --git a/tests/testthat/test-3-makeRepo.R b/tests/testthat/test-3-makeRepo.R index 06ed216..10b6011 100644 --- a/tests/testthat/test-3-makeRepo.R +++ b/tests/testthat/test-3-makeRepo.R @@ -2,19 +2,22 @@ if (interactive()) {library(testthat); Sys.setenv(NOT_CRAN = "true")} context("makeRepo") -revolution <- MRAN("2014-10-15") -if(!is.online(revolution, tryHttp = FALSE)) { - # Use http:// for older versions of R - revolution <- sub("^https://", "http://", revolution) +{ + revolution <- MRAN("2014-10-15") + if (!is.online(revolution, tryHttp = FALSE)) { + # Use http:// for older versions of R + revolution <- sub("^https://", "http://", revolution) + } + rvers = "3.1" + pkgs <- c("Bmix") + repo_root <- file.path(tempdir(), "miniCRAN", Sys.Date()) + if (file.exists(repo_root)) unlink(repo_root, recursive = TRUE) + + # list.files(repo_root, recursive = TRUE) + + unlink(list.files(tempdir(), pattern = ".rds$", full.names = TRUE)) + } -rvers = "3.1" -pkgs <- c("Bmix") -repo_root <- file.path(tempdir(), "miniCRAN", Sys.Date()) -if (file.exists(repo_root)) unlink(repo_root, recursive = TRUE) - -# list.files(repo_root, recursive = TRUE) - -unlink(list.files(tempdir(), pattern = ".rds$", full.names = TRUE)) types <- c("source", "win.binary", "mac.binary", "mac.binary.mavericks") @@ -22,9 +25,13 @@ for (pkg_type in (types)) { context(sprintf(" - %s", pkg_type)) test_that(sprintf("makeRepo downloads %s files and builds PACKAGES", pkg_type), { + skip_on_cran() skip_if_offline() - + + mockery::stub(makeRepo, "download.packages", mock.download.packages) + mockery::stub(makeRepo, "updateRepoIndex", mock.updateRepoIndex) + pdb <- pkgAvail(repos = revolution, type = pkg_type, Rversion = rvers, quiet = TRUE) pkgList <- pkgDep(pkgs, availPkgs = pdb, repos = revolution, type = pkg_type, suggests = FALSE, Rversion = rvers, quiet = FALSE) diff --git a/tests/testthat/test-4-makeRepo-from-localCRAN.R b/tests/testthat/test-4-makeRepo-from-localCRAN.R index 5ff40dd..97435b9 100644 --- a/tests/testthat/test-4-makeRepo-from-localCRAN.R +++ b/tests/testthat/test-4-makeRepo-from-localCRAN.R @@ -2,20 +2,21 @@ if (interactive()) {library(testthat); Sys.setenv(NOT_CRAN = "true")} context("makeRepo from local miniCRAN") -revolution <- MRAN("2014-10-15") -if(!is.online(revolution, tryHttp = FALSE)) { - # Use http:// for older versions of R - revolution <- sub("^https://", "http://", revolution) +{ + revolution <- MRAN("2014-10-15") + if (!is.online(revolution, tryHttp = FALSE)) { + # Use http:// for older versions of R + revolution <- sub("^https://", "http://", revolution) + } + rvers = "3.2" + pkgs <- c("MASS") + repo_root <- file.path(tempdir(), "miniCRAN", Sys.Date()) + new_repo_root <- file.path(tempdir(), "newMiniCRAN", Sys.Date()) + if (file.exists(repo_root)) unlink(repo_root, recursive = TRUE) + if (file.exists(new_repo_root)) unlink(new_repo_root, recursive = TRUE) + + # list.files(repo_root, recursive = TRUE) } -rvers = "3.2" -pkgs <- c("MASS") -repo_root <- file.path(tempdir(), "miniCRAN", Sys.Date()) -new_repo_root <- file.path(tempdir(), "newMiniCRAN", Sys.Date()) -if (file.exists(repo_root)) unlink(repo_root, recursive = TRUE) -if (file.exists(new_repo_root)) unlink(new_repo_root, recursive = TRUE) - -# list.files(repo_root, recursive = TRUE) - types <- c("win.binary") names(types) <- c("win.binary") @@ -24,6 +25,8 @@ for (pkg_type in names(types)) { test_that(sprintf("makeRepo downloads %s files and builds PACKAGES file", pkg_type), { # skip_on_cran() skip_if_offline() + mockery::stub(makeRepo, "download.packages", mock.download.packages) + mockery::stub(makeRepo, "updateRepoIndex", mock.updateRepoIndex) # Create local miniCRAN diff --git a/tests/testthat/test-5-updateRepo.R b/tests/testthat/test-5-updateRepo.R index fd08871..aa52f02 100644 --- a/tests/testthat/test-5-updateRepo.R +++ b/tests/testthat/test-5-updateRepo.R @@ -4,27 +4,30 @@ context("updateRepo") # make baseline repo ------------------------------------------------------ - -repo_root <- file.path(tempdir(), "miniCRAN", Sys.Date()) -if (file.exists(repo_root)) unlink(repo_root, recursive = TRUE) -dir.create(repo_root, recursive = TRUE, showWarnings = FALSE) - -revolution <- MRAN("2014-10-15") -if (!is.online(revolution, tryHttp = FALSE)) { - # Use http:// for older versions of R - revolution <- sub("^https://", "http://", revolution) +{ + repo_root <- file.path(tempdir(), "miniCRAN", Sys.Date()) + if (file.exists(repo_root)) unlink(repo_root, recursive = TRUE) + dir.create(repo_root, recursive = TRUE, showWarnings = FALSE) + + revolution <- MRAN("2014-10-15") + if (!is.online(revolution, tryHttp = FALSE)) { + # Use http:// for older versions of R + revolution <- sub("^https://", "http://", revolution) + } + rvers <- "3.1" + pkgs <- c("chron", "adaptivetau") + + types <- c("win.binary", "mac.binary", "source") + # types <- c("win.binary") + + names(types) <- types + pdb <- list() + pkgList <- list() } -rvers <- "3.1" -pkgs <- c("chron", "adaptivetau") - -types <- c("win.binary", "mac.binary", "source") -# types <- c("win.binary") - -names(types) <- types test_that("sample repo is setup correctly", { skip_if_offline(revolution) - + pdb <<- lapply(types, pkgAvail, repos = revolution, Rversion = rvers, quiet = TRUE) expect_is(pdb, "list") pkgList <<- lapply(types, function(type) { @@ -32,7 +35,7 @@ test_that("sample repo is setup correctly", { repos = revolution, suggests = FALSE, Rversion = rvers) }) expect_is(pkgList, "list") - + z <- .createSampleRepo(path = repo_root, MRAN = revolution, Rversion = rvers) expect_is(z, "character") expect_equal(unname(pkgAvail(repo_root, quiet = TRUE)[, "Package"]), sort(pkgs)) @@ -45,95 +48,103 @@ pkgsAdd <- c("forecast") pkg_type <- names(types)[1] for (pkg_type in names(types)) { - + context(sprintf(" - Add packages to repo (%s)", pkg_type)) - + test_that(sprintf( "addPackage downloads %s files and rebuilds PACKAGES file", pkg_type), { - - skip_on_cran() - skip_if_offline(revolution) - - pkgListAdd <- pkgDep(pkgsAdd, availPkgs = pdb[[pkg_type]], - repos = revolution, - type = pkg_type, - suggests = FALSE, - Rversion = rvers) - prefix <- repoPrefix(pkg_type, Rversion = rvers) - - addPackage(pkgListAdd, path = repo_root, repos = revolution, type = pkg_type, - quiet = TRUE, Rversion = rvers) - - expect_true( - .checkForRepoFiles(repo_root, pkgListAdd, prefix) - ) - expect_true( - file.exists(file.path(repo_root, prefix, "PACKAGES.gz")) - ) - expect_true( - all( - pkgListAdd %in% pkgAvail(repo_root, - type = pkg_type, - Rversion = rvers, - quiet = TRUE)[, "Package"] + + skip_on_cran() + skip_if_offline(revolution) + + mockery::stub(addPackage, "makeRepo", mock.makeRepo) + mockery::stub(addPackage, "updateRepoIndex", mock.updateRepoIndex) + + pkgListAdd <- pkgDep(pkgsAdd, availPkgs = pdb[[pkg_type]], + repos = revolution, + type = pkg_type, + suggests = FALSE, + Rversion = rvers) + prefix <- repoPrefix(pkg_type, Rversion = rvers) + + addPackage(pkgListAdd, path = repo_root, repos = revolution, type = pkg_type, + quiet = TRUE, Rversion = rvers) + + expect_true( + .checkForRepoFiles(repo_root, pkgListAdd, prefix) ) - ) - }) + expect_true( + file.exists(file.path(repo_root, prefix, "PACKAGES.gz")) + ) + expect_true( + all( + pkgListAdd %in% pkgAvail(repo_root, + type = pkg_type, + Rversion = rvers, + quiet = TRUE)[, "Package"] + ) + ) + }) } -# Add local packages to repo ---------------------------------------------------- +# Add local packages to repo ---------------------------------------------- pkgsAddLocal <- c("MASS") for (pkg_type in names(types)) { - + context(sprintf(" - Add local packages to repo (%s)", pkg_type)) + + test_that( + sprintf("addLocalPackage copies %s files and rebuilds PACKAGES", + pkg_type), + { + + skip_on_cran() + skip_if_offline(revolution) + + mockery::stub(addLocalPackage, "updateRepoIndex", mock.updateRepoIndex) + + tmpdir <- file.path(tempdir(), "miniCRAN", "local", pkg_type) + expect_true(dir.create(tmpdir, recursive = TRUE, showWarnings = FALSE)) + tmpdir <- normalizePath(tmpdir) + expect_true(dir.exists(tmpdir)) + on.exit(unlink(tmpdir, recursive = TRUE), add = TRUE) + + # get most recent version + res <- mock.download.packages( + pkgsAddLocal, destdir = tmpdir, + type = pkg_type, + available = pkgAvail(revolution, pkg_type, rvers), + contriburl = contribUrl(revolution, pkg_type, rvers), + quiet = TRUE) + + # simulate older version also present in pkgPath directory + f <- res[, 2] + expect_true( + file.copy(from = f, to = file.path(tmpdir, "MASS_7.3-0.tar.gz")) + ) + expect_equal(length(list.files(tmpdir)), 2) + + addLocalPackage(pkgs = pkgsAddLocal, pkgPath = tmpdir, path = repo_root, + type = pkg_type, quiet = TRUE, Rversion = rvers) - test_that(sprintf("addLocalPackage copies %s files and rebuilds PACKAGES", - pkg_type), { - - skip_on_cran() - skip_if_offline(revolution) - - tmpdir <- file.path(tempdir(), "miniCRAN", "local", pkg_type) - expect_true(dir.create(tmpdir, recursive = TRUE)) - tmpdir <- normalizePath(tmpdir) - expect_true(dir.exists(tmpdir)) - on.exit(unlink(tmpdir, recursive = TRUE), add = TRUE) - - # get most recent version - res <- download.packages(pkgsAddLocal, destdir = tmpdir, - type = pkg_type, - available = pkgAvail(revolution, pkg_type, rvers), - contriburl = contribUrl(revolution, pkg_type, rvers), - quiet = TRUE) - - # simulate older version also present in pkgPath directory - f <- res[, 2] - expect_true( - file.copy(from = f, to = file.path(tmpdir, "MASS_7.3-0.tar.gz")) - ) - expect_equal(length(list.files(tmpdir)), 2) - - addLocalPackage(pkgs = pkgsAddLocal, pkgPath = tmpdir, path = repo_root, - type = pkg_type, quiet = TRUE, Rversion = rvers) - - prefix <- repoPrefix(pkg_type, Rversion = rvers) - expect_true( - .checkForRepoFiles(repo_root, pkgsAddLocal, prefix) - ) - expect_true( - file.exists(file.path(repo_root, prefix, "PACKAGES.gz")) - ) - expect_true( - all( - pkgsAddLocal %in% pkgAvail(repo_root, type = pkg_type, - Rversion = rvers)[, "Package"] + prefix <- repoPrefix(pkg_type, Rversion = rvers) + expect_true( + .checkForRepoFiles(repo_root, pkgsAddLocal, prefix) ) - ) - }) + expect_true( + file.exists(file.path(repo_root, prefix, "PACKAGES.gz")) + ) + expect_true( + all( + pkgsAddLocal %in% pkgAvail(repo_root, type = pkg_type, + Rversion = rvers)[, "Package"] + ) + ) + }) } @@ -148,56 +159,60 @@ if (!is.online(MRAN_mirror, tryHttp = FALSE)) { for (pkg_type in names(types)) { context(sprintf(" - Check for updates (%s)", pkg_type)) - - test_that(sprintf("updatePackages downloads %s files and builds PACKAGES", - pkg_type), { - - skip_on_cran() - skip_if_offline(MRAN_mirror) - - prefix <- repoPrefix(pkg_type, Rversion = rvers) - - suppressWarnings( - old <- oldPackages(path = repo_root, repos = MRAN_mirror, - type = pkg_type, Rversion = rvers, - quiet = FALSE) - ) - - # In the following allow for differences between mac.binary and other types - expect_true(nrow(old) >= 10) - expect_true(nrow(old) <= 12) - expect_equal(ncol(old), 4) - expect_true( - all( - rownames(old) %in% - c("adaptivetau", "BH", "digest", "forecast", "Hmisc", "mvtnorm", - "RColorBrewer", "RcppArmadillo", "reshape2", "timeDate", - "timeSeries", "tis") + + test_that( + sprintf("updatePackages downloads %s files and builds PACKAGES", pkg_type), + { + + skip_on_cran() + skip_if_offline(MRAN_mirror) + + mockery::stub(updatePackages, "updateRepoIndex", mock.updateRepoIndex, depth = 3) + mockery::stub(updatePackages, "makeRepo", mock.makeRepo, depth = 3) + + prefix <- repoPrefix(pkg_type, Rversion = rvers) + + suppressWarnings( + old <- oldPackages(path = repo_root, repos = MRAN_mirror, + type = pkg_type, Rversion = rvers, + quiet = FALSE) ) - ) - - updatePackages(path = repo_root, repos = MRAN_mirror, type = pkg_type, - ask = FALSE, quiet = TRUE, Rversion = rvers) - - updateVers <- getPkgVersFromFile( - list.files(file.path(repo_root, prefix)) - ) - - expect_true( - .checkForRepoFiles(repo_root, pkgList[[pkg_type]], prefix) - ) - - expect_true( - file.exists(file.path(repo_root, prefix, "PACKAGES.gz")) - ) - - old <- oldPackages(path = repo_root, repos = MRAN_mirror, - type = pkg_type, Rversion = rvers) - # browser() - expect_equal(nrow(old), 0) - expect_equal(ncol(old), 4) + + # In the following allow for differences between mac.binary and other types + expect_true(nrow(old) >= 10) + expect_true(nrow(old) <= 12) + expect_equal(ncol(old), 4) + expect_true( + all( + rownames(old) %in% + c("adaptivetau", "BH", "digest", "forecast", "Hmisc", "mvtnorm", + "RColorBrewer", "RcppArmadillo", "reshape2", "timeDate", + "timeSeries", "tis") + ) + ) + + updatePackages(path = repo_root, repos = MRAN_mirror, type = pkg_type, + ask = FALSE, quiet = TRUE, Rversion = rvers) - }) + updateVers <- getPkgVersFromFile( + list.files(file.path(repo_root, prefix)) + ) + + expect_true( + .checkForRepoFiles(repo_root, pkgList[[pkg_type]], prefix) + ) + + expect_true( + file.exists(file.path(repo_root, prefix, "PACKAGES.gz")) + ) + + old <- oldPackages(path = repo_root, repos = MRAN_mirror, + type = pkg_type, Rversion = rvers) + # browser() + expect_equal(nrow(old), 0) + expect_equal(ncol(old), 4) + + }) } @@ -206,38 +221,45 @@ for (pkg_type in names(types)) { context("Check for duplicate files") for (pkg_type in names(types)) { - - test_that(sprintf("checkVersions() finds out-of-date %s packages", pkg_type), { - - skip_on_cran() - skip_if_offline(MRAN_mirror) - - oldVersions <- list(package = c("acepack"), - version = c("1.3-2")) - if (pkg_type != "source") { - expect_error( - addOldPackage(oldVersions[["package"]], path = repo_root, - vers = oldVersions[["version"]], - repos = MRAN_mirror, type = pkg_type) - ) - } else { - addOldPackage(oldVersions[["package"]], path = repo_root, - vers = oldVersions[["version"]], - repos = MRAN_mirror, type = pkg_type) - files <- suppressWarnings( - checkVersions(path = repo_root, type = pkg_type)[[pkg_type]] - ) - - expect_true( - all(file.exists(files)) - ) - - pkgs <- sapply(strsplit(basename(files), "_"), "[[", 1) - dupes <- pkgs[duplicated(pkgs)] - expect_true( - all(dupes == oldVersions[["package"]]) - ) - - } - }) + + test_that( + sprintf("checkVersions() finds out-of-date %s packages", pkg_type), + { + + skip_on_cran() + skip_if_offline(MRAN_mirror) + with_mock( + download.packages = mock.download.packages, + updateRepoIndex = mock.updateRepoIndex, + { + + oldVersions <- list(package = c("acepack"), + version = c("1.3-2")) + if (pkg_type != "source") { + expect_error( + addOldPackage(oldVersions[["package"]], path = repo_root, + vers = oldVersions[["version"]], + repos = MRAN_mirror, type = pkg_type) + ) + } else { + addOldPackage(oldVersions[["package"]], path = repo_root, + vers = oldVersions[["version"]], + repos = MRAN_mirror, type = pkg_type) + files <- suppressWarnings( + checkVersions(path = repo_root, type = pkg_type)[[pkg_type]] + ) + + expect_true( + all(file.exists(files)) + ) + + pkgs <- sapply(strsplit(basename(files), "_"), "[[", 1) + dupes <- pkgs[duplicated(pkgs)] + expect_true( + all(dupes == oldVersions[["package"]]) + ) + + } + }) + }) }