Skip to content

Commit

Permalink
More mocking experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
andrie committed Dec 27, 2017
1 parent 9253f79 commit d5e141f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
2 changes: 1 addition & 1 deletion R/addPackages.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ addOldPackage <- function(pkgs = NULL, path = NULL, vers = NULL,
if (!file.exists(pkgPath)) dir.create(pkgPath, recursive = TRUE)

do_one <- function(x) {
result <- utils::download.file(x, destfile = file.path(pkgPath, basename(x)),
result <- download.file(x, destfile = file.path(pkgPath, basename(x)),
method = "auto", mode = "wb", quiet = quiet)
if (result != 0) warning("error downloading file ", x)
}
Expand Down
2 changes: 1 addition & 1 deletion R/updatePackages.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ oldPackages <- function(path = NULL, repos = getOption("repos"),
#'
#' @export
#'
updatePackages <- function(path = NULL, repos = getOption("repos"), method, ask = TRUE,
updatePackages <- function(path = NULL, repos = getOption("repos"), method = NULL, ask = TRUE,
availPkgs = pkgAvail(repos = repos, type = type, Rversion = Rversion),
oldPkgs = NULL, type = "source", Rversion = R.version, quiet = FALSE) {

Expand Down
2 changes: 1 addition & 1 deletion man/updatePackages.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions tests/testthat/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,32 @@ mock.makeRepo <- function(...){
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 Down
34 changes: 17 additions & 17 deletions tests/testthat/test-5-updateRepo.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ 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)
# mockery::stub(addPackage, "makeRepo", mock.makeRepo)
# mockery::stub(addPackage, "updateRepoIndex", mock.updateRepoIndex)

pkgListAdd <- pkgDep(pkgsAdd, availPkgs = pdb[[pkg_type]],
repos = revolution,
Expand All @@ -68,9 +68,9 @@ for (pkg_type in names(types)) {
Rversion = rvers)
prefix <- repoPrefix(pkg_type, Rversion = rvers)

addPackage(pkgListAdd, path = repo_root, repos = revolution, type = pkg_type,
mock.addPackage(pkgListAdd, path = repo_root, repos = revolution, type = pkg_type,
quiet = TRUE, Rversion = rvers)

expect_true(
.checkForRepoFiles(repo_root, pkgListAdd, prefix)
)
Expand Down Expand Up @@ -105,7 +105,7 @@ for (pkg_type in names(types)) {
skip_on_cran()
skip_if_offline(revolution)

mockery::stub(addLocalPackage, "updateRepoIndex", mock.updateRepoIndex)
# mockery::stub(addLocalPackage, "updateRepoIndex", mock.updateRepoIndex)

tmpdir <- file.path(tempdir(), "miniCRAN", "local", pkg_type)
expect_true(dir.create(tmpdir, recursive = TRUE, showWarnings = FALSE))
Expand All @@ -128,7 +128,7 @@ for (pkg_type in names(types)) {
)
expect_equal(length(list.files(tmpdir)), 2)

addLocalPackage(pkgs = pkgsAddLocal, pkgPath = tmpdir, path = repo_root,
mock.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 @@ -157,6 +157,7 @@ if (!is.online(MRAN_mirror, tryHttp = FALSE)) {
MRAN_mirror <- sub("^https://", "http://", revolution)
}

pkg_type <- names(types)[1]
for (pkg_type in names(types)) {
context(sprintf(" - Check for updates (%s)", pkg_type))

Expand All @@ -167,9 +168,9 @@ for (pkg_type in names(types)) {
skip_on_cran()
skip_if_offline(MRAN_mirror)

mockery::stub(updatePackages, "updateRepoIndex", mock.updateRepoIndex, depth = 2)
mockery::stub(updatePackages, "makeRepo", mock.makeRepo, depth = 2)

# mockery::stub(addPackage, "makeRepo", mock.makeRepo)
# mockery::stub(addPackage, "updateRepoIndex", mock.updateRepoIndex)
prefix <- repoPrefix(pkg_type, Rversion = rvers)

suppressWarnings(
Expand All @@ -190,8 +191,12 @@ for (pkg_type in names(types)) {
"timeSeries", "tis")
)
)

# mockery::stub(updatePackages, "download.packages", mock.download.packages, depth = 2)
# mockery::stub(updatePackages, "updateRepoIndex", mock.updateRepoIndex, depth = 2)
# # mockery::stub(updatePackages, "updateRepoIndex", mock.updateRepoIndex)

updatePackages(path = repo_root, repos = MRAN_mirror, type = pkg_type,
mock.updatePackages(path = repo_root, repos = MRAN_mirror, type = pkg_type,
ask = FALSE, quiet = TRUE, Rversion = rvers)

updateVers <- getPkgVersFromFile(
Expand Down Expand Up @@ -228,21 +233,17 @@ for (pkg_type in names(types)) {

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,
mock.addOldPackage(oldVersions[["package"]], path = repo_root,
vers = oldVersions[["version"]],
repos = MRAN_mirror, type = pkg_type)
)
} else {
addOldPackage(oldVersions[["package"]], path = repo_root,
mock.addOldPackage(oldVersions[["package"]], path = repo_root,
vers = oldVersions[["version"]],
repos = MRAN_mirror, type = pkg_type)
files <- suppressWarnings(
Expand All @@ -260,6 +261,5 @@ for (pkg_type in names(types)) {
)

}
})
})
}

0 comments on commit d5e141f

Please sign in to comment.