Skip to content

Commit

Permalink
Mock without using mockery or testthat::with_mock
Browse files Browse the repository at this point in the history
  • Loading branch information
andrie committed Dec 27, 2017
1 parent d5e141f commit a3a0348
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 97 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ Imports:
utils,
XML,
igraph,
mockery(>= 0.4.0)
Suggests:
devtools,
knitr,
Expand Down
16 changes: 8 additions & 8 deletions R/makeRepo.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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]

Expand All @@ -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)
}
Expand All @@ -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
Expand Down
113 changes: 84 additions & 29 deletions R/mock-makeRepo.R
Original file line number Diff line number Diff line change
@@ -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, ...){
Expand All @@ -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
# }
#
7 changes: 6 additions & 1 deletion tests/test-all.R
Original file line number Diff line number Diff line change
@@ -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)

}
38 changes: 3 additions & 35 deletions tests/testthat/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
}
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-3-makeRepo.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-4-makeRepo-from-localCRAN.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 7 additions & 18 deletions tests/testthat/test-5-updateRepo.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
if (interactive()) {library(testthat); Sys.setenv(NOT_CRAN = "true")}
# set_mock_environment()

context("updateRepo")

Expand Down Expand Up @@ -58,17 +59,14 @@ 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,
suggests = FALSE,
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(
Expand Down Expand Up @@ -105,16 +103,14 @@ 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)
expect_true(dir.exists(tmpdir))
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),
Expand All @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit a3a0348

Please sign in to comment.