tests/testthat/test-runtime-versions.R

test_that("resolve_runtime_version returns the newest R, and the pin for the others, when unset", {
  local_mocked_bindings(r_portable_latest_version = function(platform = NULL) "4.6.3")
  cfg <- list(dependencies = list())
  expect_equal(resolve_runtime_version("r", cfg), "4.6.3")
  expect_equal(resolve_runtime_version("python", cfg), SHINYELECTRON_DEFAULTS$runtime_versions$python$version)
  expect_equal(resolve_runtime_version("electron", cfg), SHINYELECTRON_DEFAULTS$runtime_versions$electron)
})

test_that("resolve_runtime_version looks up the newest R when unset or 'latest'", {
  lookup <- mockery::mock("4.6.3", cycle = TRUE)
  local_mocked_bindings(r_portable_latest_version = lookup)

  expect_equal(resolve_runtime_version("r", list()), "4.6.3")
  expect_equal(
    resolve_runtime_version("r", list(dependencies = list(r = list(version = "latest"))),
                            platform = "win"),
    "4.6.3"
  )
  # The platform reaches the lookup.
  expect_equal(mockery::mock_args(lookup), list(list(NULL), list("win")))
})

test_that("resolve_runtime_version honors an explicit R version without a lookup", {
  local_mocked_bindings(r_portable_latest_version = function(...) stop("must not be called"))
  cfg <- list(dependencies = list(r = list(version = "4.5.3")))
  expect_equal(resolve_runtime_version("r", cfg), "4.5.3")
})

test_that("resolve_runtime_version 'latest' calls the live resolver for electron", {
  skip_if_not_installed("mockery")
  cfg <- list(dependencies = list(electron = list(version = "latest")))
  mockery::stub(resolve_runtime_version, "electron_latest_version", function() "99.9.9")
  expect_equal(resolve_runtime_version("electron", cfg), "99.9.9")
})

test_that("electron_latest_version returns a version string from the registry", {
  skip_if_not_installed("mockery")
  fake_json <- '{"version": "41.0.0"}'
  fake_tf <- tempfile(fileext = ".json")
  withr::defer(unlink(fake_tf))
  writeLines(fake_json, fake_tf)
  mockery::stub(electron_latest_version, "utils::download.file", function(url, dest, ...) {
    file.copy(fake_tf, dest)
    invisible(0L)
  })
  result <- electron_latest_version()
  expect_equal(result, "41.0.0")
})

test_that("the maintained pins are the current ones", {
  pins <- SHINYELECTRON_DEFAULTS$runtime_versions
  expect_equal(pins$r, "4.6.2")
  expect_equal(pins$python, list(version = "3.14.10", release = "20261124"))
  expect_equal(pins$electron, "45.5.0")
  expect_equal(SHINYELECTRON_DEFAULTS$electron_toolchain,
               list(builder = "26.18.0", updater = "6.8.11", log = "5.4.4"))
  expect_equal(NODE_MIN_VERSION, "22.12.0")
})

electron_config <- function(version) {
  list(dependencies = list(electron = list(version = version)))
}

test_that("an Electron more than two majors behind is a problem", {
  local_mocked_bindings(electron_latest_version = function() "45.5.0")

  expect_match(electron_support_problem(electron_config("41.0.0")), "41", fixed = TRUE)
  expect_match(electron_support_problem(electron_config("42.11.18")), "43", fixed = TRUE)
  expect_null(electron_support_problem(electron_config("43.0.0")))
  expect_null(electron_support_problem(electron_config("45.5.0")))
  expect_null(electron_support_problem(list()))
})

test_that("'latest' is never looked up", {
  local_mocked_bindings(electron_latest_version = function() stop("must not be called"))

  expect_null(electron_support_problem(electron_config("latest")))
})

test_that("a version that is not a number is not a problem", {
  local_mocked_bindings(electron_latest_version = function() "45.5.0")

  expect_null(electron_support_problem(electron_config("lates")))
})

test_that("a failed lookup is silent", {
  local_mocked_bindings(electron_latest_version = function() cli::cli_abort("offline"))

  expect_no_condition(res <- electron_support_problem(electron_config("41.0.0")))
  expect_null(res)
})

test_that("the lookup timeout is five seconds and is restored", {
  withr::local_options(timeout = 60)
  seen <- mockery::mock("45.5.0")
  local_mocked_bindings(electron_latest_version = function() {
    seen(getOption("timeout"))
  })

  electron_support_problem(electron_config("41.0.0"))

  expect_equal(mockery::mock_args(seen)[[1]][[1]], 5)
  expect_equal(getOption("timeout"), 60)
})

test_that("the warning carries its class", {
  local_mocked_bindings(electron_latest_version = function() "45.5.0")

  expect_warning(warn_if_electron_unsupported(electron_config("41.0.0")),
                 class = "shinyelectron_electron_unsupported")
  expect_no_warning(warn_if_electron_unsupported(electron_config("45.5.0")))
})

test_that("a lookup that warns before it fails is silent", {
  local_mocked_bindings(electron_latest_version = function() {
    warning("URL 'https://registry.npmjs.org/electron/latest': status was 'Could not resolve host'")
    stop("cannot open URL")
  })

  expect_no_condition(res <- electron_support_problem(electron_config("41.0.0")))
  expect_null(res)
})

# --- The newest portable R ---

# The headers GitHub answers a request for the latest release of a repository
# with, its location header named `name`.
latest_release_headers <- function(name = "location") {
  c(
    "HTTP/2 302 \r\n",
    paste0(name, ": https://github.com/portable-r/portable-r-macos/releases/tag/v4.6.2\r\n"),
    "\r\n"
  )
}

test_that("github_latest_release_tag reads the tag the latest release redirects to", {
  seen <- mockery::mock(latest_release_headers())
  mockery::stub(github_latest_release_tag, "curlGetHeaders", seen)

  expect_equal(github_latest_release_tag("portable-r/portable-r-macos"), "v4.6.2")
  expect_equal(
    mockery::mock_args(seen)[[1]],
    list("https://github.com/portable-r/portable-r-macos/releases/latest",
         redirect = FALSE, timeout = 5)
  )
})

test_that("github_latest_release_tag matches the header name in any letter case", {
  for (name in c("Location", "LOCATION")) {
    lookup <- github_latest_release_tag
    mockery::stub(lookup, "curlGetHeaders", function(...) latest_release_headers(name))
    expect_equal(lookup("portable-r/portable-r-macos"), "v4.6.2")
  }
})

test_that("github_latest_release_tag gives NULL, silently, when it finds no tag", {
  answers <- list(
    throws = function(...) stop("Could not resolve host: github.com"),
    warns = function(...) {
      warning("Operation timed out")
      latest_release_headers()
    },
    no_header = function(...) c("HTTP/2 404 \r\n", "content-type: text/html\r\n", "\r\n"),
    no_release = function(...) c(
      "HTTP/2 302 \r\n",
      "location: https://github.com/portable-r/portable-r-macos/releases\r\n",
      "\r\n"
    )
  )
  for (answer in answers) {
    lookup <- github_latest_release_tag
    mockery::stub(lookup, "curlGetHeaders", answer)
    expect_no_condition(tag <- lookup("portable-r/portable-r-macos"))
    expect_null(tag)
  }
})

test_that("r_portable_latest_version gives the newest portable R for the platform", {
  lookup <- mockery::mock("v4.6.2", cycle = TRUE)
  local_mocked_bindings(github_latest_release_tag = lookup)

  expect_no_message(expect_equal(r_portable_latest_version("mac"), "4.6.2"))
  expect_no_message(expect_equal(r_portable_latest_version("win"), "4.6.2"))
  expect_equal(
    mockery::mock_args(lookup),
    list(list("portable-r/portable-r-macos"), list("portable-r/portable-r-windows"))
  )
})

test_that("r_portable_latest_version falls back to the pin, and says so once", {
  pin <- SHINYELECTRON_DEFAULTS$runtime_versions$r
  for (tag in list(NULL, "not-a-version")) {
    local_mocked_bindings(github_latest_release_tag = function(...) tag)

    expect_message(r_portable_latest_version("mac"),
                   class = "shinyelectron_r_version_fallback")
    messages <- capture_messages(version <- r_portable_latest_version("mac"))
    expect_equal(version, pin)
    expect_length(messages, 1)
    expect_match(messages, pin, fixed = TRUE)
  }
})

test_that("r_portable_latest_version asks r-hub for other platforms, with the same fallback", {
  local_mocked_bindings(
    github_latest_release_tag = function(...) stop("must not be called"),
    r_latest_version = function() "4.6.2"
  )
  expect_no_message(expect_equal(r_portable_latest_version("linux"), "4.6.2"))

  # readLines() warns before it fails.
  local_mocked_bindings(r_latest_version = function() {
    warning("cannot open URL 'https://api.r-hub.io/rversions/r-release'")
    cli::cli_abort("Failed to query latest R version")
  })
  expect_no_warning(
    expect_message(version <- r_portable_latest_version("linux"),
                   class = "shinyelectron_r_version_fallback")
  )
  expect_equal(version, SHINYELECTRON_DEFAULTS$runtime_versions$r)
})

test_that("the r-hub lookup has five seconds, and the timeout is restored", {
  withr::local_options(timeout = 60)
  seen <- mockery::mock("4.6.2")
  local_mocked_bindings(r_latest_version = function() seen(getOption("timeout")))

  r_portable_latest_version("linux")

  expect_equal(mockery::mock_args(seen)[[1]][[1]], 5)
  expect_equal(getOption("timeout"), 60)
})

# --- Exports look up the newest R once ---

# An R app in a temporary directory with the given build settings.
local_r_app <- function(build, env = parent.frame()) {
  appdir <- withr::local_tempdir(.local_envir = env)
  writeLines("library(shiny)\nshinyApp(fluidPage(), function(input, output) {})",
             fs::path(appdir, "app.R"))
  yaml::write_yaml(list(build = build), fs::path(appdir, "_shinyelectron.yml"))
  appdir
}

# Build on a Mac for itself, without reading the app's packages.
local_mac_export <- function(env = parent.frame()) {
  local_no_electron_lookup(env)
  testthat::local_mocked_bindings(
    detect_current_platform = function() "mac",
    detect_current_arch = function() "arm64",
    resolve_app_dependencies = function(...) NULL,
    validate_container_available = function(...) "docker",
    .env = env
  )
}

test_that("a bundled or container export looks up R once and builds with that version", {
  local_mac_export()
  for (strategy in c("bundled", "container")) {
    lookup <- mockery::mock("v4.6.2")
    build_rec <- mockery::mock(tempdir())
    local_mocked_bindings(github_latest_release_tag = lookup,
                          build_electron_app = build_rec)

    export(local_r_app(list(runtime_strategy = strategy)), withr::local_tempdir(),
           overwrite = TRUE, verbose = FALSE)

    mockery::expect_called(lookup, 1)
    expect_equal(mockery::mock_args(lookup)[[1]][[1]], "portable-r/portable-r-macos")
    config <- mockery::mock_args(build_rec)[[1]]$config
    expect_equal(config$dependencies$r$version, "4.6.2")
  }
})

test_that("an auto-download export looks up R once and writes that version", {
  local_mac_export()
  lookup <- mockery::mock("v4.6.2")
  manifest_rec <- mockery::mock()
  local_mocked_bindings(github_latest_release_tag = lookup,
                        write_runtime_manifest = manifest_rec)

  export(local_r_app(list(runtime_strategy = "auto-download")), withr::local_tempdir(),
         build = FALSE, overwrite = TRUE, verbose = FALSE)

  mockery::expect_called(lookup, 1)
  # write_runtime_manifest(app_dir, app_type, platform, arch, config, ...)
  config <- mockery::mock_args(manifest_rec)[[1]][[5]]
  expect_equal(config$dependencies$r$version, "4.6.2")
})

test_that("a suite export looks up R once for its R apps", {
  local_mac_export()
  suite <- withr::local_tempdir()
  for (id in c("one", "two")) {
    fs::dir_create(fs::path(suite, "apps", id))
    writeLines("library(shiny)\nshinyApp(fluidPage(), function(input, output) {})",
               fs::path(suite, "apps", id, "app.R"))
  }
  yaml::write_yaml(list(
    build = list(type = "r-shiny", runtime_strategy = "bundled"),
    apps = list(
      list(id = "one", name = "One", path = "apps/one"),
      list(id = "two", name = "Two", path = "apps/two")
    )
  ), fs::path(suite, "_shinyelectron.yml"))
  lookup <- mockery::mock("v4.6.2")
  build_rec <- mockery::mock(tempdir())
  local_mocked_bindings(github_latest_release_tag = lookup,
                        build_multi_app = build_rec)

  export(suite, withr::local_tempdir(), overwrite = TRUE, verbose = FALSE)

  mockery::expect_called(lookup, 1)
  config <- mockery::mock_args(build_rec)[[1]]$config
  expect_equal(config$dependencies$r$version, "4.6.2")
})

test_that("a shinylive export or a system R export does not look up R", {
  local_mac_export()
  lookup <- mockery::mock("v4.6.2", cycle = TRUE)
  local_mocked_bindings(
    github_latest_release_tag = lookup,
    r_latest_version = lookup,
    convert_app_to_shinylive = function(appdir, destdir, ...) {
      fs::path(destdir, "shinylive-app")
    },
    build_electron_app = function(...) tempdir()
  )

  for (strategy in c("shinylive", "system")) {
    export(local_r_app(list(runtime_strategy = strategy)), withr::local_tempdir(),
           overwrite = TRUE, verbose = FALSE)
  }
  mockery::expect_called(lookup, 0)
})

Try the shinyelectron package in your browser

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

shinyelectron documentation built on Dec. 3, 2026, 9:07 a.m.