File: test-status-bar.R

package info (click to toggle)
r-cran-cli 3.6.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,288 kB
  • sloc: ansic: 16,412; cpp: 37; sh: 13; makefile: 2
file content (334 lines) | stat: -rw-r--r-- 8,938 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
start_app()
on.exit(stop_app(), add = TRUE)

# We can't easily use snapshot tests here, because they don't capture \r

test_that("create and clear", {
  f <- function() {
    cli_status("* This is the current status")
    cli_status_clear()
  }
  out <- ansi_strip(capt0(f()))
  expect_match(out, "* This is the current status", fixed = TRUE)
  out <- ansi_strip(capt0(f()))
  expect_match(out, "* This is the current status", fixed = TRUE)
})

test_that("output while status bar is active", {
  withr::local_options(list(cli.ansi = FALSE, cli.dynamic = TRUE))
  f <- function() {
    cli_text("out1")
    sb <- cli_status("status1")
    cli_text("out2")
    cli_status_update("status2", id = sb)
  }
  out <- ansi_strip(capt0(f()))
  expect_equal(out, paste0(
    "out1\n",
    "\rstatus1\r",
    "\r       \rout2\nstatus1\r",
    "\rstatus2\r",
    "\r       \r"))
})

test_that("interpolation", {
  withr::local_options(list(cli.ansi = FALSE, cli.dynamic = TRUE))
  f <- function() {
    cli_div(theme = list("span.pkg" = list("before" = "{", after = "}")))
    cli_status("You see 1+1={1+1}, this is {.pkg cli}")
    cli_status_clear()
  }
  out <- ansi_strip(capt0(f()))
  expect_equal(out, paste0(
    "\rYou see 1+1=2, this is {cli}\r",
    "\r                            \r"))
})

test_that("update", {
  withr::local_options(list(cli.ansi = FALSE, cli.dynamic = TRUE))
  f <- function() {
    cli_text("out1")
    sb <- cli_status("status1")
    cli_status_update("status2", id = sb)
  }
  out <- ansi_strip(capt0(f()))
  expect_equal(out, paste0(
    "out1\n",
    "\rstatus1\r",
    "\rstatus2\r",
    "\r       \r"))
})

test_that("keep", {
  withr::local_options(list(cli.ansi = FALSE, cli.dynamic = TRUE))
  f <- function() {
    cli_status("* This is the current status", .keep = TRUE)
    cli_status_clear()
  }
  out <- ansi_strip(capt0(f()))
  expect_equal(out, "\r* This is the current status\r\n")
})

test_that("multiple status bars", {
  withr::local_options(list(cli.ansi = FALSE, cli.dynamic = TRUE))
  f <- function() {
    sb1 <- cli_status("status1")
    cli_text("text1")
    sb2 <- cli_status("status2")
    cli_text("text2")
    cli_status_clear(sb2)
    cli_text("text3")
  }
  out <- ansi_strip(capt0(f()))
  expect_equal(out, paste0(
    "\rstatus1\r",
    "\r       \rtext1\nstatus1\r",      # emit text1, restore status1
    "\rstatus2\r",                      # show status2
    "\r       \rtext2\nstatus2\r",      # emit text2, restore status2
    "\r       \rstatus1\r",             # clear status2, restore status1
    "\r       \rtext3\nstatus1\r",      # emit text3, restore status1
    "\r       \r"))                     # (auto)clear status1
})

test_that("truncating", {
  withr::local_options(list(
    cli.ansi = FALSE,
    cli.dynamic = TRUE,
    cli.unicode = FALSE
  ))
  f <- function() {
    withr::local_options(list(cli.width = 40))
    txt <- "Eiusmod enim mollit aute aliquip Lorem sunt cupidatat."
    cli_status(c(txt, txt))
  }
  out <- ansi_strip(capt0(f()))
  expect_equal(out, paste0(
    "\rEiusmod enim mollit aute aliquip Lore...\r",
    "\r                                        \r"))
})

test_that("ansi colors and clearing", {
  withr::local_options(list(
    cli.num_colors = 256L,
    cli.ansi = FALSE,
    cli.dynamic = TRUE
  ))
  f <- function() {
    withr::local_options(list(num_ansi_colors = 256L))
    cli_status(col_red("This is red"))
    cli_status_clear()
  }
  out <- capt0(f())
  expect_match(out, "\033[31m", fixed = TRUE)
  expect_match(out, "\r           \r", fixed = TRUE)
})

test_that("theming status bar", {
  f <- function() {
    cli_text("out1")
    sb <- cli_status("{.alert-info status1}")
    cli_text("out2")
    cli_status_update("status2", id = sb)
  }
  out <- ansi_strip(capt0(f()))
  out2 <- ansi_strip(capt0(cli_alert_info("status1")))
  expect_match(out, str_trim(out2), fixed = TRUE)
})

test_that("successful termination", {
  withr::local_options(list(cli.ansi = FALSE, cli.dynamic = TRUE))
  f <- function() {
    cli_text("out1")
    sb <- cli_status("status1")
    cli_text("out2")
    cli_status_clear(result = "done")
  }
  out <- ansi_strip(capt0(f()))
  expect_equal(out, paste0(
    "out1\n",
    "\rstatus1\r",
    "\r       \rout2\nstatus1\r",
    "\rstatus1 ... done\r\n"
  ))
})

test_that("terminate with failed", {
  withr::local_options(list(cli.ansi = FALSE, cli.dynamic = TRUE))
  f <- function() {
    cli_text("out1")
    sb <- cli_status("status1")
    cli_text("out2")
    cli_status_clear(result = "failed")
  }
  out <- ansi_strip(capt0(f()))
  expect_equal(out, paste0(
    "out1\n",
    "\rstatus1\r",
    "\r       \rout2\nstatus1\r",
    "\rstatus1 ... failed\r\n"
  ))
})

test_that("auto close with success", {
  withr::local_options(list(cli.ansi = FALSE, cli.dynamic = TRUE))
  f <- function() {
    cli_text("out1")
    sb <- cli_status("status1", .auto_result = "done")
    cli_text("out2")
  }
  out <- ansi_strip(capt0(f()))
  expect_equal(out, paste0(
    "out1\n",
    "\rstatus1\r",
    "\r       \rout2\nstatus1\r",
    "\rstatus1 ... done\r\n"
  ))
})

test_that("auto close wtih failure", {
  withr::local_options(list(cli.ansi = FALSE, cli.dynamic = TRUE))
  f <- function() {
    cli_text("out1")
    sb <- cli_status("status1", .auto_result = "failed")
    if (is_interactive()) Sys.sleep(2)
    cli_text("out2")
    if (is_interactive()) Sys.sleep(2)
  }
  out <- ansi_strip(capt0(f()))
  expect_equal(out, paste0(
    "out1\n",
    "\rstatus1\r",
    "\r       \rout2\nstatus1\r",
    "\rstatus1 ... failed\r\n"
  ))
})

test_that("auto close with styling", {
  f <- function() {
    cli_text("out1")
    sb <- cli_status(
      msg = "{.alert-info status1}",
      msg_done = "{.alert-success status1 ... done}",
      msg_failed = "{.alert-danger status1 ... failed}",
      .auto_result = "failed"
    )
    if (is_interactive()) Sys.sleep(1)
    cli_text("out2")
    if (is_interactive()) Sys.sleep(1)
  }
  out <- ansi_strip(capt0(f()))
  expect_match(out, "status1 ... failed", fixed = TRUE)

  f2 <- function() {
    cli_text("out1")
    sb <- cli_status(
      msg = "{.alert-info status1}",
      msg_done = "{.alert-success status1 ... done}",
      msg_failed = "{.alert-danger status1 ... failed}",
      .auto_result = "done"
    )
    if (is_interactive()) Sys.sleep(1)
    cli_text("out2")
    if (is_interactive()) Sys.sleep(1)
  }
  out2 <- ansi_strip(capt0(f2()))
  expect_match(out2, "status1 ... done", fixed = TRUE)
})

test_that("process auto close with success", {
  f <- function() {
    cli_text("out1")
    sb <- cli_process_start("status1", on_exit = "done")
    cli_text("out2")
  }
  out <- ansi_strip(capt0(f()))
  expect_match(out, "status1 ... done")
})

test_that("process auto close with failure", {
  f <- function() {
    cli_text("out1")
    sb <- cli_process_start("status1", on_exit = "failed")
    if (is_interactive()) Sys.sleep(2)
    cli_text("out2")
    if (is_interactive()) Sys.sleep(2)
  }
  out <- ansi_strip(capt0(f()))
  expect_match(out, "status1 ... failed")
})

test_that("Multiple spaces are no condensed in a status bar", {
  f <- function() {
    cli_status("* This  is the current  status")
    cli_status_clear()
  }
  out <- ansi_strip(capt0(f()))
  expect_match(out, "* This  is the current  status", fixed = TRUE)
  out <- ansi_strip(capt0(f()))
  expect_match(out, "* This  is the current  status", fixed = TRUE)
})

test_that("Emojis are cleaned up properly", {
  skip_on_os("windows")
  withr::local_options(list(cli.ansi = FALSE, cli.dynamic = TRUE))
  f <- function() {
    cli_text("out1")
    sb <- cli_status("\U0001F477")
    cli_text("out2")
    cli_status_update("\u2728", id = sb)
  }
  out <- ansi_strip(capt0(f()))
  exps <- c(
    paste0(
      "out1\n",
      "\r\U0001F477\r",
      "\r  \rout2\n\U0001F477\r",
      "\r\u2728\r",
      "\r  \r"),
    paste0(
      "out1\n",
      "\r<U+0001F477>\r",
      "\r  \rout2\n<U+0001F477>\r",
      "\r<U+2728>\r",
      "\r  \r")
  )
  expect_true(out %in% exps)
})

test_that("auto-close with done or failure", {
  withr::local_options(list(
    cli.ansi = FALSE,
    cli.dynamic = TRUE,
    cli.unicode = FALSE
  ))
  f <- function() {
    cli_text("out1")
    sb <- cli_process_start("status1")
    cli_text("out2")
  }
  out <- ansi_strip(capt0(f()))
  expect_match(out, "status1 ... done")

  out <- ansi_strip(capt0(f()))
  expect_match(out, "status1 ... done")

  # This fails on older R versions, only if f2() is tryCatch()-ed.
  if (getRversion() < "3.5.0") skip("Needs R 3.5.0")

  f2 <- function() {
    cli_text("out1")
    sb <- cli_process_start("status1")
    cli_text("out2")
    stop("oops")
  }

  out2 <- ansi_strip(capt0(tryCatch(f2(), error = function(err) NULL)))
  expect_match(out2, fixed = TRUE, paste0(
    "out1\n",
    "\ri status1\r",
    "\r         \r",
    "out2\n",
    "i status1\r",
    "\rx status1 ... failed\r\n"
  ))
})