From 7c492bce99cf6533daef4331d7b0db8e6b630bd1 Mon Sep 17 00:00:00 2001 From: Maximilian Muecke Date: Mon, 7 Oct 2024 14:58:15 +0200 Subject: [PATCH] tests: migrate to testthat mocking --- DESCRIPTION | 1 - R/mocks.R | 4 + tests/async/test-synchronise.R | 2 +- tests/testthat/test-1-metadata-cache-1.R | 6 +- tests/testthat/test-1-metadata-cache-3.R | 8 +- tests/testthat/test-4-async-http.R | 6 +- tests/testthat/test-cache-dir.R | 16 ++- tests/testthat/test-platform.R | 18 ++-- tests/testthat/test-ppm.R | 118 ++++++++++------------- tests/testthat/test-repo-set.R | 38 ++++---- tests/testthat/test-utils.R | 4 +- 11 files changed, 104 insertions(+), 117 deletions(-) create mode 100644 R/mocks.R diff --git a/DESCRIPTION b/DESCRIPTION index 7da58af4..f0f076ca 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -28,7 +28,6 @@ Suggests: debugme, desc, fs, - mockery, pillar, pingr, rprojroot, diff --git a/R/mocks.R b/R/mocks.R new file mode 100644 index 00000000..07223d4a --- /dev/null +++ b/R/mocks.R @@ -0,0 +1,4 @@ +Sys.info <- NULL +getRversion <- NULL +interactive <- NULL +readline <- NULL diff --git a/tests/async/test-synchronise.R b/tests/async/test-synchronise.R index eee01e4c..886bef96 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(length = 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..5df45341 100644 --- a/tests/testthat/test-1-metadata-cache-1.R +++ b/tests/testthat/test-1-metadata-cache-1.R @@ -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") @@ -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") diff --git a/tests/testthat/test-1-metadata-cache-3.R b/tests/testthat/test-1-metadata-cache-3.R index b71c17d5..a4377a26 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,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") }) @@ -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") 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..62810ee2 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") } @@ -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)) }) diff --git a/tests/testthat/test-platform.R b/tests/testthat/test-platform.R index defbed1f..c654c170 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") + local_mocked_bindings( + 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..11a6e661 100644 --- a/tests/testthat/test-ppm.R +++ b/tests/testthat/test-ppm.R @@ -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", { @@ -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 @@ -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()) }) diff --git a/tests/testthat/test-repo-set.R b/tests/testthat/test-repo-set.R index 6c26e214..685931b0 100644 --- a/tests/testthat/test-repo-set.R +++ b/tests/testthat/test-repo-set.R @@ -71,27 +71,27 @@ 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" + ) + } + ) + + local_mocked_bindings(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,7 +185,7 @@ test_that("repo_sugar_ppm", { ) called <- FALSE - mockery::stub(repo_sugar_ppm, "synchronise", function(...) { + local_mocked_bindings(synchronise = function(...) { called <<- TRUE NULL }) @@ -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,7 +217,7 @@ test_that("parse_spec", { test_that("parse_spec_r", { called <- FALSE - mockery::stub(parse_spec_r, "get_r_versions", function(...) { + local_mocked_bindings(get_r_versions = function(...) { called <<- TRUE pkgenv$r_versions }) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 01097939..455a09fc 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -315,10 +315,10 @@ test_that("encode_path", { # 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")) })