Skip to content

Commit 0cea43e

Browse files
authored
✨Use cli for all errors etc ✨ (#2185)
1 parent 00b3cc1 commit 0cea43e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+241
-240
lines changed

R/edition.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ edition_deprecate <- function(in_edition, what, instead = NULL) {
2727
return()
2828
}
2929

30-
warn(c(
31-
paste0("`", what, "` was deprecated in ", edition_name(in_edition), "."),
30+
cli::cli_warn(c(
31+
"{.code {what}} was deprecated in {edition_name(in_edition)}.",
3232
i = instead
3333
))
3434
}
@@ -40,7 +40,7 @@ edition_require <- function(in_edition, what) {
4040
return()
4141
}
4242

43-
stop(paste0("`", what, "` requires ", edition_name(in_edition), "."))
43+
cli::cli_abort("{.code {what}} requires {edition_name(in_edition)}.")
4444
}
4545

4646
edition_name <- function(x) {

R/expect-comparison.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ expect_compare_ <- function(
3939

4040
cmp <- op(act$val, exp$val)
4141
if (length(cmp) != 1 || !is.logical(cmp)) {
42-
abort(
43-
"Result of comparison must be a single logical value",
42+
cli::cli_abort(
43+
"Result of comparison must be a single logical value.",
4444
call = trace_env
4545
)
4646
}
@@ -112,14 +112,14 @@ expect_gte <- function(object, expected, label = NULL, expected.label = NULL) {
112112
#' @param ... All arguments passed on to `expect_lt()`/`expect_gt()`.
113113
#' @keywords internal
114114
expect_less_than <- function(...) {
115-
warning("Deprecated: please use `expect_lt()` instead", call. = FALSE)
115+
cli::cli_warn("Deprecated: please use {.fn expect_lt} instead.")
116116
expect_lt(...)
117117
}
118118

119119
#' @rdname expect_less_than
120120
#' @export
121121
expect_more_than <- function(...) {
122-
warning("Deprecated: please use `expect_gt()` instead", call. = FALSE)
122+
cli::cli_warn("Deprecated: please use {.fn expect_gt} instead.")
123123
expect_gt(...)
124124
}
125125

R/expect-condition.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ expect_warning <- function(
169169

170170
if (edition_get() >= 3) {
171171
if (!missing(all)) {
172-
warn("The `all` argument is deprecated")
172+
cli::cli_warn("The {.arg all} argument is deprecated.")
173173
}
174174

175175
expect_condition_matching_(

R/expect-known.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ expect_known_output <- function(
7979

8080
compare_file <- function(path, lines, ..., update = TRUE, info = NULL) {
8181
if (!file.exists(path)) {
82-
warning("Creating reference output", call. = FALSE)
82+
cli::cli_warn("Creating reference output.")
8383
brio::write_lines(lines, path)
8484
return(pass(NULL))
8585
}
@@ -88,11 +88,11 @@ compare_file <- function(path, lines, ..., update = TRUE, info = NULL) {
8888
if (update) {
8989
brio::write_lines(lines, path)
9090
if (!all_utf8(lines)) {
91-
warning("New reference output is not UTF-8 encoded", call. = FALSE)
91+
cli::cli_warn("New reference output is not UTF-8 encoded.")
9292
}
9393
}
9494
if (!all_utf8(old_lines)) {
95-
warning("Reference output is not UTF-8 encoded", call. = FALSE)
95+
cli::cli_warn("Reference output is not UTF-8 encoded.")
9696
}
9797

9898
comp <- waldo_compare(
@@ -178,7 +178,7 @@ expect_known_value <- function(
178178
act <- quasi_label(enquo(object), label)
179179

180180
if (!file.exists(file)) {
181-
warning("Creating reference value", call. = FALSE)
181+
cli::cli_warn("Creating reference value.")
182182
saveRDS(object, file, version = version)
183183
} else {
184184
ref_val <- readRDS(file)
@@ -232,7 +232,7 @@ expect_known_hash <- function(object, hash = NULL) {
232232
}
233233

234234
if (is.null(hash)) {
235-
warning(paste0("No recorded hash: use ", substr(act_hash, 1, 10)))
235+
cli::cli_warn("No recorded hash: use {substr(act_hash, 1, 10)}.")
236236
} else {
237237
if (hash != act_hash) {
238238
msg <- sprintf("Value hashes to %s, not %s", act_hash, hash)

R/expect-setequal.R

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -131,35 +131,6 @@ expect_in <- function(object, expected) {
131131

132132
# Helpers ----------------------------------------------------------------------
133133

134-
check_map_names <- function(
135-
x,
136-
error_arg = caller_arg(x),
137-
error_call = caller_env()
138-
) {
139-
nms <- names2(x)
140-
141-
if (anyDuplicated(nms)) {
142-
dups <- unique(nms[duplicated(nms)])
143-
cli::cli_abort(
144-
c(
145-
"All elements in {.arg {error_arg}} must have unique names.",
146-
x = "Duplicate names: {.str {dups}}"
147-
),
148-
call = error_call
149-
)
150-
}
151-
if (any(nms == "")) {
152-
empty <- which(nms == "")
153-
cli::cli_abort(
154-
c(
155-
"All elements in {.arg {error_arg}} must have names.",
156-
x = "Empty names at position{?s}: {empty}"
157-
),
158-
call = error_call
159-
)
160-
}
161-
}
162-
163134
check_vector <- function(
164135
x,
165136
error_arg = caller_arg(x),

R/expectation.R

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,9 @@ as.expectation.skip <- function(x, ..., srcref = NULL) {
189189

190190
#' @export
191191
as.expectation.default <- function(x, srcref = NULL) {
192-
stop(
193-
"Don't know how to convert '",
194-
paste(class(x), collapse = "', '"),
195-
"' to expectation.",
196-
call. = FALSE
192+
cli::cli_abort(
193+
"Don't know how to convert {.cls {class(x)}} to expectation.",
194+
call = NULL
197195
)
198196
}
199197

R/mock.R

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ with_mock <- function(..., .env = topenv()) {
2727
mock_qual_names <- names(dots)
2828

2929
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
30+
cli::cli_warn(
31+
c(
32+
"Not mocking anything.",
33+
"i" = "Please use named parameters to specify the functions you want to mock."
34+
)
3335
)
3436
code_pos <- rep(TRUE, length(dots))
3537
} else {
@@ -85,11 +87,8 @@ extract_mocks <- function(funs, .env) {
8587
pkg_name <- gsub(pkg_and_name_rx, "\\1", qual_name)
8688

8789
if (is_base_pkg(pkg_name)) {
88-
stop(
89-
"Can't mock functions in base packages (",
90-
pkg_name,
91-
")",
92-
call. = FALSE
90+
cli::cli_abort(
91+
"Can't mock functions in base package {.pkg {pkg_name}}."
9392
)
9493
}
9594

@@ -102,13 +101,8 @@ extract_mocks <- function(funs, .env) {
102101
env <- asNamespace(pkg_name)
103102

104103
if (!exists(name, envir = env, mode = "function")) {
105-
stop(
106-
"Function ",
107-
name,
108-
" not found in environment ",
109-
environmentName(env),
110-
".",
111-
call. = FALSE
104+
cli::cli_abort(
105+
"Function {.fn {name}} not found in environment {environmentName(env)}."
112106
)
113107
}
114108
mock(name = name, env = env, new = funs[[qual_name]])

R/mock2.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,18 +185,18 @@ local_bindings_rebind <- function(..., .env = .frame, .frame = caller_env()) {
185185
invisible()
186186
}
187187

188-
dev_package <- function() {
188+
dev_package <- function(call = caller_env()) {
189189
if (is_testing() && testing_package() != "") {
190190
testing_package()
191191
} else {
192192
loaded <- loadedNamespaces()
193193
is_dev <- map_lgl(loaded, function(x) !is.null(pkgload::dev_meta(x)))
194194
if (sum(is_dev) == 0) {
195-
cli::cli_abort("No packages loaded with pkgload")
195+
cli::cli_abort("No packages loaded with pkgload", call = call)
196196
} else if (sum(is_dev) == 1) {
197197
loaded[is_dev]
198198
} else {
199-
cli::cli_abort("Multiple packages loaded with pkgload")
199+
cli::cli_abort("Multiple packages loaded with pkgload", call = call)
200200
}
201201
}
202202
}

R/old-school.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,8 @@ throws_error <- function(regexp = NULL, ...) {
139139
#' @export
140140
#' @param amount maximum duration in seconds
141141
takes_less_than <- function(amount) {
142-
warning(
143-
"takes_less_than() is deprecated because it is stochastic and unreliable",
144-
call. = FALSE
142+
cli::cli_warn(
143+
"{.fn takes_less_than} is deprecated because it is stochastic and unreliable."
145144
)
146145

147146
function(expr) {
@@ -165,7 +164,7 @@ takes_less_than <- function(amount) {
165164
#' @keywords internal
166165
#' @export
167166
not <- function(f) {
168-
warning("`not()` is deprecated.", call. = FALSE)
167+
cli::cli_warn("{.fn not} is deprecated.")
169168
stopifnot(is.function(f))
170169

171170
negate <- function(expt) {

R/parallel-config.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ find_parallel <- function(path, load_package = "source", package = NULL) {
88
if (toupper(parenv) == "FALSE") {
99
return(FALSE)
1010
}
11-
abort("`TESTTHAT_PARALLEL` must be `TRUE` or `FALSE`")
11+
cli::cli_abort(
12+
"{.envvar TESTTHAT_PARALLEL} must be {.code TRUE} or {.code FALSE}."
13+
)
1214
}
1315

1416
# Make sure we get the local package package if not "installed"
@@ -28,7 +30,7 @@ find_parallel <- function(path, load_package = "source", package = NULL) {
2830
if (par) {
2931
ed <- as.integer(desc$get_field("Config/testthat/edition", default = 2L))
3032
if (ed < 3) {
31-
inform("Running tests in parallel requires the 3rd edition")
33+
cli::cli_inform("Running tests in parallel requires the 3rd edition.")
3234
par <- FALSE
3335
}
3436
}

0 commit comments

Comments
 (0)