Skip to content

Commit 0a02726

Browse files
committed
feat: prevent assignment to qenv.error
1 parent 4efca24 commit 0a02726

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Generated by roxygen2: do not edit by hand
22

33
S3method("$",qenv.error)
4+
S3method("$<-",qenv.error)
45
S3method("[[",qenv.error)
6+
S3method("[[<-",qenv.error)
57
S3method(within,qenv)
68
S3method(within,qenv.error)
79
export(concat)

R/qenv-get_var.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,14 @@ setMethod("[[", signature = c("qenv", "ANY"), function(x, i) {
7070
}
7171
result
7272
}
73+
74+
#' @export
75+
`[[<-.qenv.error` <- function(x, name, value) {
76+
stop(errorCondition(
77+
list(message = conditionMessage(x)),
78+
class = c("validation", "try-error", "simpleError")
79+
))
80+
}
81+
82+
#' @export
83+
`$<-.qenv.error` <- `[[<-.qenv.error`

tests/testthat/test-qenv_get_var.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
testthat::test_that("get_var, `$` and `[[`return error if object is qenv.error", {
1+
testthat::test_that("get_var, `$` and `[[` return error if object is qenv.error", {
22
q <- eval_code(qenv(), quote(x <- 1))
33
q <- eval_code(q, quote(y <- w * x))
44

@@ -53,3 +53,11 @@ testthat::test_that("get_var, `$` and `[[` only returns objects from qenv, not .
5353
testthat::expect_null(q[["iris"]])
5454
testthat::expect_null(q$iris)
5555
})
56+
57+
testthat::test_that("`$<-` and `[[<-` always return error", {
58+
q <- eval_code(qenv(), quote(x <- 1))
59+
q <- eval_code(q, quote(y <- w * x))
60+
61+
testthat::expect_error(q[["x2"]] <- 3, "when evaluating qenv code")
62+
testthat::expect_error(q$x2 <- 3, "when evaluating qenv code")
63+
})

0 commit comments

Comments
 (0)