Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace mockery with local_mocked_bindings. #118

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ Suggests:
debugme,
desc,
fs,
mockery,
pillar,
pingr,
rprojroot,
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export(repo_get)
export(repo_resolve)
export(repo_status)
export(with_repo)
if (getRversion() >= "4.0.0") importFrom(tools, R_user_dir)
importFrom(R6,R6Class)
importFrom(tools,file_ext)
importFrom(utils,URLencode)
Expand Down
6 changes: 5 additions & 1 deletion R/aaa-async.R
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ async_env <- new.env(parent = emptyenv())
async_env$loops <- list()

get_default_event_loop <- function() {
num_loops <- length(async_env$loops)
num_loops <- length2(async_env$loops)
if (num_loops == 0) {
err <- make_error(
"You can only call async functions from an async context",
Expand All @@ -321,6 +321,10 @@ get_default_event_loop <- function() {

async_env$loops[[num_loops]]
}
# Mockable version of length.
length2 <- function(x) {
length(x)
}

push_event_loop <- function() {
num_loops <- length(async_env$loops)
Expand Down
2 changes: 2 additions & 0 deletions R/cache-dirs.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ win_path_local <- function() {

if (getRversion() < "4.0.0") {
R_user_dir <- my_R_user_dir
} else {
R_user_dir <- tools::R_user_dir
}

cleanup_old_cache_dir <- function(force = FALSE) {
Expand Down
9 changes: 9 additions & 0 deletions R/mocks.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Define these objects so they can be mocked in tests.
interactive <- base::interactive
readline <- function(prompt = "") {
base::readline(prompt = prompt)
}
Sys.info <- function() {
base::Sys.info()
}
getRversion <- base::getRversion
2 changes: 1 addition & 1 deletion tests/async/test-synchronise.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

test_that("error if async function is called from sync context", {

mockery::stub(get_default_event_loop, "length", 0)
local_mocked_bindings(length2 = function(x) 0)
expect_error(
get_default_event_loop(),
class = "async_synchronization_barrier_error")
Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-1-metadata-cache-1.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,11 @@ test_that("locking failures", {

cmc <- cranlike_metadata_cache$new(pri, rep, "source", bioc = FALSE)

mockery::stub(cmc__load_primary_rds, "filelock::lock", function(...) NULL)
local_mocked_bindings(lock = function(...) NULL, .package = "filelock")
expect_error(
cmc__load_primary_rds(cmc, get_private(cmc), oneday()),
"Cannot acquire lock to copy RDS")

mockery::stub(cmc__load_primary_pkgs, "filelock::lock", function(...) NULL)
expect_error(
cmc__load_primary_pkgs(cmc, get_private(cmc), oneday()),
"Cannot acquire lock to copy PACKAGES")
Expand Down Expand Up @@ -291,7 +290,7 @@ test_that("update_primary 2", {
cmc <- cranlike_metadata_cache$new(pri, rep, c("macos", "source"),
bioc = FALSE)

mockery::stub(cmc__update_primary, "filelock::lock", function(...) NULL)
local_mocked_bindings(lock = function(...) NULL, .package = "filelock")
expect_error(
cmc__update_primary(cmc, get_private(cmc), TRUE, TRUE, TRUE),
"Cannot acquire lock to update primary cache")
Expand Down
10 changes: 6 additions & 4 deletions tests/testthat/test-1-metadata-cache-3.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ test_that("cmc__get_repos", {
})

test_that("cleanup", {
mockery::stub(cmc_cleanup, "interactive", FALSE)
local_mocked_bindings(interactive = function() FALSE)
expect_error(cmc_cleanup(NULL, NULL, FALSE), "Not cleaning up cache")
})

Expand All @@ -73,8 +73,10 @@ test_that("cleanup", {

cmc <- cranlike_metadata_cache$new(pri, rep, "source", bioc = FALSE)

mockery::stub(cmc_cleanup, "interactive", TRUE)
mockery::stub(cmc_cleanup, "readline", "")
local_mocked_bindings(
interactive = function() TRUE,
readline = function(...) ""
)
expect_error(cmc_cleanup(cmc, get_private(cmc), FALSE), "Aborted")
})

Expand Down Expand Up @@ -106,7 +108,7 @@ test_that("update_memory_cache", {
cmc <- cranlike_metadata_cache$new(pri, rep, c("macos", "source"),
bioc = FALSE)

mockery::stub(cmc__copy_to_replica, "filelock::lock", function(...) NULL)
local_mocked_bindings(lock = function(...) NULL, .package = "filelock")
expect_error(
cmc__copy_to_replica(cmc, get_private(cmc), TRUE, TRUE, TRUE),
"Cannot acquire lock to copy primary cache")
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-4-async-http.R
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,10 @@ test_that("update_async_timeouts", {
})

test_that("default_http_version", {
mockery::stub(default_http_version, "Sys.info", c(sysname = "Darwin"))
local_mocked_bindings(Sys.info = function() c(sysname = "Darwin"))
expect_equal(default_http_version(), 2)
mockery::stub(default_http_version, "Sys.info", c(sysname = "Linux"))
local_mocked_bindings(Sys.info = function() c(sysname = "Linux"))
expect_equal(default_http_version(), 2)
mockery::stub(default_http_version, "Sys.info", c(sysname = "Windows"))
local_mocked_bindings(Sys.info = function() c(sysname = "Windows"))
expect_equal(default_http_version(), 0)
})
21 changes: 10 additions & 11 deletions tests/testthat/test-cache-dir.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ test_that("error in R CMD check", {

test_that("fall back to R_USER_CACHE_DIR via R_user_dir()", {
args <- NULL
mockery::stub(
get_user_cache_dir,
"R_user_dir",
function(...) {
local_mocked_bindings(
R_user_dir = function(...) {
args <<- list(...)
stop("wait")
}
Expand All @@ -65,22 +63,23 @@ test_that("fall back to R_USER_CACHE_DIR via R_user_dir()", {
})

test_that("cleanup_old_cache_dir", {
tmp <- tempfile()
on.exit(unlink(tmp, recursive = TRUE), add = TRUE)
mockery::stub(cleanup_old_cache_dir, "user_cache_dir", function(...) tmp)
tmp <- withr::local_tempdir()
local_mocked_bindings(user_cache_dir = function(...) tmp)
expect_message(cleanup_old_cache_dir(), "nothing to do")

cachedir <- file.path(tmp, "R-pkg")
mkdirp(cachedir)
mockery::stub(cleanup_old_cache_dir, "interactive", FALSE)
local_mocked_bindings(interactive = function() FALSE)
expect_error(cleanup_old_cache_dir(), "non-interactive session")

mockery::stub(cleanup_old_cache_dir, "interactive", TRUE)
mockery::stub(cleanup_old_cache_dir, "readline", "n")
local_mocked_bindings(
interactive = function() TRUE,
readline = function(...) "n"
)
expect_error(cleanup_old_cache_dir(), "Aborted")

expect_true(file.exists(cachedir))
mockery::stub(cleanup_old_cache_dir, "readline", "y")
local_mocked_bindings(readline = function(...) "y")
expect_message(cleanup_old_cache_dir(), "Cleaned up cache")
expect_false(file.exists(cachedir))
})
18 changes: 9 additions & 9 deletions tests/testthat/test-platform.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@

test_that("current_r_platform_data", {
mockery::stub(current_r_platform_data, "get_platform", "x86_64-apple-darwin17.0")
local_mocked_bindings(get_platform = function(...) "x86_64-apple-darwin17.0")
expect_equal(current_r_platform_data()$platform, "x86_64-apple-darwin17.0")
})

test_that("default_platforms", {
mockery::stub(default_platforms, "current_r_platform", "macos")
local_mocked_bindings(current_r_platform = function() "macos")
expect_equal(default_platforms(), c("macos", "source"))

mockery::stub(default_platforms, "current_r_platform", "windows")
local_mocked_bindings(current_r_platform = function() "windows")
expect_equal(default_platforms(), c("windows", "source"))

mockery::stub(default_platforms, "current_r_platform", "source")
local_mocked_bindings(current_r_platform = function() "source")
expect_equal(default_platforms(), "source")
})

Expand Down Expand Up @@ -101,11 +101,11 @@ test_that("current_r_platform_data_linux", {
})

test_that("linux", {
mockery::stub(current_r_platform_data, "get_platform", "x86_64-pc-linux-gnu")
mockery::stub(
current_r_platform_data,
"current_r_platform_data_linux",
data.frame(stringsAsFactors = FALSE, x = "boo")
local_mocked_bindings(
get_platform = function(...) "x86_64-pc-linux-gnu",
current_r_platform_data_linux = function(...) {
data.frame(stringsAsFactors = FALSE, x = "boo")
}
)
expect_equal(current_r_platform_data()$platform, "boo")
})
Expand Down
116 changes: 53 additions & 63 deletions tests/testthat/test-ppm.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ test_that("ppm_snapshots", {
`2023-02-27T00:00:00Z` = "17028146",
`2023-02-28T00:00:00Z` = "17054670"
)
mockery::stub(
ppm_snapshots,
"async_get_ppm_versions",
function(...) async_constant(ver)
)
Comment on lines -34 to -38
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what this one is supposed to do. I don't see async_get_ppm_versions defined anywhere. Was this a leftover (that mockery didn't complain about)?

expect_snapshot(ppm_snapshots()[1:1000,])
})

Expand All @@ -49,19 +44,15 @@ test_that("ppm_platforms", {
binaries = c(TRUE, TRUE, TRUE)
), row.names = c(NA, 3L), class = "data.frame")

mockery::stub(
ppm_platforms,
"async_get_ppm_status",
function(...) async_constant(list(distros = plt))
local_mocked_bindings(
async_get_ppm_status = function(...) async_constant(list(distros = plt))
)
expect_snapshot(ppm_platforms())
})

test_that("async_get_ppm_status", {
mockery::stub(
async_get_ppm_status,
"download_file",
function(...) stop("nope")
local_mocked_bindings(
download_file = function(...) stop("nope")
)

# uses cache by default
Expand Down Expand Up @@ -143,81 +134,80 @@ test_that("ppm_has_binaries", {

test_that("ppm_has_binaries 2", {
withr::local_envvar(PKGCACHE_PPM_BINARIES = NA_character_)
mockery::stub(
ppm_has_binaries,
"current_r_platform_data",
structure(list(
cpu = "aarch64", vendor = "pc", os = "linux-gnu",
distribution = "ubuntu", release = "22.04",
platform = "aarch64-pc-linux-gnu-ubuntu-22.04"
), row.names = c(NA, -1L), class = "data.frame")
local_mocked_bindings(
current_r_platform_data = function(...) {
structure(list(
cpu = "aarch64", vendor = "pc", os = "linux-gnu",
distribution = "ubuntu", release = "22.04",
platform = "aarch64-pc-linux-gnu-ubuntu-22.04"
), row.names = c(NA, -1L), class = "data.frame")
}
)
expect_false(ppm_has_binaries())

mockery::stub(
ppm_has_binaries,
"current_r_platform_data",
structure(list(
cpu = "x86_64", vendor = "apple", os = "darwin20",
platform = "x86_64-apple-darwin20"
), row.names = c(NA, -1L), class = "data.frame")
local_mocked_bindings(
current_r_platform_data = function(...) {
structure(list(
cpu = "x86_64", vendor = "apple", os = "darwin20",
platform = "x86_64-apple-darwin20"
), row.names = c(NA, -1L), class = "data.frame")
}
)
expect_false(ppm_has_binaries())

# Use cached values, no HTTP
pkgenv$ppm_distros <- pkgenv$ppm_distros_cached
pkgenv$ppm_r_versions <- pkgenv$ppm_r_versions_cached
mockery::stub(ppm_has_binaries, "async_ppm_get_status", NULL)

# Windows
mockery::stub(
ppm_has_binaries,
"current_r_platform_data",
structure(list(
cpu = "x86_64", vendor = "w64", os = "mingw32",
platform = "x86_64-w64-mingw32"
), row.names = c(NA, -1L), class = "data.frame")
)
mockery::stub(ppm_has_binaries, "getRversion", "4.2.2")
local_mocked_bindings(
current_r_platform_data = function(...) {
structure(list(
cpu = "x86_64", vendor = "w64", os = "mingw32",
platform = "x86_64-w64-mingw32"
), row.names = c(NA, -1L), class = "data.frame")
},
getRversion = function() "4.2.2"
)
expect_true(ppm_has_binaries())

# Not supported Linux
mockery::stub(
ppm_has_binaries,
"current_r_platform_data",
structure(list(
cpu = "x86_64", vendor = "pc", os = "linux-gnu",
distribution = "ubuntu", release = "14.04",
platform = "x86_64-pc-linux-gnu-ubuntu-14.04"
), row.names = c(NA, -1L), class = "data.frame")
)
mockery::stub(ppm_has_binaries, "getRversion", "4.2.2")
local_mocked_bindings(
current_r_platform_data = function(...) {
structure(list(
cpu = "x86_64", vendor = "pc", os = "linux-gnu",
distribution = "ubuntu", release = "14.04",
platform = "x86_64-pc-linux-gnu-ubuntu-14.04"
), row.names = c(NA, -1L), class = "data.frame")
},
getRversion = function() "4.2.2"
)
expect_false(ppm_has_binaries())

# Supported Linux
mockery::stub(
ppm_has_binaries,
"current_r_platform_data",
structure(list(
cpu = "x86_64", vendor = "pc", os = "linux-gnu",
distribution = "ubuntu", release = "22.04",
platform = "x86_64-pc-linux-gnu-ubuntu-22.04"
), row.names = c(NA, -1L), class = "data.frame")
)
mockery::stub(ppm_has_binaries, "getRversion", "4.2.2")
local_mocked_bindings(
current_r_platform_data = function(...) {
structure(list(
cpu = "x86_64", vendor = "pc", os = "linux-gnu",
distribution = "ubuntu", release = "22.04",
platform = "x86_64-pc-linux-gnu-ubuntu-22.04"
), row.names = c(NA, -1L), class = "data.frame")
},
getRversion = function() "4.2.2"
)
expect_true(ppm_has_binaries())

# Not supported R version
mockery::stub(ppm_has_binaries, "getRversion", "1.0.0")
local_mocked_bindings(getRversion = function() "1.0.0")
expect_false(ppm_has_binaries())
})

test_that("ppm_r_versions", {
rver <- c("3.5", "3.6", "4.2")
mockery::stub(
ppm_r_versions,
"async_get_ppm_status",
function(...) async_constant(list(r_versions = rver))
local_mocked_bindings(
async_get_ppm_status = function(...) {
async_constant(list(r_versions = rver))
}
)
expect_snapshot(ppm_r_versions())
})
Loading
Loading