Nothing
# tests/testthat/test-build-runtime.R
test_that("embed_r_runtime resolves the recursive closure minus pre_installed and targets the sibling library", {
skip_if_not_installed("mockery")
out <- withr::local_tempdir()
install_rec <- mockery::mock()
mockery::stub(embed_r_runtime, "install_r_portable", function(version, platform, arch, verbose) {
install_rec(version = version, platform = platform, arch = arch)
fs::path(out, "cached-r")
})
copy_rec <- mockery::mock()
mockery::stub(embed_r_runtime, "copy_dir_contents", function(src, dst) {
copy_rec(src = src, dst = dst)
# Pre-populate one transitive dep so the pre_installed setdiff is exercised,
# and drop a real Rscript so the cached-binary existence check passes.
fs::dir_create(fs::path(dst, "library", "rlang"), recurse = TRUE)
fs::dir_create(fs::path(dst, "bin"))
writeLines("#!/bin/sh", fs::path(dst, "bin", "Rscript"))
invisible(dst)
})
mockery::stub(embed_r_runtime, "r_executable",
function(version, platform, arch) fs::path(out, "runtime", "R", "bin", "Rscript"))
mockery::stub(embed_r_runtime, "utils::available.packages",
function(repos) matrix(nrow = 0, ncol = 0))
mockery::stub(embed_r_runtime, "tools::package_dependencies",
function(packages, db, which, recursive) {
list(shiny = c("htmltools", "rlang"), htmltools = "rlang")
})
# Force the non-linux binary clause so the captured r_code is deterministic.
mockery::stub(embed_r_runtime, "detect_current_platform", function() "mac")
run_rec <- mockery::mock()
mockery::stub(embed_r_runtime, "processx::run", function(command, args, ...) {
run_rec(command = command, args = args)
lib <- fs::path(out, "runtime", "R", "library")
for (p in c("shiny", "htmltools")) fs::dir_create(fs::path(lib, p))
list(status = 0, stdout = "", stderr = "")
})
embed_r_runtime(
output_dir = out,
packages = c("shiny", "htmltools"),
repos = c(CRAN = "https://cloud.r-project.org"),
version = "4.4.1",
platform = "mac",
arch = "arm64",
verbose = FALSE
)
# Runtime install + copy happened with the resolved version and scalar plat/arch.
install_r_called <- mockery::mock_args(install_rec)[[1]]
expect_equal(install_r_called$version, "4.4.1")
expect_equal(install_r_called$platform, "mac")
expect_equal(install_r_called$arch, "arm64")
expect_equal(mockery::mock_args(copy_rec)[[1]]$dst, fs::path(out, "runtime", "R"))
# The install ran against the SIBLING runtime/R/library path.
r_code <- mockery::mock_args(run_rec)[[1]]$args[[2]]
expect_match(r_code, "runtime/R/library", fixed = TRUE)
# Resolved set == recursive closure {shiny,htmltools,rlang} minus pre_installed {rlang}.
expect_match(r_code, "'shiny'", fixed = TRUE)
expect_match(r_code, "'htmltools'", fixed = TRUE)
expect_false(grepl("'rlang'", r_code, fixed = TRUE))
})
test_that("embed_r_runtime embeds the interpreter even when packages is empty", {
skip_if_not_installed("mockery")
out <- withr::local_tempdir()
install_rec <- mockery::mock()
mockery::stub(embed_r_runtime, "install_r_portable", function(version, platform, arch, verbose) {
install_rec()
fs::path(out, "cached-r")
})
copy_rec <- mockery::mock()
mockery::stub(embed_r_runtime, "copy_dir_contents", function(src, dst) {
copy_rec()
fs::dir_create(dst, recurse = TRUE)
invisible(dst)
})
run_rec <- mockery::mock(list(status = 0, stdout = "", stderr = ""))
mockery::stub(embed_r_runtime, "processx::run", run_rec)
# The package-install branch (and thus r_executable) must not be entered.
mockery::stub(embed_r_runtime, "r_executable",
function(...) stop("r_executable must not be called for empty packages"))
embed_r_runtime(
output_dir = out,
packages = character(0),
repos = c(CRAN = "https://cloud.r-project.org"),
version = "4.4.1",
platform = "mac",
arch = "arm64",
verbose = FALSE
)
mockery::expect_called(install_rec, 1) # interpreter still installed
mockery::expect_called(copy_rec, 1) # runtime still copied
mockery::expect_called(run_rec, 0) # install.packages NOT invoked
})
test_that("embed_r_runtime installs local packages after the repository packages and prunes last", {
skip_if_not_installed("mockery")
out <- withr::local_tempdir()
src <- withr::local_tempdir()
local_pkg <- file.path(src, "MyPkg")
dir.create(local_pkg)
writeLines(c("Package: MyPkg", "Version: 0.1.0", "Imports: jsonlite (>= 1.8.0)"),
file.path(local_pkg, "DESCRIPTION"))
mockery::stub(embed_r_runtime, "install_r_portable", function(...) fs::path(out, "cached-r"))
mockery::stub(embed_r_runtime, "copy_dir_contents", function(src, dst) {
fs::dir_create(fs::path(dst, "bin"), recurse = TRUE)
writeLines("#!/bin/sh", fs::path(dst, "bin", "Rscript"))
invisible(dst)
})
cached_rscript <- fs::path(out, "cached-r", "bin", "Rscript")
fs::dir_create(fs::path_dir(cached_rscript))
writeLines("#!/bin/sh", cached_rscript)
mockery::stub(embed_r_runtime, "r_executable", function(...) cached_rscript)
mockery::stub(embed_r_runtime, "utils::available.packages",
function(repos) matrix(nrow = 0, ncol = 0))
deps_rec <- mockery::mock()
mockery::stub(embed_r_runtime, "tools::package_dependencies",
function(packages, db, which, recursive) {
deps_rec(packages = packages)
list()
})
mockery::stub(embed_r_runtime, "detect_current_platform", function() "mac")
# One recorder shared by the three stubs keeps the order of the steps.
steps_rec <- mockery::mock()
run_rec <- mockery::mock()
mockery::stub(embed_r_runtime, "processx::run", function(command, args, ...) {
steps_rec("repository")
run_rec(args = args)
lib <- fs::path(out, "runtime", "R", "library")
for (p in c("shiny", "jsonlite")) fs::dir_create(fs::path(lib, p), recurse = TRUE)
list(status = 0, stdout = "", stderr = "")
})
local_rec <- mockery::mock()
mockery::stub(embed_r_runtime, "install_local_r_packages",
function(rscript, local_packages, lib_path, verbose) {
steps_rec("local")
local_rec(rscript = rscript, local_packages = local_packages,
lib_path = lib_path)
invisible("MyPkg")
})
# Pruning runs once everything is installed, so it also covers the local
# packages.
mockery::stub(embed_r_runtime, "prune_bundled_r_runtime",
function(runtime_dir, verbose) {
steps_rec("prune")
invisible(list(files = 0L, bytes = 0))
})
embed_r_runtime(
output_dir = out, packages = c("shiny", "MyPkg"),
repos = "https://cloud.r-project.org", version = "4.4.1",
platform = "mac", arch = "arm64", verbose = FALSE,
local_packages = local_pkg
)
# The local package's declared import comes from the repository; the local
# package itself never does.
r_code <- mockery::mock_args(run_rec)[[1]]$args[[2]]
resolved_for <- mockery::mock_args(deps_rec)[[1]]$packages
expect_match(r_code, "'jsonlite'", fixed = TRUE)
expect_match(r_code, "'shiny'", fixed = TRUE)
expect_false(grepl("'MyPkg'", r_code, fixed = TRUE))
expect_false("MyPkg" %in% resolved_for)
steps <- vapply(mockery::mock_args(steps_rec), `[[`, character(1), 1)
expect_equal(steps, c("repository", "local", "prune"))
local_args <- mockery::mock_args(local_rec)[[1]]
expect_equal(local_args$rscript, cached_rscript)
expect_equal(names(local_args$local_packages), "MyPkg")
expect_equal(normalizePath(local_args$local_packages$MyPkg$path), normalizePath(local_pkg))
expect_equal(local_args$lib_path, fs::path(out, "runtime", "R", "library"))
})
test_that("embed_r_runtime falls back to the default repository when repos is NULL", {
skip_if_not_installed("mockery")
out <- withr::local_tempdir()
mockery::stub(embed_r_runtime, "install_r_portable", function(...) fs::path(out, "cached-r"))
mockery::stub(embed_r_runtime, "copy_dir_contents", function(src, dst) {
fs::dir_create(fs::path(dst, "bin"), recurse = TRUE)
writeLines("#!/bin/sh", fs::path(dst, "bin", "Rscript"))
invisible(dst)
})
mockery::stub(embed_r_runtime, "r_executable",
function(...) fs::path(out, "runtime", "R", "bin", "Rscript"))
avail_rec <- mockery::mock()
mockery::stub(embed_r_runtime, "utils::available.packages", function(repos) {
avail_rec(repos = repos)
matrix(nrow = 0, ncol = 0)
})
mockery::stub(embed_r_runtime, "tools::package_dependencies", function(...) list())
mockery::stub(embed_r_runtime, "detect_current_platform", function() "mac")
run_rec <- mockery::mock()
mockery::stub(embed_r_runtime, "processx::run", function(command, args, ...) {
run_rec(args = args)
fs::dir_create(fs::path(out, "runtime", "R", "library", "shiny"), recurse = TRUE)
list(status = 0, stdout = "", stderr = "")
})
embed_r_runtime(
output_dir = out, packages = "shiny", repos = NULL, version = "4.4.1",
platform = "mac", arch = "arm64", verbose = FALSE
)
expect_equal(mockery::mock_args(avail_rec)[[1]]$repos, "https://cloud.r-project.org")
r_code <- mockery::mock_args(run_rec)[[1]]$args[[2]]
expect_match(r_code, "repos = c('https://cloud.r-project.org')", fixed = TRUE)
})
# --- Python runtime embedding ---
test_that("embed_python_runtime targets bundled site-packages and embeds unconditionally", {
skip_if_not_installed("mockery")
out <- withr::local_tempdir()
install_rec <- mockery::mock()
mockery::stub(embed_python_runtime, "install_python_standalone", function(version, platform, arch, verbose) {
install_rec()
fs::path(out, "cached-py")
})
copy_rec <- mockery::mock()
mockery::stub(embed_python_runtime, "copy_dir_contents", function(src, dst) {
copy_rec(dst = dst)
fs::dir_create(dst, recurse = TRUE)
invisible(dst)
})
mockery::stub(embed_python_runtime, "python_executable",
function(version, platform, arch) fs::path(out, "cached-py", "bin", "python3"))
run_rec <- mockery::mock()
mockery::stub(embed_python_runtime, "processx::run", function(command, args, ...) {
run_rec(args = args)
list(status = 0, stdout = "", stderr = "")
})
embed_python_runtime(
output_dir = out,
packages = c("shiny", "pandas"),
index_urls = "https://pypi.org/simple",
version = "3.14.6",
platform = "mac",
arch = "arm64",
verbose = FALSE
)
mockery::expect_called(install_rec, 1)
expect_equal(mockery::mock_args(copy_rec)[[1]]$dst, fs::path(out, "runtime", "Python"))
pip_args <- mockery::mock_args(run_rec)[[1]]$args
target_idx <- which(pip_args == "--target")
expect_length(target_idx, 1)
expect_equal(
pip_args[[target_idx + 1]],
as.character(fs::path(out, "runtime", "Python", "lib", "python", "site-packages"))
)
expect_true(all(c("shiny", "pandas") %in% pip_args))
})
test_that("embed_python_runtime warns (does not abort) when pip fails", {
skip_if_not_installed("mockery")
out <- withr::local_tempdir()
mockery::stub(embed_python_runtime, "install_python_standalone", function(...) fs::path(out, "cached-py"))
mockery::stub(embed_python_runtime, "copy_dir_contents", function(src, dst) {
fs::dir_create(dst, recurse = TRUE); invisible(dst)
})
mockery::stub(embed_python_runtime, "python_executable",
function(...) fs::path(out, "cached-py", "bin", "python3"))
mockery::stub(embed_python_runtime, "processx::run",
function(...) list(status = 1, stdout = "", stderr = "boom"))
expect_warning(
embed_python_runtime(
output_dir = out, packages = "shiny", index_urls = "https://pypi.org/simple",
version = "3.14.6", platform = "mac", arch = "arm64", verbose = FALSE
),
"Failed to install some Python packages"
)
})
test_that("embed_python_runtime embeds the interpreter even when packages is empty", {
skip_if_not_installed("mockery")
out <- withr::local_tempdir()
install_rec <- mockery::mock(fs::path(out, "cached-py"))
mockery::stub(embed_python_runtime, "install_python_standalone", install_rec)
copy_rec <- mockery::mock()
mockery::stub(embed_python_runtime, "copy_dir_contents", function(src, dst) {
copy_rec(); fs::dir_create(dst, recurse = TRUE); invisible(dst)
})
mockery::stub(embed_python_runtime, "python_executable",
function(...) fs::path(out, "cached-py", "bin", "python3"))
run_rec <- mockery::mock(list(status = 0, stdout = "", stderr = ""))
mockery::stub(embed_python_runtime, "processx::run", run_rec)
embed_python_runtime(
output_dir = out, packages = character(0), index_urls = "https://pypi.org/simple",
version = "3.14.6", platform = "mac", arch = "arm64", verbose = FALSE
)
mockery::expect_called(install_rec, 1)
mockery::expect_called(copy_rec, 1)
mockery::expect_called(run_rec, 0) # pip install NOT invoked
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.