From 158a7deaacf37ddce55d8cca5c77779977980ec9 Mon Sep 17 00:00:00 2001 From: Maximilian Muecke Date: Mon, 7 Oct 2024 17:01:24 +0200 Subject: [PATCH] tests: migrate to testthat mocking --- DESCRIPTION | 3 +- tests/testthat/test-compiler-flags.R | 6 +--- tests/testthat/test-exclude.R | 16 ++++------- tests/testthat/test-utils.R | 42 +++++----------------------- 4 files changed, 15 insertions(+), 52 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 8b9579c..4179d03 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,10 +25,9 @@ Suggests: covr, cpp11, knitr, - mockery, Rcpp, rmarkdown, - testthat (>= 3.0.0), + testthat (>= 3.2.0), withr (>= 2.3.0) Config/Needs/website: tidyverse/tidytemplate Config/testthat/edition: 3 diff --git a/tests/testthat/test-compiler-flags.R b/tests/testthat/test-compiler-flags.R index 2c69487..a1de580 100644 --- a/tests/testthat/test-compiler-flags.R +++ b/tests/testthat/test-compiler-flags.R @@ -1,10 +1,6 @@ test_that("has_compiler_colored_diagnostics", { - mockery::stub( - has_compiler_colored_diagnostics, - "cache_exists", - function(...) stop("nope") - ) + local_mocked_bindings(cache_exists = function(...) stop("nope")) withr::local_envvar(PKG_BUILD_COLOR_DIAGNOSTICS = "true") expect_true(has_compiler_colored_diagnostics()) diff --git a/tests/testthat/test-exclude.R b/tests/testthat/test-exclude.R index c8403ab..8fbea3b 100644 --- a/tests/testthat/test-exclude.R +++ b/tests/testthat/test-exclude.R @@ -70,7 +70,7 @@ test_that("exclusions", { }) test_that("get_copy_method", { - mockery::stub(get_copy_method, "is_windows", TRUE) + local_mocked_bindings(is_windows = function() TRUE) withr::local_options(pkg.build_copy_method = "link") expect_equal(get_copy_method(), "copy") @@ -81,7 +81,7 @@ test_that("get_copy_method", { expect_error(get_copy_method(), "It must be a string") withr::local_options(pkg.build_copy_method = NULL) - mockery::stub(get_copy_method, "desc::desc_get", "link") + local_mocked_bindings(desc_get = function(...) "link", .package = "desc") expect_equal(get_copy_method(), "copy") }) @@ -135,17 +135,13 @@ test_that("cp error", { }) test_that("detect_cp_args", { - mockery::stub( - detect_cp_args, - "processx::run", - function(...) stop("nope") + local_mocked_bindings( + run = function(...) stop("nope"), .package = "processx" ) expect_snapshot(detect_cp_args()) - mockery::stub( - detect_cp_args, - "processx::run", - function(f1, f2) file.create(f2) + local_mocked_bindings( + run = function(f1, f2) file.create(f2), .package = "processx" ) expect_snapshot(detect_cp_args()) }) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 42ce127..9acb8e3 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -38,11 +38,7 @@ test_that("isFALSE", { test_that("should_add_compiler_flags", { # should not be called if option is set - mockery::stub( - should_add_compiler_flags, - "makevars_user", - function() stop("dont") - ) + local_mocked_bindings(makevars_user = function() stop("dont")) # options is TRUE withr::local_options(pkg.build_extra_flags = TRUE) @@ -54,25 +50,13 @@ test_that("should_add_compiler_flags", { # depends on whether Makevars exists withr::local_options(pkg.build_extra_flags = "missing") - mockery::stub( - should_add_compiler_flags, - "makevars_user", - function() character() - ) + local_mocked_bindings(makevars_user = function() character()) expect_true(should_add_compiler_flags()) - mockery::stub( - should_add_compiler_flags, - "makevars_user", - function() "foobar" - ) + local_mocked_bindings(makevars_user = function() "foobar") withr::local_options(pkg.build_extra_flags = "missing") expect_false(should_add_compiler_flags()) - mockery::stub( - should_add_compiler_flags, - "makevars_user", - function() stop("dont") - ) + local_mocked_bindings(makevars_user = function() stop("dont")) withr::local_options(pkg.build_extra_flags = NULL) # env var true @@ -85,25 +69,13 @@ test_that("should_add_compiler_flags", { # depends on whether Makevars exists withr::local_envvar(PKG_BUILD_EXTRA_FLAGS = "missing") - mockery::stub( - should_add_compiler_flags, - "makevars_user", - function() character() - ) + local_mocked_bindings(makevars_user = function() character()) expect_true(should_add_compiler_flags()) - mockery::stub( - should_add_compiler_flags, - "makevars_user", - function() "foobar" - ) + local_mocked_bindings(makevars_user = function() "foobar") expect_false(should_add_compiler_flags()) # no option or env var, then TRUE - mockery::stub( - should_add_compiler_flags, - "makevars_user", - function() stop("dont") - ) + local_mocked_bindings(makevars_user = function() stop("dont")) withr::local_options(pkg.build_extra_flags = NULL) withr::local_envvar(PKG_BUILD_EXTRA_FLAGS = NA_character_) expect_true(should_add_compiler_flags())