| 1 |
test_that("can access reactive values directly", { |
| 2 |
reactiveConsole(TRUE) |
| 3 |
on.exit(reactiveConsole(FALSE)) |
| 4 |
|
| 5 |
x1 <- reactiveVal(1) |
| 6 |
x1(2) |
| 7 |
expect_equal(x1(), 2) |
| 8 |
|
| 9 |
x2 <- reactiveValues(a = 1) |
| 10 |
x2$a <- 2 |
| 11 |
expect_equal(x2$a, 2) |
| 12 |
|
| 13 |
y <- reactive(x1() + x2$a) |
| 14 |
expect_equal(y(), 4) |
| 15 |
}) |
| 16 |
|
| 17 |
describe("srcfilealias in reactive labels", { |
| 18 |
# When a #line directive specifies a path that differs from the srcfilecopy |
| 19 |
# filename, R's parser wraps the srcfile in a srcfilealias whose $lines is |
| 20 |
# NULL. This is exactly what happens in sourceUTF8() when the normalized path |
| 21 |
# differs from the original. |
| 22 |
parse_as_srcfilealias <- function(user_code) { |
| 23 |
code <- c('#line 1 "/absolute/path/to/app.R"', user_code) |
| 24 |
src <- base::srcfilecopy("app.R", code, isFile = TRUE) |
| 25 |
exprs <- parse(text = code, keep.source = TRUE, srcfile = src) |
| 26 |
list(code = code, exprs = exprs, srcrefs = attr(exprs, "srcref")) |
| 27 |
} |
| 28 |
|
| 29 |
it("getSrcfileLines() resolves lines from srcfilealias", { |
| 30 |
parsed <- parse_as_srcfilealias("my_val <- reactiveVal(1)") |
| 31 |
|
| 32 |
srcref <- parsed$srcrefs[[1]] |
| 33 |
srcfile <- attr(srcref, "srcfile", exact = TRUE) |
| 34 |
|
| 35 |
expect_s3_class(srcfile, "srcfilealias") |
| 36 |
expect_null(srcfile$lines) |
| 37 |
|
| 38 |
result <- getSrcfileLines(srcfile, srcref) |
| 39 |
expect_false(is.null(result$lines)) |
| 40 |
expect_equal(result$lines, parsed$code) |
| 41 |
expect_match(result$lines[result$line_num], "my_val <- reactiveVal") |
| 42 |
}) |
| 43 |
|
| 44 |
it("getSrcfileLines() works with regular srcfile", { |
| 45 |
code <- c("x <- 1", "y <- 2") |
| 46 |
src <- base::srcfilecopy("test.R", code, isFile = TRUE) |
| 47 |
exprs <- parse(text = code, keep.source = TRUE, srcfile = src) |
| 48 |
|
| 49 |
srcref <- attr(exprs, "srcref")[[1]] |
| 50 |
srcfile <- attr(srcref, "srcfile", exact = TRUE) |
| 51 |
|
| 52 |
expect_false(inherits(srcfile, "srcfilealias")) |
| 53 |
|
| 54 |
result <- getSrcfileLines(srcfile, srcref) |
| 55 |
expect_equal(result$lines, code) |
| 56 |
expect_equal(result$line_num, 1L) |
| 57 |
}) |
| 58 |
|
| 59 |
it("rassignSrcrefToLabel() extracts label from srcfilealias", { |
| 60 |
parsed <- parse_as_srcfilealias("my_val <- reactiveVal(1)") |
| 61 |
srcref <- parsed$srcrefs[[1]] |
| 62 |
|
| 63 |
label <- rassignSrcrefToLabel(srcref, defaultLabel = "fallback") |
| 64 |
expect_equal(label, "my_val") |
| 65 |
}) |
| 66 |
|
| 67 |
it("rexprSrcrefToLabel() extracts label from srcfilealias", { |
| 68 |
parsed <- parse_as_srcfilealias("my_r <- reactive({ 1 + 1 })") |
| 69 |
|
| 70 |
# rexprSrcrefToLabel() expects the srcref of the reactive body (the { }), |
| 71 |
# not the entire assignment. This mirrors how exprToLabel() calls it with |
| 72 |
# the srcref from the body of the expression created by installExprFunction. |
| 73 |
assign_expr <- parsed$exprs[[1]] |
| 74 |
reactive_body <- assign_expr[[3]][[2]] # reactive( <body> ) |
| 75 |
body_srcrefs <- attr(reactive_body, "srcref") |
| 76 |
srcref <- body_srcrefs[[1]] |
| 77 |
|
| 78 |
label <- rexprSrcrefToLabel(srcref, defaultLabel = "fallback", fnName = "reactive") |
| 79 |
expect_equal(label, "my_r") |
| 80 |
}) |
| 81 |
}) |
| 82 |
|
| 83 |
test_that("sourceUTF8() auto-labels reactives despite srcfilealias", { |
| 84 |
# sourceUTF8() uses normalizePath() in its #line directive but the original |
| 85 |
# path for srcfilecopy. When these differ (e.g. macOS /tmp -> /private/tmp), |
| 86 |
# R creates a srcfilealias whose $lines is NULL. When they match (e.g. |
| 87 |
# Ubuntu), the #line directive still remaps line numbers. getSrcfileLines() |
| 88 |
# handles both cases by using srcref[7] (the pre-remap line number). |
| 89 |
tmp <- tempfile(fileext = ".R") |
| 90 |
on.exit(unlink(tmp), add = TRUE) |
| 91 |
|
| 92 |
reactiveConsole(TRUE) |
| 93 |
on.exit(reactiveConsole(FALSE), add = TRUE) |
| 94 |
|
| 95 |
writeLines(c( |
| 96 |
"my_val <- reactiveVal(1)", |
| 97 |
"my_react <- reactive({ my_val() + 1 })" |
| 98 |
), tmp) |
| 99 |
|
| 100 |
env <- new.env(parent = globalenv()) |
| 101 |
sourceUTF8(tmp, envir = env) |
| 102 |
|
| 103 |
# reactiveVal label (uses rassignSrcrefToLabel) |
| 104 |
rv_impl <- attr(env$my_val, ".impl", exact = TRUE) |
| 105 |
expect_equal( |
| 106 |
rv_impl$.__enclos_env__$private$label, |
| 107 |
"my_val" |
| 108 |
) |
| 109 |
|
| 110 |
# reactive label (uses rexprSrcrefToLabel via exprToLabel) |
| 111 |
r_observable <- attr(env$my_react, "observable", exact = TRUE) |
| 112 |
expect_equal(as.character(r_observable$.label), "my_react") |
| 113 |
}) |
| 114 |
|
| 115 |
describe("srcfilealias filename selection", { |
| 116 |
parse_as_srcfilealias <- function(user_code, alias_path = "/absolute/path/to/app.R") { |
| 117 |
code <- c(sprintf('#line 1 "%s"', alias_path), user_code) |
| 118 |
src <- base::srcfilecopy("app.R", code, isFile = TRUE) |
| 119 |
exprs <- parse(text = code, keep.source = TRUE, srcfile = src) |
| 120 |
list(code = code, exprs = exprs, srcrefs = attr(exprs, "srcref")) |
| 121 |
} |
| 122 |
|
| 123 |
it("getSrcfileFilename() prefers original unless package file", { |
| 124 |
lib <- normalizePath(.libPaths()[[1]], winslash = "/", mustWork = FALSE) |
| 125 |
pkg_path <- file.path(lib, "pkg", "R", "foo.R") |
| 126 |
|
| 127 |
parsed_pkg <- parse_as_srcfilealias("x <- 1", alias_path = pkg_path) |
| 128 |
srcref_pkg <- parsed_pkg$srcrefs[[1]] |
| 129 |
srcfile_pkg <- attr(srcref_pkg, "srcfile", exact = TRUE) |
| 130 |
expect_equal(getSrcfileFilename(srcfile_pkg), pkg_path) |
| 131 |
|
| 132 |
parsed_user <- parse_as_srcfilealias("y <- 2", alias_path = "/tmp/user.R") |
| 133 |
srcref_user <- parsed_user$srcrefs[[1]] |
| 134 |
srcfile_user <- attr(srcref_user, "srcfile", exact = TRUE) |
| 135 |
expect_equal(getSrcfileFilename(srcfile_user), "app.R") |
| 136 |
}) |
| 137 |
}) |
| 138 |
|
| 139 |
test_that("isPackageFile() uses path-boundary matching", { |
| 140 |
lib <- normalizePath(.libPaths()[[1]], winslash = "/", mustWork = FALSE) |
| 141 |
|
| 142 |
# A path like "{lib}Extra/foo.R" shares the prefix but is NOT inside the lib |
| 143 |
fake_path <- paste0(lib, "Extra/foo.R") |
| 144 |
expect_false(isPackageFile(fake_path)) |
| 145 |
|
| 146 |
# A path actually inside the library SHOULD match |
| 147 |
real_path <- file.path(lib, "pkg", "R", "foo.R") |
| 148 |
expect_true(isPackageFile(real_path)) |
| 149 |
}) |
| 150 |
|
| 151 |
test_that("errors in throttled/debounced reactives are catchable", { |
| 152 |
reactiveConsole(TRUE) |
| 153 |
on.exit(reactiveConsole(FALSE)) |
| 154 |
|
| 155 |
# In Shiny 1.7 and earlier, if a throttled/debounced reactive threw an error, |
| 156 |
# it would cause internal observers used by the implementations of |
| 157 |
# debounce/throttle to error, which would kill the session. The correct |
| 158 |
# behavior is to only expose the error to consumers of the throttled/debounced |
| 159 |
# reactive. |
| 160 |
|
| 161 |
r <- reactive({ |
| 162 |
stop("boom") |
| 163 |
}) |
| 164 |
|
| 165 |
rd <- r %>% debounce(1000) |
| 166 |
rt <- r %>% throttle(1000) |
| 167 |
|
| 168 |
observe({ |
| 169 |
try(rd(), silent = TRUE) |
| 170 |
try(rt(), silent = TRUE) |
| 171 |
}) |
| 172 |
|
| 173 |
expect_silent(flushReact()) |
| 174 |
}) |