From d3e56ca75cca56944a296d7b218e59842270b112 Mon Sep 17 00:00:00 2001 From: Jon Harmon Date: Sun, 17 May 2026 07:57:44 -0500 Subject: [PATCH 1/3] Make `use_testthat()` quiet --- R/generate_pkg-setup.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/generate_pkg-setup.R b/R/generate_pkg-setup.R index f52873f..e577873 100644 --- a/R/generate_pkg-setup.R +++ b/R/generate_pkg-setup.R @@ -38,7 +38,7 @@ usethis::local_project(pkg_dir, quiet = TRUE) # nocov } usethis::use_directory("R") - usethis::use_testthat() + withr::with_options(list(usethis.quiet = TRUE), usethis::use_testthat()) purrr::quietly(httptest2::use_httptest2)() usethis::use_package("nectar") if (include_stbl) { From 0d1432dfba6c9207be4ec3350965a905d23b81f7 Mon Sep 17 00:00:00 2001 From: Jon Harmon Date: Sun, 17 May 2026 08:20:48 -0500 Subject: [PATCH 2/3] Separate stbl checking from main `setup_r()`, and add `.use_package()` helper. --- R/aaa-shared_params.R | 1 - R/generate_pkg-setup.R | 53 +++++++++++++++++++----- R/generate_pkg.R | 6 +-- man/dot-maybe_setup_stbl.Rd | 26 ++++++++++++ man/dot-setup_r.Rd | 4 +- man/dot-shared-params.Rd | 2 - man/dot-use_package.Rd | 24 +++++++++++ tests/testthat/test-generate_pkg-setup.R | 31 +++++++++----- 8 files changed, 116 insertions(+), 31 deletions(-) create mode 100644 man/dot-maybe_setup_stbl.Rd create mode 100644 man/dot-use_package.Rd diff --git a/R/aaa-shared_params.R b/R/aaa-shared_params.R index a3c0675..7befacc 100644 --- a/R/aaa-shared_params.R +++ b/R/aaa-shared_params.R @@ -25,7 +25,6 @@ #' @param endpoint (`character(1)`) The operation endpoint. #' @param endpoints (`character`) Endpoint paths paired with operations. #' @param filter_in (`character(1)`) The parameter location to keep. -#' @param include_stbl (`logical(1)`) Whether to add `stbl` to Imports. #' @param methods (`character`) HTTP methods paired with endpoints. #' @param operation (`character(1)`) The HTTP method. #' @param operation_description (`character(1)`) The operation description. diff --git a/R/generate_pkg-setup.R b/R/generate_pkg-setup.R index e577873..92f0b93 100644 --- a/R/generate_pkg-setup.R +++ b/R/generate_pkg-setup.R @@ -33,16 +33,47 @@ #' @inheritParams .shared-params #' @returns (`NULL`, invisibly) Called for setup side effects. #' @keywords internal -.setup_r <- function(pkg_dir, include_stbl = FALSE) { - if (as.character(pkg_dir) != ".") { - usethis::local_project(pkg_dir, quiet = TRUE) # nocov - } - usethis::use_directory("R") - withr::with_options(list(usethis.quiet = TRUE), usethis::use_testthat()) - purrr::quietly(httptest2::use_httptest2)() - usethis::use_package("nectar") - if (include_stbl) { - usethis::use_package("stbl") +.setup_r <- function(pkg_dir) { + usethis::with_project( + pkg_dir, + { + usethis::use_directory("R") + withr::with_options(list(usethis.quiet = TRUE), usethis::use_testthat()) + purrr::quietly(httptest2::use_httptest2)() + }, + quiet = TRUE + ) + .use_package("nectar", "Imports", pkg_dir) + .use_package("beekeeper", "Suggests", pkg_dir) +} + +#' Add a package dependency to the DESCRIPTION file +#' +#' @param pkg (`character(1)`) The package name. +#' @param type (`character(1)`) The dependency type, one of "Imports", +#' "Suggests", or "Depends". +#' @inheritParams .shared-params +#' @returns (`character(1)`, invisibly) The package name. +#' @keywords internal +.use_package <- function(pkg, type = "Imports", pkg_dir = ".") { + usethis::with_project( + pkg_dir, + { + usethis::use_package(pkg, type = type) + }, + quiet = TRUE + ) + invisible(pkg) +} + +#' Add stbl to dependencies if needed +#' +#' @inheritParams .shared-params +#' @returns (`character(1)` or `NULL`, invisibly) "stbl" if stbl is used, `NULL` +#' otherwise. +#' @keywords internal +.maybe_setup_stbl <- function(pkg_dir, paths, security_arg_names) { + if (.paths_need_stbl(paths, security_arg_names)) { + .use_package("stbl", "Imports", pkg_dir) } - usethis::use_package("beekeeper", type = "Suggests") } diff --git a/R/generate_pkg.R b/R/generate_pkg.R index 42ffb63..9e903a2 100644 --- a/R/generate_pkg.R +++ b/R/generate_pkg.R @@ -32,10 +32,8 @@ generate_pkg <- function( api_definition@components@security_schemes ) security_arg_names <- security_data$security_arg_names %|0|% character() - .setup_r( - pkg_dir, - include_stbl = .paths_need_stbl(api_definition@paths, security_arg_names) - ) + .setup_r(pkg_dir) + .maybe_setup_stbl(pkg_dir, api_definition@paths, security_arg_names) touched_files <- .generate_pkg_impl(config, api_definition, security_data) return(invisible(touched_files)) } diff --git a/man/dot-maybe_setup_stbl.Rd b/man/dot-maybe_setup_stbl.Rd new file mode 100644 index 0000000..d96d791 --- /dev/null +++ b/man/dot-maybe_setup_stbl.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/generate_pkg-setup.R +\name{.maybe_setup_stbl} +\alias{.maybe_setup_stbl} +\title{Add stbl to dependencies if needed} +\usage{ +.maybe_setup_stbl(pkg_dir, paths, security_arg_names) +} +\arguments{ +\item{pkg_dir}{(\code{character(1)} or \code{fs_path}) The directory containing package +files.} + +\item{paths}{(\code{rapid::class_paths}) API path definitions.} + +\item{security_arg_names}{(\code{character}) Security argument names to exclude +from generated operation signatures (because they are defined once, +separately).} +} +\value{ +(\code{character(1)} or \code{NULL}, invisibly) "stbl" if stbl is used, \code{NULL} +otherwise. +} +\description{ +Add stbl to dependencies if needed +} +\keyword{internal} diff --git a/man/dot-setup_r.Rd b/man/dot-setup_r.Rd index 2aba67e..413ab74 100644 --- a/man/dot-setup_r.Rd +++ b/man/dot-setup_r.Rd @@ -4,13 +4,11 @@ \alias{.setup_r} \title{Set up package directories and dependencies} \usage{ -.setup_r(pkg_dir, include_stbl = FALSE) +.setup_r(pkg_dir) } \arguments{ \item{pkg_dir}{(\code{character(1)} or \code{fs_path}) The directory containing package files.} - -\item{include_stbl}{(\code{logical(1)}) Whether to add \code{stbl} to Imports.} } \value{ (\code{NULL}, invisibly) Called for setup side effects. diff --git a/man/dot-shared-params.Rd b/man/dot-shared-params.Rd index 3193049..7ff0906 100644 --- a/man/dot-shared-params.Rd +++ b/man/dot-shared-params.Rd @@ -41,8 +41,6 @@ written.} \item{filter_in}{(\code{character(1)}) The parameter location to keep.} -\item{include_stbl}{(\code{logical(1)}) Whether to add \code{stbl} to Imports.} - \item{methods}{(\code{character}) HTTP methods paired with endpoints.} \item{operation}{(\code{character(1)}) The HTTP method.} diff --git a/man/dot-use_package.Rd b/man/dot-use_package.Rd new file mode 100644 index 0000000..ceb7314 --- /dev/null +++ b/man/dot-use_package.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/generate_pkg-setup.R +\name{.use_package} +\alias{.use_package} +\title{Add a package dependency to the DESCRIPTION file} +\usage{ +.use_package(pkg, type = "Imports", pkg_dir = ".") +} +\arguments{ +\item{pkg}{(\code{character(1)}) The package name.} + +\item{type}{(\code{character(1)}) The dependency type, one of "Imports", +"Suggests", or "Depends".} + +\item{pkg_dir}{(\code{character(1)} or \code{fs_path}) The directory containing package +files.} +} +\value{ +(\code{character(1)}, invisibly) The package name. +} +\description{ +Add a package dependency to the DESCRIPTION file +} +\keyword{internal} diff --git a/tests/testthat/test-generate_pkg-setup.R b/tests/testthat/test-generate_pkg-setup.R index cdd213a..2a217c5 100644 --- a/tests/testthat/test-generate_pkg-setup.R +++ b/tests/testthat/test-generate_pkg-setup.R @@ -38,14 +38,25 @@ test_that(".setup_r() sets up dependencies (#16)", { ) }) -test_that(".setup_r() can include stbl in imports (#69)", { - skip_on_cran() - - create_local_package() - .setup_r(".", include_stbl = TRUE) - - dependencies <- desc::desc()$get_deps() - imports <- dependencies$package[dependencies$type == "Imports"] - expect_contains(imports, "nectar") - expect_contains(imports, "stbl") +test_that(".maybe_setup_stbl() can include stbl in imports (#69)", { + local_mocked_bindings( + .use_package = function(pkg, type, pkg_dir) { + if (pkg == "stbl") { + expect_equal(type, "Imports") + expect_equal(pkg_dir, "DIR") + } + return(pkg) + }, + .paths_need_stbl = function(paths, security_arg_names) { + expect_equal(security_arg_names, "SECURITY_ARG_NAMES") + return(paths == "INSTALL") + } + ) + expect_equal( + .maybe_setup_stbl("DIR", "INSTALL", "SECURITY_ARG_NAMES"), + "stbl" + ) + expect_null( + .maybe_setup_stbl("DIR", "DONOTINSTALL", "SECURITY_ARG_NAMES") + ) }) From 6e05ebdb4480d981d7d3cfe56cdfde0f7aab2451 Mon Sep 17 00:00:00 2001 From: Jon Harmon Date: Sun, 17 May 2026 08:25:03 -0500 Subject: [PATCH 3/3] Rename to `.maybe_use_stbl` --- R/generate_pkg-setup.R | 2 +- R/generate_pkg.R | 2 +- man/{dot-maybe_setup_stbl.Rd => dot-maybe_use_stbl.Rd} | 6 +++--- tests/testthat/test-generate_pkg-setup.R | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) rename man/{dot-maybe_setup_stbl.Rd => dot-maybe_use_stbl.Rd} (86%) diff --git a/R/generate_pkg-setup.R b/R/generate_pkg-setup.R index 92f0b93..b960aad 100644 --- a/R/generate_pkg-setup.R +++ b/R/generate_pkg-setup.R @@ -72,7 +72,7 @@ #' @returns (`character(1)` or `NULL`, invisibly) "stbl" if stbl is used, `NULL` #' otherwise. #' @keywords internal -.maybe_setup_stbl <- function(pkg_dir, paths, security_arg_names) { +.maybe_use_stbl <- function(pkg_dir, paths, security_arg_names) { if (.paths_need_stbl(paths, security_arg_names)) { .use_package("stbl", "Imports", pkg_dir) } diff --git a/R/generate_pkg.R b/R/generate_pkg.R index 9e903a2..3e50785 100644 --- a/R/generate_pkg.R +++ b/R/generate_pkg.R @@ -33,7 +33,7 @@ generate_pkg <- function( ) security_arg_names <- security_data$security_arg_names %|0|% character() .setup_r(pkg_dir) - .maybe_setup_stbl(pkg_dir, api_definition@paths, security_arg_names) + .maybe_use_stbl(pkg_dir, api_definition@paths, security_arg_names) touched_files <- .generate_pkg_impl(config, api_definition, security_data) return(invisible(touched_files)) } diff --git a/man/dot-maybe_setup_stbl.Rd b/man/dot-maybe_use_stbl.Rd similarity index 86% rename from man/dot-maybe_setup_stbl.Rd rename to man/dot-maybe_use_stbl.Rd index d96d791..8f679ad 100644 --- a/man/dot-maybe_setup_stbl.Rd +++ b/man/dot-maybe_use_stbl.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/generate_pkg-setup.R -\name{.maybe_setup_stbl} -\alias{.maybe_setup_stbl} +\name{.maybe_use_stbl} +\alias{.maybe_use_stbl} \title{Add stbl to dependencies if needed} \usage{ -.maybe_setup_stbl(pkg_dir, paths, security_arg_names) +.maybe_use_stbl(pkg_dir, paths, security_arg_names) } \arguments{ \item{pkg_dir}{(\code{character(1)} or \code{fs_path}) The directory containing package diff --git a/tests/testthat/test-generate_pkg-setup.R b/tests/testthat/test-generate_pkg-setup.R index 2a217c5..8861ad2 100644 --- a/tests/testthat/test-generate_pkg-setup.R +++ b/tests/testthat/test-generate_pkg-setup.R @@ -38,7 +38,7 @@ test_that(".setup_r() sets up dependencies (#16)", { ) }) -test_that(".maybe_setup_stbl() can include stbl in imports (#69)", { +test_that(".maybe_use_stbl() can add stbl to imports (#69)", { local_mocked_bindings( .use_package = function(pkg, type, pkg_dir) { if (pkg == "stbl") { @@ -53,10 +53,10 @@ test_that(".maybe_setup_stbl() can include stbl in imports (#69)", { } ) expect_equal( - .maybe_setup_stbl("DIR", "INSTALL", "SECURITY_ARG_NAMES"), + .maybe_use_stbl("DIR", "INSTALL", "SECURITY_ARG_NAMES"), "stbl" ) expect_null( - .maybe_setup_stbl("DIR", "DONOTINSTALL", "SECURITY_ARG_NAMES") + .maybe_use_stbl("DIR", "DONOTINSTALL", "SECURITY_ARG_NAMES") ) })