tests/session-isolation.R

app_file <- function(name) {
  path <- system.file("app", name, package = "iSTATS")
  stopifnot(nzchar(path))
  path
}

# Recover the declared session bindings without evaluating shinyServer().
server_expressions <- parse(app_file("server.R"))
declaration <- server_expressions[[1L]]
stopifnot(is.call(declaration), identical(declaration[[1L]], as.name("<-")),
          identical(as.character(declaration[[2L]]),
                    ".istats_session_state_names"))
declared <- eval(declaration[[3L]], envir = baseenv())

root_binding <- function(lhs) {
  while (is.call(lhs) && as.character(lhs[[1L]]) %in% c("$", "[", "[["))
    lhs <- lhs[[2L]]
  if (is.name(lhs)) as.character(lhs) else NA_character_
}

superassignment_roots <- function(expression) {
  found <- character()
  if (is.call(expression)) {
    if (identical(expression[[1L]], as.name("<<-")))
      found <- root_binding(expression[[2L]])
    if (length(expression) > 1L)
      for (index in 2:length(expression))
        found <- c(found, superassignment_roots(expression[[index]]))
  }
  found
}

session_files <- c(
  "session_state.R", "functions.R", "uploader_server.R",
  "uploader_server_CSV.R", "uploader_server_Rdata.R",
  "plot_interativo_server.R", "select_signals_server.R",
  "Pretreatment_server.R", "stocsy_i_server.R", "stocsy_is_server.R",
  "stocsy_rt_server.R", "example_data_server.R")
used <- sort(unique(unlist(lapply(session_files, function(file) {
  expressions <- as.list(parse(app_file(file)))
  unlist(lapply(expressions, superassignment_roots), use.names = FALSE)
}), use.names = FALSE)))
stopifnot(!anyNA(used), !length(setdiff(used, declared)))

# Every legacy superassignment is intercepted by a session-owned parent.
session_parent <- new.env(parent = globalenv())
for (name in declared) assign(name, NULL, envir = session_parent)
session_code <- new.env(parent = session_parent)
probe_name <- ".istats_session_isolation_probe"
if (exists(probe_name, envir = .GlobalEnv, inherits = FALSE))
  rm(list = probe_name, envir = .GlobalEnv)
eval(call("<<-", as.name(probe_name), 1L), envir = session_code)
# An undeclared name demonstrates why the exhaustive declaration test matters.
stopifnot(exists(probe_name, envir = .GlobalEnv, inherits = FALSE))
rm(list = probe_name, envir = .GlobalEnv)
eval(call("<-", as.name(probe_name), NULL), envir = session_parent)
eval(call("<<-", as.name(probe_name), 2L), envir = session_code)
stopifnot(identical(get(probe_name, envir = session_parent), 2L),
          !exists(probe_name, envir = .GlobalEnv, inherits = FALSE))

# Closing the launcher restores both options and the search path. The package
# is detached only because this test deliberately starts from a detached state.
launcher <- getFromNamespace(".run_iSTATS_app", "iSTATS")
search_before <- search()
options(shiny.maxRequestSize = 12345)
if (exists(".Random.seed", envir = .GlobalEnv, inherits = FALSE))
  rm(list = ".Random.seed", envir = .GlobalEnv)
runner <- function(appDir, launch.browser) {
  stopifnot(dir.exists(appDir), isTRUE(launch.browser),
            "package:iSTATS" %in% search(),
            identical(getOption("shiny.maxRequestSize"), 500 * 1024^2))
  attach(list(temporary = TRUE), name = "package:istatsLeakProbe")
  runif(1)
  "closed"
}
stopifnot(identical(launcher(runner), "closed"),
          identical(search(), search_before),
          identical(getOption("shiny.maxRequestSize"), 12345),
          !exists(".Random.seed", envir = .GlobalEnv, inherits = FALSE))
options(shiny.maxRequestSize = NULL)

state_text <- paste(readLines(app_file("session_state.R"), warn = FALSE),
                    collapse = "\n")
stopifnot(!file.exists(file.path(dirname(app_file("session_state.R")), "global.R")),
          !grepl("\\.GlobalEnv|globalenv\\(", state_text),
          !grepl("rm\\s*\\(", state_text))

cat("PASS app session isolation: all superassignments contained; search path, option and user workspace preserved\n")

Try the iSTATS package in your browser

Any scripts or data that you put into this service are public.

iSTATS documentation built on Nov. 6, 2026, 1:07 a.m.