Skip to content

Commit 5b2c26f

Browse files
committed
rewrites test of prompts
1 parent daa0958 commit 5b2c26f

File tree

3 files changed

+216
-50
lines changed

3 files changed

+216
-50
lines changed

R/utils-prompt.R

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
check_approval <- function(
22
no_prompt = FALSE,
33
what = "Something",
4+
not_action = "approved",
45
see_more_at = NULL,
56
prompt_message = NULL,
6-
interactive_info = NULL # could use `{ what }` as used in `cli_inform()`
7+
interactive_info = NULL, # could use `{ what }` as used in `cli_inform()`
8+
.call = rlang::caller_env()
79
) {
810
if (no_prompt) return(TRUE)
911

1012
if (!is_interactive()) {
11-
cli::cli_abort(c(
12-
"{ what } requires explicit approval.",
13-
">" = "Set {.arg no_prompt = TRUE} if you agree.",
14-
if (!is.null(see_more_at)) {
15-
c(i = "See more at {.url {see_more_at}}")
16-
}
17-
))
13+
cli::cli_abort(
14+
c(
15+
"{ what } requires explicit approval.",
16+
">" = "Set {.arg no_prompt = TRUE} if you agree.",
17+
if (!is.null(see_more_at)) {
18+
c(i = "See more at {.url {see_more_at}}")
19+
}
20+
),
21+
call = .call
22+
)
1823
} else {
1924
if (!is.null(interactive_info)) {
2025
cli::cli_inform(interactive_info)
2126
}
2227
prompt_value <- tolower(readline(prompt_message))
2328
if (!prompt_value %in% c("", "y")) {
24-
cli::cli_inform(paste0(what, " not installed."))
29+
cli::cli_alert_info(paste0(what, " not {not_action}"))
30+
return(invisible(FALSE))
2531
}
26-
return(invisible(FALSE))
2732
}
2833
return(invisible(TRUE))
2934
}
@@ -46,6 +51,7 @@ check_extension_approval <- function(
4651
check_approval(
4752
no_prompt = no_prompt,
4853
what = what,
54+
not_action = "installed",
4955
see_more_at = see_more_at,
5056
prompt_message = prompt_message,
5157
interactive_info = interactive_info
@@ -65,6 +71,7 @@ check_removal_approval <- function(
6571
check_approval(
6672
no_prompt = no_prompt,
6773
what = what,
74+
not_action = "removed",
6875
see_more_at = see_more_at,
6976
prompt_message = prompt_message,
7077
interactive_info = NULL
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Checking non interactive approval
2+
3+
Code
4+
expect_true(check_approval(TRUE, "My thing"))
5+
6+
---
7+
8+
Code
9+
check_approval(FALSE, "My thing")
10+
Condition
11+
Error:
12+
! My thing requires explicit approval.
13+
> Set `no_prompt = TRUE` if you agree.
14+
15+
---
16+
17+
Code
18+
check_approval(FALSE, "My thing", see_more_at = "https://example.com")
19+
Condition
20+
Error:
21+
! My thing requires explicit approval.
22+
> Set `no_prompt = TRUE` if you agree.
23+
i See more at <https://example.com>
24+
25+
# Checking interactive approval with prompt mocked n
26+
27+
Code
28+
expect_false({
29+
check_approval(FALSE, "my-thing", see_more_at = "https://example.com")
30+
})
31+
Message
32+
i my-thing not approved
33+
34+
# Checking non interactive extension approval
35+
36+
Code
37+
expect_true(check_extension_approval(TRUE, "My thing"))
38+
39+
---
40+
41+
Code
42+
check_extension_approval(FALSE, "My thing")
43+
Condition
44+
Error in `check_extension_approval()`:
45+
! My thing requires explicit approval.
46+
> Set `no_prompt = TRUE` if you agree.
47+
48+
---
49+
50+
Code
51+
check_extension_approval(FALSE, "My thing", see_more_at = "https://example.com")
52+
Condition
53+
Error in `check_extension_approval()`:
54+
! My thing requires explicit approval.
55+
> Set `no_prompt = TRUE` if you agree.
56+
i See more at <https://example.com>
57+
58+
# Checking interactive extension approval with prompt mocked y
59+
60+
Code
61+
expect_true({
62+
check_extension_approval(FALSE, "my-thing")
63+
})
64+
Message
65+
my-thing may execute code when documents are rendered.
66+
* If you do not trust the author(s) of this my-thing, we recommend that you do not install or use this my-thing.
67+
68+
# Checking interactive extension approval with prompt mocked n
69+
70+
Code
71+
expect_false({
72+
check_extension_approval(FALSE, "my-thing")
73+
})
74+
Message
75+
my-thing may execute code when documents are rendered.
76+
* If you do not trust the author(s) of this my-thing, we recommend that you do not install or use this my-thing.
77+
i my-thing not installed
78+
79+
# Checking non interactive removal approval
80+
81+
Code
82+
expect_true(check_removal_approval(TRUE, "My thing"))
83+
84+
---
85+
86+
Code
87+
check_removal_approval(FALSE, "My thing")
88+
Condition
89+
Error in `check_removal_approval()`:
90+
! My thing requires explicit approval.
91+
> Set `no_prompt = TRUE` if you agree.
92+
93+
---
94+
95+
Code
96+
check_removal_approval(FALSE, "My thing", see_more_at = "https://example.com")
97+
Condition
98+
Error in `check_removal_approval()`:
99+
! My thing requires explicit approval.
100+
> Set `no_prompt = TRUE` if you agree.
101+
i See more at <https://example.com>
102+
103+
# Checking interactive removal approval with prompt mocked y
104+
105+
Code
106+
expect_true({
107+
check_removal_approval(FALSE, "my-thing")
108+
})
109+
110+
# Checking interactive removal approval with prompt mocked n
111+
112+
Code
113+
expect_false({
114+
check_removal_approval(FALSE, "my-thing")
115+
})
116+
Message
117+
i my-thing not removed
118+

tests/testthat/test-utils-prompt.R

Lines changed: 81 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,101 @@
1-
test_that("Checking extension with approval prompt mocked y", {
1+
test_that("Checking non interactive approval", {
2+
rlang::local_interactive(FALSE)
3+
expect_snapshot(expect_true(check_approval(TRUE, "My thing")))
4+
expect_snapshot(error = TRUE, {
5+
check_approval(FALSE, "My thing")
6+
})
7+
expect_snapshot(error = TRUE, {
8+
check_approval(FALSE, "My thing", see_more_at = "https://example.com")
9+
})
10+
})
11+
12+
13+
test_that("Checking interactive approval with prompt mocked y", {
214
local_mocked_bindings(
3-
readline = function(...) "y",
4-
is_interactive = function() TRUE
15+
readline = function(...) "y"
516
)
17+
rlang::local_interactive(TRUE)
618
expect_true({
7-
check_extension_approval(
8-
FALSE,
9-
"Quarto extensions",
10-
"https://quarto.org/docs/extensions/managing.html"
11-
)
19+
check_approval(FALSE, "my-thing")
1220
})
1321
})
1422

15-
test_that("Checking extension with approval prompt mocked n", {
23+
test_that("Checking interactive approval with prompt mocked n", {
1624
local_mocked_bindings(
17-
readline = function(...) "n",
18-
is_interactive = function() TRUE
25+
readline = function(...) "n"
1926
)
20-
expect_false({
27+
rlang::local_interactive(TRUE)
28+
expect_snapshot(expect_false({
29+
check_approval(FALSE, "my-thing", see_more_at = "https://example.com")
30+
}))
31+
})
32+
33+
test_that("Checking non interactive extension approval", {
34+
rlang::local_interactive(FALSE)
35+
expect_snapshot(expect_true(check_extension_approval(TRUE, "My thing")))
36+
expect_snapshot(error = TRUE, {
37+
check_extension_approval(FALSE, "My thing")
38+
})
39+
expect_snapshot(error = TRUE, {
2140
check_extension_approval(
2241
FALSE,
23-
"Quarto extensions",
24-
"https://quarto.org/docs/extensions/managing.html"
42+
"My thing",
43+
see_more_at = "https://example.com"
2544
)
2645
})
2746
})
2847

29-
test_that("Checking extension approval", {
30-
skip_if_no_quarto()
31-
skip_if_offline("github.com")
48+
test_that("Checking interactive extension approval with prompt mocked y", {
49+
local_mocked_bindings(
50+
readline = function(...) "y"
51+
)
52+
rlang::local_interactive(TRUE)
53+
expect_snapshot(expect_true({
54+
check_extension_approval(FALSE, "my-thing")
55+
}))
56+
})
3257

33-
expect_true(check_extension_approval(
34-
TRUE,
35-
"Quarto extensions",
36-
"https://quarto.org/docs/extensions/managing.html"
37-
))
38-
expect_true(check_extension_approval(
39-
TRUE,
40-
"Quarto templates",
41-
"https://quarto.org/docs/extensions/formats.html#distributing-formats"
42-
))
58+
test_that("Checking interactive extension approval with prompt mocked n", {
59+
local_mocked_bindings(
60+
readline = function(...) "n"
61+
)
62+
rlang::local_interactive(TRUE)
63+
expect_snapshot(expect_false({
64+
check_extension_approval(FALSE, "my-thing")
65+
}))
66+
})
4367

44-
expect_error({
45-
local_reproducible_output(rlang_interactive = FALSE)
46-
check_extension_approval(
47-
FALSE,
48-
"Quarto extensions",
49-
"https://quarto.org/docs/extensions/managing.html"
50-
)
68+
test_that("Checking non interactive removal approval", {
69+
rlang::local_interactive(FALSE)
70+
expect_snapshot(expect_true(check_removal_approval(TRUE, "My thing")))
71+
expect_snapshot(error = TRUE, {
72+
check_removal_approval(FALSE, "My thing")
5173
})
52-
expect_error({
53-
local_reproducible_output(rlang_interactive = FALSE)
54-
check_extension_approval(
55-
TRUE,
56-
"Quarto extensions",
57-
"https://quarto.org/docs/extensions/managing.html"
74+
expect_snapshot(error = TRUE, {
75+
check_removal_approval(
76+
FALSE,
77+
"My thing",
78+
see_more_at = "https://example.com"
5879
)
5980
})
6081
})
82+
83+
test_that("Checking interactive removal approval with prompt mocked y", {
84+
local_mocked_bindings(
85+
readline = function(...) "y"
86+
)
87+
rlang::local_interactive(TRUE)
88+
expect_snapshot(expect_true({
89+
check_removal_approval(FALSE, "my-thing")
90+
}))
91+
})
92+
93+
test_that("Checking interactive removal approval with prompt mocked n", {
94+
local_mocked_bindings(
95+
readline = function(...) "n"
96+
)
97+
rlang::local_interactive(TRUE)
98+
expect_snapshot(expect_false({
99+
check_removal_approval(FALSE, "my-thing")
100+
}))
101+
})

0 commit comments

Comments
 (0)