tests/testthat/test-updates.R

test_that("enable_auto_updates writes config correctly", {
  tmp <- withr::local_tempdir()
  dir.create(tmp, showWarnings = FALSE, recursive = TRUE)

  # Create a minimal config
  writeLines(
    "app:\n  name: test\nbuild:\n  type: r-shiny\n  runtime_strategy: shinylive\n",
    file.path(tmp, "_shinyelectron.yml")
  )

  result <- enable_auto_updates(
    tmp,
    provider = "github",
    owner = "testowner",
    repo = "testrepo",
    check_on_startup = TRUE,
    auto_download = FALSE,
    auto_install = FALSE,
    verbose = FALSE
  )

  config <- yaml::read_yaml(result)
  expect_true(config$updates$enabled)
  expect_equal(config$updates$provider, "github")
  expect_equal(config$updates$github$owner, "testowner")
  expect_equal(config$updates$github$repo, "testrepo")
  expect_true(config$updates$check_on_startup)
  expect_false(config$updates$auto_download)
  expect_false(config$updates$auto_install)
})

test_that("enable_auto_updates requires owner/repo for github provider", {
  tmp <- withr::local_tempdir()
  dir.create(tmp, showWarnings = FALSE, recursive = TRUE)
  writeLines(
    "app:\n  name: test\nbuild:\n  type: r-shiny\n  runtime_strategy: shinylive\n",
    file.path(tmp, "_shinyelectron.yml")
  )

  expect_error(
    enable_auto_updates(tmp, provider = "github", owner = NULL, repo = NULL),
    "owner.*repo"
  )
})

test_that("enable_auto_updates creates config if none exists", {
  tmp <- withr::local_tempdir()
  dir.create(tmp, showWarnings = FALSE, recursive = TRUE)

  # No config file exists yet
  expect_false(file.exists(file.path(tmp, "_shinyelectron.yml")))

  result <- enable_auto_updates(
    tmp,
    provider = "github",
    owner = "myorg",
    repo = "myapp",
    verbose = FALSE
  )

  expect_true(file.exists(result))
  config <- yaml::read_yaml(result)
  expect_true(config$updates$enabled)
})

test_that("disable_auto_updates sets enabled to FALSE", {
  tmp <- withr::local_tempdir()
  dir.create(tmp, showWarnings = FALSE, recursive = TRUE)

  # First enable
  writeLines(
    "app:\n  name: test\nupdates:\n  enabled: true\n  provider: github\n",
    file.path(tmp, "_shinyelectron.yml")
  )

  result <- disable_auto_updates(tmp, verbose = FALSE)
  config <- yaml::read_yaml(result)
  expect_false(config$updates$enabled)
})

test_that("disable_auto_updates is no-op when already disabled", {
  tmp <- withr::local_tempdir()
  dir.create(tmp, showWarnings = FALSE, recursive = TRUE)

  writeLines(
    "app:\n  name: test\nupdates:\n  enabled: false\n",
    file.path(tmp, "_shinyelectron.yml")
  )

  # Should not error
  result <- disable_auto_updates(tmp, verbose = FALSE)
  config <- yaml::read_yaml(result)
  expect_false(config$updates$enabled)
})

test_that("disable_auto_updates errors without config file", {
  tmp <- withr::local_tempdir()
  dir.create(tmp, showWarnings = FALSE, recursive = TRUE)

  expect_error(
    disable_auto_updates(tmp),
    "No configuration file"
  )
})

test_that("check_auto_update_status reports enabled status", {
  tmp <- withr::local_tempdir()
  dir.create(tmp, showWarnings = FALSE, recursive = TRUE)

  writeLines(
    "app:\n  name: test\nupdates:\n  enabled: true\n  provider: github\n  github:\n    owner: foo\n    repo: bar\n",
    file.path(tmp, "_shinyelectron.yml")
  )

  result <- check_auto_update_status(tmp)
  expect_true(result$enabled)
  expect_equal(result$provider, "github")
})

test_that("check_auto_update_status reports disabled status", {
  tmp <- withr::local_tempdir()
  dir.create(tmp, showWarnings = FALSE, recursive = TRUE)

  writeLines(
    "app:\n  name: test\n",
    file.path(tmp, "_shinyelectron.yml")
  )

  result <- check_auto_update_status(tmp)
  expect_type(result, "list")
  expect_false(result$enabled)
})

test_that("check_auto_update_status reports the generic address", {
  tmp <- withr::local_tempdir()
  enable_auto_updates(tmp, provider = "generic",
                      url = "https://updates.example.com/app/", verbose = FALSE)
  # The other status lines are not under test.
  suppressMessages(expect_message(res <- check_auto_update_status(tmp),
                                  "https://updates.example.com/app/", fixed = TRUE))
  expect_equal(res$generic$url, "https://updates.example.com/app/")
})

test_that("enable_auto_updates still rejects s3", {
  tmp <- withr::local_tempdir()
  expect_error(
    enable_auto_updates(tmp, provider = "s3"),
    "not yet supported"
  )
})

test_that("enable_auto_updates says which providers it takes when provider is not one string", {
  for (p in list(c("github", "generic"), NULL, 1, NA_character_)) {
    tmp <- withr::local_tempdir()
    expect_error(
      enable_auto_updates(tmp, provider = p, owner = "me", repo = "app", verbose = FALSE),
      '"github" or "generic"', fixed = TRUE
    )
    expect_false(file.exists(file.path(tmp, "_shinyelectron.yml")))
  }
})

test_that("enable_auto_updates stops when url is given for the github provider", {
  tmp <- withr::local_tempdir()
  expect_error(
    enable_auto_updates(tmp, owner = "me", repo = "app",
                        url = "https://updates.example.com/app/", verbose = FALSE),
    'provider = "generic"', fixed = TRUE
  )
  expect_false(file.exists(file.path(tmp, "_shinyelectron.yml")))
})

test_that("enable_auto_updates stops when owner or repo is given for the generic provider", {
  for (github in list(list(owner = "me"), list(repo = "app"), list(owner = "me", repo = "app"))) {
    tmp <- withr::local_tempdir()
    expect_error(
      do.call(enable_auto_updates,
              c(list(tmp, provider = "generic", url = "https://updates.example.com/app/",
                     verbose = FALSE), github)),
      "GitHub provider"
    )
    expect_false(file.exists(file.path(tmp, "_shinyelectron.yml")))
  }
})

test_that("check_auto_update_status shows how to turn on either provider", {
  tmp <- withr::local_tempdir()
  writeLines("app:\n  name: test\n", file.path(tmp, "_shinyelectron.yml"))
  shown <- testthat::capture_messages(check_auto_update_status(tmp))
  expect_true(any(grepl("owner = '...', repo = '...'", shown, fixed = TRUE)))
  expect_true(any(grepl("provider = 'generic', url = '...'", shown, fixed = TRUE)))
})

test_that("enable_auto_updates writes the generic provider and its address", {
  tmp <- withr::local_tempdir()
  result <- enable_auto_updates(tmp, provider = "generic",
                                url = "https://updates.example.com/app/", verbose = FALSE)
  config <- yaml::read_yaml(result)
  expect_equal(config$updates$provider, "generic")
  expect_equal(config$updates$generic, list(url = "https://updates.example.com/app/"))
  expect_null(config$updates$github)
})

test_that("enable_auto_updates names the generic address it set", {
  tmp <- withr::local_tempdir()
  # The other progress lines are not under test.
  suppressMessages(expect_message(
    enable_auto_updates(tmp, provider = "generic", url = "https://updates.example.com/app/"),
    "https://updates.example.com/app/", fixed = TRUE
  ))
})

test_that("enable_auto_updates needs a web address for the generic provider", {
  for (u in list(NULL, "updates.example.com")) {
    tmp <- withr::local_tempdir()
    err <- expect_error(
      enable_auto_updates(tmp, provider = "generic", url = u, verbose = FALSE),
      class = "shinyelectron_invalid_update_url"
    )
    expect_match(conditionMessage(err), "^url ")
    expect_no_match(conditionMessage(err), "updates.generic", fixed = TRUE)
    expect_false(file.exists(file.path(tmp, "_shinyelectron.yml")))
  }
})

test_that("enable_auto_updates warns for http outside the local machine and still writes it", {
  tmp <- withr::local_tempdir()
  w <- expect_warning(
    result <- enable_auto_updates(tmp, provider = "generic",
                                  url = "http://updates.example.com/", verbose = FALSE),
    class = "shinyelectron_insecure_update_url"
  )
  expect_match(conditionMessage(w), "^url ")
  expect_no_match(conditionMessage(w), "updates.generic", fixed = TRUE)
  expect_equal(yaml::read_yaml(result)$updates$generic$url, "http://updates.example.com/")
})

test_that("enable_auto_updates drops the github block when switching to generic", {
  tmp <- withr::local_tempdir()
  writeLines(
    c("app:", "  name: test", "updates:", "  enabled: true", "  provider: github",
      "  github:", "    owner: me", "    repo: app", "    private: true"),
    file.path(tmp, "_shinyelectron.yml")
  )
  result <- enable_auto_updates(tmp, provider = "generic",
                                url = "https://updates.example.com/app/", verbose = FALSE)
  config <- yaml::read_yaml(result)
  expect_equal(config$app$name, "test")
  expect_setequal(names(config$updates),
                  c("enabled", "provider", "check_on_startup", "auto_download",
                    "auto_install", "generic"))
  expect_equal(config$updates$generic, list(url = "https://updates.example.com/app/"))
})

test_that("enable_auto_updates drops the generic block when switching to github", {
  tmp <- withr::local_tempdir()
  writeLines(
    c("app:", "  name: test", "updates:", "  enabled: true", "  provider: generic",
      "  generic:", "    url: https://updates.example.com/app/"),
    file.path(tmp, "_shinyelectron.yml")
  )
  result <- enable_auto_updates(tmp, provider = "github", owner = "me", repo = "app",
                                verbose = FALSE)
  config <- yaml::read_yaml(result)
  expect_equal(config$app$name, "test")
  expect_setequal(names(config$updates),
                  c("enabled", "provider", "check_on_startup", "auto_download",
                    "auto_install", "github"))
  expect_equal(config$updates$github, list(owner = "me", repo = "app", private = FALSE))
})

test_that("enable_auto_updates preserves existing config values", {
  tmp <- withr::local_tempdir()
  dir.create(tmp, showWarnings = FALSE, recursive = TRUE)

  writeLines(
    "app:\n  name: My App\n  version: '2.0.0'\nbuild:\n  type: r-shiny\nwindow:\n  width: 1400\n",
    file.path(tmp, "_shinyelectron.yml")
  )

  result <- enable_auto_updates(
    tmp, provider = "github", owner = "me", repo = "app", verbose = FALSE
  )

  config <- yaml::read_yaml(result)
  expect_equal(config$app$name, "My App")
  expect_equal(config$app$version, "2.0.0")
  expect_equal(config$build$type, "r-shiny")
  expect_equal(config$window$width, 1400)
  expect_true(config$updates$enabled)
})

test_that("check_generic_update_url accepts https and local http quietly", {
  for (u in c("https://updates.example.com/app/", "http://localhost:8080/",
              "http://127.0.0.1:8080/feed/", "HTTP://LOCALHOST:8080/")) {
    expect_no_warning(expect_identical(check_generic_update_url(u), u))
  }
})

test_that("check_generic_update_url warns for http to any other host, look-alikes included", {
  for (u in c("http://updates.example.com/", "http://localhost.example.com/",
              "http://127.0.0.1.nip.io/", "http://localhost@evil.example/",
              "http://[::1]:8080/")) {
    expect_warning(check_generic_update_url(u),
                   class = "shinyelectron_insecure_update_url")
  }
})

test_that("check_generic_update_url stops for a missing or non-web address", {
  for (u in list(NULL, "", NA, NA_character_, 8080, TRUE,
                 "ftp://updates.example.com/", "updates.example.com",
                 c("https://a.example/", "https://b.example/"))) {
    expect_error(check_generic_update_url(u),
                 class = "shinyelectron_invalid_update_url")
  }
})

test_that("check_generic_update_url names the setting it was given", {
  expect_error(check_generic_update_url("ftp://x.example/", arg = "my_setting"),
               "my_setting", class = "shinyelectron_invalid_update_url")
  expect_warning(check_generic_update_url("http://x.example/", arg = "my_setting"),
                 "my_setting", class = "shinyelectron_insecure_update_url")
})

test_that("check_generic_update_url refuses backslashes, spaces and control characters", {
  for (u in c("http://evil.example\\@localhost/", "http://evil.example\\@127.0.0.1/",
              "HTTP://EVIL.EXAMPLE\\@LOCALHOST:8080/", "http://evil.example:80\\@localhost/",
              "https://updates.example.com/my app/", "http://localhost/\n@evil.example",
              "http://local\thost/")) {
    expect_error(check_generic_update_url(u), class = "shinyelectron_invalid_update_url")
  }
})

.generic_config_dir <- function(...) {
  dir <- withr::local_tempdir(.local_envir = parent.frame())
  writeLines(c(...), file.path(dir, "_shinyelectron.yml"))
  dir
}

test_that("read_config accepts the generic provider with an https address", {
  dir <- .generic_config_dir("updates:", "  enabled: true", "  provider: generic",
                             "  generic:", "    url: https://updates.example.com/app/")
  expect_no_warning(config <- read_config(dir))
  expect_identical(config$updates$generic$url, "https://updates.example.com/app/")
})

test_that("read_config stops when generic updates have no usable address", {
  no_block <- .generic_config_dir("updates:", "  enabled: true", "  provider: generic")
  expect_error(read_config(no_block), class = "shinyelectron_invalid_update_url")

  ftp <- .generic_config_dir("updates:", "  enabled: true", "  provider: generic",
                             "  generic:", "    url: ftp://updates.example.com/")
  expect_error(read_config(ftp), class = "shinyelectron_invalid_update_url")
})

test_that("read_config stops for an address with a backslash", {
  dir <- .generic_config_dir("updates:", "  enabled: true", "  provider: generic",
                             "  generic:", "    url: http://evil.example\\@localhost/")
  expect_error(read_config(dir), class = "shinyelectron_invalid_update_url")
})

test_that("read_config skips the address check while updates are off", {
  dir <- .generic_config_dir("updates:", "  enabled: false", "  provider: generic")
  expect_no_error(read_config(dir))
})

test_that("read_config skips the address check for the github provider", {
  dir <- .generic_config_dir("updates:", "  enabled: true", "  provider: github")
  expect_no_error(read_config(dir))
})

test_that("read_config warns for an http address outside the local machine", {
  remote <- .generic_config_dir("updates:", "  enabled: true", "  provider: generic",
                                "  generic:", "    url: http://updates.example.com/")
  expect_warning(read_config(remote), class = "shinyelectron_insecure_update_url")

  local <- .generic_config_dir("updates:", "  enabled: true", "  provider: generic",
                               "  generic:", "    url: http://localhost:8080/")
  expect_no_warning(read_config(local))
})

test_that("read_config stops for the s3 provider only while updates are on", {
  on <- .generic_config_dir("updates:", "  enabled: true", "  provider: s3")
  err <- expect_error(read_config(on), class = "shinyelectron_unsupported_update_provider")
  expect_match(conditionMessage(err), '"github"', fixed = TRUE)
  expect_match(conditionMessage(err), '"generic"', fixed = TRUE)

  off <- .generic_config_dir("updates:", "  enabled: false", "  provider: s3")
  expect_silent(read_config(off))
})

# A converted shinylive app that build_electron_app() accepts.
.local_shinylive_dir <- function(env = parent.frame()) {
  dir <- withr::local_tempdir(.local_envir = env)
  writeLines("<html></html>", file.path(dir, "index.html"))
  dir
}

# Run the real build steps without npm or electron-builder.
.local_fake_build <- function(env = parent.frame()) {
  local_mocked_bindings(
    validate_node_npm = function(...) invisible(TRUE),
    install_npm_dependencies = function(...) invisible(TRUE),
    build_for_platforms = function(...) invisible(TRUE),
    validate_build_output = function(...) invisible(TRUE),
    .env = env
  )
}

# The messages of the http:// address warnings that `expr` gives, muffled.
.insecure_url_warnings <- function(expr) {
  seen <- mockery::mock()
  withCallingHandlers(expr, shinyelectron_insecure_update_url = function(w) {
    seen(conditionMessage(w))
    invokeRestart("muffleWarning")
  })
  vapply(mockery::mock_args(seen), `[[`, character(1), 1)
}

.http_updates <- list(enabled = TRUE, provider = "generic",
                      generic = list(url = "http://updates.example.com/"))

test_that("build_electron_app stops for an unusable generic address before touching the output", {
  # Should the check ever be missed, the build still never reaches npm.
  .local_fake_build()
  app_dir <- .local_shinylive_dir()
  for (generic in list(list(url = "http://evil.example\\@localhost/"), NULL)) {
    out <- file.path(withr::local_tempdir(), "electron-app")
    dir.create(out)
    writeLines("previous build", file.path(out, "keep.txt"))
    config <- list(updates = list(enabled = TRUE, provider = "generic", generic = generic))
    expect_error(
      build_electron_app(app_dir, out, app_name = "Direct", config = config,
                         overwrite = TRUE, verbose = FALSE),
      class = "shinyelectron_invalid_update_url"
    )
    expect_true(file.exists(file.path(out, "keep.txt")))
  }
})

test_that("build_electron_app stops for the s3 provider only while updates are on", {
  .local_fake_build()
  app_dir <- .local_shinylive_dir()
  out <- file.path(withr::local_tempdir(), "electron-app")
  expect_error(
    build_electron_app(app_dir, out, app_name = "Direct",
                       config = list(updates = list(enabled = TRUE, provider = "s3")),
                       verbose = FALSE),
    class = "shinyelectron_unsupported_update_provider"
  )
  expect_false(dir.exists(out))

  expect_no_warning(
    built <- build_electron_app(app_dir, out, app_name = "Direct",
                                config = list(updates = list(enabled = FALSE, provider = "s3")),
                                verbose = FALSE)
  )
  expect_null(jsonlite::fromJSON(file.path(built, "package.json"))$build$publish)
})

test_that("build_electron_app warns about an http address in a config passed to it", {
  .local_fake_build()
  out <- file.path(withr::local_tempdir(), "electron-app")
  seen <- .insecure_url_warnings(
    build_electron_app(.local_shinylive_dir(), out, app_name = "Direct",
                       config = list(updates = .http_updates), verbose = FALSE)
  )
  expect_length(seen, 1)
})

test_that("export warns once about an http update address", {
  local_no_electron_lookup()
  .local_fake_build()
  local_mocked_bindings(
    convert_app_to_shinylive = function(appdir, destdir, ...) {
      out <- fs::path(destdir, "shinylive-app")
      fs::dir_create(out)
      writeLines("<html></html>", fs::path(out, "index.html"))
      out
    }
  )
  appdir <- withr::local_tempdir()
  writeLines("library(shiny)\nshinyApp(fluidPage(), function(input, output) {})",
             file.path(appdir, "app.R"))
  yaml::write_yaml(list(updates = .http_updates), file.path(appdir, "_shinyelectron.yml"))

  seen <- .insecure_url_warnings(
    result <- export(appdir, withr::local_tempdir(), overwrite = TRUE, verbose = FALSE)
  )

  expect_length(seen, 1)
  pkg <- jsonlite::fromJSON(fs::path(result$electron_app, "package.json"))
  expect_equal(pkg$build$publish$url, "http://updates.example.com/")
})

test_that("export of a suite warns once about an http update address", {
  local_no_electron_lookup()
  suite <- withr::local_tempdir()
  for (id in c("a", "b")) {
    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 = "shinylive"),
    updates = .http_updates,
    apps = list(list(id = "a", name = "A", path = "apps/a"),
                list(id = "b", name = "B", path = "apps/b"))
  ), fs::path(suite, "_shinyelectron.yml"))
  templates <- mockery::mock(invisible(TRUE))
  .local_fake_build()
  local_mocked_bindings(
    convert_shiny_to_shinylive = function(appdir, output_dir, subdir = NULL, ...) {
      fs::dir_create(fs::path(output_dir, subdir), recurse = TRUE)
      writeLines("<html></html>", fs::path(output_dir, subdir, "index.html"))
      fs::path_abs(output_dir)
    },
    process_templates = templates
  )

  seen <- .insecure_url_warnings(
    export(suite, withr::local_tempdir(), overwrite = TRUE, verbose = FALSE)
  )

  expect_length(seen, 1)
  # The suite reached build_multi_app(), which checks the settings again.
  mockery::expect_called(templates, 1)
})

test_that("init_config documents the generic provider", {
  dir <- withr::local_tempdir()
  init_config(dir, app_name = "Demo", verbose = FALSE)
  lines <- readLines(file.path(dir, "_shinyelectron.yml"))
  expect_contains(lines, "#   generic:")
  expect_true(any(grepl("^#     url: null", lines)))
})

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.