Skip to content

Commit 3dca27b

Browse files
committed
Merge commit 'aeaa1c7fb5d88efa0a59bbaf93dd16f88299e787'
2 parents 5762dfd + aeaa1c7 commit 3dca27b

File tree

8 files changed

+18
-152
lines changed

8 files changed

+18
-152
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# testthat (development version)
22

3+
4+
* `local_mock()` and `with_mock()` have been deprecated because they are no longer permitted in R 4.5.
5+
36
* `with_mock()` and `local_mock()` have been unconditionally deprecated as they will no longer work in future versions of R (#1999).
47
* `expect_condition()` and friends now include the `class` of the expected condition in the failure mesage, if used (#1987).
58
* `LANGUAGE` is now set to `"C"` in reprocucible environments (i.e.

R/mock.R

Lines changed: 2 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -21,116 +21,11 @@
2121
#' @return The result of the last unnamed parameter
2222
#' @export
2323
with_mock <- function(..., .env = topenv()) {
24-
lifecycle::deprecate_warn("3.3.0", "with_mock()", "with_mocked_bindings()")
25-
26-
dots <- eval(substitute(alist(...)))
27-
mock_qual_names <- names(dots)
28-
29-
if (all(mock_qual_names == "")) {
30-
warning(
31-
"Not mocking anything. Please use named parameters to specify the functions you want to mock.",
32-
call. = FALSE
33-
)
34-
code_pos <- rep(TRUE, length(dots))
35-
} else {
36-
code_pos <- (mock_qual_names == "")
37-
}
38-
code <- dots[code_pos]
39-
40-
mock_funs <- lapply(dots[!code_pos], eval, parent.frame())
41-
mocks <- extract_mocks(mock_funs, .env = .env)
42-
43-
on.exit(lapply(mocks, reset_mock), add = TRUE)
44-
lapply(mocks, set_mock)
45-
46-
# Evaluate the code
47-
if (length(code) > 0) {
48-
for (expression in code[-length(code)]) {
49-
eval(expression, parent.frame())
50-
}
51-
# Isolate last item for visibility
52-
eval(code[[length(code)]], parent.frame())
53-
}
24+
lifecycle::deprecate_stop("3.3.0", "with_mock()", "with_mocked_bindings()")
5425
}
5526

5627
#' @export
5728
#' @rdname with_mock
5829
local_mock <- function(..., .env = topenv(), .local_envir = parent.frame()) {
59-
lifecycle::deprecate_warn("3.3.0", "local_mock()", "local_mocked_bindings()")
60-
61-
mocks <- extract_mocks(list(...), .env = .env)
62-
on_exit <- bquote(
63-
on.exit(lapply(.(mocks), .(reset_mock)), add = TRUE),
64-
)
65-
66-
lapply(mocks, set_mock)
67-
eval_bare(on_exit, .local_envir)
68-
invisible()
69-
}
70-
71-
pkg_rx <- ".*[^:]"
72-
colons_rx <- "::(?:[:]?)"
73-
name_rx <- ".*"
74-
pkg_and_name_rx <- sprintf("^(?:(%s)%s)?(%s)$", pkg_rx, colons_rx, name_rx)
75-
76-
extract_mocks <- function(funs, .env) {
77-
if (is.environment(.env)) {
78-
.env <- environmentName(.env)
79-
}
80-
mock_qual_names <- names(funs)
81-
82-
lapply(
83-
stats::setNames(nm = mock_qual_names),
84-
function(qual_name) {
85-
pkg_name <- gsub(pkg_and_name_rx, "\\1", qual_name)
86-
87-
if (is_base_pkg(pkg_name)) {
88-
stop(
89-
"Can't mock functions in base packages (", pkg_name, ")",
90-
call. = FALSE
91-
)
92-
}
93-
94-
name <- gsub(pkg_and_name_rx, "\\2", qual_name)
95-
96-
if (pkg_name == "") {
97-
pkg_name <- .env
98-
}
99-
100-
env <- asNamespace(pkg_name)
101-
102-
if (!exists(name, envir = env, mode = "function")) {
103-
stop("Function ", name, " not found in environment ",
104-
environmentName(env), ".",
105-
call. = FALSE
106-
)
107-
}
108-
mock(name = name, env = env, new = funs[[qual_name]])
109-
}
110-
)
111-
}
112-
113-
mock <- function(name, env, new) {
114-
target_value <- get(name, envir = env, mode = "function")
115-
structure(
116-
list(
117-
env = env,
118-
name = as.name(name),
119-
orig_value = .Call(duplicate_, target_value), target_value = target_value,
120-
new_value = new
121-
),
122-
class = "mock"
123-
)
124-
}
125-
126-
set_mock <- function(mock) {
127-
.Call(reassign_function, mock$name, mock$env, mock$target_value, mock$new_value)
128-
}
129-
130-
reset_mock <- function(mock) {
131-
.Call(reassign_function, mock$name, mock$env, mock$target_value, mock$orig_value)
132-
}
133-
134-
is_base_pkg <- function(x) {
135-
x %in% rownames(utils::installed.packages(priority = "base"))
30+
lifecycle::deprecate_stop("3.3.0", "local_mock()", "local_mocked_bindings()")
13631
}

src/init.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@
44
#include <R_ext/Rdynload.h>
55

66
/* .Call calls */
7-
extern SEXP duplicate_(SEXP);
8-
extern SEXP reassign_function(SEXP, SEXP, SEXP, SEXP);
97
extern SEXP run_testthat_tests(SEXP);
108

119
static const R_CallMethodDef CallEntries[] = {
12-
{"duplicate_", (DL_FUNC) &duplicate_, 1},
13-
{"reassign_function", (DL_FUNC) &reassign_function, 4},
1410
{"run_testthat_tests", (DL_FUNC) &run_testthat_tests, 1},
1511
{NULL, NULL, 0}
1612
};

src/reassign.c

Lines changed: 0 additions & 24 deletions
This file was deleted.

tests/testthat/_snaps/mock.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
# deprecated
1+
# now defunct
22

33
Code
44
local_mock()
55
Condition
6-
Warning:
7-
`local_mock()` was deprecated in testthat 3.3.0.
6+
Error:
7+
! `local_mock()` was deprecated in testthat 3.3.0 and is now defunct.
88
i Please use `local_mocked_bindings()` instead.
9-
10-
---
11-
129
Code
1310
with_mock(is_testing = function() FALSE)
1411
Condition
15-
Warning:
16-
`with_mock()` was deprecated in testthat 3.3.0.
12+
Error:
13+
! `with_mock()` was deprecated in testthat 3.3.0 and is now defunct.
1714
i Please use `with_mocked_bindings()` instead.
1815

tests/testthat/test-examples.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
test_that("test_examples works with installed packages", {
2-
local_edition(2)
3-
4-
local_mock(test_rd = identity)
2+
local_mocked_bindings(test_rd = identity)
53
expect_true(length(test_examples()) > 1)
64
})
75

tests/testthat/test-mock.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
test_that("deprecated", {
2-
expect_snapshot(local_mock())
3-
expect_snapshot(with_mock(is_testing = function() FALSE))
1+
test_that("now defunct", {
2+
expect_snapshot(error = TRUE, {
3+
local_mock()
4+
with_mock(is_testing = function() FALSE)
5+
})
46
})

tests/testthat/test-reporter-debug.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
test_that("produces consistent output", {
22
withr::local_options(testthat.edition_ignore = TRUE)
33
local_edition(2)
4-
local_mock(
4+
local_mocked_bindings(
55
show_menu = function(choices, title = NULL) {
66
cat(paste0(format(seq_along(choices)), ": ", choices, sep = "\n"), "\n", sep = "")
77
0L
@@ -22,7 +22,7 @@ get_frame_from_debug_reporter <- function(choice, fun, envir = parent.frame()) {
2222
force(choice)
2323
test_debug_reporter_parent_frame <- NULL
2424

25-
with_mock(
25+
with_mocked_bindings(
2626
show_menu = function(choices, title = NULL) {
2727
# if (choice > 0) print(choices)
2828
my_choice <- choice
@@ -178,4 +178,3 @@ test_that("browser() is called for the correct frame for skips", {
178178
expect_equal(get_vars_from_debug_reporter(3, fun_3), "g")
179179
expect_equal(get_vars_from_debug_reporter(4, fun_3), character())
180180
})
181-

0 commit comments

Comments
 (0)