Nothing
# tests/testthat/test-dependencies.R
# --- R dependency detection ---
test_that("detect_r_dependencies finds library/require calls via renv", {
skip_if_not_installed("renv")
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
writeLines(c(
"library(shiny)",
"library(ggplot2)",
"require(DT)"
), file.path(tmpdir, "app.R"))
deps <- detect_r_dependencies(tmpdir)
expect_true("shiny" %in% deps)
expect_true("ggplot2" %in% deps)
expect_true("DT" %in% deps)
})
test_that("detect_r_dependencies finds namespace references", {
skip_if_not_installed("renv")
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
writeLines(c(
"library(shiny)",
"dplyr::filter(df, x > 1)",
"ggplot2::ggplot(df)"
), file.path(tmpdir, "app.R"))
deps <- detect_r_dependencies(tmpdir)
expect_true("dplyr" %in% deps)
expect_true("ggplot2" %in% deps)
})
test_that("detect_r_dependencies excludes base R packages", {
skip_if_not_installed("renv")
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
writeLines(c(
"library(shiny)",
"stats::lm(y ~ x)",
"utils::head(df)"
), file.path(tmpdir, "app.R"))
deps <- detect_r_dependencies(tmpdir)
expect_true("shiny" %in% deps)
expect_false("stats" %in% deps)
expect_false("utils" %in% deps)
})
test_that("detect_r_dependencies scans multiple files", {
skip_if_not_installed("renv")
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
writeLines("library(shiny)", file.path(tmpdir, "ui.R"))
writeLines("library(DT)", file.path(tmpdir, "server.R"))
deps <- detect_r_dependencies(tmpdir)
expect_true("shiny" %in% deps)
expect_true("DT" %in% deps)
})
test_that("detect_r_dependencies returns sorted unique vector", {
skip_if_not_installed("renv")
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
writeLines(c(
"library(shiny)",
"library(shiny)",
"library(ggplot2)"
), file.path(tmpdir, "app.R"))
deps <- detect_r_dependencies(tmpdir)
expect_equal(deps, sort(unique(deps)))
expect_equal(sum(deps == "shiny"), 1)
})
test_that("detect_r_dependencies returns empty for no dependencies", {
skip_if_not_installed("renv")
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
writeLines("# no packages used", file.path(tmpdir, "app.R"))
deps <- detect_r_dependencies(tmpdir)
expect_length(deps, 0)
})
test_that("detect_r_dependencies errors gracefully when renv not installed", {
mockery::stub(detect_r_dependencies, "requireNamespace", function(...) FALSE)
expect_error(detect_r_dependencies(tempdir()), "renv")
})
# --- Python dependency detection ---
test_that("detect_py_dependencies reads requirements.txt", {
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
writeLines(c(
"pandas>=2.0",
"numpy==1.24.0",
"# this is a comment",
"",
"scikit-learn",
"shiny"
), file.path(tmpdir, "requirements.txt"))
deps <- detect_py_dependencies(tmpdir)
expect_true("pandas" %in% deps)
expect_true("numpy" %in% deps)
expect_true("scikit-learn" %in% deps)
expect_true("shiny" %in% deps)
})
test_that("detect_py_dependencies strips version specifiers", {
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
writeLines(c(
"pandas>=2.0,<3.0",
"numpy~=1.24",
"flask[async]>=2.0",
"plotly (>=5.0,<6.0)"
), file.path(tmpdir, "requirements.txt"))
deps <- detect_py_dependencies(tmpdir)
expect_true("pandas" %in% deps)
expect_true("numpy" %in% deps)
expect_true("flask" %in% deps)
expect_true("plotly" %in% deps)
})
test_that("detect_py_dependencies reads pyproject.toml dependencies", {
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
writeLines(c(
'[project]',
'name = "my-app"',
'dependencies = [',
' "shiny>=0.6",',
' "pandas",',
' "numpy>=1.24",',
']'
), file.path(tmpdir, "pyproject.toml"))
deps <- detect_py_dependencies(tmpdir)
expect_true("shiny" %in% deps)
expect_true("pandas" %in% deps)
expect_true("numpy" %in% deps)
})
test_that("detect_py_dependencies keeps pyproject.toml entries after one with extras", {
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
pyproject <- file.path(tmpdir, "pyproject.toml")
writeLines(c(
'[project]',
'dependencies = [',
' "uvicorn[standard]>=0.30",',
' "pandas",',
' "plotly",',
']'
), pyproject)
expect_equal(detect_py_dependencies(tmpdir), c("pandas", "plotly", "uvicorn"))
# Extras on the opening line, with the closing bracket after the last entry.
writeLines(c(
'[project]',
'dependencies = ["uvicorn[standard]>=0.30",',
' "pandas"]'
), pyproject)
expect_equal(detect_py_dependencies(tmpdir), c("pandas", "uvicorn"))
writeLines(c(
'[project]',
'dependencies = ["uvicorn[standard]>=0.30", "pandas"]'
), pyproject)
expect_equal(detect_py_dependencies(tmpdir), c("pandas", "uvicorn"))
})
test_that("detect_py_dependencies reads single-quoted and escaped pyproject.toml entries", {
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
writeLines(c(
'[project]',
'dependencies = [',
" 'shiny>=1.0',",
" 'tomli; python_version < \"3.11\"',",
" \"exceptiongroup; python_version < '3.11'\",",
' "pywin32; sys_platform == \\"win32\\" and python_version >= \\"3.8\\"",',
']'
), file.path(tmpdir, "pyproject.toml"))
deps <- detect_py_dependencies(tmpdir)
expect_equal(deps, c("exceptiongroup", "pywin32", "shiny", "tomli"))
})
test_that("detect_py_dependencies reads Poetry 2 style pyproject.toml entries", {
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
# Poetry 2 writes the version constraint in parentheses after the name.
writeLines(c(
'[project]',
'dependencies = [',
' "shiny (>=1.0)",',
' "uvicorn[standard] (>=0.54.0,<0.55.0)",',
' "plotly (>=7.1.0,<8.0.0)",',
' "tomli (>=2.4.1,<3.0.0) ; python_version < \\"3.11\\""',
']'
), file.path(tmpdir, "pyproject.toml"))
deps <- detect_py_dependencies(tmpdir)
expect_equal(deps, c("plotly", "shiny", "tomli", "uvicorn"))
})
test_that("detect_py_dependencies skips comments in pyproject.toml dependencies", {
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
writeLines(c(
'[project]',
'dependencies = [ # runtime packages [see README]',
' "shiny", # UI framework ]',
' # "dash", removed [2024]',
' "mypkg @ https://example.com/mypkg-1.0.tar.gz#sha256=abc123",',
' "pandas",',
']'
), file.path(tmpdir, "pyproject.toml"))
deps <- detect_py_dependencies(tmpdir)
expect_equal(deps, c("mypkg", "pandas", "shiny"))
})
test_that("detect_py_dependencies reads only [project] dependencies from pyproject.toml", {
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
writeLines(c(
'[build-system]',
'requires = ["hatchling"]',
'',
'[tool.hatch.envs.test]',
'dependencies = ["pytest", "coverage"]',
'',
'[ project ] # package metadata',
'name = "my-app"',
'dependencies = ["shiny", "pandas"]',
'',
# An extra named "dependencies" belongs to a sub-table, not to [project].
'[project.optional-dependencies]',
'dependencies = ["ruff"]',
'',
'[tool.hatch.envs.docs]',
'dependencies = [',
' "mkdocs",',
']'
), file.path(tmpdir, "pyproject.toml"))
deps <- detect_py_dependencies(tmpdir)
expect_equal(deps, c("pandas", "shiny"))
})
test_that("detect_py_dependencies reads [project] dependencies after a multi-line string", {
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
# Readme lines that start with "[" are not table headers.
writeLines(c(
'[project]',
'name = "my-app"',
'readme = {content-type = "text/markdown", text = """',
'[](https://pypi.org/project/my-app)',
'[Shiny] dashboard',
'"""}',
'dependencies = ["shiny", "pandas"]'
), file.path(tmpdir, "pyproject.toml"))
deps <- detect_py_dependencies(tmpdir)
expect_equal(deps, c("pandas", "shiny"))
})
test_that("detect_py_dependencies reads a pyproject.toml that starts with a byte order mark", {
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
writeBin(
c(as.raw(c(0xef, 0xbb, 0xbf)), charToRaw('[project]\ndependencies = ["shiny"]\n')),
file.path(tmpdir, "pyproject.toml")
)
# readLines() drops the mark in a UTF-8 locale but keeps it in the C locale.
withr::local_locale(c(LC_CTYPE = "C"))
expect_equal(detect_py_dependencies(tmpdir), "shiny")
})
test_that("detect_py_dependencies warns when pyproject.toml has no [project] dependencies", {
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
pyproject <- file.path(tmpdir, "pyproject.toml")
# Poetry 1 lists packages in a table that is not read.
writeLines(c(
'[tool.poetry.dependencies]',
'python = "^3.10"',
'shiny = "^1.0"'
), pyproject)
expect_warning(
deps <- detect_py_dependencies(tmpdir),
"No packages found"
)
expect_length(deps, 0)
# A dependencies key outside any table is not the [project] one.
writeLines('dependencies = ["shiny"]', pyproject)
expect_warning(
deps <- detect_py_dependencies(tmpdir),
"No packages found"
)
expect_length(deps, 0)
})
test_that("detect_py_dependencies prefers requirements.txt over pyproject.toml", {
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
writeLines("pandas", file.path(tmpdir, "requirements.txt"))
writeLines(c(
'[project]',
'dependencies = ["numpy"]'
), file.path(tmpdir, "pyproject.toml"))
deps <- detect_py_dependencies(tmpdir)
expect_true("pandas" %in% deps)
expect_false("numpy" %in% deps)
})
test_that("detect_py_dependencies warns when no dependency file found", {
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
writeLines("from shiny import App", file.path(tmpdir, "app.py"))
expect_warning(
deps <- detect_py_dependencies(tmpdir),
"requirements.txt"
)
expect_length(deps, 0)
})
test_that("detect_py_dependencies skips comments and flags in requirements.txt", {
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
writeLines(c(
"# Comment line",
"-r other-requirements.txt",
"--extra-index-url https://example.com",
"pandas",
""
), file.path(tmpdir, "requirements.txt"))
deps <- detect_py_dependencies(tmpdir)
expect_true("pandas" %in% deps)
expect_equal(length(deps), 1)
})
test_that("detect_py_dependencies returns sorted unique list", {
tmpdir <- tempfile()
dir.create(tmpdir)
on.exit(unlink(tmpdir, recursive = TRUE))
writeLines(c("pandas", "numpy", "pandas"), file.path(tmpdir, "requirements.txt"))
deps <- detect_py_dependencies(tmpdir)
expect_equal(deps, sort(unique(deps)))
})
# --- Dependency merging ---
test_that("merge_r_dependencies combines detected and declared packages", {
detected <- c("shiny", "ggplot2")
config_deps <- list(
auto_detect = TRUE,
r = list(
packages = list("DT", "plotly"),
repos = list("https://cloud.r-project.org")
)
)
result <- merge_r_dependencies(detected, config_deps)
expect_true(all(c("shiny", "ggplot2", "DT", "plotly") %in% result$packages))
expect_equal(result$repos, list("https://cloud.r-project.org"))
})
test_that("merge_r_dependencies uses only declared when auto_detect is FALSE", {
detected <- c("shiny", "ggplot2")
config_deps <- list(
auto_detect = FALSE,
r = list(
packages = list("DT"),
repos = list("https://cloud.r-project.org")
)
)
result <- merge_r_dependencies(detected, config_deps)
expect_equal(result$packages, "DT")
expect_false("shiny" %in% result$packages)
})
test_that("merge_r_dependencies includes extra_packages", {
detected <- c("shiny")
config_deps <- list(
auto_detect = TRUE,
extra_packages = list("custom.pkg"),
r = list(
packages = list(),
repos = list("https://cloud.r-project.org")
)
)
result <- merge_r_dependencies(detected, config_deps)
expect_true("custom.pkg" %in% result$packages)
expect_true("shiny" %in% result$packages)
})
test_that("merge_py_dependencies combines detected and declared", {
detected <- c("pandas", "numpy")
config_deps <- list(
auto_detect = TRUE,
python = list(
packages = list("scikit-learn"),
index_urls = list("https://pypi.org/simple")
)
)
result <- merge_py_dependencies(detected, config_deps)
expect_true(all(c("pandas", "numpy", "scikit-learn") %in% result$packages))
})
# --- Manifest generation ---
test_that("generate_dependency_manifest creates valid JSON for R", {
# Keep the system-requirements lookup offline (no network during checks).
local_mocked_bindings(query_sysreqs = function(...) character(0))
manifest <- generate_dependency_manifest(
packages = c("shiny", "ggplot2"),
language = "r",
repos = list("https://cloud.r-project.org")
)
parsed <- jsonlite::fromJSON(manifest, simplifyVector = FALSE)
expect_equal(parsed$language, "r")
expect_equal(length(parsed$packages), 2)
expect_true("shiny" %in% parsed$packages)
expect_equal(parsed$repos[[1]], "https://cloud.r-project.org")
expect_true(parsed$binary_only)
})
test_that("generate_dependency_manifest creates valid JSON for Python", {
manifest <- generate_dependency_manifest(
packages = c("pandas", "numpy"),
language = "python",
index_urls = list("https://pypi.org/simple")
)
parsed <- jsonlite::fromJSON(manifest, simplifyVector = FALSE)
expect_equal(parsed$language, "python")
expect_equal(length(parsed$packages), 2)
expect_equal(parsed$index_urls[[1]], "https://pypi.org/simple")
})
test_that("generate_dependency_manifest handles empty packages", {
manifest <- generate_dependency_manifest(
packages = character(0),
language = "r"
)
parsed <- jsonlite::fromJSON(manifest, simplifyVector = FALSE)
expect_equal(length(parsed$packages), 0)
})
# --- Orchestration ---
test_that("resolve_app_dependencies detects and merges for r-shiny", {
skip_if_not_installed("renv")
local_no_repo_check()
tmpdir <- tempfile()
dir.create(tmpdir)
writeLines(c("library(shiny)", "library(ggplot2)"), file.path(tmpdir, "app.R"))
on.exit(unlink(tmpdir, recursive = TRUE), add = TRUE)
config <- list(dependencies = list(
auto_detect = TRUE,
r = list(
packages = list("DT"),
repos = list("https://cloud.r-project.org")
)
))
result <- resolve_app_dependencies(
appdir = tmpdir,
app_type = "r-shiny",
runtime_strategy = "auto-download",
config = config
)
expect_true("shiny" %in% result$packages)
expect_true("ggplot2" %in% result$packages)
expect_true("DT" %in% result$packages)
expect_equal(result$language, "r")
})
test_that("resolve_app_dependencies reads requirements.txt for py-shiny", {
tmpdir <- tempfile()
dir.create(tmpdir)
writeLines("from shiny import App", file.path(tmpdir, "app.py"))
writeLines(c("pandas", "shiny"), file.path(tmpdir, "requirements.txt"))
on.exit(unlink(tmpdir, recursive = TRUE))
config <- list(dependencies = list(
auto_detect = TRUE,
python = list(
packages = list(),
index_urls = list("https://pypi.org/simple")
)
))
result <- resolve_app_dependencies(
appdir = tmpdir,
app_type = "py-shiny",
runtime_strategy = "auto-download",
config = config
)
expect_true("pandas" %in% result$packages)
expect_true("shiny" %in% result$packages)
expect_equal(result$language, "python")
})
test_that("resolve_app_dependencies returns NULL for the shinylive strategy", {
result <- resolve_app_dependencies(
appdir = tempdir(),
app_type = "r-shiny",
runtime_strategy = "shinylive",
config = list()
)
expect_null(result)
})
test_that("app_dependencies detects R packages for single apps and suites", {
appdir <- withr::local_tempdir()
writeLines("library(shiny)\nlibrary(bslib)\nshinyApp(bslib::page_fluid(), function(i, o) {})",
file.path(appdir, "app.R"))
expect_true(all(c("shiny", "bslib") %in% app_dependencies(appdir)))
# A suite-like directory has no single entrypoint; resolve by files present.
suite <- withr::local_tempdir()
dir.create(file.path(suite, "apps", "a"), recursive = TRUE)
writeLines("library(shiny)\nlibrary(bslib)\nshinyApp(fluidPage(), function(i, o) {})",
file.path(suite, "apps", "a", "app.R"))
expect_true("bslib" %in% app_dependencies(suite))
})
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.