|
1 | 1 | #' Mock functions in a package. |
2 | 2 | #' |
3 | 3 | #' @description |
4 | | -#' `r lifecycle::badge("superseded")` |
5 | | -#' |
6 | | -#' `with_mock()` and `local_mock()` are superseded in favour of |
| 4 | +#' `with_mock()` and `local_mock()` are deprecated in favour of |
7 | 5 | #' [with_mocked_bindings()] and [local_mocked_bindings()]. |
8 | 6 | #' |
9 | | -#' These works by using some C code to temporarily modify the mocked function |
10 | | -#' _in place_. This is abusive of R's internals, which makes it dangerous, and |
11 | | -#' no longer recommended. |
| 7 | +#' These functions worked by using some C code to temporarily modify the mocked |
| 8 | +#' function _in place_. This was an abuse of R's internals and it is no longer |
| 9 | +#' permitted. |
12 | 10 | #' |
13 | 11 | #' @section 3rd edition: |
14 | 12 | #' `r lifecycle::badge("deprecated")` |
|
26 | 24 | #' @return The result of the last unnamed parameter |
27 | 25 | #' @export |
28 | 26 | with_mock <- function(..., .env = topenv()) { |
29 | | - edition_deprecate(3, "with_mock()", "Please use with_mocked_bindings() instead") |
30 | | - |
31 | | - dots <- eval(substitute(alist(...))) |
32 | | - mock_qual_names <- names(dots) |
33 | | - |
34 | | - if (all(mock_qual_names == "")) { |
35 | | - warning( |
36 | | - "Not mocking anything. Please use named parameters to specify the functions you want to mock.", |
37 | | - call. = FALSE |
38 | | - ) |
39 | | - code_pos <- rep(TRUE, length(dots)) |
40 | | - } else { |
41 | | - code_pos <- (mock_qual_names == "") |
42 | | - } |
43 | | - code <- dots[code_pos] |
44 | | - |
45 | | - mock_funs <- lapply(dots[!code_pos], eval, parent.frame()) |
46 | | - mocks <- extract_mocks(mock_funs, .env = .env) |
47 | | - |
48 | | - on.exit(lapply(mocks, reset_mock), add = TRUE) |
49 | | - lapply(mocks, set_mock) |
50 | | - |
51 | | - # Evaluate the code |
52 | | - if (length(code) > 0) { |
53 | | - for (expression in code[-length(code)]) { |
54 | | - eval(expression, parent.frame()) |
55 | | - } |
56 | | - # Isolate last item for visibility |
57 | | - eval(code[[length(code)]], parent.frame()) |
58 | | - } |
| 27 | + lifecycle::deprecate_stop("3.3.0", "with_mock()", "with_mocked_bindings()") |
59 | 28 | } |
60 | 29 |
|
61 | 30 | #' @export |
62 | 31 | #' @rdname with_mock |
63 | 32 | local_mock <- function(..., .env = topenv(), .local_envir = parent.frame()) { |
64 | | - edition_deprecate(3, "local_mock()", "Please use local_mocked_bindings() instead") |
65 | | - |
66 | | - mocks <- extract_mocks(list(...), .env = .env) |
67 | | - on_exit <- bquote( |
68 | | - on.exit(lapply(.(mocks), .(reset_mock)), add = TRUE), |
69 | | - ) |
70 | | - |
71 | | - lapply(mocks, set_mock) |
72 | | - eval_bare(on_exit, .local_envir) |
73 | | - invisible() |
74 | | -} |
75 | | - |
76 | | -pkg_rx <- ".*[^:]" |
77 | | -colons_rx <- "::(?:[:]?)" |
78 | | -name_rx <- ".*" |
79 | | -pkg_and_name_rx <- sprintf("^(?:(%s)%s)?(%s)$", pkg_rx, colons_rx, name_rx) |
80 | | - |
81 | | -extract_mocks <- function(funs, .env) { |
82 | | - if (is.environment(.env)) { |
83 | | - .env <- environmentName(.env) |
84 | | - } |
85 | | - mock_qual_names <- names(funs) |
86 | | - |
87 | | - lapply( |
88 | | - stats::setNames(nm = mock_qual_names), |
89 | | - function(qual_name) { |
90 | | - pkg_name <- gsub(pkg_and_name_rx, "\\1", qual_name) |
91 | | - |
92 | | - if (is_base_pkg(pkg_name)) { |
93 | | - stop( |
94 | | - "Can't mock functions in base packages (", pkg_name, ")", |
95 | | - call. = FALSE |
96 | | - ) |
97 | | - } |
98 | | - |
99 | | - name <- gsub(pkg_and_name_rx, "\\2", qual_name) |
100 | | - |
101 | | - if (pkg_name == "") { |
102 | | - pkg_name <- .env |
103 | | - } |
104 | | - |
105 | | - env <- asNamespace(pkg_name) |
106 | | - |
107 | | - if (!exists(name, envir = env, mode = "function")) { |
108 | | - stop("Function ", name, " not found in environment ", |
109 | | - environmentName(env), ".", |
110 | | - call. = FALSE |
111 | | - ) |
112 | | - } |
113 | | - mock(name = name, env = env, new = funs[[qual_name]]) |
114 | | - } |
115 | | - ) |
116 | | -} |
117 | | - |
118 | | -mock <- function(name, env, new) { |
119 | | - target_value <- get(name, envir = env, mode = "function") |
120 | | - structure( |
121 | | - list( |
122 | | - env = env, |
123 | | - name = as.name(name), |
124 | | - orig_value = .Call(duplicate_, target_value), target_value = target_value, |
125 | | - new_value = new |
126 | | - ), |
127 | | - class = "mock" |
128 | | - ) |
129 | | -} |
130 | | - |
131 | | -set_mock <- function(mock) { |
132 | | - .Call(reassign_function, mock$name, mock$env, mock$target_value, mock$new_value) |
133 | | -} |
134 | | - |
135 | | -reset_mock <- function(mock) { |
136 | | - .Call(reassign_function, mock$name, mock$env, mock$target_value, mock$orig_value) |
137 | | -} |
138 | | - |
139 | | -is_base_pkg <- function(x) { |
140 | | - x %in% rownames(utils::installed.packages(priority = "base")) |
| 33 | + lifecycle::deprecate_stop("3.3.0", "local_mock()", "local_mocked_bindings()") |
141 | 34 | } |
0 commit comments