Skip to content

Commit 92899e9

Browse files
authoredFeb 18, 2025··
Merge pull request #601 from yjunechoe/glue-safely
Fallback on failed glue in `label` and `brief`
2 parents 0d8bc28 + e3eebfc commit 92899e9

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed
 

‎R/steps_and_briefs.R

+10-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,16 @@ create_validation_step <- function(
116116
}
117117

118118
glue_chr <- function(x, glue_mask) {
119-
if (is.na(x)) return(x)
120-
as.character(glue::glue_data(glue_mask, x, .envir = NULL))
119+
# TODO: also skip autobriefs (#273)
120+
if (is.na(x) || !grepl("{", x, fixed = TRUE)) {
121+
return(x)
122+
}
123+
# Try glue and fallback to original string if failed
124+
out <- tryCatch(
125+
expr = as.character(glue::glue_data(glue_mask, x, .envir = NULL)),
126+
error = function(e) x
127+
)
128+
out
121129
}
122130

123131
get_hash_version <- function() {

‎tests/testthat/test-glue_label_brief.R

+33-8
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,19 @@ test_that("label supports glue syntax for {.seg_val} {.seg_col} {.step} {.col}",
6767
test_that("glue scope doesn't expose internal variables", {
6868

6969
# Ex: should not be able to access `columns` local variable in `col_vals_lt()`
70-
expect_error(create_agent(small_table) %>% col_vals_lt(c, 8, label = "{columns}"))
70+
expect_identical(
71+
create_agent(small_table) %>%
72+
col_vals_lt(c, 8, label = "{columns}") %>%
73+
{.$validation_set$label},
74+
"{columns}"
75+
)
7176
# Ex: should not be able to access `i` local variable in `create_validation_step()`
72-
expect_error(create_agent(small_table) %>% col_vals_lt(c, 8, label = "{i}"))
77+
expect_identical(
78+
create_agent(small_table) %>%
79+
col_vals_lt(c, 8, label = "{i}") %>%
80+
{.$validation_set$label},
81+
"{i}"
82+
)
7383

7484
# Should be able to access global vars/fns
7585
expect_equal(
@@ -83,23 +93,23 @@ test_that("glue scope doesn't expose internal variables", {
8393

8494
test_that("glue env searches from the caller env of the validation function", {
8595

86-
to_upper <- function(x) stop("Oh no!")
96+
to_upper <- function(x) "global"
8797

88-
expect_error(
98+
expect_identical(
8999
create_agent(small_table) %>%
90100
col_vals_lt(c, 8, label = "{to_upper(.col)}") %>%
91101
{.$validation_set$label},
92-
"Oh no!"
102+
"global"
93103
)
94104

95-
expect_equal(
105+
expect_identical(
96106
local({
97-
to_upper <- function(x) toupper(x)
107+
to_upper <- function(x) "local"
98108
create_agent(small_table) %>%
99109
col_vals_lt(c, 8, label = "{to_upper(.col)}") %>%
100110
{.$validation_set$label}
101111
}),
102-
"C"
112+
"local"
103113
)
104114

105115
})
@@ -281,3 +291,18 @@ test_that("glue works for brief too", {
281291
)
282292

283293
})
294+
295+
test_that("safe fallback when glue fails (#598)", {
296+
297+
expect_no_error({
298+
agent <- create_agent(small_table) %>%
299+
col_vals_regex(b, "^\\d{1,2}") %>%
300+
interrogate()
301+
})
302+
303+
expect_identical(
304+
agent$validation_set$brief,
305+
generate_autobriefs(agent, "b", NULL, "^\\d{1,2}", "col_vals_regex"),
306+
)
307+
308+
})

0 commit comments

Comments
 (0)
Please sign in to comment.