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 4fe21d2 commit 158a7de
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 52 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions tests/testthat/test-compiler-flags.R
Original file line number Diff line number Diff line change
@@ -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())
Expand Down
16 changes: 6 additions & 10 deletions tests/testthat/test-exclude.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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")
})

Expand Down Expand Up @@ -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())
})
Expand Down
42 changes: 7 additions & 35 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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())
Expand Down

0 comments on commit 158a7de

Please sign in to comment.