diff --git a/DESCRIPTION b/DESCRIPTION index ee36150..5427fc9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -27,7 +27,6 @@ Imports: utils, XML, igraph, - mockery(>= 0.4.0) Suggests: devtools, knitr, diff --git a/R/makeRepo.R b/R/makeRepo.R index 234ee0a..824aaba 100644 --- a/R/makeRepo.R +++ b/R/makeRepo.R @@ -58,7 +58,7 @@ makeRepo <- function(pkgs, path, repos = getOption("repos"), type = "source", Rversion = R.version, download = TRUE, writePACKAGES = TRUE, quiet = FALSE) { if (!file.exists(path)) stop("Download path does not exist") - + downloaded <- lapply(type, function(t) { pkgPath <- repoBinPath(path = path, type = t, Rversion = Rversion) if (!file.exists(pkgPath)) { @@ -69,16 +69,16 @@ makeRepo <- function(pkgs, path, repos = getOption("repos"), type = "source", stop("Unable to create repo path: ", pkgPath) } } - + pdb <- pkgAvail(repos = repos, type = t, Rversion = Rversion) - + if (download) { download.packages(pkgs, destdir = pkgPath, available = pdb, repos = repos, - contriburl = contribUrl(repos, t, Rversion), - type = t, quiet = quiet) + contriburl = contribUrl(repos, t, Rversion), + type = t, quiet = quiet) } }) - + if (download) { downloaded <- downloaded[[1]][, 2] @@ -99,7 +99,7 @@ makeRepo <- function(pkgs, path, repos = getOption("repos"), type = "source", downloaded <- newPath } } - + if (writePACKAGES) updateRepoIndex(path = path, type = type, Rversion = Rversion) if (download) downloaded else character(0) } @@ -112,7 +112,7 @@ updateRepoIndex <- function(path, type = "source", Rversion = R.version) { n <- lapply(type, function(t) { pkgPath <- repoBinPath(path = path, type = t, Rversion = Rversion) if (grepl("mac.binary", t)) t <- "mac.binary" - tools::write_PACKAGES(dir = pkgPath, type = t) + write_packages(dir = pkgPath, type = t) }) names(n) <- type n diff --git a/R/mock-makeRepo.R b/R/mock-makeRepo.R index 07c9440..d361879 100644 --- a/R/mock-makeRepo.R +++ b/R/mock-makeRepo.R @@ -1,5 +1,32 @@ # These functions mock downloading of packages, for fast testing purposes only +is_mock_environment <- function()Sys.getenv("miniCRAN.mock.download") == TRUE +set_mock_environment <- function()Sys.setenv("miniCRAN.mock.download" = TRUE) +reset_mock_environment <- function()Sys.setenv("miniCRAN.mock.download" = FALSE) + + +# lib_in_tempdir <- function(lib){ +# np <- function(x)normalizePath(x, winslash = "/") +# grepl(np(tempdir()), np(lib)) +# } + +download.packages <- function(...){ + if (is_mock_environment()) { + mock.download.packages(...) + } else { + utils::download.packages(...) + } +} + +write_packages <- function(dir, type){ + if (is_mock_environment()) { + mock.write_packages(dir = dir, type = type) + } else { + tools::write_PACKAGES(dir = dir, type = type) + } +} + + #' @importFrom utils available.packages mock.download.packages <- function(pkgs, destdir, available, type, ...){ @@ -19,35 +46,63 @@ mock.download.packages <- function(pkgs, destdir, available, type, ...){ 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 +mock.write_packages <- function(dir, type = "source"){ + pattern <- ".tgz$|.zip$|.tar.gz$" + if (grepl("mac.binary", type)) type <- "mac.binary" + ff <- list.files(dir, 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(dir, "PACKAGES"), "wt") + write.dcf(db, con) + close(con) + con <- gzfile(file.path(dir, "PACKAGES.gz"), "wt") + write.dcf(db, con) + close(con) + rownames(db) <- db[, "Package"] + saveRDS(db, file.path(dir, "PACKAGES.rds"), compress = "xz") } - n <- sapply(type, do_one, USE.NAMES = TRUE, simplify = FALSE) - names(n) <- type - n + np } + + +# 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/tests/test-all.R b/tests/test-all.R index fd1af6a..a487af4 100644 --- a/tests/test-all.R +++ b/tests/test-all.R @@ -1,4 +1,9 @@ if (require(testthat)) { - source(file.path("testthat", "helpers.R")) + old.env <- Sys.getenv("miniCRAN.mock.download") + Sys.setenv("miniCRAN.mock.download" = TRUE) + test_check("miniCRAN") + + Sys.setenv("miniCRAN.mock.download" = old.env) + } diff --git a/tests/testthat/helpers.R b/tests/testthat/helpers.R index 93f818f..6b83488 100644 --- a/tests/testthat/helpers.R +++ b/tests/testthat/helpers.R @@ -41,38 +41,6 @@ skip_if_offline <- function(url = MRAN()){ } -mock.makeRepo <- function(...){ - mockery::stub(makeRepo, "download.packages", mock.download.packages) - mockery::stub(makeRepo, "updateRepoIndex", mock.updateRepoIndex) - makeRepo(...) -} - -mock.addPackage <- function(...){ - # mockery::stub(addPackage, "makeRepo", mock.makeRepo) - mockery::stub(addPackage, "download.packages", mock.download.packages, depth = 2) - mockery::stub(addPackage, "updateRepoIndex", mock.updateRepoIndex, depth = 2) - addPackage(...) -} - -mock.updatePackages <- function(...){ - mockery::stub(updatePackages, "download.packages", mock.download.packages, depth = 3) - mockery::stub(updatePackages, "updateRepoIndex", mock.updateRepoIndex, depth = 3) - updatePackages(...) -} - -mock.addLocalPackage <- function(...){ - mockery::stub(addLocalPackage, "updateRepoIndex", mock.updateRepoIndex) - addLocalPackage(...) -} - - -mock.addOldPackage <- function(...){ - mockery::stub(addOldPackage, "download.packages", mock.download.packages, depth = 2) - mockery::stub(addOldPackage, "updateRepoIndex", mock.updateRepoIndex, depth = 2) - addOldPackage(...) -} - - # Create sample repo from MRAN snapshot .createSampleRepo <- function(MRAN, path, pkgs, Rversion = "3.1"){ if (missing(MRAN)) MRAN <- MRAN("2014-10-15") @@ -87,21 +55,21 @@ mock.addOldPackage <- function(...){ pkgList_source <- pkgDep(pkgs, availPkgs = pdb_source, repos = MRAN, type = "source", suggests = FALSE, Rversion = Rversion) - mock.makeRepo(pkgList_source, path = path, repos = MRAN, + 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) - mock.makeRepo(pkgList_win, path = path, repos = MRAN, + 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) - mock.makeRepo(pkgList_mac, path = path, repos = MRAN, + 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 10b6011..dbd0e6c 100644 --- a/tests/testthat/test-3-makeRepo.R +++ b/tests/testthat/test-3-makeRepo.R @@ -29,9 +29,6 @@ for (pkg_type in (types)) { 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 97435b9..2a627e7 100644 --- a/tests/testthat/test-4-makeRepo-from-localCRAN.R +++ b/tests/testthat/test-4-makeRepo-from-localCRAN.R @@ -25,8 +25,6 @@ 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 3a3abf4..7fc2bf5 100644 --- a/tests/testthat/test-5-updateRepo.R +++ b/tests/testthat/test-5-updateRepo.R @@ -1,4 +1,5 @@ if (interactive()) {library(testthat); Sys.setenv(NOT_CRAN = "true")} +# set_mock_environment() context("updateRepo") @@ -58,9 +59,6 @@ for (pkg_type in names(types)) { 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, @@ -68,7 +66,7 @@ for (pkg_type in names(types)) { Rversion = rvers) prefix <- repoPrefix(pkg_type, Rversion = rvers) - mock.addPackage(pkgListAdd, path = repo_root, repos = revolution, type = pkg_type, + addPackage(pkgListAdd, path = repo_root, repos = revolution, type = pkg_type, quiet = TRUE, Rversion = rvers) expect_true( @@ -105,8 +103,6 @@ for (pkg_type in names(types)) { 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) @@ -114,7 +110,7 @@ for (pkg_type in names(types)) { on.exit(unlink(tmpdir, recursive = TRUE), add = TRUE) # get most recent version - res <- mock.download.packages( + res <- download.packages( pkgsAddLocal, destdir = tmpdir, type = pkg_type, available = pkgAvail(revolution, pkg_type, rvers), @@ -128,7 +124,7 @@ for (pkg_type in names(types)) { ) expect_equal(length(list.files(tmpdir)), 2) - mock.addLocalPackage(pkgs = pkgsAddLocal, pkgPath = tmpdir, path = repo_root, + addLocalPackage(pkgs = pkgsAddLocal, pkgPath = tmpdir, path = repo_root, type = pkg_type, quiet = TRUE, Rversion = rvers) prefix <- repoPrefix(pkg_type, Rversion = rvers) @@ -168,9 +164,6 @@ for (pkg_type in names(types)) { skip_on_cran() skip_if_offline(MRAN_mirror) - # mockery::stub(addPackage, "makeRepo", mock.makeRepo) - # mockery::stub(addPackage, "updateRepoIndex", mock.updateRepoIndex) - prefix <- repoPrefix(pkg_type, Rversion = rvers) suppressWarnings( @@ -192,11 +185,7 @@ for (pkg_type in names(types)) { ) ) - # mockery::stub(updatePackages, "download.packages", mock.download.packages, depth = 2) - # mockery::stub(updatePackages, "updateRepoIndex", mock.updateRepoIndex, depth = 2) - # # mockery::stub(updatePackages, "updateRepoIndex", mock.updateRepoIndex) - - mock.updatePackages(path = repo_root, repos = MRAN_mirror, type = pkg_type, + updatePackages(path = repo_root, repos = MRAN_mirror, type = pkg_type, ask = FALSE, quiet = TRUE, Rversion = rvers) updateVers <- getPkgVersFromFile( @@ -238,12 +227,12 @@ for (pkg_type in names(types)) { version = c("1.3-2")) if (pkg_type != "source") { expect_error( - mock.addOldPackage(oldVersions[["package"]], path = repo_root, + addOldPackage(oldVersions[["package"]], path = repo_root, vers = oldVersions[["version"]], repos = MRAN_mirror, type = pkg_type) ) } else { - mock.addOldPackage(oldVersions[["package"]], path = repo_root, + addOldPackage(oldVersions[["package"]], path = repo_root, vers = oldVersions[["version"]], repos = MRAN_mirror, type = pkg_type) files <- suppressWarnings(