tests/testthat/test-generated-features.R

generate_feature_metapackage <- function(
  name = "featureverse", workflow = NULL, reexport = FALSE
) {
  root <- tempfile("bigbang-generated-features-")
  destination <- file.path(root, "output")
  dir.create(destination, recursive = TRUE)
  fixture <- system.file(
    "extdata", "toycomponent_0.1.0.tar.gz", package = "bigbang"
  )
  if (!nzchar(fixture)) {
    fixture <- testthat::test_path(
      "..", "..", "inst", "extdata", "toycomponent_0.1.0.tar.gz"
    )
  }
  create_metapackage(
    name = name,
    packages = "toycomponent_0.1.0",
    pkg_dir = dirname(fixture),
    dest_dir = destination,
    document = FALSE,
    verbose = FALSE,
    reexport = reexport,
    import_deps = character(),
    force_deps = character(),
    workflow = workflow
  )
}

test_that("generated startup supports cli formatting, fallback, and quiet mode", {
  skip_on_cran()
  result <- generate_feature_metapackage()
  runtime <- new.env(parent = baseenv())
  sys.source(file.path(result$path, "R", "utils.R"), envir = runtime)
  sys.source(file.path(result$path, "R", "attach.R"), envir = runtime)
  sys.source(file.path(result$path, "R", "zzz.R"), envir = runtime)

  component <- list2env(list(shared_name = function() "component"))
  competitor <- list2env(list(shared_name = function() "competitor"))
  attach(
    competitor, name = "package:featurecompetitor", pos = 2L,
    warn.conflicts = FALSE
  )
  on.exit(detach("package:featurecompetitor"), add = TRUE)
  attach(
    component, name = "package:toycomponent", pos = 2L,
    warn.conflicts = FALSE
  )
  on.exit(detach("package:toycomponent"), add = TRUE)

  withr::local_options(featureverse.quiet = TRUE)
  quiet_output <- capture.output(
    runtime$.onAttach(NULL, "featureverse"), type = "message"
  )
  expect_length(quiet_output, 0L)

  options(featureverse.quiet = FALSE)
  cli_output <- capture.output(
    runtime$.onAttach(NULL, "featureverse"), type = "message"
  )
  expect_match(paste(cli_output, collapse = "\n"), "Attaching packages")
  expect_match(paste(cli_output, collapse = "\n"), "toycomponent")
  fallback_runtime <- new.env(parent = baseenv())
  fallback_runtime$requireNamespace <- function(package, ...) {
    if (identical(package, "cli")) FALSE else
      base::requireNamespace(package, ...)
  }
  for (file in c("utils.R", "attach.R", "zzz.R")) {
    source <- paste(
      readLines(file.path(result$path, "R", file), warn = FALSE),
      collapse = "\n"
    )
    source <- gsub("base::requireNamespace", "requireNamespace",
                   source, fixed = TRUE)
    source <- gsub("utils::globalVariables(\".pkgs\")", "invisible(NULL)",
                   source, fixed = TRUE)
    eval(parse(text = source, keep.source = FALSE), envir = fallback_runtime)
  }
  fallback_output <- capture.output(
    fallback_runtime$.onAttach(NULL, "featureverse"), type = "message"
  )
  expect_match(paste(fallback_output, collapse = "\n"), "featureverse")
  expect_match(paste(fallback_output, collapse = "\n"), "=", fixed = TRUE)
  version_table <- runtime$format_cli_startup("bigbang", c("stats", "utils"))
  expect_match(version_table, as.character(utils::packageVersion("stats")), fixed = TRUE)
  expect_match(version_table, as.character(utils::packageVersion("utils")), fixed = TRUE)

  generated_attach <- paste(
    readLines(file.path(result$path, "R", "attach.R"), warn = FALSE),
    collapse = "\n"
  )
  expect_match(generated_attach, "base::find.package", fixed = TRUE)
})

test_that("generated banners are lazy, byte-stable, and label reexports", {
  skip_on_cran()
  fake_library <- tempfile("bigbang-banner-library-")
  fake_package <- file.path(fake_library, "bannercomponent")
  dir.create(fake_package, recursive = TRUE)
  writeLines(c(
    "Package: bannercomponent",
    "Version: 0.1.0",
    "Title: Banner Component",
    "Description: A temporary package description.",
    "License: MIT"
  ), file.path(fake_package, "DESCRIPTION"))
  old_paths <- .libPaths()
  .libPaths(c(fake_library, old_paths))
  on.exit(.libPaths(old_paths), add = TRUE)

  for (reexport in c(FALSE, TRUE)) {
    result <- generate_feature_metapackage(
      paste0("banner", if (reexport) "reexport" else "attach"),
      reexport = reexport
    )
    runtime <- new.env(parent = baseenv())
    sys.source(file.path(result$path, "R", "utils.R"), envir = runtime)

    before <- loadedNamespaces()
    banner <- runtime$generate_ascii_banner(
      "bannerverse", "bannercomponent", reexport = reexport
    )
    after <- loadedNamespaces()
    expect_false("bannercomponent" %in% setdiff(after, before))

    width <- 60L
    border <- paste0(rep("=", width), collapse = "")
    title <- " bannerverse "
    left <- paste0(rep("-", floor((width - nchar(title)) / 2L)), collapse = "")
    right <- paste0(
      rep("-", width - nchar(left) - nchar(title)), collapse = ""
    )
    label <- if (reexport) "Components available:" else "Included packages:"
    expected <- paste(c(
      border, paste0(left, title, right), border, "",
      label, "  * bannercomponent (v0.1.0)", "", border
    ), collapse = "\n")
    expect_identical(banner, expected)
    expect_identical(
      banner,
      runtime$generate_ascii_banner(
        "bannerverse", "bannercomponent", reexport = reexport
      )
    )

    cli_output <- runtime$format_cli_startup(
      "bannerverse", character(), reexport = reexport
    )
    expect_match(
      cli_output,
      if (reexport) "Components available" else "Attaching packages",
      fixed = TRUE
    )

    fallback <- new.env(parent = baseenv())
    fallback$requireNamespace <- function(package, ...) {
      if (identical(package, "cli")) FALSE else
        base::requireNamespace(package, ...)
    }
    utils_source <- paste(
      readLines(file.path(result$path, "R", "utils.R"), warn = FALSE),
      collapse = "\n"
    )
    utils_source <- gsub(
      "base::requireNamespace", "requireNamespace", utils_source, fixed = TRUE
    )
    eval(parse(text = utils_source, keep.source = FALSE), envir = fallback)
    expect_null(fallback$format_cli_startup(
      "bannerverse", character(), reexport = reexport
    ))
    expect_match(
      fallback$generate_ascii_banner(
        "bannerverse", "bannercomponent", reexport = reexport
      ),
      label,
      fixed = TRUE
    )
  }
})

test_that("generated conflict reports inspect package namespace exports", {
  skip_on_cran()
  result <- generate_feature_metapackage("conflictverse")
  runtime <- new.env(parent = baseenv())
  sys.source(file.path(result$path, "R", "utils.R"), envir = runtime)
  sys.source(file.path(result$path, "R", "attach.R"), envir = runtime)

  component <- list2env(list(shared_name = function() "component"))
  competitor <- list2env(list(shared_name = function() "competitor"))
  attach(
    competitor, name = "package:conflictcompetitor", pos = 2L,
    warn.conflicts = FALSE
  )
  on.exit(detach("package:conflictcompetitor"), add = TRUE)
  attach(
    component, name = "package:toycomponent", pos = 2L,
    warn.conflicts = FALSE
  )
  on.exit(detach("package:toycomponent"), add = TRUE)

  conflicts <- runtime$conflictverse_conflicts()
  expect_s3_class(conflicts, "conflictverse_conflicts")
  # These hand-built search environments have no package namespace. They are
  # deliberately ignored; namespace exports are checked for real packages.
  expect_length(conflicts, 0L)
  output <- capture.output(returned <- runtime$print.conflictverse_conflicts(conflicts))
  expect_identical(returned, conflicts)
  expect_match(paste(output, collapse = "\n"), "No conflicts found", fixed = TRUE)
})

test_that("generated metadata and base test agree on component identity", {
  skip_on_cran()
  result <- generate_feature_metapackage("consistentverse")
  description <- read.dcf(file.path(result$path, "DESCRIPTION"))
  expect_identical(
    unname(description[1L, "Config/bigbang/packages"]), "toycomponent"
  )
  imports <- trimws(strsplit(description[1L, "Imports"], ",", fixed = TRUE)[[1L]])
  expect_false("toycomponent" %in% imports)

  consistency_test <- file.path(
    result$path, "tests", "component-consistency.R"
  )
  expect_true(file.exists(consistency_test))
  expect_match(
    paste(readLines(consistency_test, warn = FALSE), collapse = "\n"),
    "Config/bigbang/packages",
    fixed = TRUE
  )
  namespace <- readLines(file.path(result$path, "NAMESPACE"), warn = FALSE)
  expect_true("export(consistentverse_conflicts)" %in% namespace)
  expect_true("S3method(print,consistentverse_conflicts)" %in% namespace)

  readme <- paste(
    readLines(file.path(result$path, "README.md"), warn = FALSE),
    collapse = "\n"
  )
  expect_match(readme, "consistentverse.quiet", fixed = TRUE)
  # The archives ship inside the meta-package here, so the documented call
  # takes no argument. The variant that does is covered in
  # test-self-contained-metapackage.R.
  expect_match(readme, "consistentverse_install()", fixed = TRUE)
  expect_no_match(readme, "pkg_dir =", fixed = TRUE)
})

test_that("workflow creates an ordered vignette and validates all components", {
  skip_on_cran()
  workflow <- c("Import data" = "toycomponent")
  result <- generate_feature_metapackage("workflowverse", workflow)
  expect_identical(result$workflow, workflow)
  path <- file.path(
    result$path, "vignettes", "workflow-workflowverse.Rmd"
  )
  expect_true(file.exists(path))
  content <- paste(readLines(path, warn = FALSE), collapse = "\n")
  expect_match(content, "## Import data", fixed = TRUE)
  expect_match(content, "Pipeline component: `toycomponent`", fixed = TRUE)
  expect_match(content, "workflowverse_packages(), citation", fixed = TRUE)

  root <- tempfile("bigbang-invalid-workflow-")
  dir.create(root)
  fixture <- system.file(
    "extdata", "toycomponent_0.1.0.tar.gz", package = "bigbang"
  )
  expect_error(
    create_metapackage(
      "badworkflowverse", "toycomponent_0.1.0", dirname(fixture),
      dest_dir = root, workflow = c("Wrong" = "otherpackage"),
      document = FALSE, verbose = FALSE
    ),
    class = "bigbang_error_workflow"
  )
  expect_false(dir.exists(file.path(root, "badworkflowverse")))
})

test_that("generated installer honors force and upgrade policies", {
  skip_on_cran()
  result <- generate_feature_metapackage("upgradeverse")
  runtime <- new.env(parent = baseenv())
  for (file in c("utils.R", "install_packages.R", "attach.R")) {
    sys.source(file.path(result$path, "R", file), envir = runtime)
  }
  library <- tempfile("bigbang-generated-upgrade-library-")
  dir.create(library)
  withr::local_libpaths(c(library, .libPaths()))
  fixture <- system.file(
    "extdata", "toycomponent_0.1.0.tar.gz", package = "bigbang"
  )

  first <- runtime$upgradeverse_install(dirname(fixture), verbose = FALSE)
  expect_named(first$installed, "toycomponent_0.1.0")
  runtime$upgradeverse_detach()

  unchanged <- runtime$upgradeverse_install(dirname(fixture), verbose = FALSE)
  expect_named(unchanged$unchanged, "toycomponent_0.1.0")
  runtime$upgradeverse_detach()

  forced <- runtime$upgradeverse_install(
    dirname(fixture), force = TRUE, verbose = FALSE
  )
  expect_named(forced$installed, "toycomponent_0.1.0")
  runtime$upgradeverse_detach()

  expect_error(
    runtime$upgradeverse_install(
      dirname(fixture), force = TRUE, upgrade = "never", verbose = FALSE
    ),
    class = "bigbang_error_install_policy"
  )
  if ("package:toycomponent" %in% search()) runtime$upgradeverse_detach()
  if ("toycomponent" %in% loadedNamespaces()) unloadNamespace("toycomponent")
})

test_that("generated package installs and attaches without cli", {
  skip_on_cran()
  skip_if(
    identical(Sys.getenv("R_COVR"), "true"),
    "covr injects instrumented dependencies into child library paths"
  )
  result <- generate_feature_metapackage("fallbackverse")
  child_root <- dirname(dirname(result$path))
  library <- tempfile("bigbang-no-cli-library-")
  dir.create(library)
  writeLines(
    c(paste0("R_LIBS_USER=", library), "R_LIBS_SITE=", "R_LIBS="),
    file.path(child_root, ".Renviron")
  )
  fixture <- system.file(
    "extdata", "toycomponent_0.1.0.tar.gz", package = "bigbang"
  )
  code <- paste0(
    ".libPaths(c(", deparse(library), ", .Library)); ",
    "stopifnot(!requireNamespace('cli', quietly = TRUE)); ",
    "utils::install.packages(", deparse(fixture),
    ", repos = NULL, type = 'source', lib = ", deparse(library), "); ",
    "utils::install.packages(", deparse(result$path),
    ", repos = NULL, type = 'source', lib = ", deparse(library), "); ",
    "options(fallbackverse.quiet = FALSE); ",
    "library(fallbackverse, lib.loc = ", deparse(library), "); ",
    "stopifnot('package:toycomponent' %in% search()); ",
    "writeLines('NO_CLI_OK')"
  )
  output <- withr::with_dir(child_root, system2(
    file.path(R.home("bin"), "Rscript"),
    c("--no-site-file", "--no-init-file", "--no-restore", "-e", shQuote(code)),
    stdout = TRUE,
    stderr = TRUE
  ))
  status <- attr(output, "status")
  if (is.null(status)) status <- 0L
  expect_identical(status, 0L, info = paste(output, collapse = "\n"))
  expect_true("NO_CLI_OK" %in% output)
  expect_true(any(grepl("={20}", output)))
})

Try the bigbang package in your browser

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

bigbang documentation built on Nov. 29, 2026, 1:08 a.m.