diff --git a/DESCRIPTION b/DESCRIPTION index 0c858698..f09b99a9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -28,7 +28,6 @@ Suggests: debugme, desc, fs, - mockery, pillar, pingr, rprojroot, diff --git a/NAMESPACE b/NAMESPACE index dc0dded2..ee502fee 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/aaa-async.R b/R/aaa-async.R index ac3da725..e5fc7d1b 100644 --- a/R/aaa-async.R +++ b/R/aaa-async.R @@ -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", @@ -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) diff --git a/R/cache-dirs.R b/R/cache-dirs.R index 9bca8dd9..5b8880c2 100644 --- a/R/cache-dirs.R +++ b/R/cache-dirs.R @@ -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) { diff --git a/R/mocks.R b/R/mocks.R new file mode 100644 index 00000000..332b2ced --- /dev/null +++ b/R/mocks.R @@ -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 diff --git a/tests/async/test-synchronise.R b/tests/async/test-synchronise.R index eee01e4c..0a51e2f2 100644 --- a/tests/async/test-synchronise.R +++ b/tests/async/test-synchronise.R @@ -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") diff --git a/tests/testthat/test-1-metadata-cache-1.R b/tests/testthat/test-1-metadata-cache-1.R index be26aaec..617a93f0 100644 --- a/tests/testthat/test-1-metadata-cache-1.R +++ b/tests/testthat/test-1-metadata-cache-1.R @@ -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") @@ -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") diff --git a/tests/testthat/test-1-metadata-cache-3.R b/tests/testthat/test-1-metadata-cache-3.R index b71c17d5..c320fffa 100644 --- a/tests/testthat/test-1-metadata-cache-3.R +++ b/tests/testthat/test-1-metadata-cache-3.R @@ -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") }) @@ -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") }) @@ -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") diff --git a/tests/testthat/test-4-async-http.R b/tests/testthat/test-4-async-http.R index dc293206..305a8cdc 100644 --- a/tests/testthat/test-4-async-http.R +++ b/tests/testthat/test-4-async-http.R @@ -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) }) diff --git a/tests/testthat/test-cache-dir.R b/tests/testthat/test-cache-dir.R index 11e4099f..9e46e904 100644 --- a/tests/testthat/test-cache-dir.R +++ b/tests/testthat/test-cache-dir.R @@ -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") } @@ -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)) }) diff --git a/tests/testthat/test-platform.R b/tests/testthat/test-platform.R index defbed1f..6fc3dcdf 100644 --- a/tests/testthat/test-platform.R +++ b/tests/testthat/test-platform.R @@ -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") }) @@ -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") }) diff --git a/tests/testthat/test-ppm.R b/tests/testthat/test-ppm.R index c3dba96c..0f97aa1d 100644 --- a/tests/testthat/test-ppm.R +++ b/tests/testthat/test-ppm.R @@ -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) - ) expect_snapshot(ppm_snapshots()[1:1000,]) }) @@ -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 @@ -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()) }) diff --git a/tests/testthat/test-repo-set.R b/tests/testthat/test-repo-set.R index 6c26e214..caf7c45f 100644 --- a/tests/testthat/test-repo-set.R +++ b/tests/testthat/test-repo-set.R @@ -71,27 +71,26 @@ test_that("repo_resolve with PPM", { ) withr::local_options(repos = NULL) - mockery::stub( - repo_sugar_ppm, - "current_r_platform_data", - data.frame( - stringsAsFactors = FALSE, - cpu = "x86_64", - vendor = "pc", - os = "linux-gnu", - distribution = "ubuntu", - release = "22.04", - platform = "x86_64-pc-linux-gnu-ubuntu-22.04" - ) - ) - - mockery::stub(repo_sugar_ppm, "getRversion", "4.2.2") + local_mocked_bindings( + current_r_platform_data = function(...) { + data.frame( + stringsAsFactors = FALSE, + cpu = "x86_64", + vendor = "pc", + os = "linux-gnu", + distribution = "ubuntu", + release = "22.04", + platform = "x86_64-pc-linux-gnu-ubuntu-22.04" + ) + }, + getRversion = function() "4.2.2" + ) expect_equal( repo_sugar_ppm("PPM@2021-01-26", nm = NULL), c(CRAN = "https://packagemanager.posit.co/cran/__linux__/jammy/2021-01-26") ) - mockery::stub(repo_sugar_ppm, "getRversion", "1.0.0") + local_mocked_bindings(getRversion = function() "1.0.0") expect_equal( repo_sugar_ppm("PPM@2021-01-26", nm = NULL), c(CRAN = "https://packagemanager.posit.co/cran/2021-01-26") @@ -185,11 +184,12 @@ test_that("repo_sugar_ppm", { ) called <- FALSE - mockery::stub(repo_sugar_ppm, "synchronise", function(...) { - called <<- TRUE - NULL - }) - + local_mocked_bindings( + synchronise = function(...) { + called <<- TRUE + NULL + } + ) expect_error( repo_sugar_ppm(as.Date("2017-10-01"), NULL), "PPM snapshots go back to 2017-10-10" @@ -208,7 +208,7 @@ test_that("parse_spec", { as.Date("2019-11-19") ) - mockery::stub(parse_spec, "parse_spec_pkg", TRUE) + local_mocked_bindings(parse_spec_pkg = function(...) TRUE) expect_equal( parse_spec("dplyr-1.0.0"), TRUE @@ -217,10 +217,12 @@ test_that("parse_spec", { test_that("parse_spec_r", { called <- FALSE - mockery::stub(parse_spec_r, "get_r_versions", function(...) { - called <<- TRUE - pkgenv$r_versions - }) + local_mocked_bindings( + get_r_versions = function(...) { + called <<- TRUE + pkgenv$r_versions + } + ) expect_error( parse_spec_r("100.0.0"), "Unknown R version" diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 01097939..64f4e1c6 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -128,7 +128,7 @@ test_that("zip_vecs", { zip_vecs(1:2, 3:4, 5:6), list(c(1L, 3L, 5L), c(2L, 4L, 6L)) ) - + expect_equal( zip_vecs(1:2, 3L), list(c(1L, 3L), c(2L, 3L)) @@ -196,7 +196,7 @@ test_that("is_na_scalar", { NA ) for (c in good) expect_true(is_na_scalar(c), info = c) - + bad <- list( NULL, c(NA, NA), @@ -309,16 +309,16 @@ test_that("get_os_type", { }) test_that("encode_path", { - # To test this properly properlt, we would need to be able to create and + # To test this properly properly, we would need to be able to create and # delete files non-ascii names. But this is very buggy in base R, # so we do it with our own C code. In addition, we would also need to # craete file with names that are in the current locale, and are # supported by the file system. So it is a bit cumbersome to test this # currently.... - mockery::stub(encode_path, "get_os_type", "windows") + local_mocked_bindings(get_os_type = function() "windows") expect_silent(encode_path("G\u00e1bor")) - mockery::stub(encode_path, "get_os_type", "unix") + local_mocked_bindings(get_os_type = function() "unix") expect_silent(encode_path("G\u00e1bor")) })