tests/testthat/test-run.R

# An exported app folder. `main_js` is the text of its main.js; this version's
# main.js turns on electron-updater's forceDevUpdateConfig.
local_electron_app <- function(updates = TRUE,
                               main_js = "autoUpdater.forceDevUpdateConfig = true;",
                               env = parent.frame()) {
  dir <- withr::local_tempdir(.local_envir = env)
  pkg <- list(name = "update-check", version = "1.0.0", main = "main.js",
              scripts = list(electron = "electron ."))
  if (updates) pkg$dependencies <- list(`electron-updater` = "^6.8.11")
  jsonlite::write_json(pkg, file.path(dir, "package.json"), auto_unbox = TRUE)
  writeLines(main_js, file.path(dir, "main.js"))
  dir
}
feed_server <- list(process = NULL, url = "http://127.0.0.1:43210/")
interrupt <- function(...) base::stop(structure(class = c("interrupt", "condition"),
                                                 list(message = "", call = NULL)))

dev_config <- function(app) file.path(app, "dev-app-update.yml")

feed_folders <- function() {
  list.files(tempdir(), pattern = "^shinyelectron-update-feed-")
}

test_that("update_version serves a feed for the run and cleans up after it", {
  app <- local_electron_app()
  start_server <- mockery::mock(feed_server)
  stop_server <- mockery::mock(NULL)
  local_mocked_bindings(validate_node_npm = function() invisible(TRUE),
                        start_update_feed_server = start_server,
                        stop_update_feed_server = stop_server)
  mockery::stub(run_electron_app, "processx::run", function(...) {
    feed_dir <- mockery::mock_args(start_server)[[1]][[1]]
    list(status = 0L, stdout = "", stderr = "",
         dev = yaml::read_yaml(file.path(app, "dev-app-update.yml")),
         feed = yaml::read_yaml(file.path(feed_dir, "latest.yml")))
  })

  res <- run_electron_app(app, update_version = "2.0.0", verbose = FALSE)

  expect_equal(res$dev, list(provider = "generic", url = feed_server$url,
                             updaterCacheDirName = "update-check-dev-updater"))
  expect_identical(res$feed$version, "2.0.0")
  expect_false(file.exists(file.path(app, "dev-app-update.yml")))
  expect_false(dir.exists(mockery::mock_args(start_server)[[1]][[1]]))
  mockery::expect_args(stop_server, 1, feed_server)
})

test_that("a plain run starts no feed and writes no file", {
  app <- local_electron_app(updates = FALSE)
  start_server <- mockery::mock(feed_server)
  local_mocked_bindings(validate_node_npm = function() invisible(TRUE),
                        start_update_feed_server = start_server)
  mockery::stub(run_electron_app, "processx::run", function(...) list(
    status = 0L, stdout = "", stderr = "", dev = file.exists(file.path(app, "dev-app-update.yml"))))

  res <- run_electron_app(app, verbose = FALSE)

  expect_identical(res$status, 0L)
  expect_false(res$dev)
  mockery::expect_called(start_server, 0)
})

test_that("update_version cleans up after Ctrl+C", {
  app <- local_electron_app()
  withr::local_envvar(ELECTRON_DEV_PORT = "1234", ELECTRON_DEV_TOOLS = "false")
  start_server <- mockery::mock(feed_server)
  stop_server <- mockery::mock(NULL)
  during_run <- mockery::mock(NULL)
  local_mocked_bindings(validate_node_npm = function() invisible(TRUE),
                        start_update_feed_server = start_server,
                        stop_update_feed_server = stop_server)
  mockery::stub(run_electron_app, "processx::run", function(...) {
    during_run(file.exists(dev_config(app)))
    interrupt()
  })

  expect_null(run_electron_app(app, update_version = "2.0.0", verbose = FALSE))

  mockery::expect_args(during_run, 1, TRUE)
  expect_false(file.exists(dev_config(app)))
  expect_false(dir.exists(mockery::mock_args(start_server)[[1]][[1]]))
  mockery::expect_args(stop_server, 1, feed_server)
  expect_identical(Sys.getenv(c("ELECTRON_DEV_PORT", "ELECTRON_DEV_TOOLS"), names = FALSE),
                   c("1234", "false"))
})

test_that("update_version cleans up when Electron fails", {
  app <- local_electron_app()
  withr::local_envvar(ELECTRON_DEV_PORT = "1234", ELECTRON_DEV_TOOLS = "false")
  start_server <- mockery::mock(feed_server)
  stop_server <- mockery::mock(NULL)
  during_run <- mockery::mock(NULL)
  local_mocked_bindings(validate_node_npm = function() invisible(TRUE),
                        start_update_feed_server = start_server,
                        stop_update_feed_server = stop_server)
  mockery::stub(run_electron_app, "processx::run", function(...) {
    during_run(file.exists(dev_config(app)))
    list(status = 1L, stdout = "", stderr = "boom")
  })

  expect_error(run_electron_app(app, update_version = "2.0.0", verbose = FALSE), "Failed to start")

  mockery::expect_args(during_run, 1, TRUE)
  expect_false(file.exists(dev_config(app)))
  expect_false(dir.exists(mockery::mock_args(start_server)[[1]][[1]]))
  mockery::expect_args(stop_server, 1, feed_server)
  expect_identical(Sys.getenv(c("ELECTRON_DEV_PORT", "ELECTRON_DEV_TOOLS"), names = FALSE),
                   c("1234", "false"))
})

test_that("update_version cleans up when the run itself errors", {
  app <- local_electron_app()
  withr::local_envvar(ELECTRON_DEV_PORT = "1234", ELECTRON_DEV_TOOLS = "false")
  start_server <- mockery::mock(feed_server)
  stop_server <- mockery::mock(NULL)
  during_run <- mockery::mock(NULL)
  local_mocked_bindings(validate_node_npm = function() invisible(TRUE),
                        start_update_feed_server = start_server,
                        stop_update_feed_server = stop_server)
  mockery::stub(run_electron_app, "processx::run", function(...) {
    during_run(file.exists(dev_config(app)))
    base::stop("could not start npm")
  })

  expect_error(run_electron_app(app, update_version = "2.0.0", verbose = FALSE),
               "Failed to run Electron application")

  mockery::expect_args(during_run, 1, TRUE)
  expect_false(file.exists(dev_config(app)))
  expect_false(dir.exists(mockery::mock_args(start_server)[[1]][[1]]))
  mockery::expect_args(stop_server, 1, feed_server)
  expect_identical(Sys.getenv(c("ELECTRON_DEV_PORT", "ELECTRON_DEV_TOOLS"), names = FALSE),
                   c("1234", "false"))
})

test_that("update_version stops, starting nothing, when the app has its own dev-app-update.yml", {
  app <- local_electron_app()
  withr::local_envvar(ELECTRON_DEV_PORT = "1234", ELECTRON_DEV_TOOLS = "false")
  writeBin(charToRaw("provider: generic\nurl: http://127.0.0.1:8080/\n"), dev_config(app))
  before <- readBin(dev_config(app), "raw", 10000)
  folders <- feed_folders()
  start_server <- mockery::mock(feed_server)
  run <- mockery::mock(list(status = 0L, stdout = "", stderr = ""))
  local_mocked_bindings(validate_node_npm = function() invisible(TRUE),
                        start_update_feed_server = start_server)
  mockery::stub(run_electron_app, "processx::run", run)

  expect_error(run_electron_app(app, update_version = "2.0.0", verbose = FALSE),
               class = "shinyelectron_dev_update_config_exists")

  expect_identical(readBin(dev_config(app), "raw", 10000), before)
  mockery::expect_called(start_server, 0)
  mockery::expect_called(run, 0)
  expect_setequal(feed_folders(), folders)
  expect_identical(Sys.getenv(c("ELECTRON_DEV_PORT", "ELECTRON_DEV_TOOLS"), names = FALSE),
                   c("1234", "false"))
})

test_that("update_version needs an app exported with auto-updates", {
  app <- local_electron_app(updates = FALSE)
  folders <- feed_folders()
  start_server <- mockery::mock(feed_server)
  run <- mockery::mock(list(status = 0L, stdout = "", stderr = ""))
  local_mocked_bindings(validate_node_npm = function() invisible(TRUE),
                        start_update_feed_server = start_server)
  mockery::stub(run_electron_app, "processx::run", run)

  err <- expect_error(run_electron_app(app, update_version = "2.0.0", verbose = FALSE),
                      class = "shinyelectron_updates_not_exported")

  expect_match(conditionMessage(err), "enable_auto_updates()", fixed = TRUE)
  expect_match(conditionMessage(err), "export()", fixed = TRUE)
  mockery::expect_called(start_server, 0)
  mockery::expect_called(run, 0)
  expect_false(file.exists(dev_config(app)))
  expect_setequal(feed_folders(), folders)
})

test_that("update_version needs an app exported by this version of shinyelectron", {
  # An earlier main.js checks for updates but never reads dev-app-update.yml.
  app <- local_electron_app(main_js = "autoUpdater.checkForUpdatesAndNotify();")
  folders <- feed_folders()
  start_server <- mockery::mock(feed_server)
  run <- mockery::mock(list(status = 0L, stdout = "", stderr = ""))
  local_mocked_bindings(validate_node_npm = function() invisible(TRUE),
                        start_update_feed_server = start_server)
  mockery::stub(run_electron_app, "processx::run", run)

  err <- expect_error(run_electron_app(app, update_version = "2.0.0", verbose = FALSE),
                      class = "shinyelectron_updates_not_exported")

  expect_match(conditionMessage(err), "earlier version of shinyelectron", fixed = TRUE)
  expect_match(conditionMessage(err), "export()", fixed = TRUE)
  expect_no_match(conditionMessage(err), "enable_auto_updates", fixed = TRUE)
  mockery::expect_called(start_server, 0)
  mockery::expect_called(run, 0)
  expect_false(file.exists(dev_config(app)))
  expect_setequal(feed_folders(), folders)
})

test_that("update_version must be major.minor.patch", {
  app <- local_electron_app()
  folders <- feed_folders()
  start_server <- mockery::mock(feed_server)
  run <- mockery::mock(list(status = 0L, stdout = "", stderr = ""))
  local_mocked_bindings(validate_node_npm = function() invisible(TRUE),
                        start_update_feed_server = start_server)
  mockery::stub(run_electron_app, "processx::run", run)

  bad <- list("2.0", "v2.0.0", "2.0.0-beta", 2, c("2.0.0", "3.0.0"), NA_character_, "2.0.0\n")
  for (version in bad) {
    expect_error(run_electron_app(app, update_version = version, verbose = FALSE),
                 class = "shinyelectron_invalid_update_version")
  }

  mockery::expect_called(start_server, 0)
  mockery::expect_called(run, 0)
  expect_false(file.exists(dev_config(app)))
  expect_setequal(feed_folders(), folders)
})

test_that("a run without update_version removes a feed config an earlier run left, and nothing else", {
  app <- local_electron_app()
  start_server <- mockery::mock(feed_server)
  local_mocked_bindings(validate_node_npm = function() invisible(TRUE),
                        start_update_feed_server = start_server)
  mockery::stub(run_electron_app, "processx::run", function(...) list(
    status = 0L, stdout = "", stderr = "", left = file.exists(dev_config(app))))

  write_dev_update_config(app, feed_server$url, "update-check")
  expect_false(run_electron_app(app, verbose = FALSE)$left)
  expect_false(file.exists(dev_config(app)))

  writeBin(charToRaw("provider: generic\nurl: http://127.0.0.1:8080/\n"), dev_config(app))
  before <- readBin(dev_config(app), "raw", 10000)
  expect_true(run_electron_app(app, verbose = FALSE)$left)
  expect_identical(readBin(dev_config(app), "raw", 10000), before)

  mockery::expect_called(start_server, 0)
})

test_that("a verbose update_version run names the version and the feed address", {
  app <- local_electron_app()
  local_mocked_bindings(validate_node_npm = function() invisible(TRUE),
                        start_update_feed_server = mockery::mock(feed_server),
                        stop_update_feed_server = mockery::mock(NULL))
  mockery::stub(run_electron_app, "processx::run", function(...) list(status = 0L, stdout = "", stderr = ""))

  messages <- capture_messages(run_electron_app(app, update_version = "2.0.0"))

  expect_length(grep("2.0.0", messages, fixed = TRUE), 1)
  expect_match(messages[grep("2.0.0", messages, fixed = TRUE)], feed_server$url, fixed = TRUE)
})

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.