Skip to content

Commit

Permalink
tests: migrate to testthat mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
m-muecke committed Oct 7, 2024
1 parent dc70ac1 commit 7c492bc
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 117 deletions.
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
4 changes: 4 additions & 0 deletions R/mocks.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Sys.info <- NULL
getRversion <- NULL
interactive <- NULL
readline <- NULL
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(length = function(x) 0)
expect_error(
get_default_event_loop(),
class = "async_synchronization_barrier_error")
Expand Down
6 changes: 3 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,12 @@ 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)
local_mocked_bindings(lock = function(...) NULL, .package = "filelock")
expect_error(
cmc__load_primary_pkgs(cmc, get_private(cmc), oneday()),
"Cannot acquire lock to copy PACKAGES")
Expand Down Expand Up @@ -291,7 +291,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
8 changes: 4 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,8 @@ 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)
local_mocked_bindings(readline = function(prompt) "")
expect_error(cmc_cleanup(cmc, get_private(cmc), FALSE), "Aborted")
})

Expand Down Expand Up @@ -106,7 +106,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)
})
16 changes: 7 additions & 9 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 @@ -67,20 +65,20 @@ 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)
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)
local_mocked_bindings(readline = function(prompt) "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(prompt) "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")
local_mocked_bindings(
current_r_platform_data_linux = function(...) {
data.frame(stringsAsFactors = FALSE, x = "boo")
}
)
expect_equal(current_r_platform_data()$platform, "boo")
})
Expand Down
118 changes: 52 additions & 66 deletions tests/testthat/test-ppm.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ 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)
)
expect_snapshot(ppm_snapshots()[1:1000,])
expect_snapshot(ppm_snapshots()[1:1000, ])
})

test_that("ppm_platforms", {
Expand All @@ -49,20 +44,14 @@ 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
pkgenv$ppm_distros <- NULL
Expand Down Expand Up @@ -143,81 +132,78 @@ 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")
}
)
local_mocked_bindings(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")
}
)
local_mocked_bindings(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")
}
)
local_mocked_bindings(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

0 comments on commit 7c492bc

Please sign in to comment.